Skip to content

Commit

Permalink
feat: delete bucket OLM rules (#352)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
athakor committed Jun 16, 2020
1 parent 61b09e5 commit 0a528c6
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 7 deletions.
7 changes: 6 additions & 1 deletion google-cloud-storage/clirr-ignored-differences.xml
Expand Up @@ -21,4 +21,9 @@
<method>com.google.cloud.storage.PostPolicyV4 generateSignedPostPolicyV4(com.google.cloud.storage.BlobInfo, long, java.util.concurrent.TimeUnit, com.google.cloud.storage.Storage$PostPolicyV4Option[])</method>
<differenceType>7012</differenceType>
</difference>
</differences>
<difference>
<className>com/google/cloud/storage/BucketInfo$Builder</className>
<method>com.google.cloud.storage.BucketInfo$Builder deleteLifecycleRules()</method>
<differenceType>7013</differenceType>
</difference>
</differences>
Expand Up @@ -571,6 +571,12 @@ public Builder setLifecycleRules(Iterable<? extends LifecycleRule> rules) {
return this;
}

@Override
public Builder deleteLifecycleRules() {
infoBuilder.deleteLifecycleRules();
return this;
}

@Override
public Builder setStorageClass(StorageClass storageClass) {
infoBuilder.setStorageClass(storageClass);
Expand Down
Expand Up @@ -24,14 +24,15 @@
import com.google.api.client.util.Data;
import com.google.api.client.util.DateTime;
import com.google.api.core.BetaApi;
import com.google.api.services.storage.model.*;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.Bucket.Encryption;
import com.google.api.services.storage.model.Bucket.Lifecycle;
import com.google.api.services.storage.model.Bucket.Lifecycle.Rule;
import com.google.api.services.storage.model.Bucket.Owner;
import com.google.api.services.storage.model.Bucket.Versioning;
import com.google.api.services.storage.model.Bucket.Website;
import com.google.api.services.storage.model.BucketAccessControl;
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.cloud.storage.Acl.Entity;
import com.google.common.base.Function;
import com.google.common.base.Functions;
Expand Down Expand Up @@ -978,6 +979,9 @@ public abstract static class Builder {
*/
public abstract Builder setLifecycleRules(Iterable<? extends LifecycleRule> rules);

/** Deletes the lifecycle rules of this bucket. */
public abstract Builder deleteLifecycleRules();

/**
* Sets the bucket's storage class. This defines how blobs in the bucket are stored and
* determines the SLA and the cost of storage. A list of supported values is available <a
Expand Down Expand Up @@ -1187,7 +1191,15 @@ public Builder setDeleteRules(Iterable<? extends DeleteRule> rules) {

@Override
public Builder setLifecycleRules(Iterable<? extends LifecycleRule> rules) {
this.lifecycleRules = rules != null ? ImmutableList.copyOf(rules) : null;
this.lifecycleRules =
rules != null ? ImmutableList.copyOf(rules) : ImmutableList.<LifecycleRule>of();
return this;
}

@Override
public Builder deleteLifecycleRules() {
setDeleteRules(null);
setLifecycleRules(null);
return this;
}

Expand Down Expand Up @@ -1434,7 +1446,7 @@ public List<? extends DeleteRule> getDeleteRules() {
}

public List<? extends LifecycleRule> getLifecycleRules() {
return lifecycleRules;
return lifecycleRules != null ? lifecycleRules : ImmutableList.<LifecycleRule>of();
}

/**
Expand Down Expand Up @@ -1711,11 +1723,13 @@ public Rule apply(LifecycleRule lifecycleRule) {
}
}));
}
if (!rules.isEmpty()) {

if (rules != null) {
Lifecycle lifecycle = new Lifecycle();
lifecycle.setRule(ImmutableList.copyOf(rules));
bucketPb.setLifecycle(lifecycle);
}

if (labels != null) {
bucketPb.setLabels(labels);
}
Expand Down Expand Up @@ -1765,6 +1779,7 @@ static BucketInfo fromPb(com.google.api.services.storage.model.Bucket bucketPb)
if (bucketPb.getId() != null) {
builder.setGeneratedId(bucketPb.getId());
}

if (bucketPb.getEtag() != null) {
builder.setEtag(bucketPb.getEtag());
}
Expand Down
Expand Up @@ -1999,7 +1999,6 @@ Blob create(
* only if supplied Decrpytion Key decrypts the blob successfully, otherwise a {@link
* StorageException} is thrown. For more information review
*
* @throws StorageException upon failure
* @see <a
* href="https://cloud.google.com/storage/docs/encryption/customer-supplied-keys#encrypted-elements">Encrypted
* Elements</a>
Expand Down
Expand Up @@ -225,7 +225,11 @@ public void testBuilder() {
@Test
public void testToPbAndFromPb() {
compareBuckets(BUCKET_INFO, BucketInfo.fromPb(BUCKET_INFO.toPb()));
BucketInfo bucketInfo = BucketInfo.of("b");
BucketInfo bucketInfo =
BucketInfo.newBuilder("b")
.setDeleteRules(DELETE_RULES)
.setLifecycleRules(LIFECYCLE_RULES)
.build();
compareBuckets(bucketInfo, BucketInfo.fromPb(bucketInfo.toPb()));
}

Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.storage;

import static com.google.cloud.storage.Acl.Role.WRITER;
import static com.google.common.truth.Truth.assertThat;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expect;
Expand Down Expand Up @@ -846,4 +847,20 @@ public void testBuilder() {
assertEquals(storage.getOptions(), bucket.getStorage().getOptions());
assertTrue(LOCATION_TYPES.contains(LOCATION_TYPE));
}

@Test
public void testDeleteLifecycleRules() {
initializeExpectedBucket(6);
Bucket bucket =
new Bucket(serviceMockReturnsOptions, new BucketInfo.BuilderImpl(FULL_BUCKET_INFO));
assertThat(bucket.getLifecycleRules()).hasSize(1);
Bucket expectedUpdatedBucket = bucket.toBuilder().deleteLifecycleRules().build();
expect(storage.getOptions()).andReturn(mockOptions).times(2);
expect(storage.update(expectedUpdatedBucket)).andReturn(expectedUpdatedBucket);
replay(storage);
initializeBucket();
Bucket updatedBucket = new Bucket(storage, new BucketInfo.BuilderImpl(expectedUpdatedBucket));
Bucket actualUpdatedBucket = updatedBucket.update();
assertThat(actualUpdatedBucket.getLifecycleRules()).hasSize(0);
}
}
Expand Up @@ -181,6 +181,20 @@ public class ITStorageTest {
&& System.getenv("GOOGLE_CLOUD_TESTS_IN_VPCSC").equalsIgnoreCase("true");
private static final List<String> LOCATION_TYPES =
ImmutableList.of("multi-region", "region", "dual-region");
private static final LifecycleRule LIFECYCLE_RULE_1 =
new LifecycleRule(
LifecycleAction.newSetStorageClassAction(StorageClass.COLDLINE),
LifecycleCondition.newBuilder()
.setAge(1)
.setNumberOfNewerVersions(3)
.setIsLive(false)
.setMatchesStorageClass(ImmutableList.of(StorageClass.COLDLINE))
.build());
private static final LifecycleRule LIFECYCLE_RULE_2 =
new LifecycleRule(
LifecycleAction.newDeleteAction(), LifecycleCondition.newBuilder().setAge(1).build());
private static final ImmutableList<LifecycleRule> LIFECYCLE_RULES =
ImmutableList.of(LIFECYCLE_RULE_1, LIFECYCLE_RULE_2);

@BeforeClass
public static void beforeClass() throws IOException {
Expand Down Expand Up @@ -3279,4 +3293,23 @@ public void testBlobReload() throws Exception {
updated.delete();
assertNull(updated.reload());
}

@Test
public void testDeleteLifecycleRules() throws ExecutionException, InterruptedException {
String bucketName = RemoteStorageHelper.generateBucketName();
Bucket bucket =
storage.create(
BucketInfo.newBuilder(bucketName)
.setLocation("us")
.setLifecycleRules(LIFECYCLE_RULES)
.build());
assertThat(bucket.getLifecycleRules()).isNotNull();
assertThat(bucket.getLifecycleRules()).hasSize(2);
try {
Bucket updatedBucket = bucket.toBuilder().deleteLifecycleRules().build().update();
assertThat(updatedBucket.getLifecycleRules()).hasSize(0);
} finally {
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
}
}
}

0 comments on commit 0a528c6

Please sign in to comment.