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
231 lines (198 loc) · 6.64 KB
/
ChangeRequest.java
File metadata and controls
231 lines (198 loc) · 6.64 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
/*
* 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.cloud.dns;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.services.dns.model.Change;
import com.google.common.base.Function;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Objects;
/**
* An immutable class representing an atomic update to a collection of {@link RecordSet}s within a
* {@code Zone}.
*
* @see <a href="https://cloud.google.com/dns/api/v1/changes">Google Cloud DNS documentation</a>
*/
public class ChangeRequest extends ChangeRequestInfo {
private static final long serialVersionUID = 5335667200595081449L;
private final DnsOptions options;
private final String zone;
private transient Dns dns;
/**
* A builder for {@code ChangeRequest}s.
*/
public static class Builder extends ChangeRequestInfo.Builder {
private final Dns dns;
private final String zone;
private final ChangeRequestInfo.BuilderImpl infoBuilder;
private Builder(ChangeRequest cr) {
this.dns = cr.dns;
this.zone = cr.zone;
this.infoBuilder = new ChangeRequestInfo.BuilderImpl(cr);
}
@Override
public Builder additions(List<RecordSet> additions) {
infoBuilder.additions(additions);
return this;
}
@Override
public Builder deletions(List<RecordSet> deletions) {
infoBuilder.deletions(deletions);
return this;
}
@Override
public Builder add(RecordSet recordSet) {
infoBuilder.add(recordSet);
return this;
}
@Override
public Builder delete(RecordSet recordSet) {
infoBuilder.delete(recordSet);
return this;
}
@Override
public Builder clearAdditions() {
infoBuilder.clearAdditions();
return this;
}
@Override
public Builder clearDeletions() {
infoBuilder.clearDeletions();
return this;
}
@Override
public Builder removeAddition(RecordSet recordSet) {
infoBuilder.removeAddition(recordSet);
return this;
}
@Override
public Builder removeDeletion(RecordSet recordSet) {
infoBuilder.removeDeletion(recordSet);
return this;
}
@Override
Builder generatedId(String generatedId) {
infoBuilder.generatedId(generatedId);
return this;
}
@Override
Builder startTimeMillis(long startTimeMillis) {
infoBuilder.startTimeMillis(startTimeMillis);
return this;
}
@Override
Builder status(Status status) {
infoBuilder.status(status);
return this;
}
@Override
public ChangeRequest build() {
return new ChangeRequest(dns, zone, infoBuilder);
}
}
ChangeRequest(Dns dns, String zone, ChangeRequest.BuilderImpl infoBuilder) {
super(infoBuilder);
this.zone = checkNotNull(zone);
this.dns = checkNotNull(dns);
this.options = dns.options();
}
/**
* Returns the name of the {@link Zone} associated with this change request.
*/
public String zone() {
return this.zone;
}
/**
* Returns the change request's {@code Dns} object used to issue requests.
*/
public Dns dns() {
return dns;
}
/**
* Applies this change request to the zone identified by {@code zoneName}.
*
* @throws DnsException upon failure or if zone is not found
*/
public ChangeRequest applyTo(String zoneName, Dns.ChangeRequestOption... options) {
return dns.applyChangeRequest(zoneName, this, options);
}
/**
* Retrieves the up-to-date information about the change request from Google Cloud DNS. Parameter
* {@code options} can be used to restrict the fields to be included in the updated object the
* same way as in {@link Dns#getChangeRequest(String, String, Dns.ChangeRequestOption...)}. If
* {@code options} are provided, any field other than generatedId which is not included in the
* {@code options} will be {@code null} regardless of whether they are initialized or not in
* {@code this} instance.
*
* @return an object with the updated information or {@code null} if it does not exist
* @throws DnsException upon failure of the API call or if the associated zone was not found
*/
public ChangeRequest reload(Dns.ChangeRequestOption... options) {
return dns.getChangeRequest(zone, generatedId(), options);
}
/**
* Returns {@code true} if the change request has been completed. If the status is not {@link
* ChangeRequestInfo.Status#DONE} already, the method makes an API call to Google Cloud DNS to
* update the change request first.
*
* @throws DnsException upon failure of the API call or if the associated zone was not found
*/
public boolean isDone() {
if (status() == Status.DONE) {
return true;
}
ChangeRequest updated = reload(Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS));
return updated == null || updated.status() == Status.DONE;
}
@Override
public Builder toBuilder() {
return new Builder(this);
}
@Override
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !obj.getClass().equals(ChangeRequest.class)) {
return false;
}
ChangeRequest other = (ChangeRequest) obj;
return Objects.equals(toPb(), other.toPb())
&& Objects.equals(options, other.options)
&& Objects.equals(zone, other.zone);
}
@Override
public final int hashCode() {
return Objects.hash(super.hashCode(), options, zone);
}
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
this.dns = options.service();
}
static ChangeRequest fromPb(Dns dns, String zoneName, Change pb) {
ChangeRequestInfo info = ChangeRequestInfo.fromPb(pb);
return new ChangeRequest(dns, zoneName, new ChangeRequestInfo.BuilderImpl(info));
}
static Function<Change, ChangeRequest> fromPbFunction(final Dns dns, final String zoneName) {
return new Function<Change, ChangeRequest>() {
@Override
public ChangeRequest apply(Change pb) {
return ChangeRequest.fromPb(dns, zoneName, pb);
}
};
}
}