def perform_action(self, action, check_applicable=True, wait_applicable=True): action_fn = self.get_action_fn(action) if not action_fn: self.module.fail_json( msg="{0} not supported by the module.".format(action)) try: get_response = self.get_resource() except ServiceError as se: self.module.fail_json( msg="Getting resource failed with exception: {0}".format( se.message)) else: resource = to_dict(get_response.data) if check_applicable: is_action_necessary = self.is_action_necessary(action) if not is_action_necessary: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=resource) if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=resource) action_response = action_fn() if not (wait_applicable and self.module.params.get("wait")): if action_response.data: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(action_response.data), ) return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=resource) try: updated_resource = self.action_wait(action, action_response) except ServiceError as se: self.module.fail_json( msg="Performing action failed with exception: {0}".format( se.message)) else: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(updated_resource), )
def update(self, check_applicable=True, wait_applicable=True): try: get_response = self.get_resource() except ServiceError as se: self.module.fail_json( msg="Getting resource failed with exception: {0}".format( se.message)) else: resource = to_dict(get_response.data) if check_applicable: is_update_necessary = self.is_update_necessary() if not is_update_necessary: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=resource) if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=resource) update_response = self.update_resource() if not (wait_applicable and self.module.params.get("wait")): updated_resource_response = self.get_resource() updated_resource = to_dict(updated_resource_response.data) return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=updated_resource, ) try: updated_resource = self.update_wait(update_response) except ServiceError as se: self.module.fail_json( msg="Updating resource failed with exception: {0}".format( se.message)) else: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(updated_resource), )
def _add_network_security_group_rules_idempotency_check(self): existing_security_rules = self.list_security_rules() provided_security_rules = self.module.params.get("security_rules", []) provided_security_rules_to_add = [] existing_security_rules_as_dicts = [ to_dict(security_rule) for security_rule in existing_security_rules ] for provided_security_rule in provided_security_rules: if not oci_common_utils.is_in_list( existing_security_rules_as_dicts, element=provided_security_rule): provided_security_rules_to_add.append(provided_security_rule) if len(provided_security_rules_to_add) == 0: resource = AddedNetworkSecurityGroupSecurityRules( security_rules=self.list_security_rules()) return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=to_dict(resource), ) else: self.module.params[ "security_rules"] = provided_security_rules_to_add
def delete(self): super_result = super(ApiKeyHelperCustom, self).delete() return oci_common_utils.get_result( changed=super_result.get("changed"), resource_type=self.resource_type, resource=oci_common_utils.get_resource_with_state( super_result.get(self.resource_type), "DELETED"), )
def delete(self, *args, **kwargs): super_result = super(ApiKeyHelperCustom, self).delete(wait_applicable=False) return oci_common_utils.get_result( changed=super_result.get("changed"), resource_type=self.resource_type, resource=oci_common_utils.get_resource_with_state( super_result.get(self.resource_type), "DELETED"), )
def create(self, check_applicable=True, wait_applicable=True): if self.module.params.get("force_create"): if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=dict()) elif check_applicable: resource_matched = self.get_matching_resource() if resource_matched: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=to_dict(resource_matched), ) if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=dict()) create_response = self.create_resource() if not (wait_applicable and self.module.params.get("wait", None)): return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=create_response.data, ) try: created_resource = self.create_wait(create_response) except MaximumWaitTimeExceeded as ex: self.module.fail_json(msg=str(ex)) except ServiceError as se: self.module.fail_json(msg=se.message) else: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(created_resource), )
def perform_action(self, action): action_fn = self.get_action_fn(action) if not action_fn: self.module.fail_json( msg="{0} not supported by the module.".format(action)) try: get_response = self.get_resource() except ServiceError as se: self.module.fail_json( msg="Getting resource failed with exception: {0}".format( se.message)) else: resource = to_dict(get_response.data) is_action_necessary = self.is_action_necessary(action) if not is_action_necessary: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=resource) if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=resource) try: actioned_resource = action_fn() except MaximumWaitTimeExceeded as mwtex: self.module.fail_json(msg=str(mwtex)) except ServiceError as se: self.module.fail_json( msg="Performing action failed with exception: {0}".format( se.message)) else: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(actioned_resource), )
def update(self): try: get_response = self.get_resource() except ServiceError as se: self.module.fail_json( msg="Getting resource failed with exception: {0}".format( se.message)) else: resource = to_dict(get_response.data) is_update_necessary = self.is_update_necessary() if not is_update_necessary: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=resource) if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=resource) try: updated_resource = self.update_resource() except MaximumWaitTimeExceeded as mwtex: self.module.fail_json(msg=str(mwtex)) except ServiceError as se: self.module.fail_json( msg="Updating resource failed with exception: {0}".format( se.message)) else: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(updated_resource), )
def create(self): if self.module.params.get("force_create"): if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=dict()) else: resource_matched = self.get_matching_resource() if resource_matched: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=to_dict(resource_matched), ) if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=dict()) try: created_resource = self.create_resource() except MaximumWaitTimeExceeded as ex: self.module.fail_json(msg=str(ex)) except ServiceError as se: self.module.fail_json(msg=se.message) else: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(created_resource), )
def _remove_network_security_group_rules_idempotency_check(self): existing_security_rules = self.list_security_rules() provided_security_rule_ids_to_delete = self.module.params.get( "security_rule_ids", []) security_rule_ids_to_delete = [] for existing_security_rule in existing_security_rules: if existing_security_rule.id in provided_security_rule_ids_to_delete: security_rule_ids_to_delete.append(existing_security_rule.id) if len(security_rule_ids_to_delete) == 0: # RemoveNetworkSecurityGroupSecurityRules returns nothing, but in order to keep return type consistent # across add / remove / delete, we choose to return UpdatedNetworkSecurityGroupSecurityRules with an # empty 'security_rules' list resource = UpdatedNetworkSecurityGroupSecurityRules( security_rules=self.list_security_rules()) return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=to_dict(resource), ) else: self.module.params[ "security_rule_ids"] = security_rule_ids_to_delete
def _update_network_security_group_rules_idempotency_check(self): existing_security_rules = self.list_security_rules() provided_security_rules = self.module.params.get("security_rules", []) existing_security_rules_as_dicts = [ to_dict(security_rule) for security_rule in existing_security_rules ] all_rules_to_update_already_exist_and_match = True for provided_security_rule in provided_security_rules: if not oci_common_utils.is_in_list( existing_security_rules_as_dicts, element=provided_security_rule): all_rules_to_update_already_exist_and_match = False if all_rules_to_update_already_exist_and_match: resource = UpdatedNetworkSecurityGroupSecurityRules( security_rules=self.list_security_rules()) return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=to_dict(resource), )
def delete(self, wait_applicable=True): try: if not self.get_module_resource_id(): self.module.fail_json( msg="Specify {0} with state as 'absent' to delete a {1}.".format( self.get_module_resource_id_param(), self.resource_type.upper() ) ) except NotImplementedError: # a few resources have no resource identifier (because they don't follow the # normal path convention: DELETE /resources/{resourceId} (e.g. AppCatalogSubscription) # so there can be a delete without a resourceId pass try: get_response = self.get_resource() except ServiceError as se: if se.status == 404: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=dict() ) self.module.fail_json( msg="Getting resource failed with exception: {0}".format(se.message) ) else: resource = to_dict(get_response.data) if "lifecycle_state" in resource and resource["lifecycle_state"] in [ "DETACHING", "DETACHED", "DELETING", "DELETED", "TERMINATING", "TERMINATED", ]: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=resource ) if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=oci_common_utils.get_resource_with_state(resource, "DELETED"), ) delete_response = self.delete_resource() if not (wait_applicable and self.module.params.get("wait", None)): return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=resource ) try: deleted_resource = self.delete_wait(delete_response) except ServiceError as se: if se.status == 404: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=oci_common_utils.get_resource_with_state( resource, "DELETED" ), ) self.module.fail_json( msg="Deleting resource failed with exception: {0}".format(se.message) ) else: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(deleted_resource), )
def perform_action(self, action): action_fn = self.get_action_fn(action) if not action_fn: self.module.fail_json( msg="{0} not supported by the module.".format(action)) # the idempotency checks for these actions are custom since we aren't doing the regular # check for existence, we are checking if a requested resource is present within a list if action == "add_network_security_group_security_rules": action_idempotency_checks_fn = ( self._add_network_security_group_rules_idempotency_check) check_mode_response_resource = to_dict( AddedNetworkSecurityGroupSecurityRules(security_rules=[])) elif action == "update_network_security_group_security_rules": action_idempotency_checks_fn = ( self._update_network_security_group_rules_idempotency_check) check_mode_response_resource = to_dict( UpdatedNetworkSecurityGroupSecurityRules(security_rules=[])) elif action == "remove_network_security_group_security_rules": action_idempotency_checks_fn = ( self._remove_network_security_group_rules_idempotency_check) # RemoveNetworkSecurityGroupSecurityRules returns nothing, but in order to keep return type consistent # across add / remove / delete, we choose to return UpdatedNetworkSecurityGroupSecurityRules with an # empty 'security_rules' list check_mode_response_resource = to_dict( UpdatedNetworkSecurityGroupSecurityRules(security_rules=[])) else: self.module.fail_json( msg="Performing action failed for unrecognized action: {0}". format(action)) result = action_idempotency_checks_fn() if result: return result if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=check_mode_response_resource, ) try: actioned_resource = action_fn() except MaximumWaitTimeExceeded as mwtex: self.module.fail_json(msg=str(mwtex)) except ServiceError as se: self.module.fail_json( msg="Performing action failed with exception: {0}".format( se.message)) else: # - the individual action operations return the rules that were acted on (except REMOVE which returns nothing) # to keep consistent with patterns in other modules, we override here to return the current set of all rules # - in order to return the same format as the generated docs for actions operations (result.security_rule.security_rules) # we use AddedNetworkSecurityGroupSecurityRules here as a wrapper resource = AddedNetworkSecurityGroupSecurityRules( security_rules=self.list_security_rules()) return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(resource), )
def delete(self, wait_applicable=True): if not self.get_module_resource_id(): self.module.fail_json( msg="Specify {0} with state as 'absent' to delete a {1}.". format(self.get_module_resource_id_param(), self.resource_type.upper())) try: get_response = self.get_resource() except ServiceError as se: if se.status == 404: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=dict()) self.module.fail_json( msg="Getting resource failed with exception: {0}".format( se.message)) else: resource = to_dict(get_response.data) if "lifecycle_state" in resource and resource[ "lifecycle_state"] in [ "DETACHING", "DETACHED", "DELETING", "DELETED", "TERMINATING", "TERMINATED", ]: return oci_common_utils.get_result( changed=False, resource_type=self.resource_type, resource=resource) if self.check_mode: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=oci_common_utils.get_resource_with_state( resource, "DELETED"), ) delete_response = self.delete_resource() if not (wait_applicable and self.module.params.get("wait", None)): return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=resource) try: deleted_resource = self.delete_wait(delete_response) except ServiceError as se: if se.status == 404: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=oci_common_utils.get_resource_with_state( resource, "DELETED"), ) self.module.fail_json( msg="Deleting resource failed with exception: {0}".format( se.message)) else: return oci_common_utils.get_result( changed=True, resource_type=self.resource_type, resource=to_dict(deleted_resource), )