diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index 30f9ce61f1fc..4ec54435aba6 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -661,6 +661,12 @@ public Builder setIamConfiguration(IamConfiguration iamConfiguration) { return this; } + @Override + Builder setLocationType(String locationType) { + infoBuilder.setLocationType(locationType); + return this; + } + @Override public Bucket build() { return new Bucket(storage, infoBuilder); diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java index c8d1d07a274b..73ba7dc121ac 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java @@ -96,6 +96,7 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo) private final Boolean retentionPolicyIsLocked; private final Long retentionPeriod; private final IamConfiguration iamConfiguration; + private final String locationType; /** * The Bucket's IAM Configuration. @@ -848,6 +849,8 @@ public abstract static class Builder { abstract Builder setMetageneration(Long metageneration); + abstract Builder setLocationType(String locationType); + /** * Sets the bucket's Cross-Origin Resource Sharing (CORS) configuration. * @@ -938,6 +941,7 @@ static final class BuilderImpl extends Builder { private Boolean retentionPolicyIsLocked; private Long retentionPeriod; private IamConfiguration iamConfiguration; + private String locationType; BuilderImpl(String name) { this.name = name; @@ -969,6 +973,7 @@ static final class BuilderImpl extends Builder { retentionPolicyIsLocked = bucketInfo.retentionPolicyIsLocked; retentionPeriod = bucketInfo.retentionPeriod; iamConfiguration = bucketInfo.iamConfiguration; + locationType = bucketInfo.locationType; } @Override @@ -1127,6 +1132,12 @@ public Builder setIamConfiguration(IamConfiguration iamConfiguration) { return this; } + @Override + Builder setLocationType(String locationType) { + this.locationType = locationType; + return this; + } + @Override public BucketInfo build() { checkNotNull(name); @@ -1160,6 +1171,7 @@ public BucketInfo build() { retentionPolicyIsLocked = builder.retentionPolicyIsLocked; retentionPeriod = builder.retentionPeriod; iamConfiguration = builder.iamConfiguration; + locationType = builder.locationType; } /** Returns the service-generated id for the bucket. */ @@ -1283,6 +1295,15 @@ public String getLocation() { return location; } + /** + * Returns the bucket's locationType. + * + * @see Bucket LocationType + */ + public String getLocationType() { + return locationType; + } + /** * Returns the bucket's storage class. This defines how blobs in the bucket are stored and * determines the SLA and the cost of storage. @@ -1442,6 +1463,9 @@ com.google.api.services.storage.model.Bucket toPb() { if (location != null) { bucketPb.setLocation(location); } + if (locationType != null) { + bucketPb.setLocationType(locationType); + } if (storageClass != null) { bucketPb.setStorageClass(storageClass.toString()); } @@ -1666,6 +1690,11 @@ public DeleteRule apply(Rule rule) { } } Bucket.IamConfiguration iamConfiguration = bucketPb.getIamConfiguration(); + + if (bucketPb.getLocationType() != null) { + builder.setLocationType(bucketPb.getLocationType()); + } + if (iamConfiguration != null) { builder.setIamConfiguration(IamConfiguration.fromPb(iamConfiguration)); } diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java index 919afeb0adea..857da2b4ba9f 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java @@ -83,6 +83,9 @@ public class BucketInfoTest { private static final Long RETENTION_EFFECTIVE_TIME = 10L; private static final Long RETENTION_PERIOD = 10L; private static final Boolean RETENTION_POLICY_IS_LOCKED = false; + private static final List LOCATION_TYPES = + ImmutableList.of("multi-region", "region", "dual-region"); + private static final String LOCATION_TYPE = "multi-region"; private static final BucketInfo BUCKET_INFO = BucketInfo.newBuilder("b") .setAcl(ACL) @@ -100,6 +103,7 @@ public class BucketInfoTest { .setIamConfiguration(IAM_CONFIGURATION) .setNotFoundPage(NOT_FOUND_PAGE) .setLocation(LOCATION) + .setLocationType(LOCATION_TYPE) .setStorageClass(STORAGE_CLASS) .setVersioningEnabled(VERSIONING_ENABLED) .setLabels(BUCKET_LABELS) @@ -159,6 +163,7 @@ public void testBuilder() { assertEquals(RETENTION_EFFECTIVE_TIME, BUCKET_INFO.getRetentionEffectiveTime()); assertEquals(RETENTION_PERIOD, BUCKET_INFO.getRetentionPeriod()); assertEquals(RETENTION_POLICY_IS_LOCKED, BUCKET_INFO.retentionPolicyIsLocked()); + assertTrue(LOCATION_TYPES.contains(BUCKET_INFO.getLocationType())); } @Test diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java index 6bbc67b4adfa..9923c6107f24 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java @@ -92,6 +92,9 @@ public class BucketTest { private static final Long RETENTION_EFFECTIVE_TIME = 10L; private static final Long RETENTION_PERIOD = 10L; private static final Boolean RETENTION_POLICY_IS_LOCKED = false; + private static final List LOCATION_TYPES = + ImmutableList.of("multi-region", "region", "dual-region"); + private static final String LOCATION_TYPE = "multi-region"; private static final BucketInfo FULL_BUCKET_INFO = BucketInfo.newBuilder("b") .setAcl(ACLS) @@ -786,6 +789,7 @@ public void testBuilder() { .setIndexPage(INDEX_PAGE) .setNotFoundPage(NOT_FOUND_PAGE) .setLocation(LOCATION) + .setLocationType(LOCATION_TYPE) .setStorageClass(STORAGE_CLASS) .setVersioningEnabled(VERSIONING_ENABLED) .setLabels(BUCKET_LABELS) @@ -821,5 +825,6 @@ public void testBuilder() { assertEquals(RETENTION_PERIOD, bucket.getRetentionPeriod()); assertEquals(RETENTION_POLICY_IS_LOCKED, bucket.retentionPolicyIsLocked()); assertEquals(storage.getOptions(), bucket.getStorage().getOptions()); + assertTrue(LOCATION_TYPES.contains(LOCATION_TYPE)); } } diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 8b2d627b23db..7c3c3e942c5a 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -156,6 +156,8 @@ public class ITStorageTest { private static final boolean IS_VPC_TEST = System.getenv("GOOGLE_CLOUD_TESTS_IN_VPCSC") != null && System.getenv("GOOGLE_CLOUD_TESTS_IN_VPCSC").equalsIgnoreCase("true"); + private static final List LOCATION_TYPES = + ImmutableList.of("multi-region", "region", "dual-region"); @BeforeClass public static void beforeClass() throws IOException { @@ -2584,4 +2586,39 @@ public void testUploadUsingSignedURL() throws Exception { assertEquals(bytesArrayToUpload.length, lengthOfDownLoadBytes); assertTrue(storage.delete(BUCKET, blobName)); } + + @Test + public void testBucketLocationType() throws ExecutionException, InterruptedException { + String bucketName = RemoteStorageHelper.generateBucketName(); + long bucketMetageneration = 42; + storage.create( + BucketInfo.newBuilder(bucketName) + .setLocation("us") + .setRetentionPeriod(RETENTION_PERIOD) + .build()); + Bucket bucket = + storage.get( + bucketName, Storage.BucketGetOption.metagenerationNotMatch(bucketMetageneration)); + assertTrue(LOCATION_TYPES.contains(bucket.getLocationType())); + + Bucket bucket1 = + storage.lockRetentionPolicy(bucket, Storage.BucketTargetOption.metagenerationMatch()); + assertTrue(LOCATION_TYPES.contains(bucket1.getLocationType())); + + Bucket updatedBucket = + storage.update( + BucketInfo.newBuilder(bucketName) + .setLocation("asia") + .setRetentionPeriod(RETENTION_PERIOD) + .build()); + assertTrue(LOCATION_TYPES.contains(updatedBucket.getLocationType())); + + Iterator bucketIterator = + storage.list(Storage.BucketListOption.prefix(bucketName)).iterateAll().iterator(); + while (bucketIterator.hasNext()) { + Bucket remoteBucket = bucketIterator.next(); + assertTrue(LOCATION_TYPES.contains(remoteBucket.getLocationType())); + } + RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS); + } }