|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import com.google.common.base.MoreObjects; |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | + |
| 24 | +import java.io.Serializable; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +/** |
| 30 | + * Objects of this class represent a resource that can be used for monitoring, logging, billing, or |
| 31 | + * other purposes. Examples include virtual machine instances, databases, and storage devices such |
| 32 | + * as disks. The type field identifies a {@link MonitoredResourceDescriptor} object that describes |
| 33 | + * the resource's schema. Information in the labels field identifies the actual resource and its |
| 34 | + * attributes according to the schema. |
| 35 | + * |
| 36 | + * <p>For example, the monitored resource for Google Compute Engine VM instances has |
| 37 | + * {@code gce_instance} type and specifies values for the labels {@code instance_id} and |
| 38 | + * {@code zone} to identify particular VM instances. |
| 39 | + */ |
| 40 | +public final class MonitoredResource implements Serializable { |
| 41 | + |
| 42 | + private static final long serialVersionUID = -4393604148752640581L; |
| 43 | + |
| 44 | + private final String type; |
| 45 | + private final Map<String, String> labels; |
| 46 | + |
| 47 | + /** |
| 48 | + * A builder for {@code MonitoredResource} objects. |
| 49 | + */ |
| 50 | + public static class Builder { |
| 51 | + |
| 52 | + private String type; |
| 53 | + private Map<String, String> labels = new HashMap<>(); |
| 54 | + |
| 55 | + Builder(String type) { |
| 56 | + this.type = type; |
| 57 | + } |
| 58 | + |
| 59 | + Builder(MonitoredResource monitoredResource) { |
| 60 | + this.type = monitoredResource.type; |
| 61 | + this.labels = new HashMap<>(monitoredResource.labels); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sets the monitored resource type. This value must match the one of |
| 66 | + * {@link MonitoredResourceDescriptor#type()} of a {@code MonitoredResourceDescriptor} object. |
| 67 | + * For example, the type {@code cloudsql_database} represent databases in Google Cloud SQL. |
| 68 | + */ |
| 69 | + public Builder type(String type) { |
| 70 | + this.type = type; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Sets the values for all the labels required by the corresponding monitored resource |
| 76 | + * descriptor (see {@link MonitoredResourceDescriptor#labels()}. For example, Google Compute |
| 77 | + * Engine VM instances use the labels {@code instance_id} and {@code zone}. |
| 78 | + */ |
| 79 | + public Builder labels(Map<String, String> labels) { |
| 80 | + this.labels = new HashMap<>(checkNotNull(labels)); |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Adds a label to the labels of the monitored resource. |
| 86 | + */ |
| 87 | + public Builder addLabel(String key, String value) { |
| 88 | + this.labels.put(key, value); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Clears all the labels of the monitored resource. |
| 94 | + */ |
| 95 | + public Builder clearLabels() { |
| 96 | + this.labels.clear(); |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + public MonitoredResource build() { |
| 101 | + return new MonitoredResource(this); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + MonitoredResource(Builder builder) { |
| 106 | + this.type = checkNotNull(builder.type); |
| 107 | + this.labels = ImmutableMap.copyOf(builder.labels); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns the monitored resource type. This value must match the one of |
| 112 | + * {@link MonitoredResourceDescriptor#type()} of a {@code MonitoredResourceDescriptor} object. |
| 113 | + * For example, the type {@code cloudsql_database} represent databases in Google Cloud SQL. |
| 114 | + */ |
| 115 | + public String type() { |
| 116 | + return type; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the values for all the labels required by the corresponding monitored resource |
| 121 | + * descriptor (see {@link MonitoredResourceDescriptor#labels()}. For example, Google Compute |
| 122 | + * Engine VM instances use the labels {@code instance_id} and {@code zone}. |
| 123 | + */ |
| 124 | + public Map<String, String> labels() { |
| 125 | + return labels; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public int hashCode() { |
| 130 | + return Objects.hash(type, labels); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public boolean equals(Object obj) { |
| 135 | + if (obj == this) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + if (!(obj instanceof MonitoredResource)) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + MonitoredResource other = (MonitoredResource) obj; |
| 142 | + return Objects.equals(type, other.type) && Objects.equals(labels, other.labels); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String toString() { |
| 147 | + return MoreObjects.toStringHelper(this) |
| 148 | + .add("type", type) |
| 149 | + .add("labels", labels) |
| 150 | + .toString(); |
| 151 | + } |
| 152 | + |
| 153 | + public com.google.api.MonitoredResource toPb() { |
| 154 | + return com.google.api.MonitoredResource.newBuilder() |
| 155 | + .setType(type) |
| 156 | + .putAllLabels(labels) |
| 157 | + .build(); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns a builder for this {@code MonitoredResource} object. |
| 162 | + */ |
| 163 | + public Builder toBuilder() { |
| 164 | + return new Builder(this); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Returns a builder for {@code MonitoredResource} objects given the resource's type. |
| 169 | + */ |
| 170 | + public static Builder builder(String type) { |
| 171 | + return new Builder(type); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Creates a {@code MonitoredResource} object given the resource's type and labels. |
| 176 | + */ |
| 177 | + public static MonitoredResource of(String type, Map<String, String> labels) { |
| 178 | + return builder(type).labels(labels).build(); |
| 179 | + } |
| 180 | + |
| 181 | + public static MonitoredResource fromPb(com.google.api.MonitoredResource descriptorPb) { |
| 182 | + return new Builder(descriptorPb.getType()).labels(descriptorPb.getLabels()).build(); |
| 183 | + } |
| 184 | +} |
0 commit comments