Search in sources :

Example 1 with BucketDeleteTimeoutException

use of bio.terra.workspace.service.resource.controlled.exception.BucketDeleteTimeoutException in project terra-workspace-manager by DataBiosphere.

the class DeleteGcsBucketStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException {
    int deleteTries = 0;
    final GcpCloudContext gcpCloudContext = FlightUtils.getRequired(flightContext.getWorkingMap(), ControlledResourceKeys.GCP_CLOUD_CONTEXT, GcpCloudContext.class);
    final StorageCow storageCow = crlService.createStorageCow(gcpCloudContext.getGcpProjectId());
    // If the bucket is already deleted (e.g. this step is being retried), storageCow.get() will
    // return null.
    BucketCow bucket = storageCow.get(resource.getBucketName());
    boolean bucketExists = bucket != null;
    while (bucketExists) {
        // We always replace the lifecycle rules. This covers the case where the step is rerun
        // and covers the case where the rules are changed out of band of this operation.
        BucketCow bucketCow = bucket.toBuilder().setLifecycleRules(ImmutableList.of(new BucketInfo.LifecycleRule(BucketInfo.LifecycleRule.LifecycleAction.newDeleteAction(), BucketInfo.LifecycleRule.LifecycleCondition.newBuilder().setAge(0).build()))).build();
        bucket = bucketCow.update();
        bucketExists = tryBucketDelete(bucket);
        if (bucketExists) {
            TimeUnit.HOURS.sleep(1);
        }
        deleteTries++;
        if (deleteTries >= MAX_DELETE_TRIES) {
            // This will cause the flight to fail.
            throw new BucketDeleteTimeoutException(String.format("Failed to delete bucket after %d tries", MAX_DELETE_TRIES));
        }
    }
    return StepResult.getStepResultSuccess();
}
Also used : BucketCow(bio.terra.cloudres.google.storage.BucketCow) BucketDeleteTimeoutException(bio.terra.workspace.service.resource.controlled.exception.BucketDeleteTimeoutException) StorageCow(bio.terra.cloudres.google.storage.StorageCow) BucketInfo(com.google.cloud.storage.BucketInfo) GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext)

Aggregations

BucketCow (bio.terra.cloudres.google.storage.BucketCow)1 StorageCow (bio.terra.cloudres.google.storage.StorageCow)1 BucketDeleteTimeoutException (bio.terra.workspace.service.resource.controlled.exception.BucketDeleteTimeoutException)1 GcpCloudContext (bio.terra.workspace.service.workspace.model.GcpCloudContext)1 BucketInfo (com.google.cloud.storage.BucketInfo)1