From 5161649fe9a8c0d1951d9dd63cc1c322d0006d32 Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Mon, 15 Jul 2024 18:19:08 +0300 Subject: [PATCH] Implement functionality of `/cloudapi/account/getResourceConsumption` API method --- library/decort_account_info.py | 6 ++++++ module_utils/decort_utils.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/library/decort_account_info.py b/library/decort_account_info.py index e98924a..c022eb5 100644 --- a/library/decort_account_info.py +++ b/library/decort_account_info.py @@ -64,6 +64,11 @@ class DecortAccountInfo(DecortController): required=False, fallback=(env_fallback, ['DECORT_OAUTH2_URL']) ), + resource_consumption=dict( + type='bool', + required=False, + default=False + ), verify_ssl=dict( type='bool', required=False, @@ -91,6 +96,7 @@ class DecortAccountInfo(DecortController): self.id, self.facts = self.account_find( account_name=amodule.params['name'], account_id=amodule.params['id'], + resource_consumption=amodule.params['resource_consumption'], fail_if_not_found=True ) diff --git a/module_utils/decort_utils.py b/module_utils/decort_utils.py index ae03ea0..293e4af 100644 --- a/module_utils/decort_utils.py +++ b/module_utils/decort_utils.py @@ -1930,7 +1930,7 @@ class DecortController(object): return def account_find(self, account_name, account_id=0, - fail_if_not_found=False): + resource_consumption=False, 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). @@ -1995,6 +1995,16 @@ class DecortController(object): account_details['updatedTime'] ) + if resource_consumption: + resource_consumption = self.account_resource_consumption( + account_id=account_details['id'], + fail_if_not_found=True + ) + account_details['resource_consumed'] =\ + resource_consumption['Consumed'] + account_details['resource_reserved'] =\ + resource_consumption['Reserved'] + return account_details['id'], account_details else: if fail_if_not_found: @@ -2005,6 +2015,25 @@ class DecortController(object): else: return 0, None + def account_resource_consumption(self, account_id: int, + fail_if_not_found=False) -> None | dict: + self.result['waypoints'] += f' -> account_resource_consumption' + + api_resp = self.decort_api_call( + arg_req_function=requests.post, + arg_api_name='/restmachine/cloudapi/account/getResourceConsumption', + arg_params={'accountId': account_id}, + not_fail_codes=[404] + ) + if api_resp.status_code == 200: + return api_resp.json() + else: + 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) + ################################### # GPU resource manipulation methods ###################################