Skip to content

Commit be72027

Browse files
authored
feat: Add support to disable logging from bucket (#390)
* feat: implement disabling logging api * feat: add support to disable bucket logging * feat: modified tests * feat: addressed review changes * feat: use alternative solution * feat: use or instead of and
1 parent 9457f3a commit be72027

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,14 @@ public String getLogObjectPrefix() {
286286
}
287287

288288
Bucket.Logging toPb() {
289-
Bucket.Logging logging = new Bucket.Logging();
290-
logging.setLogBucket(logBucket);
291-
logging.setLogObjectPrefix(logObjectPrefix);
289+
Bucket.Logging logging;
290+
if (logBucket != null || logObjectPrefix != null) {
291+
logging = new Bucket.Logging();
292+
logging.setLogBucket(logBucket);
293+
logging.setLogObjectPrefix(logObjectPrefix);
294+
} else {
295+
logging = Data.nullOf(Bucket.Logging.class);
296+
}
292297
return logging;
293298
}
294299

@@ -1310,7 +1315,7 @@ public Builder setIamConfiguration(IamConfiguration iamConfiguration) {
13101315

13111316
@Override
13121317
public Builder setLogging(Logging logging) {
1313-
this.logging = logging;
1318+
this.logging = logging != null ? logging : BucketInfo.Logging.newBuilder().build();
13141319
return this;
13151320
}
13161321

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

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public void testToPbAndFromPb() {
229229
BucketInfo.newBuilder("b")
230230
.setDeleteRules(DELETE_RULES)
231231
.setLifecycleRules(LIFECYCLE_RULES)
232+
.setLogging(LOGGING)
232233
.build();
233234
compareBuckets(bucketInfo, BucketInfo.fromPb(bucketInfo.toPb()));
234235
}

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

+23
Original file line numberDiff line numberDiff line change
@@ -863,4 +863,27 @@ public void testDeleteLifecycleRules() {
863863
Bucket actualUpdatedBucket = updatedBucket.update();
864864
assertThat(actualUpdatedBucket.getLifecycleRules()).hasSize(0);
865865
}
866+
867+
@Test
868+
public void testUpdateBucketLogging() {
869+
initializeExpectedBucket(6);
870+
BucketInfo.Logging logging =
871+
BucketInfo.Logging.newBuilder()
872+
.setLogBucket("logs-bucket")
873+
.setLogObjectPrefix("test-logs")
874+
.build();
875+
BucketInfo bucketInfo = BucketInfo.newBuilder("b").setLogging(logging).build();
876+
Bucket bucket = new Bucket(serviceMockReturnsOptions, new BucketInfo.BuilderImpl(bucketInfo));
877+
assertThat(bucket.getLogging().getLogBucket()).isEqualTo("logs-bucket");
878+
assertThat(bucket.getLogging().getLogObjectPrefix()).isEqualTo("test-logs");
879+
Bucket expectedUpdatedBucket = bucket.toBuilder().setLogging(null).build();
880+
expect(storage.getOptions()).andReturn(mockOptions).times(2);
881+
expect(storage.update(expectedUpdatedBucket)).andReturn(expectedUpdatedBucket);
882+
replay(storage);
883+
initializeBucket();
884+
Bucket updatedBucket = new Bucket(storage, new BucketInfo.BuilderImpl(expectedUpdatedBucket));
885+
Bucket actualUpdatedBucket = updatedBucket.update();
886+
assertThat(actualUpdatedBucket.getLogging().getLogBucket()).isNull();
887+
assertThat(actualUpdatedBucket.getLogging().getLogObjectPrefix()).isNull();
888+
}
866889
}

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -3228,13 +3228,7 @@ public void testBucketLogging() throws ExecutionException, InterruptedException
32283228
try {
32293229
assertNotNull(storage.create(BucketInfo.newBuilder(logsBucket).setLocation("us").build()));
32303230
Policy policy = storage.getIamPolicy(logsBucket);
3231-
assertNotNull(
3232-
storage.setIamPolicy(
3233-
logsBucket,
3234-
policy
3235-
.toBuilder()
3236-
.addIdentity(StorageRoles.legacyBucketWriter(), Identity.allAuthenticatedUsers())
3237-
.build()));
3231+
assertNotNull(policy);
32383232
BucketInfo.Logging logging =
32393233
BucketInfo.Logging.newBuilder()
32403234
.setLogBucket(logsBucket)
@@ -3245,6 +3239,11 @@ public void testBucketLogging() throws ExecutionException, InterruptedException
32453239
BucketInfo.newBuilder(loggingBucket).setLocation("us").setLogging(logging).build());
32463240
assertEquals(logsBucket, bucket.getLogging().getLogBucket());
32473241
assertEquals("test-logs", bucket.getLogging().getLogObjectPrefix());
3242+
3243+
// Disable bucket logging.
3244+
Bucket updatedBucket = bucket.toBuilder().setLogging(null).build().update();
3245+
assertNull(updatedBucket.getLogging());
3246+
32483247
} finally {
32493248
RemoteStorageHelper.forceDelete(storage, logsBucket, 5, TimeUnit.SECONDS);
32503249
RemoteStorageHelper.forceDelete(storage, loggingBucket, 5, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)