Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete bucket OLM rules #352

Merged
merged 11 commits into from Jun 16, 2020
2 changes: 1 addition & 1 deletion google-cloud-storage/clirr-ignored-differences.xml
Expand Up @@ -21,4 +21,4 @@
<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>
</differences>
Expand Up @@ -571,6 +571,12 @@ public Builder setLifecycleRules(Iterable<? extends LifecycleRule> rules) {
return this;
}

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

athakor marked this conversation as resolved.
Show resolved Hide resolved
@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();
athakor marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

@Override
public Builder deleteLifecycleRules() {
setDeleteRules(null);
setLifecycleRules(null);
return this;
athakor marked this conversation as resolved.
Show resolved Hide resolved
}

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
athakor marked this conversation as resolved.
Show resolved Hide resolved
* @see <a
* href="https://cloud.google.com/storage/docs/encryption/customer-supplied-keys#encrypted-elements">Encrypted
* Elements</a>
Expand Down
Expand Up @@ -217,7 +217,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 @@ -833,4 +834,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);
athakor marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 @@ -3273,4 +3287,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);
athakor marked this conversation as resolved.
Show resolved Hide resolved
} finally {
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
}
}
athakor marked this conversation as resolved.
Show resolved Hide resolved
}