From 1304a0fcbfd1db177d75ad19fb3fc2f752c65aad Mon Sep 17 00:00:00 2001 From: Dmitriy Smirnov Date: Wed, 3 Apr 2024 11:00:56 +0300 Subject: [PATCH] Add call of API /cloudapi/rg/getResourceConsumption to DecortController._rg_get_by_id and update its logic and code style --- module_utils/decort_utils.py | 45 ++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/module_utils/decort_utils.py b/module_utils/decort_utils.py index 6d04c75..414a425 100644 --- a/module_utils/decort_utils.py +++ b/module_utils/decort_utils.py @@ -1561,9 +1561,10 @@ class DecortController(object): @param (int) )rg_id: ID of the RG to find and return facts for. - @return: RG ID and a dictionary of RG facts as provided by rg/get API call. Note that if it fails - to find the RG with the specified ID, it may return 0 for ID and empty dictionary for the facts. So - it is suggested to check the return values accordingly. + @return: RG ID and a dictionary of RG facts as provided by rg/get + API call. Note that if it fails to find the RG with the specified ID, + it may return 0 for ID and empty dictionary for the facts. So it is + suggested to check the return values accordingly. """ ret_rg_id = 0 ret_rg_dict = dict() @@ -1573,14 +1574,38 @@ class DecortController(object): self.result['msg'] = "rg_get_by_id(): zero RG ID specified." self.amodule.fail_json(**self.result) - api_params = dict(rgId=rg_id, ) - api_resp = self.decort_api_call(requests.post, "/restmachine/cloudapi/rg/get", api_params) - if api_resp.status_code == 200: - ret_rg_id = rg_id - ret_rg_dict = json.loads(api_resp.content.decode('utf8')) + api_params = {'rgId': rg_id} + + # Get RG base info + api_rg_resp = self.decort_api_call( + arg_req_function=requests.post, + arg_api_name='/restmachine/cloudapi/rg/get', + arg_params=api_params + ) + if api_rg_resp.status_code != 200: + self.result['warning'] = ( + f'rg_get_by_id(): failed to get RG by ID {rg_id}.' + f' HTTP code {api_rg_resp.status_code}' + f', response {api_rg_resp.reason}.' + ) + return ret_rg_id, ret_rg_dict + ret_rg_id = rg_id + ret_rg_dict = api_rg_resp.json() + + # Get RG resources info + api_rg_res_resp = self.decort_api_call( + arg_req_function=requests.post, + arg_api_name='/restmachine/cloudapi/rg/getResourceConsumption', + arg_params=api_params + ) + if api_rg_res_resp.status_code != 200: + self.result['warning'] = ( + f'rg_get_by_id(): failed to get RG Resources by ID {rg_id}.' + f' HTTP code {api_rg_res_resp.status_code}' + f', response {api_rg_res_resp.reason}.' + ) else: - self.result['warning'] = ("rg_get_by_id(): failed to get RG by ID {}. HTTP code {}, " - "response {}.").format(rg_id, api_resp.status_code, api_resp.reason) + ret_rg_dict['Resources'] = api_rg_res_resp.json() return ret_rg_id, ret_rg_dict