diff --git a/module_utils/decort_utils.py b/module_utils/decort_utils.py index c39c3b8..523d383 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,41 @@ 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 + rg_status = ret_rg_dict.get('status') + if not rg_status or rg_status == 'DELETED': + return ret_rg_id, ret_rg_dict + 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