Skip to content

Commit

Permalink
Additional admin functions. (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
firke authored Feb 1, 2024
1 parent cf09f12 commit 57450af
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions spotinst_sdk2/clients/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,81 @@ def delete_user(self, user_id):
)

return response


def update_user_group_mapping(self, user_id, user_group_ids):
"""
Update the mapping of a given user to user groups
# Arguments
user_id (String): Identifier of a user.
user_group_ids (List): A list of the user groups to register the given user to
# Returns
(Object): Spotinst API response
"""
response = self.send_put(
url=self.__base_setup_url + "/user/" + user_id + "/userGroupMapping",
body=json.dumps(dict(userGroupIds=user_group_ids)),
entity_name="user",
)

formatted_response = self.convert_json(response, self.camel_to_underscore)

return formatted_response["response"]["status"]

def update_user_policy_mapping(self, user_id, policies):
"""
Update the mapping of a given user to policies
# Arguments
user_id (String): Identifier of a user.
policies (List): A list of policies to register under the given user
# Returns
(Object): Spotinst API response
"""
response = self.send_put(
url=self.__base_setup_url + "/user/" + user_id + "/policyMapping",
body=json.dumps(dict(policies=policies)),
entity_name="user",
)

formatted_response = self.convert_json(response, self.camel_to_underscore)

return formatted_response["response"]["status"]

def create_programmatic_user(self, name, description, accounts=None, policies=None):
"""
Create a programmatic user
# Arguments
name (String): Name of the programmatic user
description (String): Brief description of the user
accounts (List): All the accounts the programmatic user will have access to
policies (List): All the policies the programmatic user will have access to
# Returns
(Object): Spotinst API response
"""
if accounts is None and policies is None:
raise ValueError(
"Either accounts or policies must be provided in create_programmatic_user"
)

payload = {
"description": description,
"name": name,
}
if accounts is not None:
payload["accounts"] = accounts
if policies is not None:
payload["policies"] = policies

response = self.send_post(
url=self.__base_setup_url + "/user/programmatic",
body=json.dumps(payload),
entity_name="programmaticUser",
)
formatted_response = self.convert_json(response, self.camel_to_underscore)
return formatted_response["response"]["items"][0]

0 comments on commit 57450af

Please sign in to comment.