use of bio.terra.cloudres.google.storage.BucketCow in project terra-workspace-manager by DataBiosphere.
the class UpdateGcsBucketStep method updateBucket.
private StepResult updateBucket(@Nullable ApiGcpGcsBucketUpdateParameters updateParameters) {
if (updateParameters == null) {
// nothing to change
logger.info("No update parameters supplied, so no changes to make.");
return StepResult.getStepResultSuccess();
}
final String projectId = gcpCloudContextService.getRequiredGcpProject(bucketResource.getWorkspaceId());
final StorageCow storageCow = crlService.createStorageCow(projectId);
final BucketCow existingBucketCow = storageCow.get(bucketResource.getBucketName());
if (existingBucketCow == null) {
IllegalStateException isEx = new IllegalStateException("No bucket found to update with name " + bucketResource.getBucketName());
logger.error("No bucket found to update with name {}.", bucketResource.getBucketName(), isEx);
return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, isEx);
}
final List<LifecycleRule> gcsLifecycleRules = toGcsApiRulesList(updateParameters.getLifecycle());
// An empty array will clear all rules. A null array will take no effect. A populated array will
// clear then set rules
final boolean doReplaceLifecycleRules = gcsLifecycleRules != null;
// Lifecycle rules need to be cleared before being set. We should only do this if
// we have changes.
final BucketCow.Builder bucketCowBuilder;
if (doReplaceLifecycleRules) {
final var deleteBuilder = existingBucketCow.toBuilder();
deleteBuilder.deleteLifecycleRules();
var clearedRulesBucket = deleteBuilder.build().update();
// do separate update to set the lifecycle rules
bucketCowBuilder = clearedRulesBucket.toBuilder();
bucketCowBuilder.setLifecycleRules(gcsLifecycleRules);
} else {
// do not delete the lifecycle rules, as they are not changing
bucketCowBuilder = existingBucketCow.toBuilder();
}
final StorageClass gcsStorageClass = toGcsApi(updateParameters.getDefaultStorageClass());
final boolean replaceStorageClass = gcsStorageClass != null;
if (replaceStorageClass) {
bucketCowBuilder.setStorageClass(gcsStorageClass);
}
if (doReplaceLifecycleRules || replaceStorageClass) {
bucketCowBuilder.build().update();
} else {
logger.info("Cloud attributes for Bucket {} were not changed as all inputs were null.", bucketResource.getBucketName());
}
return StepResult.getStepResultSuccess();
}
use of bio.terra.cloudres.google.storage.BucketCow in project terra-cli by DataBiosphere.
the class ExternalGCSBuckets method createBucket.
/**
* Create a bucket in an external project. This is helpful for testing referenced GCS bucket
* resources. This method uses SA credentials that have permissions in the external (to WSM)
* project.
*/
private static BucketInfo createBucket(boolean isUniformBucketLevelAccessEnabled) throws IOException {
String bucketName = UUID.randomUUID().toString();
StorageClass storageClass = StorageClass.STANDARD;
String location = "US";
BucketCow bucket = getStorageCow().create(BucketInfo.newBuilder(bucketName).setStorageClass(storageClass).setLocation(location).setIamConfiguration(IamConfiguration.newBuilder().setIsUniformBucketLevelAccessEnabled(isUniformBucketLevelAccessEnabled).build()).build());
BucketInfo bucketInfo = bucket.getBucketInfo();
System.out.println("Created bucket " + bucketInfo.getName() + " in " + bucketInfo.getLocation() + " with storage class " + bucketInfo.getStorageClass() + " in project " + TestConfig.get().getProjectForExternalResources());
return bucketInfo;
}
use of bio.terra.cloudres.google.storage.BucketCow 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();
}
use of bio.terra.cloudres.google.storage.BucketCow in project terra-workspace-manager by DataBiosphere.
the class RetrieveGcsBucketCloudAttributesStep method doStep.
@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
final FlightMap workingMap = flightContext.getWorkingMap();
final String projectId = gcpCloudContextService.getRequiredGcpProject(bucketResource.getWorkspaceId());
// get the storage cow
final StorageCow storageCow = crlService.createStorageCow(projectId);
// get the existing bucket cow
final BucketCow existingBucketCow = storageCow.get(bucketResource.getBucketName());
if (existingBucketCow == null) {
logger.error("Can't construct COW for pre-existing bucket {}", bucketResource.getBucketName());
return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, null);
}
// get the attributes
final BucketInfo existingBucketInfo = existingBucketCow.getBucketInfo();
switch(retrievalMode) {
case UPDATE_PARAMETERS:
final ApiGcpGcsBucketUpdateParameters existingUpdateParameters = GcsApiConversions.toUpdateParameters(existingBucketInfo);
workingMap.put(ControlledResourceKeys.PREVIOUS_UPDATE_PARAMETERS, existingUpdateParameters);
break;
case CREATION_PARAMETERS:
final ApiGcpGcsBucketCreationParameters creationParameters = GcsApiConversions.toCreationParameters(existingBucketInfo);
workingMap.put(ControlledResourceKeys.CREATION_PARAMETERS, creationParameters);
break;
default:
throw new BadRequestException(String.format("Unsupported Retrieval mode %s", retrievalMode));
}
return StepResult.getStepResultSuccess();
}
Aggregations