This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathLogDestinationName.java
More file actions
141 lines (119 loc) · 4.67 KB
/
LogDestinationName.java
File metadata and controls
141 lines (119 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* 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;
import org.jspecify.annotations.Nullable;
/**
* 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 {
private static final long serialVersionUID = 7944256748441111191L;
public 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 @Nullable 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;
}
/** Returns ID value associated with {@code LogDestinationName} object */
public String getDestinationId() {
return getValue().toString();
}
/** Returns destination type option value associated with {@code LogDestinationName} object */
public DestinationType getDestinationType() {
return getOptionType();
}
/** Creates a {@code LogDestinationName} object from given {@code LogName}. */
public static @Nullable 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;
}
}