use of com.google.cloud.storage.BucketInfo.LifecycleRule in project terra-workspace-manager by DataBiosphere.
the class GcsApiConversionsTest method testToBucketInfo.
@Test
public void testToBucketInfo() {
final String bucketName = uniqueBucketName();
final BucketInfo bucketInfo1 = toBucketInfo(bucketName, ControlledResourceFixtures.BUCKET_UPDATE_PARAMETERS_1);
assertEquals(bucketName, bucketInfo1.getName());
assertEquals(StorageClass.STANDARD, bucketInfo1.getStorageClass());
assertThat(bucketInfo1.getLifecycleRules(), hasSize(2));
final LifecycleRule gcsRule1 = bucketInfo1.getLifecycleRules().get(0);
assertEquals(DeleteLifecycleAction.TYPE, gcsRule1.getAction().getActionType());
assertEquals(31, gcsRule1.getCondition().getAge());
assertEquals(toGoogleDateTimeDateOnly(OFFSET_DATE_TIME_2), gcsRule1.getCondition().getCreatedBefore());
assertTrue(gcsRule1.getCondition().getIsLive());
assertThat(gcsRule1.getCondition().getMatchesStorageClass(), hasSize(2));
}
use of com.google.cloud.storage.BucketInfo.LifecycleRule in project terra-workspace-manager by DataBiosphere.
the class GcsApiConversionsTest method testLifecycleRuleConversions.
@Test
public void testLifecycleRuleConversions() {
final ApiGcpGcsBucketLifecycleRule wsmRule1 = toWsmApi(GCS_LIFECYCLE_RULE_1);
assertEquals(ApiGcpGcsBucketLifecycleRuleActionType.DELETE, wsmRule1.getAction().getType());
assertNull(wsmRule1.getAction().getStorageClass());
assertEquals(42, wsmRule1.getCondition().getAge());
assertFalse(wsmRule1.getCondition().isLive());
assertEquals(2, wsmRule1.getCondition().getNumNewerVersions());
// not empty list
assertNull(wsmRule1.getCondition().getMatchesStorageClass());
final LifecycleRule gcsRule1 = toGcsApi(wsmRule1);
// Direct equality comparison with LifecycleAction and LifecycleCondition
// types doesn't work for some reason. Hence, comparing fields.
assertEquals(GCS_LIFECYCLE_RULE_1.getAction().getActionType(), gcsRule1.getAction().getActionType());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getAge(), gcsRule1.getCondition().getAge());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getCreatedBefore(), gcsRule1.getCondition().getCreatedBefore());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getMatchesStorageClass(), gcsRule1.getCondition().getMatchesStorageClass());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getNumberOfNewerVersions(), gcsRule1.getCondition().getNumberOfNewerVersions());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getIsLive(), gcsRule1.getCondition().getIsLive());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getDaysSinceNoncurrentTime(), gcsRule1.getCondition().getDaysSinceNoncurrentTime());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getNoncurrentTimeBefore(), gcsRule1.getCondition().getNoncurrentTimeBefore());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getCustomTimeBefore(), gcsRule1.getCondition().getCustomTimeBefore());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getDaysSinceCustomTime(), gcsRule1.getCondition().getDaysSinceCustomTime());
}
use of com.google.cloud.storage.BucketInfo.LifecycleRule in project java-docs-samples by GoogleCloudPlatform.
the class ITStoragetransferSamplesTest method beforeClass.
@BeforeClass
public static void beforeClass() throws Exception {
RemoteStorageHelper helper = RemoteStorageHelper.create();
storage = helper.getOptions().getService();
storage.create(BucketInfo.newBuilder(SOURCE_GCS_BUCKET).setLocation("us").setLifecycleRules(ImmutableList.of(new LifecycleRule(LifecycleAction.newDeleteAction(), LifecycleCondition.newBuilder().setAge(1).build()))).build());
storage.create(BucketInfo.newBuilder(SINK_GCS_BUCKET).setLocation("us").setLifecycleRules(ImmutableList.of(new LifecycleRule(LifecycleAction.newDeleteAction(), LifecycleCondition.newBuilder().setAge(1).build()))).setStorageClass(StorageClass.NEARLINE).build());
sts = StorageTransferServiceClient.create();
String serviceAccount = sts.getGoogleServiceAccount(GetGoogleServiceAccountRequest.newBuilder().setProjectId(PROJECT_ID).build()).getAccountEmail();
grantBucketsStsPermissions(serviceAccount, SOURCE_GCS_BUCKET);
grantBucketsStsPermissions(serviceAccount, SINK_GCS_BUCKET);
s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.US_WEST_1).build();
s3.createBucket(AMAZON_BUCKET);
}
use of com.google.cloud.storage.BucketInfo.LifecycleRule in project terra-workspace-manager by DataBiosphere.
the class ControlledGcsBucketLifecycle method verifyClonedLifecycleRules.
private void verifyClonedLifecycleRules(Bucket destinationBucket) {
// We can't rely on the order of the lifecycle rules being maintained
final LifecycleRule clonedDeleteRule = destinationBucket.getLifecycleRules().stream().filter(r -> DeleteLifecycleAction.TYPE.equals(r.getAction().getActionType())).findFirst().orElseThrow(() -> new RuntimeException("Can't find Delete lifecycle rule."));
assertEquals(BUCKET_LIFECYCLE_RULE_1_CONDITION_AGE, clonedDeleteRule.getCondition().getAge());
assertEquals(BUCKET_LIFECYCLE_RULE_1_CONDITION_LIVE, clonedDeleteRule.getCondition().getIsLive());
assertEquals(StorageClass.ARCHIVE, clonedDeleteRule.getCondition().getMatchesStorageClass().get(0));
assertEquals(BUCKET_LIFECYCLE_RULE_1_CONDITION_NUM_NEWER_VERSIONS, clonedDeleteRule.getCondition().getNumberOfNewerVersions());
final LifecycleRule setStorageClassRule = destinationBucket.getLifecycleRules().stream().filter(r -> SetStorageClassLifecycleAction.TYPE.equals(r.getAction().getActionType())).findFirst().orElseThrow(() -> new RuntimeException("Can't find SetStorageClass lifecycle rule."));
final SetStorageClassLifecycleAction setStorageClassLifecycleAction = (SetStorageClassLifecycleAction) setStorageClassRule.getAction();
assertEquals(StorageClass.NEARLINE, setStorageClassLifecycleAction.getStorageClass());
assertEquals(DateTime.parseRfc3339("2007-01-03"), setStorageClassRule.getCondition().getCreatedBefore());
assertThat(setStorageClassRule.getCondition().getMatchesStorageClass(), contains(StorageClass.STANDARD));
}
use of com.google.cloud.storage.BucketInfo.LifecycleRule in project terra-workspace-manager by DataBiosphere.
the class ControlledResourceServiceTest method cloneGcsBucketDo.
@Test
void cloneGcsBucketDo() throws InterruptedException {
final ControlledGcsBucketResource resource = ControlledResourceFixtures.makeDefaultControlledGcsBucketBuilder(workspace.getWorkspaceId()).build();
final ControlledGcsBucketResource createdBucket = controlledResourceService.createControlledResourceSync(resource, null, user.getAuthenticatedRequest(), ControlledResourceFixtures.getGoogleBucketCreationParameters()).castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
assertEquals(resource, createdBucket);
final ApiJobControl apiJobControl = new ApiJobControl().id(UUID.randomUUID().toString());
final String destinationBucketName = "cloned-bucket-" + UUID.randomUUID().toString().toLowerCase();
final String destinationLocation = "US-EAST1";
final String jobId = controlledResourceService.cloneGcsBucket(workspace.getWorkspaceId(), createdBucket.getResourceId(), // copy back into same workspace
workspace.getWorkspaceId(), apiJobControl, user.getAuthenticatedRequest(), "cloned_bucket", "A bucket cloned individually into the same workspace.", destinationBucketName, destinationLocation, ApiCloningInstructionsEnum.RESOURCE);
jobService.waitForJob(jobId);
final FlightState flightState = stairwayComponent.get().getFlightState(jobId);
assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
assertTrue(flightState.getException().isEmpty());
assertTrue(flightState.getResultMap().isPresent());
var response = flightState.getResultMap().get().get(JobMapKeys.RESPONSE.getKeyName(), ApiClonedControlledGcpGcsBucket.class);
assertNotNull(response);
assertEquals(destinationBucketName, response.getBucket().getGcpBucket().getAttributes().getBucketName());
final UUID resourceId = response.getBucket().getResourceId();
final ControlledResource destinationControlledResource = controlledResourceService.getControlledResource(workspace.getWorkspaceId(), resourceId, user.getAuthenticatedRequest());
final ControlledGcsBucketResource destinationBucketResource = destinationControlledResource.castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
assertNotNull(destinationBucketResource);
assertEquals("cloned_bucket", destinationBucketResource.getName());
// check creation parameters on cloud (not stored by WSM). Source project is same as destination
// in this case.
final StorageCow storageCow = crlService.createStorageCow(gcpCloudContextService.getRequiredGcpProject(workspace.getWorkspaceId()));
final BucketCow bucketCow = storageCow.get(destinationBucketName);
final BucketInfo bucketInfo = bucketCow.getBucketInfo();
assertEquals(destinationLocation, bucketInfo.getLocation());
assertEquals(GcsApiConversions.toGcsApi(ControlledResourceFixtures.getGoogleBucketCreationParameters().getDefaultStorageClass()), bucketInfo.getStorageClass());
final List<LifecycleRule> expectedLifecycleRules = GcsApiConversions.toGcsApi(ControlledResourceFixtures.getGoogleBucketCreationParameters().getLifecycle().getRules());
assertThat(expectedLifecycleRules, containsInAnyOrder(bucketInfo.getLifecycleRules().toArray()));
}
Aggregations