def _return_if_object(self, module, response): """ :param module: A GcpModule :param response: A Requests response object :return JSON response """ # If not found, return nothing. if response.status_code == 404: return None # If no content, return nothing. if response.status_code == 204: return None try: response.raise_for_status result = response.json() except getattr(json.decoder, "JSONDecodeError", ValueError) as inst: module.fail_json(msg="Invalid JSON response with error: %s" % inst) except GcpRequestException as inst: module.fail_json(msg="Network error: %s" % inst) if navigate_hash(result, ["error", "errors"]): module.fail_json(msg=navigate_hash(result, ["error", "errors"])) return result
def wait_for_operation(module, response): op_result = return_if_object(module, response) if op_result is None: return {} status = navigate_hash(op_result, ['done']) wait_done = wait_for_completion(status, op_result, module) raise_if_errors(wait_done, ['error'], module) return navigate_hash(wait_done, ['response'])
def wait_for_completion(status, op_result, module): op_id = navigate_hash(op_result, ['name']) op_uri = async_op_url(module, {'op_id': op_id}) while not status: raise_if_errors(op_result, ['error'], module) time.sleep(1.0) op_result = fetch_resource(module, op_uri, False) status = navigate_hash(op_result, ['done']) return op_result
def wait_for_operation(module, response): op_result = return_if_object(module, response, 'sql#operation') if op_result is None: return {} status = navigate_hash(op_result, ['status']) wait_for_completion(status, op_result, module) return fetch_wrapped_resource(module, 'sql#user', 'sql#usersList', 'items')
def return_if_object(module, response, kind, allow_not_found=False): # If not found, return nothing. if allow_not_found and response.status_code == 404: return None # If no content, return nothing. if response.status_code == 204: return None try: module.raise_for_status(response) result = response.json() except getattr(json.decoder, 'JSONDecodeError', ValueError): module.fail_json(msg="Invalid JSON response with error: %s" % response.text) if navigate_hash(result, ['error', 'errors']): module.fail_json(msg=navigate_hash(result, ['error', 'errors'])) return result
def return_if_object(module, response): # If not found, return nothing. # return_if_object not used in any context where 404 means error. if response.status_code == 404: return None # If no content, return nothing. if response.status_code == 204: return None try: module.raise_for_status(response) result = response.json() except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst: module.fail_json(msg="Invalid JSON response with error: %s" % inst) if navigate_hash(result, ['error', 'errors']): module.fail_json(msg=navigate_hash(result, ['error', 'errors'])) return result
def return_if_object(module, response, allow_not_found=False): # If not found, return nothing. if allow_not_found and response.status_code == 404: return None # If no content, return nothing. if response.status_code == 204: return None # SQL only: return on 403 if not exist if allow_not_found and response.status_code == 403: return None try: result = response.json() except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst: module.fail_json(msg="Invalid JSON response with error: %s" % inst) if navigate_hash(result, ['error', 'message']): module.fail_json(msg=navigate_hash(result, ['error', 'message'])) return result