|
| 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.Function; |
| 22 | +import com.google.common.base.MoreObjects; |
| 23 | +import com.google.common.collect.Iterables; |
| 24 | +import com.google.common.collect.Lists; |
| 25 | + |
| 26 | +import java.io.Serializable; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +/** |
| 32 | + * This class describes the schema of Cloud monitored resources. Monitored resource descriptors |
| 33 | + * contain a type name and a set of labels. For example, the monitored resource descriptor for |
| 34 | + * Google Compute Engine VM instances has a type of {@code gce_instance} and specifies the use of |
| 35 | + * the labels {@code instance_id} and {@code zone} to identify particular VM instances. |
| 36 | + */ |
| 37 | +public class MonitoredResourceDescriptor implements Serializable { |
| 38 | + |
| 39 | + private static final long serialVersionUID = -3702077512777687441L; |
| 40 | + public static final Function<com.google.api.MonitoredResourceDescriptor, |
| 41 | + MonitoredResourceDescriptor> FROM_PB_FUNCTION = |
| 42 | + new Function<com.google.api.MonitoredResourceDescriptor, MonitoredResourceDescriptor>() { |
| 43 | + @Override |
| 44 | + public MonitoredResourceDescriptor apply( |
| 45 | + com.google.api.MonitoredResourceDescriptor pb) { |
| 46 | + return fromPb(pb); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + private final String type; |
| 51 | + private final String name; |
| 52 | + private final String displayName; |
| 53 | + private final String description; |
| 54 | + private final List<LabelDescriptor> labels; |
| 55 | + |
| 56 | + /** |
| 57 | + * This class describes a label for a monitored resource. Label descriptors contain the key for |
| 58 | + * the label, the type of data that the label can hold and an optional description. |
| 59 | + */ |
| 60 | + public static class LabelDescriptor implements Serializable { |
| 61 | + |
| 62 | + private static final long serialVersionUID = -2811608103754481777L; |
| 63 | + private static final Function<com.google.api.LabelDescriptor, LabelDescriptor> |
| 64 | + FROM_PB_FUNCTION = new Function<com.google.api.LabelDescriptor, LabelDescriptor>() { |
| 65 | + @Override |
| 66 | + public LabelDescriptor apply(com.google.api.LabelDescriptor descriptorPb) { |
| 67 | + return fromPb(descriptorPb); |
| 68 | + } |
| 69 | + }; |
| 70 | + private static final Function<LabelDescriptor, com.google.api.LabelDescriptor> |
| 71 | + TO_PB_FUNCTION = new Function<LabelDescriptor, com.google.api.LabelDescriptor>() { |
| 72 | + @Override |
| 73 | + public com.google.api.LabelDescriptor apply(LabelDescriptor descriptor) { |
| 74 | + return descriptor.toPb(); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + private final String key; |
| 79 | + private final ValueType valueType; |
| 80 | + private final String description; |
| 81 | + |
| 82 | + /** |
| 83 | + * Value types that can be used as label values. |
| 84 | + */ |
| 85 | + public enum ValueType { |
| 86 | + STRING(com.google.api.LabelDescriptor.ValueType.STRING), |
| 87 | + BOOL(com.google.api.LabelDescriptor.ValueType.BOOL), |
| 88 | + INT64(com.google.api.LabelDescriptor.ValueType.INT64); |
| 89 | + |
| 90 | + private com.google.api.LabelDescriptor.ValueType typePb; |
| 91 | + |
| 92 | + ValueType(com.google.api.LabelDescriptor.ValueType typePb) { |
| 93 | + this.typePb = typePb; |
| 94 | + } |
| 95 | + |
| 96 | + com.google.api.LabelDescriptor.ValueType toPb() { |
| 97 | + return typePb; |
| 98 | + } |
| 99 | + |
| 100 | + static ValueType fromPb(com.google.api.LabelDescriptor.ValueType typePb) { |
| 101 | + switch (typePb) { |
| 102 | + case STRING: |
| 103 | + return ValueType.STRING; |
| 104 | + case BOOL: |
| 105 | + return ValueType.BOOL; |
| 106 | + case INT64: |
| 107 | + return ValueType.INT64; |
| 108 | + default: |
| 109 | + throw new IllegalArgumentException("Unrecognized label type"); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + LabelDescriptor(String key, ValueType valueType, String description) { |
| 115 | + this.key = checkNotNull(key); |
| 116 | + this.valueType = checkNotNull(valueType); |
| 117 | + this.description = description; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns the key associated to this label. |
| 122 | + */ |
| 123 | + public String key() { |
| 124 | + return key; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the type of data that can be assigned to this label. |
| 129 | + */ |
| 130 | + public ValueType valueType() { |
| 131 | + return valueType; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns the optional human-readable description for this label. If not set, this method |
| 136 | + * returns {@code null}. |
| 137 | + */ |
| 138 | + public String description() { |
| 139 | + return description; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public final int hashCode() { |
| 144 | + return Objects.hash(key, valueType, description); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public final boolean equals(Object obj) { |
| 149 | + if (obj == this) { |
| 150 | + return true; |
| 151 | + } |
| 152 | + if (obj == null || !obj.getClass().equals(LabelDescriptor.class)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + LabelDescriptor other = (LabelDescriptor) obj; |
| 156 | + return Objects.equals(key, other.key) |
| 157 | + && Objects.equals(valueType, other.valueType) |
| 158 | + && Objects.equals(description, other.description); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public String toString() { |
| 163 | + return MoreObjects.toStringHelper(this) |
| 164 | + .add("key", key) |
| 165 | + .add("valueType", valueType) |
| 166 | + .add("description", description) |
| 167 | + .toString(); |
| 168 | + } |
| 169 | + |
| 170 | + com.google.api.LabelDescriptor toPb() { |
| 171 | + com.google.api.LabelDescriptor.Builder builder = com.google.api.LabelDescriptor.newBuilder() |
| 172 | + .setKey(key) |
| 173 | + .setValueType(valueType.toPb()); |
| 174 | + if (description != null) { |
| 175 | + builder.setDescription(description); |
| 176 | + } |
| 177 | + return builder.build(); |
| 178 | + } |
| 179 | + |
| 180 | + static LabelDescriptor fromPb(com.google.api.LabelDescriptor descriptorPb) { |
| 181 | + String description = null; |
| 182 | + if (descriptorPb.getDescription() != null && !descriptorPb.getDescription().equals("")) { |
| 183 | + description = descriptorPb.getDescription(); |
| 184 | + } |
| 185 | + return new LabelDescriptor(descriptorPb.getKey(), |
| 186 | + ValueType.fromPb(descriptorPb.getValueType()), description); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + static class Builder { |
| 191 | + |
| 192 | + private final String type; |
| 193 | + private String name; |
| 194 | + private String displayName; |
| 195 | + private String description; |
| 196 | + private List<LabelDescriptor> labels = new ArrayList<>(); |
| 197 | + |
| 198 | + Builder(String type) { |
| 199 | + this.type = type; |
| 200 | + } |
| 201 | + |
| 202 | + Builder name(String name) { |
| 203 | + this.name = name; |
| 204 | + return this; |
| 205 | + } |
| 206 | + |
| 207 | + Builder displayName(String displayName) { |
| 208 | + this.displayName = displayName; |
| 209 | + return this; |
| 210 | + } |
| 211 | + |
| 212 | + Builder description(String description) { |
| 213 | + this.description = description; |
| 214 | + return this; |
| 215 | + } |
| 216 | + |
| 217 | + Builder labels(List<LabelDescriptor> labels) { |
| 218 | + this.labels = labels; |
| 219 | + return this; |
| 220 | + } |
| 221 | + |
| 222 | + MonitoredResourceDescriptor build() { |
| 223 | + return new MonitoredResourceDescriptor(this); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + MonitoredResourceDescriptor(Builder builder) { |
| 228 | + this.type = checkNotNull(builder.type); |
| 229 | + this.name = builder.name; |
| 230 | + this.displayName = builder.displayName; |
| 231 | + this.description = builder.description; |
| 232 | + this.labels = checkNotNull(builder.labels); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Returns the monitored resource type. For example, the type {@code cloudsql_database} represents |
| 237 | + * databases in Google Cloud SQL. |
| 238 | + */ |
| 239 | + public String type() { |
| 240 | + return type; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Returns an optional name for the monitored resource descriptor. If not set, this method returns |
| 245 | + * {@code null}. |
| 246 | + */ |
| 247 | + public String name() { |
| 248 | + return name; |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Returns an optional concise name for the monitored resource type. This value might be displayed |
| 253 | + * in user interfaces. For example, {@code Google Cloud SQL Database}. If not set, this method |
| 254 | + * returns {@code null}. |
| 255 | + */ |
| 256 | + public String displayName() { |
| 257 | + return displayName; |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * Returns an optional detailed description of the monitored resource type. This value might be |
| 262 | + * used in documentation. If not set, this method returns {@code null}. |
| 263 | + */ |
| 264 | + public String description() { |
| 265 | + return description; |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * Returns a list of labels used to describe instances of this monitored resource type. For |
| 270 | + * example, an individual Google Cloud SQL database is identified by values for the labels |
| 271 | + * {@code database_id} and {@code region}. |
| 272 | + */ |
| 273 | + public List<LabelDescriptor> labels() { |
| 274 | + return labels; |
| 275 | + } |
| 276 | + |
| 277 | + @Override |
| 278 | + public final int hashCode() { |
| 279 | + return Objects.hash(type, name, displayName, description, labels); |
| 280 | + } |
| 281 | + |
| 282 | + @Override |
| 283 | + public final boolean equals(Object obj) { |
| 284 | + if (obj == this) { |
| 285 | + return true; |
| 286 | + } |
| 287 | + if (obj == null || !obj.getClass().equals(MonitoredResourceDescriptor.class)) { |
| 288 | + return false; |
| 289 | + } |
| 290 | + MonitoredResourceDescriptor other = (MonitoredResourceDescriptor) obj; |
| 291 | + return Objects.equals(type, other.type) |
| 292 | + && Objects.equals(name, other.name) |
| 293 | + && Objects.equals(displayName, other.displayName) |
| 294 | + && Objects.equals(description, other.description) |
| 295 | + && Objects.equals(labels, other.labels); |
| 296 | + } |
| 297 | + |
| 298 | + @Override |
| 299 | + public String toString() { |
| 300 | + return MoreObjects.toStringHelper(this) |
| 301 | + .add("type", type) |
| 302 | + .add("name", name) |
| 303 | + .add("displayName", displayName) |
| 304 | + .add("description", description) |
| 305 | + .add("labels", labels) |
| 306 | + .toString(); |
| 307 | + } |
| 308 | + |
| 309 | + public com.google.api.MonitoredResourceDescriptor toPb() { |
| 310 | + com.google.api.MonitoredResourceDescriptor.Builder builder = |
| 311 | + com.google.api.MonitoredResourceDescriptor.newBuilder() |
| 312 | + .setType(type) |
| 313 | + .addAllLabels(Iterables.transform(labels, LabelDescriptor.TO_PB_FUNCTION)); |
| 314 | + if (name != null) { |
| 315 | + builder.setName(name); |
| 316 | + } |
| 317 | + if (displayName != null) { |
| 318 | + builder.setDisplayName(displayName); |
| 319 | + } |
| 320 | + if (description != null) { |
| 321 | + builder.setDescription(description); |
| 322 | + } |
| 323 | + return builder.build(); |
| 324 | + } |
| 325 | + |
| 326 | + static Builder builder(String type) { |
| 327 | + return new Builder(type); |
| 328 | + } |
| 329 | + |
| 330 | + public static MonitoredResourceDescriptor fromPb( |
| 331 | + com.google.api.MonitoredResourceDescriptor descriptorPb) { |
| 332 | + Builder builder = builder(descriptorPb.getType()); |
| 333 | + if (descriptorPb.getName() != null && !descriptorPb.getName().equals("")) { |
| 334 | + builder.name(descriptorPb.getName()); |
| 335 | + } |
| 336 | + if (descriptorPb.getDisplayName() != null && !descriptorPb.getDisplayName().equals("")) { |
| 337 | + builder.displayName(descriptorPb.getDisplayName()); |
| 338 | + } |
| 339 | + if (descriptorPb.getDescription() != null && !descriptorPb.getDescription().equals("")) { |
| 340 | + builder.description(descriptorPb.getDescription()); |
| 341 | + } |
| 342 | + builder.labels(Lists.transform(descriptorPb.getLabelsList(), LabelDescriptor.FROM_PB_FUNCTION)); |
| 343 | + return builder.build(); |
| 344 | + } |
| 345 | +} |
0 commit comments