Improve DecortController.account_find method
This commit is contained in:
@@ -153,6 +153,12 @@ class DecortController(object):
|
||||
# self.run_phase = "Initializing DecortController instance complete."
|
||||
return
|
||||
|
||||
@staticmethod
|
||||
def sec_to_dt_str(sec: int, str_format: str = '%Y-%m-%d_%H-%M-%S') -> str:
|
||||
if sec:
|
||||
return time.strftime(str_format, time.gmtime(sec))
|
||||
return ''
|
||||
|
||||
def check_amodule_argument(self, arg_name, abort=True):
|
||||
"""Checks if the argument identified by the arg_name is defined in the module parameters.
|
||||
|
||||
@@ -1920,7 +1926,8 @@ class DecortController(object):
|
||||
self.result['changed'] = True
|
||||
return
|
||||
|
||||
def account_find(self, account_name, account_id=0):
|
||||
def account_find(self, account_name, account_id=0,
|
||||
fail_if_not_found=False):
|
||||
"""Find cloud account specified by the name and return facts about the account. Knowing account is
|
||||
required for certain cloud resource management tasks (e.g. creating new RG).
|
||||
|
||||
@@ -1935,35 +1942,64 @@ class DecortController(object):
|
||||
|
||||
self.result['waypoints'] = "{} -> {}".format(self.result['waypoints'], "account_find")
|
||||
|
||||
if account_name == "" and account_id == 0:
|
||||
self.result['failed'] = True
|
||||
self.result['msg'] = "Cannot find account if account name is empty and account ID is zero."
|
||||
if not account_id and not account_name:
|
||||
self.result['msg'] = ('Cannot find account if account name and'
|
||||
' id are not specified.')
|
||||
self.amodule.fail_json(**self.result)
|
||||
|
||||
api_params = dict()
|
||||
account_details = None
|
||||
_account_id = account_id
|
||||
|
||||
if account_name == "":
|
||||
api_params['accountId'] = account_id
|
||||
api_resp = self.decort_api_call(requests.post, "/restmachine/cloudapi/account/get", api_params)
|
||||
if account_name and not account_id:
|
||||
api_params = {
|
||||
'name': account_name
|
||||
}
|
||||
api_resp = self.decort_api_call(
|
||||
arg_req_function=requests.post,
|
||||
arg_api_name='/restmachine/cloudapi/account/list',
|
||||
arg_params=api_params
|
||||
)
|
||||
accounts_list = api_resp.json()['data']
|
||||
for account in accounts_list:
|
||||
if account['name'] == account_name:
|
||||
_account_id = account['id']
|
||||
break
|
||||
|
||||
if _account_id:
|
||||
api_params = {
|
||||
'accountId': _account_id
|
||||
}
|
||||
api_resp = self.decort_api_call(
|
||||
arg_req_function=requests.post,
|
||||
arg_api_name='/restmachine/cloudapi/account/get',
|
||||
arg_params=api_params,
|
||||
not_fail_codes=[404]
|
||||
)
|
||||
if api_resp.status_code == 200:
|
||||
account_details = json.loads(api_resp.content.decode('utf8'))
|
||||
account_details = api_resp.json()
|
||||
|
||||
if account_details:
|
||||
account_details['createdTime_readable'] = self.sec_to_dt_str(
|
||||
account_details['createdTime']
|
||||
)
|
||||
account_details['deactivationTime_readable'] = self.sec_to_dt_str(
|
||||
account_details['deactivationTime']
|
||||
)
|
||||
account_details['deletedTime_readable'] = self.sec_to_dt_str(
|
||||
account_details['deletedTime']
|
||||
)
|
||||
account_details['updatedTime_readable'] = self.sec_to_dt_str(
|
||||
account_details['updatedTime']
|
||||
)
|
||||
|
||||
return account_details['id'], account_details
|
||||
else:
|
||||
api_resp = self.decort_api_call(requests.post, "/restmachine/cloudapi/account/list", api_params)
|
||||
if api_resp.status_code == 200:
|
||||
# Parse response to see if a account matching arg_account_name is found in the output
|
||||
# If it is found, assign its ID to the return variable and copy dictionary with the facts
|
||||
accounts_list = json.loads(api_resp.content.decode('utf8'))
|
||||
for runner in accounts_list['data']:
|
||||
if runner['name'] == account_name:
|
||||
# get detailed information about the account from "accounts/get" call as
|
||||
# "accounts/list" does not return all necessary fields
|
||||
api_params['accountId'] = runner['id']
|
||||
api_resp = self.decort_api_call(requests.post, "/restmachine/cloudapi/account/get", api_params)
|
||||
if api_resp.status_code == 200:
|
||||
account_details = json.loads(api_resp.content.decode('utf8'))
|
||||
return account_details['id'], account_details
|
||||
|
||||
if fail_if_not_found:
|
||||
self.result['msg'] = ("Current user does not have access to"
|
||||
" the requested account or non-existent"
|
||||
" account specified.")
|
||||
self.amodule.fail_json(**self.result)
|
||||
else:
|
||||
return 0, None
|
||||
|
||||
###################################
|
||||
|
||||
Reference in New Issue
Block a user