Skip to content

Commit 0a528c6

Browse files
authored
feat: delete bucket OLM rules (#352)
* feat: delete bucket OLM rules * feat: update javadoc * feat: restructure the code and add more tests * feat: return empty instead of null to remove all the lifecycle rule of bucket * feat: moved deleteLifecycleRules inside bucketInfo * feat: rollback commented code * feat: delete all LifecycleRules instead of individual rules * feat: updated the code and change the method signature * feat: implemented in the same manner as other methods updating Buckets properties * feat: fix review changes * build: fix clirr checks
1 parent 61b09e5 commit 0a528c6

File tree

7 files changed

+86
-7
lines changed

7 files changed

+86
-7
lines changed

google-cloud-storage/clirr-ignored-differences.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
<method>com.google.cloud.storage.PostPolicyV4 generateSignedPostPolicyV4(com.google.cloud.storage.BlobInfo, long, java.util.concurrent.TimeUnit, com.google.cloud.storage.Storage$PostPolicyV4Option[])</method>
2222
<differenceType>7012</differenceType>
2323
</difference>
24-
</differences>
24+
<difference>
25+
<className>com/google/cloud/storage/BucketInfo$Builder</className>
26+
<method>com.google.cloud.storage.BucketInfo$Builder deleteLifecycleRules()</method>
27+
<differenceType>7013</differenceType>
28+
</difference>
29+
</differences>

google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java

+6
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,12 @@ public Builder setLifecycleRules(Iterable<? extends LifecycleRule> rules) {
571571
return this;
572572
}
573573

574+
@Override
575+
public Builder deleteLifecycleRules() {
576+
infoBuilder.deleteLifecycleRules();
577+
return this;
578+
}
579+
574580
@Override
575581
public Builder setStorageClass(StorageClass storageClass) {
576582
infoBuilder.setStorageClass(storageClass);

google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
import com.google.api.client.util.Data;
2525
import com.google.api.client.util.DateTime;
2626
import com.google.api.core.BetaApi;
27-
import com.google.api.services.storage.model.*;
2827
import com.google.api.services.storage.model.Bucket;
2928
import com.google.api.services.storage.model.Bucket.Encryption;
3029
import com.google.api.services.storage.model.Bucket.Lifecycle;
3130
import com.google.api.services.storage.model.Bucket.Lifecycle.Rule;
3231
import com.google.api.services.storage.model.Bucket.Owner;
3332
import com.google.api.services.storage.model.Bucket.Versioning;
3433
import com.google.api.services.storage.model.Bucket.Website;
34+
import com.google.api.services.storage.model.BucketAccessControl;
35+
import com.google.api.services.storage.model.ObjectAccessControl;
3536
import com.google.cloud.storage.Acl.Entity;
3637
import com.google.common.base.Function;
3738
import com.google.common.base.Functions;
@@ -978,6 +979,9 @@ public abstract static class Builder {
978979
*/
979980
public abstract Builder setLifecycleRules(Iterable<? extends LifecycleRule> rules);
980981

982+
/** Deletes the lifecycle rules of this bucket. */
983+
public abstract Builder deleteLifecycleRules();
984+
981985
/**
982986
* Sets the bucket's storage class. This defines how blobs in the bucket are stored and
983987
* determines the SLA and the cost of storage. A list of supported values is available <a
@@ -1187,7 +1191,15 @@ public Builder setDeleteRules(Iterable<? extends DeleteRule> rules) {
11871191

11881192
@Override
11891193
public Builder setLifecycleRules(Iterable<? extends LifecycleRule> rules) {
1190-
this.lifecycleRules = rules != null ? ImmutableList.copyOf(rules) : null;
1194+
this.lifecycleRules =
1195+
rules != null ? ImmutableList.copyOf(rules) : ImmutableList.<LifecycleRule>of();
1196+
return this;
1197+
}
1198+
1199+
@Override
1200+
public Builder deleteLifecycleRules() {
1201+
setDeleteRules(null);
1202+
setLifecycleRules(null);
11911203
return this;
11921204
}
11931205

@@ -1434,7 +1446,7 @@ public List<? extends DeleteRule> getDeleteRules() {
14341446
}
14351447

14361448
public List<? extends LifecycleRule> getLifecycleRules() {
1437-
return lifecycleRules;
1449+
return lifecycleRules != null ? lifecycleRules : ImmutableList.<LifecycleRule>of();
14381450
}
14391451

14401452
/**
@@ -1711,11 +1723,13 @@ public Rule apply(LifecycleRule lifecycleRule) {
17111723
}
17121724
}));
17131725
}
1714-
if (!rules.isEmpty()) {
1726+
1727+
if (rules != null) {
17151728
Lifecycle lifecycle = new Lifecycle();
17161729
lifecycle.setRule(ImmutableList.copyOf(rules));
17171730
bucketPb.setLifecycle(lifecycle);
17181731
}
1732+
17191733
if (labels != null) {
17201734
bucketPb.setLabels(labels);
17211735
}
@@ -1765,6 +1779,7 @@ static BucketInfo fromPb(com.google.api.services.storage.model.Bucket bucketPb)
17651779
if (bucketPb.getId() != null) {
17661780
builder.setGeneratedId(bucketPb.getId());
17671781
}
1782+
17681783
if (bucketPb.getEtag() != null) {
17691784
builder.setEtag(bucketPb.getEtag());
17701785
}

google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java

-1
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,6 @@ Blob create(
19991999
* only if supplied Decrpytion Key decrypts the blob successfully, otherwise a {@link
20002000
* StorageException} is thrown. For more information review
20012001
*
2002-
* @throws StorageException upon failure
20032002
* @see <a
20042003
* href="https://cloud.google.com/storage/docs/encryption/customer-supplied-keys#encrypted-elements">Encrypted
20052004
* Elements</a>

google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ public void testBuilder() {
225225
@Test
226226
public void testToPbAndFromPb() {
227227
compareBuckets(BUCKET_INFO, BucketInfo.fromPb(BUCKET_INFO.toPb()));
228-
BucketInfo bucketInfo = BucketInfo.of("b");
228+
BucketInfo bucketInfo =
229+
BucketInfo.newBuilder("b")
230+
.setDeleteRules(DELETE_RULES)
231+
.setLifecycleRules(LIFECYCLE_RULES)
232+
.build();
229233
compareBuckets(bucketInfo, BucketInfo.fromPb(bucketInfo.toPb()));
230234
}
231235

google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.storage;
1818

1919
import static com.google.cloud.storage.Acl.Role.WRITER;
20+
import static com.google.common.truth.Truth.assertThat;
2021
import static org.easymock.EasyMock.createMock;
2122
import static org.easymock.EasyMock.createStrictMock;
2223
import static org.easymock.EasyMock.expect;
@@ -846,4 +847,20 @@ public void testBuilder() {
846847
assertEquals(storage.getOptions(), bucket.getStorage().getOptions());
847848
assertTrue(LOCATION_TYPES.contains(LOCATION_TYPE));
848849
}
850+
851+
@Test
852+
public void testDeleteLifecycleRules() {
853+
initializeExpectedBucket(6);
854+
Bucket bucket =
855+
new Bucket(serviceMockReturnsOptions, new BucketInfo.BuilderImpl(FULL_BUCKET_INFO));
856+
assertThat(bucket.getLifecycleRules()).hasSize(1);
857+
Bucket expectedUpdatedBucket = bucket.toBuilder().deleteLifecycleRules().build();
858+
expect(storage.getOptions()).andReturn(mockOptions).times(2);
859+
expect(storage.update(expectedUpdatedBucket)).andReturn(expectedUpdatedBucket);
860+
replay(storage);
861+
initializeBucket();
862+
Bucket updatedBucket = new Bucket(storage, new BucketInfo.BuilderImpl(expectedUpdatedBucket));
863+
Bucket actualUpdatedBucket = updatedBucket.update();
864+
assertThat(actualUpdatedBucket.getLifecycleRules()).hasSize(0);
865+
}
849866
}

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ public class ITStorageTest {
181181
&& System.getenv("GOOGLE_CLOUD_TESTS_IN_VPCSC").equalsIgnoreCase("true");
182182
private static final List<String> LOCATION_TYPES =
183183
ImmutableList.of("multi-region", "region", "dual-region");
184+
private static final LifecycleRule LIFECYCLE_RULE_1 =
185+
new LifecycleRule(
186+
LifecycleAction.newSetStorageClassAction(StorageClass.COLDLINE),
187+
LifecycleCondition.newBuilder()
188+
.setAge(1)
189+
.setNumberOfNewerVersions(3)
190+
.setIsLive(false)
191+
.setMatchesStorageClass(ImmutableList.of(StorageClass.COLDLINE))
192+
.build());
193+
private static final LifecycleRule LIFECYCLE_RULE_2 =
194+
new LifecycleRule(
195+
LifecycleAction.newDeleteAction(), LifecycleCondition.newBuilder().setAge(1).build());
196+
private static final ImmutableList<LifecycleRule> LIFECYCLE_RULES =
197+
ImmutableList.of(LIFECYCLE_RULE_1, LIFECYCLE_RULE_2);
184198

185199
@BeforeClass
186200
public static void beforeClass() throws IOException {
@@ -3279,4 +3293,23 @@ public void testBlobReload() throws Exception {
32793293
updated.delete();
32803294
assertNull(updated.reload());
32813295
}
3296+
3297+
@Test
3298+
public void testDeleteLifecycleRules() throws ExecutionException, InterruptedException {
3299+
String bucketName = RemoteStorageHelper.generateBucketName();
3300+
Bucket bucket =
3301+
storage.create(
3302+
BucketInfo.newBuilder(bucketName)
3303+
.setLocation("us")
3304+
.setLifecycleRules(LIFECYCLE_RULES)
3305+
.build());
3306+
assertThat(bucket.getLifecycleRules()).isNotNull();
3307+
assertThat(bucket.getLifecycleRules()).hasSize(2);
3308+
try {
3309+
Bucket updatedBucket = bucket.toBuilder().deleteLifecycleRules().build().update();
3310+
assertThat(updatedBucket.getLifecycleRules()).hasSize(0);
3311+
} finally {
3312+
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
3313+
}
3314+
}
32823315
}

0 commit comments

Comments
 (0)