Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add destination property into LogEntry (#720)
* Add ResourceName class which will provide destination log resource name customization and integrate it with LogEntry * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Add testToAndFromPbWithExpectedFailure and fix copyright header * Adress PR comments * Fix build break which caused by missing isBlank() symbol * Address PR comments and fix tests accordingly * Add extra test in testToAndFromPb() * Fix description for fromLogName() * Address latest PR comments * Fix fromPb() to call fromLogName() only once Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1fa3a6e
commit 43ea0b4
Showing
4 changed files
with
214 additions
and
14 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
google-cloud-logging/src/main/java/com/google/cloud/logging/LogDestinationName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.logging; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.logging.v2.LogName; | ||
import java.util.Map; | ||
|
||
/** | ||
* Class for specifying resource name of the log to which this log entry belongs (see 'logName' | ||
* parameter in https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry) | ||
*/ | ||
public final class LogDestinationName extends Option { | ||
|
||
enum DestinationType implements Option.OptionType { | ||
PROJECT, | ||
FOLDER, | ||
ORGANIZATION, | ||
BILLINGACCOUNT; | ||
|
||
@SuppressWarnings("unchecked") | ||
<T> T get(Map<Option.OptionType, ?> options) { | ||
return (T) options.get(this); | ||
} | ||
} | ||
|
||
private LogDestinationName(Option.OptionType option, Object value) { | ||
super(option, value); | ||
checkArgument(!checkNotNull(value).toString().trim().isEmpty()); | ||
} | ||
|
||
/** | ||
* Returns an option which sets and validates project ID resource name for log entries. | ||
* | ||
* @param id corresponds to PROJECT_ID token in 'logName' field described in | ||
* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry | ||
*/ | ||
public static LogDestinationName project(String id) { | ||
return new LogDestinationName(DestinationType.PROJECT, id); | ||
} | ||
|
||
/** | ||
* Returns an option which sets and validates project ID resource name for log entries. | ||
* | ||
* @param id corresponds to FOLDER_ID token in 'logName' field described in | ||
* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry | ||
*/ | ||
public static LogDestinationName folder(String id) { | ||
return new LogDestinationName(DestinationType.FOLDER, id); | ||
} | ||
|
||
/** | ||
* Returns an option which sets and validates project ID resource name for log entries. | ||
* | ||
* @param id corresponds to ORGANIZATION_ID token in 'logName' field described in | ||
* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry | ||
*/ | ||
public static LogDestinationName organization(String id) { | ||
return new LogDestinationName(DestinationType.ORGANIZATION, id); | ||
} | ||
|
||
/** | ||
* Returns an option which sets and validates project ID resource name for log entries. | ||
* | ||
* @param id corresponds to BILLING_ACCOUNT_ID token in 'logName' field described in | ||
* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry | ||
*/ | ||
public static LogDestinationName billingAccount(String id) { | ||
return new LogDestinationName(DestinationType.BILLINGACCOUNT, id); | ||
} | ||
|
||
/** Creates a {@code LogEntry} object for given log ID. */ | ||
public LogName toLogName(String logId) { | ||
if (logId == null) { | ||
return null; | ||
} | ||
|
||
switch ((DestinationType) getOptionType()) { | ||
case PROJECT: | ||
return LogName.ofProjectLogName(getValue().toString(), logId); | ||
|
||
case FOLDER: | ||
return LogName.ofFolderLogName(getValue().toString(), logId); | ||
|
||
case ORGANIZATION: | ||
return LogName.ofOrganizationLogName(getValue().toString(), logId); | ||
|
||
case BILLINGACCOUNT: | ||
return LogName.ofBillingAccountLogName(getValue().toString(), logId); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** Creates a {@code LogDestinationName} object from given {@code LogName}. */ | ||
public static LogDestinationName fromLogName(LogName logName) { | ||
if (logName == null) { | ||
return null; | ||
} | ||
|
||
if (logName.getProject() != null) { | ||
return project(logName.getProject()); | ||
} else if (logName.getBillingAccount() != null) { | ||
return billingAccount(logName.getBillingAccount()); | ||
} else if (logName.getFolder() != null) { | ||
return folder(logName.getFolder()); | ||
} else if (logName.getOrganization() != null) { | ||
return organization(logName.getOrganization()); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters