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