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