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 @@ -24,25 +24,30 @@
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;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -1187,7 +1192,8 @@ 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;
}

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

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

/**
* Deletes the lifecycle rules of this bucket.
*
athakor marked this conversation as resolved.
Show resolved Hide resolved
* <p>Example of deleting the lifecycle rules of this bucket:
*
* <pre>{@code
* String bucketName = "my-unique-bucket";
* LifecycleRule lifecycleRule_1 =
* new LifecycleRule(LifecycleAction.newSetStorageClassAction(StorageClass.COLDLINE),
* LifecycleCondition.newBuilder()
* .setAge(1)
* .setNumberOfNewerVersions(3)
* .setIsLive(false)
* .setMatchesStorageClass(ImmutableList.of(StorageClass.COLDLINE))
* .build());
* LifecycleRule lifecycleRule_2 =
* new LifecycleRule(LifecycleAction.newDeleteAction(), LifecycleCondition.newBuilder().setAge(1).build());
* ImmutableList<LifecycleRule> lifecycleRules = ImmutableList.of(lifecycleRule_1, lifecycleRule_2);
* Bucket bucket =
* storage.create(
* BucketInfo.newBuilder(bucketName)
* .setLocation("us")
* .setLifecycleRules(lifecycleRules)
* .build());
* List<LifecycleRule> results = bucket.deleteLifecycleRules();
* }</pre>
*
athakor marked this conversation as resolved.
Show resolved Hide resolved
* @return the lists of deleted lifecycle rules of bucket, an empty list if the lifecycle rules
* was not found
* @throws StorageException upon failure
*/
public List<LifecycleRule> deleteLifecycleRules() {
final Storage storage = StorageOptions.getDefaultInstance().getService();
athakor marked this conversation as resolved.
Show resolved Hide resolved
final String bucket = getName();
final List<LifecycleRule> results = Lists.newArrayList();
try {
com.google.cloud.storage.Bucket remoteBucket =
storage.get(bucket, Storage.BucketGetOption.fields(Storage.BucketField.LIFECYCLE));
List<LifecycleRule> lifecycleRules = new ArrayList(remoteBucket.getLifecycleRules());
for (Iterator<LifecycleRule> iterator = lifecycleRules.iterator(); iterator.hasNext(); ) {
LifecycleRule lifecycleRule = iterator.next();
iterator.remove();
results.add(lifecycleRule);
}
if (results.size() > 0) {
storage
.get(bucket, Storage.BucketGetOption.fields())
.toBuilder()
.setLifecycleRules(lifecycleRules)
.build()
.update();
}
return Collections.unmodifiableList(results);
} catch (StorageException ex) {
throw ex;
}
athakor marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -1689,6 +1753,7 @@ public ObjectAccessControl apply(Acl acl) {
bucketPb.setWebsite(website);
}
Set<Rule> rules = new HashSet<>();
Lifecycle lifecycle = new Lifecycle();
if (deleteRules != null) {
rules.addAll(
transform(
Expand All @@ -1699,6 +1764,8 @@ public Rule apply(DeleteRule deleteRule) {
return deleteRule.toPb();
}
}));
lifecycle.setRule(ImmutableList.copyOf(rules));
bucketPb.setLifecycle(lifecycle);
}
if (lifecycleRules != null) {
rules.addAll(
Expand All @@ -1710,12 +1777,10 @@ public Rule apply(LifecycleRule lifecycleRule) {
return lifecycleRule.toPb();
}
}));
}
if (!rules.isEmpty()) {
Lifecycle lifecycle = new Lifecycle();
lifecycle.setRule(ImmutableList.copyOf(rules));
bucketPb.setLifecycle(lifecycle);
}

if (labels != null) {
bucketPb.setLabels(labels);
}
Expand Down Expand Up @@ -1765,6 +1830,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 @@ -17,6 +17,9 @@
package com.google.cloud.storage;

import static com.google.cloud.storage.Acl.Project.ProjectRole.VIEWERS;
import static com.google.common.truth.Truth.assertThat;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -42,6 +45,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.easymock.EasyMock;
import org.junit.Test;

public class BucketInfoTest {
Expand Down Expand Up @@ -333,4 +337,22 @@ public void testLogging() {
assertEquals("test-bucket", logging.getLogBucket());
assertEquals("test-", logging.getLogObjectPrefix());
}

@Test
public void testDeleteLifecycleRules() {
BucketInfo bucketInfo = EasyMock.createStrictMock(BucketInfo.class);
expect(bucketInfo.deleteLifecycleRules()).andReturn(ImmutableList.of(LIFECYCLE_RULES.get(0)));
replay(bucketInfo);
List<LifecycleRule> actualResults = bucketInfo.deleteLifecycleRules();
assertThat(actualResults).hasSize(1);
}

@Test
public void testDeleteLifecycleRuleIfNotExists() {
BucketInfo bucketInfo = EasyMock.createStrictMock(BucketInfo.class);
expect(bucketInfo.deleteLifecycleRules()).andReturn(ImmutableList.<LifecycleRule>of());
replay(bucketInfo);
List<LifecycleRule> lifecycleRules = bucketInfo.deleteLifecycleRules();
assertThat(lifecycleRules).isEmpty();
}
}
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,38 @@ 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.getName()).isEqualTo(bucketName);
assertThat(bucket.getLifecycleRules()).hasSize(2);
try {
List<LifecycleRule> results = bucket.deleteLifecycleRules();
assertThat(results).hasSize(2);
} finally {
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
}
}

@Test
public void testDeleteLifecycleRulesIfNotExists()
throws ExecutionException, InterruptedException {
String bucketName = RemoteStorageHelper.generateBucketName();
Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName).setLocation("us").build());
assertThat(bucket.getName()).isEqualTo(bucketName);
assertThat(bucket.getLifecycleRules()).isEmpty();
try {
List<LifecycleRule> lifecycleRules = bucket.deleteLifecycleRules();
assertThat(lifecycleRules).isEmpty();
} finally {
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
}
}
athakor marked this conversation as resolved.
Show resolved Hide resolved
}