|
| 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.dns; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | + |
| 22 | +import com.google.common.base.MoreObjects; |
| 23 | +import com.google.common.collect.ImmutableList; |
| 24 | + |
| 25 | +import org.joda.time.DateTime; |
| 26 | +import org.joda.time.format.ISODateTimeFormat; |
| 27 | + |
| 28 | +import java.io.Serializable; |
| 29 | +import java.math.BigInteger; |
| 30 | +import java.util.LinkedList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Objects; |
| 33 | + |
| 34 | +/** |
| 35 | + * This class is a container for the managed zone metainformation. Managed zone is a resource that |
| 36 | + * represents a DNS zone hosted by the Cloud DNS service. See <a href="https://cloud.google.com/dns/api/v1/managedZones">Google |
| 37 | + * Cloud DNS documentation</a> for more information. |
| 38 | + */ |
| 39 | +public class ManagedZoneInfo implements Serializable { |
| 40 | + |
| 41 | + private static final long serialVersionUID = 201601191647L; |
| 42 | + private final String name; |
| 43 | + private final Long id; |
| 44 | + private final Long creationTimeMillis; |
| 45 | + private final String dnsName; |
| 46 | + private final String description; |
| 47 | + private final String nameServerSet; |
| 48 | + private final List<String> nameServers; |
| 49 | + |
| 50 | + /** |
| 51 | + * A builder for {@code ManagedZoneInfo}. |
| 52 | + */ |
| 53 | + public static class Builder { |
| 54 | + private String name; |
| 55 | + private Long id; |
| 56 | + private Long creationTimeMillis; |
| 57 | + private String dnsName; |
| 58 | + private String description; |
| 59 | + private String nameServerSet; |
| 60 | + private List<String> nameServers = new LinkedList<>(); |
| 61 | + |
| 62 | + private Builder() { |
| 63 | + } |
| 64 | + |
| 65 | + private Builder(Long id) { |
| 66 | + this.id = checkNotNull(id); |
| 67 | + } |
| 68 | + |
| 69 | + private Builder(String name) { |
| 70 | + this.name = checkNotNull(name); |
| 71 | + } |
| 72 | + |
| 73 | + private Builder(String name, Long id) { |
| 74 | + this.name = checkNotNull(name); |
| 75 | + this.id = checkNotNull(id); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Creates a builder from an existing ManagedZoneInfo object. |
| 80 | + */ |
| 81 | + Builder(ManagedZoneInfo info) { |
| 82 | + this.name = info.name; |
| 83 | + this.id = info.id; |
| 84 | + this.creationTimeMillis = info.creationTimeMillis; |
| 85 | + this.dnsName = info.dnsName; |
| 86 | + this.description = info.description; |
| 87 | + this.nameServerSet = info.nameServerSet; |
| 88 | + this.nameServers.addAll(info.nameServers); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Sets a mandatory user-provided name for the zone. It must be unique within the project. |
| 93 | + */ |
| 94 | + public Builder name(String name) { |
| 95 | + this.name = checkNotNull(name); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Sets an id for the managed zone which is assigned to the managed zone by the server. |
| 101 | + */ |
| 102 | + public Builder id(long id) { |
| 103 | + this.id = id; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Sets the time when this managed zone was created. |
| 109 | + */ |
| 110 | + Builder creationTimeMillis(long creationTimeMillis) { |
| 111 | + checkArgument(creationTimeMillis >= 0, "The timestamp cannot be negative."); |
| 112 | + this.creationTimeMillis = creationTimeMillis; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets a mandatory DNS name of this managed zone, for instance "example.com.". |
| 118 | + */ |
| 119 | + public Builder dnsName(String dnsName) { |
| 120 | + this.dnsName = checkNotNull(dnsName); |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Sets a mandatory description for this managed zone. The value is a string of at most 1024 |
| 126 | + * characters (this limit is posed by Google Cloud DNS; gcloud-java does not enforce the limit) |
| 127 | + * which has no effect on the managed zone's function. |
| 128 | + */ |
| 129 | + public Builder description(String description) { |
| 130 | + this.description = checkNotNull(description); |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Optionally specifies the NameServerSet for this managed zone. A NameServerSet is a set of DNS |
| 136 | + * name servers that all host the same ManagedZones. |
| 137 | + */ |
| 138 | + public Builder nameServerSet(String nameServerSet) { |
| 139 | + // todo(mderka) add more to the doc when questions are answered by the service owner |
| 140 | + this.nameServerSet = checkNotNull(nameServerSet); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Sets a list of servers that hold the information about the managed zone. This information is |
| 146 | + * provided by Google Cloud DNS and is read only. |
| 147 | + */ |
| 148 | + Builder nameServers(List<String> nameServers) { |
| 149 | + this.nameServers.addAll(checkNotNull(nameServers)); |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Removes all the nameservers from the list. |
| 155 | + */ |
| 156 | + Builder clearNameServers() { |
| 157 | + this.nameServers.clear(); |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Builds the instance of ManagedZoneInfo based on the information set here. |
| 163 | + */ |
| 164 | + public ManagedZoneInfo build() { |
| 165 | + return new ManagedZoneInfo(this); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private ManagedZoneInfo(Builder builder) { |
| 170 | + this.name = builder.name; |
| 171 | + this.id = builder.id; |
| 172 | + this.creationTimeMillis = builder.creationTimeMillis; |
| 173 | + this.dnsName = builder.dnsName; |
| 174 | + this.description = builder.description; |
| 175 | + this.nameServerSet = builder.nameServerSet; |
| 176 | + this.nameServers = ImmutableList.copyOf(builder.nameServers); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Returns a builder for {@code ManagedZoneInfo} with an assigned {@code name}. |
| 181 | + */ |
| 182 | + public static Builder builder(String name) { |
| 183 | + return new Builder(name); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Returns a builder for {@code ManagedZoneInfo} with an assigned {@code id}. |
| 188 | + */ |
| 189 | + public static Builder builder(Long id) { |
| 190 | + return new Builder(id); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Returns a builder for {@code ManagedZoneInfo} with an assigned {@code name} and {@code id}. |
| 195 | + */ |
| 196 | + public static Builder builder(String name, Long id) { |
| 197 | + return new Builder(name, id); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Returns an empty builder for {@code ManagedZoneInfo}. We use it internally in {@code toPb()}. |
| 202 | + */ |
| 203 | + private static Builder builder() { |
| 204 | + return new Builder(); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Returns the user-defined name of the managed zone. |
| 209 | + */ |
| 210 | + public String name() { |
| 211 | + return name; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Returns the read-only managed zone id assigned by the server. |
| 216 | + */ |
| 217 | + public Long id() { |
| 218 | + return id; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Returns the time when this time that this managed zone was created on the server. |
| 223 | + */ |
| 224 | + public Long creationTimeMillis() { |
| 225 | + return creationTimeMillis; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Returns the DNS name of this managed zone, for instance "example.com.". |
| 230 | + */ |
| 231 | + public String dnsName() { |
| 232 | + return dnsName; |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Returns the description of this managed zone. This is at most 1024 long mandatory string |
| 237 | + * provided by the user. |
| 238 | + */ |
| 239 | + public String description() { |
| 240 | + return description; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Returns the optionally specified set of DNS name servers that all host this managed zone. |
| 245 | + */ |
| 246 | + public String nameServerSet() { |
| 247 | + // todo(mderka) update this doc after finding out more about this from the service owners |
| 248 | + return nameServerSet; |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * The nameservers that the managed zone should be delegated to. This is defined by the Google DNS |
| 253 | + * cloud. |
| 254 | + */ |
| 255 | + public List<String> nameServers() { |
| 256 | + return nameServers; |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Returns a builder for {@code ManagedZoneInfo} prepopulated with the metadata of this managed |
| 261 | + * zone. |
| 262 | + */ |
| 263 | + public Builder toBuilder() { |
| 264 | + return new Builder(this); |
| 265 | + } |
| 266 | + |
| 267 | + com.google.api.services.dns.model.ManagedZone toPb() { |
| 268 | + com.google.api.services.dns.model.ManagedZone pb = |
| 269 | + new com.google.api.services.dns.model.ManagedZone(); |
| 270 | + pb.setDescription(this.description()); |
| 271 | + pb.setDnsName(this.dnsName()); |
| 272 | + if (this.id() != null) { |
| 273 | + pb.setId(BigInteger.valueOf(this.id())); |
| 274 | + } |
| 275 | + pb.setName(this.name()); |
| 276 | + pb.setNameServers(this.nameServers()); |
| 277 | + pb.setNameServerSet(this.nameServerSet()); |
| 278 | + if (this.creationTimeMillis() != null) { |
| 279 | + pb.setCreationTime(ISODateTimeFormat.dateTime() |
| 280 | + .withZoneUTC() |
| 281 | + .print(this.creationTimeMillis())); |
| 282 | + } |
| 283 | + return pb; |
| 284 | + } |
| 285 | + |
| 286 | + static ManagedZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) { |
| 287 | + Builder b = builder(); |
| 288 | + if (pb.getDescription() != null) { |
| 289 | + b.description(pb.getDescription()); |
| 290 | + } |
| 291 | + if (pb.getDnsName() != null) { |
| 292 | + b.dnsName(pb.getDnsName()); |
| 293 | + } |
| 294 | + if (pb.getId() != null) { |
| 295 | + b.id(pb.getId().longValue()); |
| 296 | + } |
| 297 | + if (pb.getName() != null) { |
| 298 | + b.name(pb.getName()); |
| 299 | + } |
| 300 | + if (pb.getNameServers() != null) { |
| 301 | + b.nameServers(pb.getNameServers()); |
| 302 | + } |
| 303 | + if (pb.getNameServerSet() != null) { |
| 304 | + b.nameServerSet(pb.getNameServerSet()); |
| 305 | + } |
| 306 | + if (pb.getCreationTime() != null) { |
| 307 | + b.creationTimeMillis(DateTime.parse(pb.getCreationTime()).getMillis()); |
| 308 | + } |
| 309 | + return b.build(); |
| 310 | + } |
| 311 | + |
| 312 | + @Override |
| 313 | + public boolean equals(Object obj) { |
| 314 | + return obj instanceof ManagedZoneInfo && Objects.equals(toPb(), ((ManagedZoneInfo) obj).toPb()); |
| 315 | + } |
| 316 | + |
| 317 | + @Override |
| 318 | + public int hashCode() { |
| 319 | + return Objects.hash(name, id, creationTimeMillis, dnsName, |
| 320 | + description, nameServerSet, nameServers); |
| 321 | + } |
| 322 | + |
| 323 | + @Override |
| 324 | + public String toString() { |
| 325 | + return MoreObjects.toStringHelper(this) |
| 326 | + .add("name", name()) |
| 327 | + .add("id", id()) |
| 328 | + .add("description", description()) |
| 329 | + .add("dnsName", dnsName()) |
| 330 | + .add("nameServerSet", nameServerSet()) |
| 331 | + .add("nameServers", nameServers()) |
| 332 | + .toString(); |
| 333 | + } |
| 334 | +} |
0 commit comments