def delete_using_name(self): try: get_response = self.get_resource_using_name() except ServiceError as se: if se.status == 404: return self.prepare_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 oci_common_utils.DEAD_STATES ): return self.prepare_result( changed=False, resource_type=self.resource_type, resource=resource ) if self.check_mode: return self.prepare_result( changed=True, resource_type=self.resource_type, resource=oci_common_utils.get_resource_with_state(resource, "DELETED"), ) self.set_required_ids_in_module_when_name_is_identifier(resource) try: deleted_resource = self.delete_resource() except MaximumWaitTimeExceeded as mwtex: self.module.fail_json(msg=str(mwtex)) except ServiceError as se: if se.status == 404: return self.prepare_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: if deleted_resource: resource = to_dict(deleted_resource) return self.prepare_result( changed=True, resource_type=self.resource_type, resource=resource )
def delete(self): 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 self.prepare_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 self.is_resource_dead(get_response.data): return self.prepare_result(changed=False, resource_type=self.resource_type, resource=resource) if self.check_mode: return self.prepare_result( changed=True, resource_type=self.resource_type, resource=oci_common_utils.get_resource_with_state( resource, "DELETED"), ) try: deleted_resource = self.delete_resource() except MaximumWaitTimeExceeded as mwtex: self.module.fail_json(msg=str(mwtex)) except ServiceError as se: if se.status == 404: return self.prepare_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: if deleted_resource: resource = to_dict(deleted_resource) return self.prepare_result( changed=True, resource_type=self.resource_type, resource=oci_common_utils.get_resource_with_state( resource, "DELETED"), )