def is_action_necessary(self, action, resource, *args, **kwargs): if action.upper() == "COPY": # volume_backup copied from another backup will have the source backup in source_volume_backup_id parameter. # Use it to find if the copy already exists in the destination region. if not self.module.params.get("destination_region"): self.module.fail_json( msg= "destination_required parameter required for copying the backup." ) this_backup = resource destination_region_client = BlockstorageClient( dict(self.client._config, region=self.module.params["destination_region"]), **self.client._kwargs) for destination_region_backup in [ volume_backup for volume_backup in oci_common_utils.list_all_resources( destination_region_client.list_volume_backups, compartment_id=this_backup.compartment_id, ) if self._is_resource_active(volume_backup) ]: if destination_region_backup.source_volume_backup_id == this_backup.id: # It might not be a very good idea to exit from this method but currently is_action_necessary # does not provide a way to return a resource. # TODO: Check and modify is_action_necessary to return a resource self.module.exit_json(**self.prepare_result( changed=False, resource_type=self.resource_type, resource=to_dict(destination_region_backup), )) return True return super(VolumeBackupActionsHelperCustom, self).is_action_necessary(action, resource, *args, **kwargs)
def copy(self, *args, **kwargs): # The generated copy waits on the source backup lifecycle state. But the source backup remains in AVAILABLE # state when the copy is in progress. So override to wait until the copy is done by checking the destination # backup lifecycle state instead. destination_backup = super(BootVolumeBackupActionsHelperCustom, self).copy(*args, **kwargs) destination_region_client = BlockstorageClient( dict(self.client._config, region=self.module.params.get("destination_region")), **self.client._kwargs) return oci.wait_until( destination_region_client, destination_region_client.get_boot_volume_backup( boot_volume_backup_id=destination_backup.id), evaluate_response=lambda r: r.data.lifecycle_state in oci_common_utils.DEFAULT_READY_STATES, max_wait_seconds=self.get_wait_timeout(), ).data