Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,12 @@ public Builder setIamConfiguration(IamConfiguration iamConfiguration) {
return this;
}

@Override
public Builder setLogging(Logging logging) {
infoBuilder.setLogging(logging);
return this;
}

@Override
Builder setLocationType(String locationType) {
infoBuilder.setLocationType(locationType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo)
private final Long retentionPeriod;
private final IamConfiguration iamConfiguration;
private final String locationType;
private final Logging logging;

/**
* The Bucket's IAM Configuration.
Expand Down Expand Up @@ -208,6 +209,92 @@ public IamConfiguration build() {
}
}

/**
* The bucket's logging configuration, which defines the destination bucket and optional name
* prefix for the current bucket's logs.
*/
public static class Logging implements Serializable {

private static final long serialVersionUID = -708892101216778492L;
private String logBucket;
private String logObjectPrefix;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) {
return false;
}
Logging other = (Logging) o;
return Objects.equals(toPb(), other.toPb());
}

@Override
public int hashCode() {
return Objects.hash(logBucket, logObjectPrefix);
}

public static Builder newBuilder() {
return new Builder();
}

public Builder toBuilder() {
Builder builder = new Builder();
builder.logBucket = logBucket;
builder.logObjectPrefix = logObjectPrefix;
return builder;
}

public String getLogBucket() {
return logBucket;
}

public String getLogObjectPrefix() {
return logObjectPrefix;
}

Bucket.Logging toPb() {
Bucket.Logging logging = new Bucket.Logging();
logging.setLogBucket(logBucket);
logging.setLogObjectPrefix(logObjectPrefix);
return logging;
}

static Logging fromPb(Bucket.Logging logging) {
return newBuilder()
.setLogBucket(logging.getLogBucket())
.setLogObjectPrefix(logging.getLogObjectPrefix())
.build();
}

private Logging(Builder builder) {
this.logBucket = builder.logBucket;
this.logObjectPrefix = builder.logObjectPrefix;
}

public static class Builder {
private String logBucket;
private String logObjectPrefix;

/** The destination bucket where the current bucket's logs should be placed. */
public Builder setLogBucket(String logBucket) {
this.logBucket = logBucket;
return this;
}

/** A prefix for log object names. */
public Builder setLogObjectPrefix(String logObjectPrefix) {
this.logObjectPrefix = logObjectPrefix;
return this;
}

/** Builds an {@code Logging} object */
public Logging build() {
return new Logging(this);
}
}
}

/**
* Lifecycle rule for a bucket. Allows supported Actions, such as deleting and changing storage
* class, to be executed when certain Conditions are met.
Expand Down Expand Up @@ -909,6 +996,8 @@ public abstract static class Builder {
@BetaApi
public abstract Builder setIamConfiguration(IamConfiguration iamConfiguration);

public abstract Builder setLogging(Logging logging);

/** Creates a {@code BucketInfo} object. */
public abstract BucketInfo build();
}
Expand Down Expand Up @@ -941,6 +1030,7 @@ static final class BuilderImpl extends Builder {
private Long retentionPeriod;
private IamConfiguration iamConfiguration;
private String locationType;
private Logging logging;

BuilderImpl(String name) {
this.name = name;
Expand Down Expand Up @@ -973,6 +1063,7 @@ static final class BuilderImpl extends Builder {
retentionPeriod = bucketInfo.retentionPeriod;
iamConfiguration = bucketInfo.iamConfiguration;
locationType = bucketInfo.locationType;
logging = bucketInfo.logging;
}

@Override
Expand Down Expand Up @@ -1131,6 +1222,12 @@ public Builder setIamConfiguration(IamConfiguration iamConfiguration) {
return this;
}

@Override
public Builder setLogging(Logging logging) {
this.logging = logging;
return this;
}

@Override
Builder setLocationType(String locationType) {
this.locationType = locationType;
Expand Down Expand Up @@ -1171,6 +1268,7 @@ public BucketInfo build() {
retentionPeriod = builder.retentionPeriod;
iamConfiguration = builder.iamConfiguration;
locationType = builder.locationType;
logging = builder.logging;
}

/** Returns the service-generated id for the bucket. */
Expand Down Expand Up @@ -1424,6 +1522,11 @@ public IamConfiguration getIamConfiguration() {
return iamConfiguration;
}

/** Returns the Logging */
public Logging getLogging() {
return logging;
}

/** Returns a builder for the current bucket. */
public Builder toBuilder() {
return new BuilderImpl(this);
Expand Down Expand Up @@ -1567,7 +1670,9 @@ public Rule apply(LifecycleRule lifecycleRule) {
if (iamConfiguration != null) {
bucketPb.setIamConfiguration(iamConfiguration.toPb());
}

if (logging != null) {
bucketPb.setLogging(logging.toPb());
}
return bucketPb;
}

Expand Down Expand Up @@ -1697,6 +1802,10 @@ public DeleteRule apply(Rule rule) {
if (iamConfiguration != null) {
builder.setIamConfiguration(IamConfiguration.fromPb(iamConfiguration));
}
Bucket.Logging logging = bucketPb.getLogging();
if (logging != null) {
builder.setLogging(Logging.fromPb(logging));
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public class BucketInfoTest {
.setIsBucketPolicyOnlyEnabled(true)
.setBucketPolicyOnlyLockedTime(System.currentTimeMillis())
.build();
private static final BucketInfo.Logging LOGGING =
BucketInfo.Logging.newBuilder()
.setLogBucket("test-bucket")
.setLogObjectPrefix("test-")
.build();
private static final String NOT_FOUND_PAGE = "error.html";
private static final String LOCATION = "ASIA";
private static final StorageClass STORAGE_CLASS = StorageClass.STANDARD;
Expand Down Expand Up @@ -113,6 +118,7 @@ public class BucketInfoTest {
.setRetentionEffectiveTime(RETENTION_EFFECTIVE_TIME)
.setRetentionPeriod(RETENTION_PERIOD)
.setRetentionPolicyIsLocked(RETENTION_POLICY_IS_LOCKED)
.setLogging(LOGGING)
.build();

@Test
Expand Down Expand Up @@ -164,6 +170,7 @@ public void testBuilder() {
assertEquals(RETENTION_PERIOD, BUCKET_INFO.getRetentionPeriod());
assertEquals(RETENTION_POLICY_IS_LOCKED, BUCKET_INFO.retentionPolicyIsLocked());
assertTrue(LOCATION_TYPES.contains(BUCKET_INFO.getLocationType()));
assertEquals(LOGGING, BUCKET_INFO.getLogging());
}

@Test
Expand Down Expand Up @@ -200,6 +207,7 @@ private void compareBuckets(BucketInfo expected, BucketInfo value) {
assertEquals(expected.getRetentionEffectiveTime(), value.getRetentionEffectiveTime());
assertEquals(expected.getRetentionPeriod(), value.getRetentionPeriod());
assertEquals(expected.retentionPolicyIsLocked(), value.retentionPolicyIsLocked());
assertEquals(expected.getLogging(), value.getLogging());
}

@Test
Expand Down Expand Up @@ -272,4 +280,16 @@ public void testIamConfiguration() {
assertEquals(Boolean.TRUE, iamConfiguration.getBucketPolicyOnly().getEnabled());
assertNotNull(iamConfiguration.getBucketPolicyOnly().getLockedTime());
}

@Test
public void testLogging() {
Bucket.Logging logging =
BucketInfo.Logging.newBuilder()
.setLogBucket("test-bucket")
.setLogObjectPrefix("test-")
.build()
.toPb();
assertEquals("test-bucket", logging.getLogBucket());
assertEquals("test-", logging.getLogObjectPrefix());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2714,4 +2714,34 @@ public void testBucketLocationType() throws ExecutionException, InterruptedExcep
}
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
}

@Test
public void testBucketLogging() throws ExecutionException, InterruptedException {
String logsBucket = RemoteStorageHelper.generateBucketName();
String loggingBucket = RemoteStorageHelper.generateBucketName();
try {
assertNotNull(storage.create(BucketInfo.newBuilder(logsBucket).setLocation("us").build()));
Policy policy = storage.getIamPolicy(logsBucket);
assertNotNull(
storage.setIamPolicy(
logsBucket,
policy
.toBuilder()
.addIdentity(StorageRoles.legacyBucketWriter(), Identity.allAuthenticatedUsers())
.build()));
BucketInfo.Logging logging =
BucketInfo.Logging.newBuilder()
.setLogBucket(logsBucket)
.setLogObjectPrefix("test-logs")
.build();
Bucket bucket =
storage.create(
BucketInfo.newBuilder(loggingBucket).setLocation("us").setLogging(logging).build());
assertEquals(logsBucket, bucket.getLogging().getLogBucket());
assertEquals("test-logs", bucket.getLogging().getLogObjectPrefix());
} finally {
RemoteStorageHelper.forceDelete(storage, logsBucket, 5, TimeUnit.SECONDS);
RemoteStorageHelper.forceDelete(storage, loggingBucket, 5, TimeUnit.SECONDS);
}
}
}