|
| 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.gcloud.compute; |
| 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.ImmutableMap; |
| 24 | +import com.google.common.collect.Lists; |
| 25 | +import com.google.common.collect.Maps; |
| 26 | + |
| 27 | +import java.io.Serializable; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +/** |
| 33 | + * Metadata for Google Compute Engine Instance as ket/value pairs. This includes custom metadata |
| 34 | + * and predefined keys. |
| 35 | + * |
| 36 | + * @see <a href="https://cloud.google.com/compute/docs/metadata">Metadata</a> |
| 37 | + */ |
| 38 | +public final class Metadata implements Serializable { |
| 39 | + |
| 40 | + static final Function<com.google.api.services.compute.model.Metadata, Metadata> |
| 41 | + FROM_PB_FUNCTION = |
| 42 | + new Function<com.google.api.services.compute.model.Metadata, Metadata>() { |
| 43 | + @Override |
| 44 | + public Metadata apply(com.google.api.services.compute.model.Metadata pb) { |
| 45 | + return Metadata.fromPb(pb); |
| 46 | + } |
| 47 | + }; |
| 48 | + static final Function<Metadata, com.google.api.services.compute.model.Metadata> TO_PB_FUNCTION = |
| 49 | + new Function<Metadata, com.google.api.services.compute.model.Metadata>() { |
| 50 | + @Override |
| 51 | + public com.google.api.services.compute.model.Metadata apply(Metadata metadata) { |
| 52 | + return metadata.toPb(); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + private static final long serialVersionUID = -945038809838910107L; |
| 57 | + |
| 58 | + private final Map<String, String> values; |
| 59 | + private final String fingerprint; |
| 60 | + |
| 61 | + /** |
| 62 | + * A builder for {@code Metadata} objects. |
| 63 | + */ |
| 64 | + public static final class Builder { |
| 65 | + |
| 66 | + private Map<String, String> values; |
| 67 | + private String fingerprint; |
| 68 | + |
| 69 | + Builder() { |
| 70 | + values = Maps.newHashMap(); |
| 71 | + } |
| 72 | + |
| 73 | + Builder(Metadata metadata) { |
| 74 | + this.values = metadata.values != null ? Maps.newHashMap(metadata.values) |
| 75 | + : Maps.<String, String>newHashMap(); |
| 76 | + this.fingerprint = metadata.fingerprint; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Sets the metadata for the instance as key/value pairs. The total size of all keys and |
| 81 | + * values must be less than 512 KB. Keys must conform to the following regexp: |
| 82 | + * {@code [a-zA-Z0-9-_]+}, and be less than 128 bytes in length. This is reflected as part of |
| 83 | + * a URL in the metadata server. Additionally, to avoid ambiguity, keys must not conflict with |
| 84 | + * any other metadata keys for the project. Values must be less than or equal to 32768 bytes. |
| 85 | + */ |
| 86 | + public Builder values(Map<String, String> values) { |
| 87 | + this.values = Maps.newHashMap(checkNotNull(values)); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Adds a key/value pair to the instance metadata. The total size of all keys and values must |
| 93 | + * be less than 512 KB. Keys must conform to the following regexp: {@code [a-zA-Z0-9-_]+}, and |
| 94 | + * be less than 128 bytes in length. This is reflected as part of a URL in the metadata |
| 95 | + * server. Additionally, to avoid ambiguity, keys must not conflict with any other metadata |
| 96 | + * keys for the project. Values must be less than or equal to 32768 bytes. |
| 97 | + */ |
| 98 | + public Builder add(String key, String value) { |
| 99 | + this.values.put(key, value); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Sets the fingerprint for the metadata. This value can be used to update instance's |
| 105 | + * metadata. |
| 106 | + */ |
| 107 | + public Builder fingerprint(String fingerprint) { |
| 108 | + this.fingerprint = fingerprint; |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Creates a {@code Metadata} object. |
| 114 | + */ |
| 115 | + public Metadata build() { |
| 116 | + return new Metadata(this); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private Metadata(Builder builder) { |
| 121 | + this.values = ImmutableMap.copyOf(builder.values); |
| 122 | + this.fingerprint = builder.fingerprint; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Returns instance's metadata as key/value pairs. |
| 127 | + */ |
| 128 | + public Map<String, String> values() { |
| 129 | + return values; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns the fingerprint for the metadata. This value can be used to update instance's |
| 134 | + * metadata. |
| 135 | + */ |
| 136 | + public String fingerprint() { |
| 137 | + return fingerprint; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Returns a builder for the current instance metadata. |
| 142 | + */ |
| 143 | + public Builder toBuilder() { |
| 144 | + return new Builder(this); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String toString() { |
| 149 | + return MoreObjects.toStringHelper(this) |
| 150 | + .add("values", values) |
| 151 | + .add("fingerprint", fingerprint) |
| 152 | + .toString(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public int hashCode() { |
| 157 | + return Objects.hash(values, fingerprint); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public boolean equals(Object obj) { |
| 162 | + return obj == this |
| 163 | + || obj instanceof Metadata |
| 164 | + && Objects.equals(toPb(), ((Metadata) obj).toPb()); |
| 165 | + } |
| 166 | + |
| 167 | + com.google.api.services.compute.model.Metadata toPb() { |
| 168 | + com.google.api.services.compute.model.Metadata metadataPb = |
| 169 | + new com.google.api.services.compute.model.Metadata(); |
| 170 | + metadataPb.setFingerprint(fingerprint); |
| 171 | + List<com.google.api.services.compute.model.Metadata.Items> itemsPb = |
| 172 | + Lists.newArrayListWithCapacity(values.size()); |
| 173 | + for (Map.Entry<String, String> entry : values.entrySet()) { |
| 174 | + itemsPb.add(new com.google.api.services.compute.model.Metadata.Items() |
| 175 | + .setKey(entry.getKey()).setValue(entry.getValue())); |
| 176 | + } |
| 177 | + metadataPb.setItems(itemsPb); |
| 178 | + metadataPb.setFingerprint(fingerprint); |
| 179 | + return metadataPb; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Returns a builder for a {@code Metadata} object. |
| 184 | + */ |
| 185 | + public static Builder builder() { |
| 186 | + return new Builder(); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Returns a {@code Metadata} object given the the metadata as a map. The total size of all keys |
| 191 | + * and values must be less than 512 KB. Keys must conform to the following regexp: |
| 192 | + * {@code [a-zA-Z0-9-_]+}, and be less than 128 bytes in length. This is reflected as part of a |
| 193 | + * URL in the metadata server. Additionally, to avoid ambiguity, keys must not conflict with any |
| 194 | + * other metadata keys for the project. Values must be less than or equal to 32768 bytes. |
| 195 | + */ |
| 196 | + public static Metadata of(Map<String, String> values) { |
| 197 | + return builder().values(values).build(); |
| 198 | + } |
| 199 | + |
| 200 | + static Metadata fromPb(com.google.api.services.compute.model.Metadata metadataPb) { |
| 201 | + Builder builder = builder(); |
| 202 | + if (metadataPb.getItems() != null) { |
| 203 | + Map<String, String> metadataValues = |
| 204 | + Maps.newHashMapWithExpectedSize(metadataPb.getItems().size()); |
| 205 | + for (com.google.api.services.compute.model.Metadata.Items item : metadataPb.getItems()) { |
| 206 | + metadataValues.put(item.getKey(), item.getValue()); |
| 207 | + } |
| 208 | + builder.values(metadataValues); |
| 209 | + } |
| 210 | + return builder.fingerprint(metadataPb.getFingerprint()).build(); |
| 211 | + } |
| 212 | +} |
0 commit comments