From 60e4c90479e74162f2a3c662055c4206233cc035 Mon Sep 17 00:00:00 2001 From: losalex Date: Wed, 27 Oct 2021 08:24:24 -0700 Subject: [PATCH 1/5] fets: Add an ability to delete logs based on project, folder, organization or billing account resource names --- .../clirr-ignored-differences.xml | 9 ++++ .../com/google/cloud/logging/Logging.java | 51 +++++++++++++++++++ .../com/google/cloud/logging/LoggingImpl.java | 35 +++++++++++-- .../google/cloud/logging/LoggingImplTest.java | 41 +++++++++++++++ 4 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 google-cloud-logging/clirr-ignored-differences.xml diff --git a/google-cloud-logging/clirr-ignored-differences.xml b/google-cloud-logging/clirr-ignored-differences.xml new file mode 100644 index 000000000..8848f8c1d --- /dev/null +++ b/google-cloud-logging/clirr-ignored-differences.xml @@ -0,0 +1,9 @@ + + + + + 7012 + com/google/cloud/logging/Logging + * deleteLog*(*) + + diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java index 7f4d24dd6..94ed60433 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java @@ -588,6 +588,57 @@ default ApiFuture> listLogsAsync(ListOption... options) { */ boolean deleteLog(String log); + /** + * Deletes a log and all its log entries for given log destination (see 'logName' parameter in + * https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry). The log will reappear if + * new entries are written to it. + * + *

Example of deleting a log. + * + *

+   * {
+   *   @code
+   *   String logName = "my_log_name";
+   *   String folder = "my_folder";
+   *   boolean deleted = logging.deleteLog(logName, LogDestinationName.folder(folder));
+   *   if (deleted) {
+   *     // the log was deleted
+   *   } else {
+   *     // the log was not found
+   *   }
+   * }
+   * 
+ * + * @return {@code true} if the log was deleted, {@code false} if it was not found + */ + boolean deleteLog(String log, LogDestinationName destination); + + /** + * Sends a request for deleting a log and all its log entries for given log destination (see + * 'logName' parameter in https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry). + * This method returns a {@code ApiFuture} object to consume the result. {@link ApiFuture#get()} + * returns {@code true} if the log was deleted, {@code false} if it was not found. + * + *

Example of asynchronously deleting a log. + * + *

+   * {
+   *   @code
+   *   String logName = "my_log_name";
+   *   String folder = "my_folder";
+   *   ApiFuture future = logging.deleteLogAsync(logName, LogDestinationName.folder(folder));
+   *   // ...
+   *   boolean deleted = future.get();
+   *   if (deleted) {
+   *     // the log was deleted
+   *   } else {
+   *     // the log was not found
+   *   }
+   * }
+   * 
+ */ + ApiFuture deleteLogAsync(String log, LogDestinationName destination); + /** * Sends a request for deleting a log and all its log entries. This method returns a {@code * ApiFuture} object to consume the result. {@link ApiFuture#get()} returns {@code true} if the diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java index ce8e60ff7..63aecb0bf 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java @@ -39,6 +39,7 @@ import com.google.cloud.logging.spi.v2.LoggingRpc; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; +import com.google.common.base.Preconditions; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -442,11 +443,23 @@ public boolean deleteLog(String log) { return get(deleteLogAsync(log)); } + public boolean deleteLog(String log, LogDestinationName destination) { + return get(deleteLogAsyncImpl(log, destination)); + } + public ApiFuture deleteLogAsync(String log) { - DeleteLogRequest request = - DeleteLogRequest.newBuilder() - .setLogName(LogName.ofProjectLogName(getOptions().getProjectId(), log).toString()) - .build(); + return deleteLogAsyncImpl(log, null); + } + + public ApiFuture deleteLogAsync(String log, LogDestinationName destination) { + return deleteLogAsyncImpl(log, destination); + } + + private ApiFuture deleteLogAsyncImpl(String log, LogDestinationName destination) { + LogName name = + Preconditions.checkNotNull(getLogName(getOptions().getProjectId(), log, destination)); + + DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(name.toString()).build(); return transform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION); } @@ -756,6 +769,20 @@ private static WriteLogEntriesRequest writeLogEntriesRequest( return builder.build(); } + private static LogName getLogName( + String projectId, String logName, LogDestinationName destination) { + if (logName == null) { + return null; + } + + // If no destination specified, fallback to project based log name + if (destination == null) { + return LogName.ofProjectLogName(projectId, logName); + } + + return destination.toLogName(logName); + } + public void write(Iterable logEntries, WriteOption... options) { if (inWriteCall.get() != null) { return; diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java index 5e286f747..e9336c85d 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java @@ -91,6 +91,9 @@ public class LoggingImplTest { private static final String PROJECT = "project"; + private static final String FOLDER = "folder"; + private static final String BILLING = "billing"; + private static final String ORGANIZATION = "organization"; private static final String PROJECT_PB = "projects/" + PROJECT; private static final String SINK_NAME = "sink"; private static final SinkInfo SINK_INFO = @@ -113,6 +116,10 @@ public class LoggingImplTest { private static final String LOG_NAMES_CURSOR = "cursor"; private static final String LOG_NAME = "log"; private static final String LOG_NAME_PB = "projects/" + PROJECT + "/logs/" + LOG_NAME; + private static final String LOG_NAME_FOLDER = "folders/" + FOLDER + "/logs/" + LOG_NAME; + private static final String LOG_NAME_BILLING = "billingAccounts/" + BILLING + "/logs/" + LOG_NAME; + private static final String LOG_NAME_ORGANIZATION = + "organizations/" + ORGANIZATION + "/logs/" + LOG_NAME; private static final MonitoredResource MONITORED_RESOURCE = MonitoredResource.newBuilder("global").addLabel("project-id", PROJECT).build(); private static final LogEntry LOG_ENTRY1 = @@ -1712,6 +1719,12 @@ public void testDeleteLog() { assertTrue(logging.deleteLog(LOG_NAME)); } + @Test + public void testDeleteLog_BillingDestination() { + test_delete_by_destination( + LOG_NAME, LOG_NAME_BILLING, LogDestinationName.billingAccount(BILLING)); + } + @Test public void testDeleteLog_Null() { DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(LOG_NAME_PB).build(); @@ -1732,6 +1745,24 @@ public void testDeleteLogAsync() throws ExecutionException, InterruptedException assertTrue(logging.deleteLogAsync(LOG_NAME).get()); } + @Test + public void testDeleteLogAsync_FolderDestination() + throws ExecutionException, InterruptedException { + test_delete_by_destination(LOG_NAME, LOG_NAME_FOLDER, LogDestinationName.folder(FOLDER)); + } + + @Test + public void testDeleteLogAsync_OrgDestination() throws ExecutionException, InterruptedException { + test_delete_by_destination( + LOG_NAME, LOG_NAME_ORGANIZATION, LogDestinationName.organization(ORGANIZATION)); + } + + @Test + public void testDeleteLogAsync_ProjectDestination() + throws ExecutionException, InterruptedException { + test_delete_by_destination(LOG_NAME, LOG_NAME_PB, LogDestinationName.project(PROJECT)); + } + @Test public void testDeleteLogAsync_Null() throws ExecutionException, InterruptedException { DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(LOG_NAME_PB).build(); @@ -2183,4 +2214,14 @@ public void run() { } assertSame(0, exceptions.get()); } + + private void test_delete_by_destination( + String logId, String logName, LogDestinationName destination) { + DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(logName).build(); + ApiFuture response = ApiFutures.immediateFuture(Empty.getDefaultInstance()); + EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response); + EasyMock.replay(rpcFactoryMock, loggingRpcMock); + logging = options.getService(); + assertTrue(logging.deleteLog(logId, destination)); + } } From fa38f2ff727672fbe60a89ef67ad04655bcdb6f3 Mon Sep 17 00:00:00 2001 From: losalex Date: Tue, 2 Nov 2021 13:39:46 -0700 Subject: [PATCH 2/5] Add an ability to make async and non-async delete calls in tests --- .../google/cloud/logging/LoggingImplTest.java | 52 ++++++++++++++----- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java index 3c43c7fa4..a62558240 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java @@ -1755,12 +1755,6 @@ public void testDeleteLog() { assertTrue(logging.deleteLog(LOG_NAME)); } - @Test - public void testDeleteLogBillingDestination() { - testDeleteByBestination( - LOG_NAME, LOG_NAME_BILLING_PATH, LogDestinationName.billingAccount(BILLING)); - } - @Test public void testDeleteLog_Null() { DeleteLogRequest request = @@ -1784,21 +1778,47 @@ public void testDeleteLogAsync() throws ExecutionException, InterruptedException } @Test - public void testDeleteLogAsyncFolderDestination() + public void testDeleteLogBillingDestination() throws ExecutionException, InterruptedException { + testDeleteByBestination( + LOG_NAME, LOG_NAME_BILLING_PATH, LogDestinationName.billingAccount(BILLING), false); + } + + @Test + public void testDeleteLogBillingDestinationAsync() throws ExecutionException, InterruptedException { - testDeleteByBestination(LOG_NAME, LOG_NAME_FOLDER_PATH, LogDestinationName.folder(FOLDER)); + testDeleteByBestination( + LOG_NAME, LOG_NAME_BILLING_PATH, LogDestinationName.billingAccount(BILLING), true); } @Test - public void testDeleteLogAsyncOrgDestination() throws ExecutionException, InterruptedException { + public void testDeleteLogFolderDestination() throws ExecutionException, InterruptedException { testDeleteByBestination( - LOG_NAME, LOG_NAME_ORGANIZATION_PATH, LogDestinationName.organization(ORGANIZATION)); + LOG_NAME, LOG_NAME_FOLDER_PATH, LogDestinationName.folder(FOLDER), false); } @Test - public void testDeleteLogAsyncProjectDestination() + public void testDeleteLogFolderDestinationAsync() throws ExecutionException, InterruptedException { - testDeleteByBestination(LOG_NAME, LOG_NAME_PROJECT_PATH, LogDestinationName.project(PROJECT)); + testDeleteByBestination( + LOG_NAME, LOG_NAME_FOLDER_PATH, LogDestinationName.folder(FOLDER), true); + } + + @Test + public void testDeleteLogOrgDestination() throws ExecutionException, InterruptedException { + testDeleteByBestination( + LOG_NAME, LOG_NAME_ORGANIZATION_PATH, LogDestinationName.organization(ORGANIZATION), false); + } + + @Test + public void testDeleteLogOrgDestinationAsync() throws ExecutionException, InterruptedException { + testDeleteByBestination( + LOG_NAME, LOG_NAME_ORGANIZATION_PATH, LogDestinationName.organization(ORGANIZATION), true); + } + + @Test + public void testDeleteLogProjectDestination() throws ExecutionException, InterruptedException { + testDeleteByBestination( + LOG_NAME, LOG_NAME_PROJECT_PATH, LogDestinationName.project(PROJECT), false); } @Test @@ -2266,13 +2286,17 @@ public void run() { } private void testDeleteByBestination( - String logId, String logName, LogDestinationName destination) { + String logId, String logName, LogDestinationName destination, boolean useAsyncDelete) + throws ExecutionException, InterruptedException { DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(logName).build(); ApiFuture response = ApiFutures.immediateFuture(Empty.getDefaultInstance()); EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); - assertTrue(logging.deleteLog(logId, destination)); + assertTrue( + useAsyncDelete + ? logging.deleteLogAsync(logId, destination).get() + : logging.deleteLog(logId, destination)); } private void testWriteLogEntriesWithDestination( From 78e545277efb7e53fd2f89458ec895f892536241 Mon Sep 17 00:00:00 2001 From: losalex Date: Tue, 2 Nov 2021 16:53:47 -0700 Subject: [PATCH 3/5] Add proper validations with meaningful error message and also fix test name typo --- .../com/google/cloud/logging/LoggingImpl.java | 19 +++++++++++++------ .../google/cloud/logging/LoggingImplTest.java | 16 ++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java index 62a25209f..1c4938324 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java @@ -457,9 +457,7 @@ public ApiFuture deleteLogAsync(String log, LogDestinationName destinat } private ApiFuture deleteLogAsyncImpl(String log, LogDestinationName destination) { - LogName name = - Preconditions.checkNotNull(getLogName(getOptions().getProjectId(), log, destination)); - + LogName name = getLogName(getOptions().getProjectId(), log, destination, true); DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(name.toString()).build(); return transform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION); } @@ -755,7 +753,8 @@ private static WriteLogEntriesRequest writeLogEntriesRequest( WriteLogEntriesRequest.Builder builder = WriteLogEntriesRequest.newBuilder(); String projectId = serviceOptions.getProjectId(); - LogName logName = getLogName(projectId, LOG_NAME.get(options), LOG_DESTINATION.get(options)); + LogName logName = + getLogName(projectId, LOG_NAME.get(options), LOG_DESTINATION.get(options), false); if (logName != null) { builder.setLogName(logName.toString()); @@ -774,13 +773,21 @@ private static WriteLogEntriesRequest writeLogEntriesRequest( } private static LogName getLogName( - String projectId, String logName, LogDestinationName destination) { - if (logName == null) { + String projectId, + String logName, + LogDestinationName destination, + boolean validateParameters) { + if (validateParameters) { + Preconditions.checkNotNull(logName, "logName parameter cannot be null"); + } else if (logName == null) { return null; } // If no destination specified, fallback to project based log name if (destination == null) { + if (validateParameters) { + Preconditions.checkNotNull(projectId, "projectId parameter cannot be null"); + } return LogName.ofProjectLogName(projectId, logName); } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java index a62558240..919cdaa73 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java @@ -1779,45 +1779,45 @@ public void testDeleteLogAsync() throws ExecutionException, InterruptedException @Test public void testDeleteLogBillingDestination() throws ExecutionException, InterruptedException { - testDeleteByBestination( + testDeleteByDestination( LOG_NAME, LOG_NAME_BILLING_PATH, LogDestinationName.billingAccount(BILLING), false); } @Test public void testDeleteLogBillingDestinationAsync() throws ExecutionException, InterruptedException { - testDeleteByBestination( + testDeleteByDestination( LOG_NAME, LOG_NAME_BILLING_PATH, LogDestinationName.billingAccount(BILLING), true); } @Test public void testDeleteLogFolderDestination() throws ExecutionException, InterruptedException { - testDeleteByBestination( + testDeleteByDestination( LOG_NAME, LOG_NAME_FOLDER_PATH, LogDestinationName.folder(FOLDER), false); } @Test public void testDeleteLogFolderDestinationAsync() throws ExecutionException, InterruptedException { - testDeleteByBestination( + testDeleteByDestination( LOG_NAME, LOG_NAME_FOLDER_PATH, LogDestinationName.folder(FOLDER), true); } @Test public void testDeleteLogOrgDestination() throws ExecutionException, InterruptedException { - testDeleteByBestination( + testDeleteByDestination( LOG_NAME, LOG_NAME_ORGANIZATION_PATH, LogDestinationName.organization(ORGANIZATION), false); } @Test public void testDeleteLogOrgDestinationAsync() throws ExecutionException, InterruptedException { - testDeleteByBestination( + testDeleteByDestination( LOG_NAME, LOG_NAME_ORGANIZATION_PATH, LogDestinationName.organization(ORGANIZATION), true); } @Test public void testDeleteLogProjectDestination() throws ExecutionException, InterruptedException { - testDeleteByBestination( + testDeleteByDestination( LOG_NAME, LOG_NAME_PROJECT_PATH, LogDestinationName.project(PROJECT), false); } @@ -2285,7 +2285,7 @@ public void run() { assertSame(0, exceptions.get()); } - private void testDeleteByBestination( + private void testDeleteByDestination( String logId, String logName, LogDestinationName destination, boolean useAsyncDelete) throws ExecutionException, InterruptedException { DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(logName).build(); From fbcc3f5da52c9df233f3b36487db2379096bd4ea Mon Sep 17 00:00:00 2001 From: losalex Date: Wed, 3 Nov 2021 09:25:42 -0700 Subject: [PATCH 4/5] Address PR comments --- google-cloud-logging/clirr-ignored-differences.xml | 2 +- .../java/com/google/cloud/logging/Logging.java | 14 ++++++++++---- .../java/com/google/cloud/logging/LoggingImpl.java | 12 +++++------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/google-cloud-logging/clirr-ignored-differences.xml b/google-cloud-logging/clirr-ignored-differences.xml index 8848f8c1d..49890ed46 100644 --- a/google-cloud-logging/clirr-ignored-differences.xml +++ b/google-cloud-logging/clirr-ignored-differences.xml @@ -4,6 +4,6 @@ 7012 com/google/cloud/logging/Logging - * deleteLog*(*) + * deleteLog*(java.lang.String, com.google.cloud.logging.LogDestinationName) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java index 156361fa8..b665d96fd 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java @@ -602,7 +602,7 @@ default ApiFuture> listLogsAsync(ListOption... options) { * https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry). The log will reappear if * new entries are written to it. * - *

Example of deleting a log. + *

Example of deleting a log by folder destination. * *

    * {
@@ -620,7 +620,10 @@ default ApiFuture> listLogsAsync(ListOption... options) {
    *
    * @return {@code true} if the log was deleted, {@code false} if it was not found
    */
-  boolean deleteLog(String log, LogDestinationName destination);
+  default boolean deleteLog(String log, LogDestinationName destination) {
+    throw new UnsupportedOperationException(
+        "method deleteLog() does not have default implementation");
+  }
 
   /**
    * Sends a request for deleting a log and all its log entries for given log destination (see
@@ -628,7 +631,7 @@ default ApiFuture> listLogsAsync(ListOption... options) {
    * This method returns a {@code ApiFuture} object to consume the result. {@link ApiFuture#get()}
    * returns {@code true} if the log was deleted, {@code false} if it was not found.
    *
-   * 

Example of asynchronously deleting a log. + *

Example of asynchronously deleting a log by folder destination. * *

    * {
@@ -646,7 +649,10 @@ default ApiFuture> listLogsAsync(ListOption... options) {
    * }
    * 
*/ - ApiFuture deleteLogAsync(String log, LogDestinationName destination); + default ApiFuture deleteLogAsync(String log, LogDestinationName destination) { + throw new UnsupportedOperationException( + "method deleteLogAsync() does not have default implementation"); + } /** * Sends a request for deleting a log and all its log entries. This method returns a {@code diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java index 1c4938324..47da0fe50 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java @@ -441,22 +441,20 @@ public ApiFuture> listLogsAsync(ListOption... options) { } public boolean deleteLog(String log) { - return get(deleteLogAsync(log)); + return get(deleteLogAsync(log, null)); } + @Override public boolean deleteLog(String log, LogDestinationName destination) { - return get(deleteLogAsyncImpl(log, destination)); + return get(deleteLogAsync(log, destination)); } public ApiFuture deleteLogAsync(String log) { - return deleteLogAsyncImpl(log, null); + return deleteLogAsync(log, null); } + @Override public ApiFuture deleteLogAsync(String log, LogDestinationName destination) { - return deleteLogAsyncImpl(log, destination); - } - - private ApiFuture deleteLogAsyncImpl(String log, LogDestinationName destination) { LogName name = getLogName(getOptions().getProjectId(), log, destination, true); DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(name.toString()).build(); return transform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION); From d9139db0235749421479a73a0ff9f3e68e972453 Mon Sep 17 00:00:00 2001 From: losalex Date: Wed, 3 Nov 2021 10:40:08 -0700 Subject: [PATCH 5/5] Move NPE validations into deleteLogAsync from getLogName --- .../com/google/cloud/logging/LoggingImpl.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java index 47da0fe50..2f2d194bf 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java @@ -455,7 +455,12 @@ public ApiFuture deleteLogAsync(String log) { @Override public ApiFuture deleteLogAsync(String log, LogDestinationName destination) { - LogName name = getLogName(getOptions().getProjectId(), log, destination, true); + Preconditions.checkNotNull(log, "log parameter cannot be null"); + String projectId = getOptions().getProjectId(); + if (destination == null) { + Preconditions.checkNotNull(projectId, "projectId parameter cannot be null"); + } + LogName name = getLogName(projectId, log, destination); DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(name.toString()).build(); return transform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION); } @@ -751,8 +756,7 @@ private static WriteLogEntriesRequest writeLogEntriesRequest( WriteLogEntriesRequest.Builder builder = WriteLogEntriesRequest.newBuilder(); String projectId = serviceOptions.getProjectId(); - LogName logName = - getLogName(projectId, LOG_NAME.get(options), LOG_DESTINATION.get(options), false); + LogName logName = getLogName(projectId, LOG_NAME.get(options), LOG_DESTINATION.get(options)); if (logName != null) { builder.setLogName(logName.toString()); @@ -771,21 +775,13 @@ private static WriteLogEntriesRequest writeLogEntriesRequest( } private static LogName getLogName( - String projectId, - String logName, - LogDestinationName destination, - boolean validateParameters) { - if (validateParameters) { - Preconditions.checkNotNull(logName, "logName parameter cannot be null"); - } else if (logName == null) { + String projectId, String logName, LogDestinationName destination) { + if (logName == null) { return null; } // If no destination specified, fallback to project based log name if (destination == null) { - if (validateParameters) { - Preconditions.checkNotNull(projectId, "projectId parameter cannot be null"); - } return LogName.ofProjectLogName(projectId, logName); }