Search in sources :

Example 6 with LifecycleRule

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));
}
Also used : GcsApiConversions.toBucketInfo(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.GcsApiConversions.toBucketInfo) BucketInfo(com.google.cloud.storage.BucketInfo) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule) ApiGcpGcsBucketLifecycleRule(bio.terra.workspace.generated.model.ApiGcpGcsBucketLifecycleRule) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Example 7 with LifecycleRule

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());
}
Also used : ApiGcpGcsBucketLifecycleRule(bio.terra.workspace.generated.model.ApiGcpGcsBucketLifecycleRule) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule) ApiGcpGcsBucketLifecycleRule(bio.terra.workspace.generated.model.ApiGcpGcsBucketLifecycleRule) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Example 8 with LifecycleRule

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);
}
Also used : RemoteStorageHelper(com.google.cloud.storage.testing.RemoteStorageHelper) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule) BeforeClass(org.junit.BeforeClass)

Example 9 with LifecycleRule

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));
}
Also used : SetStorageClassLifecycleAction(com.google.cloud.storage.BucketInfo.LifecycleRule.SetStorageClassLifecycleAction) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule)

Example 10 with LifecycleRule

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()));
}
Also used : FlightState(bio.terra.stairway.FlightState) BucketCow(bio.terra.cloudres.google.storage.BucketCow) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) StorageCow(bio.terra.cloudres.google.storage.StorageCow) BucketInfo(com.google.cloud.storage.BucketInfo) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest)

Aggregations

LifecycleRule (com.google.cloud.storage.BucketInfo.LifecycleRule)14 BaseUnitTest (bio.terra.workspace.common.BaseUnitTest)5 Test (org.junit.jupiter.api.Test)5 StepResult (bio.terra.stairway.StepResult)4 SetStorageClassLifecycleAction (com.google.cloud.storage.BucketInfo.LifecycleRule.SetStorageClassLifecycleAction)4 StorageClass (com.google.cloud.storage.StorageClass)4 BucketInfo (com.google.cloud.storage.BucketInfo)3 LifecycleCondition (com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleCondition)3 Test (org.junit.Test)3 BucketCow (bio.terra.cloudres.google.storage.BucketCow)2 StorageCow (bio.terra.cloudres.google.storage.StorageCow)2 ApiGcpGcsBucketLifecycleRule (bio.terra.workspace.generated.model.ApiGcpGcsBucketLifecycleRule)2 DateTime (com.google.api.client.util.DateTime)2 Rule (com.google.api.services.storage.model.Bucket.Lifecycle.Rule)2 Bucket (com.google.cloud.storage.Bucket)2 AgeDeleteRule (com.google.cloud.storage.BucketInfo.AgeDeleteRule)2 CreatedBeforeDeleteRule (com.google.cloud.storage.BucketInfo.CreatedBeforeDeleteRule)2 DeleteRule (com.google.cloud.storage.BucketInfo.DeleteRule)2 IsLiveDeleteRule (com.google.cloud.storage.BucketInfo.IsLiveDeleteRule)2 DeleteLifecycleAction (com.google.cloud.storage.BucketInfo.LifecycleRule.DeleteLifecycleAction)2