|
| 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 com.google.common.base.MoreObjects; |
| 20 | +import com.google.common.collect.Lists; |
| 21 | + |
| 22 | +import org.joda.time.DateTime; |
| 23 | +import org.joda.time.format.ISODateTimeFormat; |
| 24 | + |
| 25 | +import java.io.Serializable; |
| 26 | +import java.util.LinkedList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Objects; |
| 29 | + |
| 30 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | + |
| 32 | +/** |
| 33 | + * A class representing a change. A change is an atomic update to a collection of {@link DnsRecord}s |
| 34 | + * within a {@code ManagedZone}. |
| 35 | + * |
| 36 | + * @see <a href="https://cloud.google.com/dns/api/v1/changes">Google Cloud DNS documentation</a> |
| 37 | + */ |
| 38 | +public class ChangeRequest implements Serializable { |
| 39 | + |
| 40 | + private static final long serialVersionUID = 201601251649L; |
| 41 | + private final List<DnsRecord> additions; |
| 42 | + private final List<DnsRecord> deletions; |
| 43 | + private final String id; |
| 44 | + private final Long startTimeMillis; |
| 45 | + private final Status status; |
| 46 | + |
| 47 | + /** |
| 48 | + * This enumerates the possible states of a {@code ChangeRequest}. |
| 49 | + * |
| 50 | + * @see <a href="https://cloud.google.com/dns/api/v1/changes#resource">Google Cloud DNS |
| 51 | + * documentation</a> |
| 52 | + */ |
| 53 | + public enum Status { |
| 54 | + PENDING("pending"), |
| 55 | + DONE("done"); |
| 56 | + |
| 57 | + private final String status; |
| 58 | + |
| 59 | + Status(String status) { |
| 60 | + this.status = status; |
| 61 | + } |
| 62 | + |
| 63 | + static Status translate(String status) { |
| 64 | + if ("pending".equals(status)) { |
| 65 | + return PENDING; |
| 66 | + } else if ("done".equals(status)) { |
| 67 | + return DONE; |
| 68 | + } else { |
| 69 | + throw new IllegalArgumentException("Such a status is unknown."); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * A builder for {@code ChangeRequest}s. |
| 76 | + */ |
| 77 | + public static class Builder { |
| 78 | + |
| 79 | + private List<DnsRecord> additions = new LinkedList<>(); |
| 80 | + private List<DnsRecord> deletions = new LinkedList<>(); |
| 81 | + private String id; |
| 82 | + private Long startTimeMillis; |
| 83 | + private Status status; |
| 84 | + |
| 85 | + private Builder(ChangeRequest cr) { |
| 86 | + this.additions = Lists.newLinkedList(cr.additions()); |
| 87 | + this.deletions = Lists.newLinkedList(cr.deletions()); |
| 88 | + this.id = cr.id(); |
| 89 | + this.startTimeMillis = cr.startTimeMillis(); |
| 90 | + this.status = cr.status(); |
| 91 | + } |
| 92 | + |
| 93 | + private Builder() { |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Sets a collection of {@link DnsRecord}s which are to be added to the zone upon executing this |
| 98 | + * {@code ChangeRequest}. |
| 99 | + */ |
| 100 | + public Builder additions(List<DnsRecord> additions) { |
| 101 | + this.additions = Lists.newLinkedList(checkNotNull(additions)); |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Adds a {@link DnsRecord} which to be <strong>added</strong> to the zone upon executing this |
| 107 | + * {@code ChangeRequest}. |
| 108 | + */ |
| 109 | + public Builder add(DnsRecord record) { |
| 110 | + this.additions.add(checkNotNull(record)); |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Adds a {@link DnsRecord} which to be <strong>deleted</strong> to the zone upon executing this |
| 116 | + * {@code ChangeRequest}. |
| 117 | + */ |
| 118 | + public Builder delete(DnsRecord record) { |
| 119 | + this.deletions.add(checkNotNull(record)); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Clears the collection of {@link DnsRecord}s which are to be added to the zone upon executing |
| 125 | + * this {@code ChangeRequest}. |
| 126 | + */ |
| 127 | + public Builder clearAdditions() { |
| 128 | + this.additions.clear(); |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Clears the collection of {@link DnsRecord}s which are to be deleted from the zone upon |
| 134 | + * executing this {@code ChangeRequest}. |
| 135 | + */ |
| 136 | + public Builder clearDeletions() { |
| 137 | + this.deletions.clear(); |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Removes a single {@link DnsRecord} from the collection of of records to be |
| 143 | + * <strong>added</strong> to the zone upon executing this {@code ChangeRequest}. |
| 144 | + */ |
| 145 | + public Builder removeAddition(DnsRecord record) { |
| 146 | + this.additions.remove(record); |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Removes a single {@link DnsRecord} from the collection of of records to be |
| 152 | + * <strong>deleted</strong> from the zone upon executing this {@code ChangeRequest}. |
| 153 | + */ |
| 154 | + public Builder removeDeletion(DnsRecord record) { |
| 155 | + this.deletions.remove(record); |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Sets a collection of {@link DnsRecord}s which are to be deleted from the zone upon executing |
| 161 | + * this {@code ChangeRequest}. |
| 162 | + */ |
| 163 | + public Builder deletions(List<DnsRecord> deletions) { |
| 164 | + this.deletions = Lists.newLinkedList(checkNotNull(deletions)); |
| 165 | + return this; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Associates a server-assigned id to this {@code ChangeRequest}. |
| 170 | + */ |
| 171 | + Builder id(String id) { |
| 172 | + this.id = checkNotNull(id); |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Sets the time when this {@code ChangeRequest} was started by a server. |
| 178 | + */ |
| 179 | + Builder startTimeMillis(long startTimeMillis) { |
| 180 | + this.startTimeMillis = startTimeMillis; |
| 181 | + return this; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Sets the current status of this {@code ChangeRequest}. |
| 186 | + */ |
| 187 | + Builder status(Status status) { |
| 188 | + this.status = checkNotNull(status); |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Creates a {@code ChangeRequest} instance populated by the values associated with this |
| 194 | + * builder. |
| 195 | + */ |
| 196 | + public ChangeRequest build() { |
| 197 | + return new ChangeRequest(this); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private ChangeRequest(Builder builder) { |
| 202 | + this.additions = builder.additions; |
| 203 | + this.deletions = builder.deletions; |
| 204 | + this.id = builder.id; |
| 205 | + this.startTimeMillis = builder.startTimeMillis; |
| 206 | + this.status = builder.status; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Returns an empty builder for the {@code ChangeRequest} class. |
| 211 | + */ |
| 212 | + public static Builder builder() { |
| 213 | + return new Builder(); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Creates a builder populated with values of this {@code ChangeRequest}. |
| 218 | + */ |
| 219 | + public Builder toBuilder() { |
| 220 | + return new Builder(this); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Returns the list of {@link DnsRecord}s to be added to the zone upon executing this {@code |
| 225 | + * ChangeRequest}. |
| 226 | + */ |
| 227 | + public List<DnsRecord> additions() { |
| 228 | + return additions; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Returns the list of {@link DnsRecord}s to be deleted from the zone upon executing this {@code |
| 233 | + * ChangeRequest}. |
| 234 | + */ |
| 235 | + public List<DnsRecord> deletions() { |
| 236 | + return deletions; |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Returns the id assigned to this {@code ChangeRequest} by the server. |
| 241 | + */ |
| 242 | + public String id() { |
| 243 | + return id; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Returns the time when this {@code ChangeRequest} was started by the server. |
| 248 | + */ |
| 249 | + public Long startTimeMillis() { |
| 250 | + return startTimeMillis; |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Returns the status of this {@code ChangeRequest}. |
| 255 | + */ |
| 256 | + public Status status() { |
| 257 | + return status; |
| 258 | + } |
| 259 | + |
| 260 | + com.google.api.services.dns.model.Change toPb() { |
| 261 | + com.google.api.services.dns.model.Change pb = |
| 262 | + new com.google.api.services.dns.model.Change(); |
| 263 | + // set id |
| 264 | + if (id() != null) { |
| 265 | + pb.setId(id()); |
| 266 | + } |
| 267 | + // set timestamp |
| 268 | + if (startTimeMillis() != null) { |
| 269 | + pb.setStartTime(ISODateTimeFormat.dateTime().withZoneUTC().print(startTimeMillis())); |
| 270 | + } |
| 271 | + // set status |
| 272 | + if (status() != null) { |
| 273 | + pb.setStatus(status().status); |
| 274 | + } |
| 275 | + // set a list of additions |
| 276 | + if (additions() != null) { |
| 277 | + LinkedList<com.google.api.services.dns.model.ResourceRecordSet> additionsPb = |
| 278 | + new LinkedList<>(); |
| 279 | + for (DnsRecord addition : additions()) { |
| 280 | + additionsPb.add(addition.toPb()); |
| 281 | + } |
| 282 | + pb.setAdditions(additionsPb); |
| 283 | + } |
| 284 | + // set a list of deletions |
| 285 | + if (deletions() != null) { |
| 286 | + LinkedList<com.google.api.services.dns.model.ResourceRecordSet> deletionsPb = |
| 287 | + new LinkedList<>(); |
| 288 | + for (DnsRecord deletion : deletions()) { |
| 289 | + deletionsPb.add(deletion.toPb()); |
| 290 | + } |
| 291 | + pb.setDeletions(deletionsPb); |
| 292 | + } |
| 293 | + return pb; |
| 294 | + } |
| 295 | + |
| 296 | + static ChangeRequest fromPb(com.google.api.services.dns.model.Change pb) { |
| 297 | + Builder b = builder(); |
| 298 | + if (pb.getId() != null) { |
| 299 | + b.id(pb.getId()); |
| 300 | + } |
| 301 | + if (pb.getStartTime() != null) { |
| 302 | + b.startTimeMillis(DateTime.parse(pb.getStartTime()).getMillis()); |
| 303 | + } |
| 304 | + if (pb.getStatus() != null) { |
| 305 | + b.status(ChangeRequest.Status.translate(pb.getStatus())); |
| 306 | + } |
| 307 | + if (pb.getDeletions() != null) { |
| 308 | + for (com.google.api.services.dns.model.ResourceRecordSet deletion : pb.getDeletions()) { |
| 309 | + b.delete(DnsRecord.fromPb(deletion)); |
| 310 | + } |
| 311 | + } |
| 312 | + if (pb.getAdditions() != null) { |
| 313 | + for (com.google.api.services.dns.model.ResourceRecordSet addition : pb.getAdditions()) { |
| 314 | + b.add(DnsRecord.fromPb(addition)); |
| 315 | + } |
| 316 | + } |
| 317 | + return b.build(); |
| 318 | + } |
| 319 | + |
| 320 | + @Override |
| 321 | + public boolean equals(Object o) { |
| 322 | + return (o instanceof ChangeRequest) && this.toPb().equals(((ChangeRequest) o).toPb()); |
| 323 | + } |
| 324 | + |
| 325 | + @Override |
| 326 | + public int hashCode() { |
| 327 | + return Objects.hash(additions, deletions, id, startTimeMillis, status); |
| 328 | + } |
| 329 | + |
| 330 | + @Override |
| 331 | + public String toString() { |
| 332 | + return MoreObjects.toStringHelper(this) |
| 333 | + .add("additions", additions) |
| 334 | + .add("deletions", deletions) |
| 335 | + .add("id", id) |
| 336 | + .add("startTimeMillis", startTimeMillis) |
| 337 | + .add("status", status) |
| 338 | + .toString(); |
| 339 | + } |
| 340 | +} |
0 commit comments