forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDnsBatch.java
354 lines (320 loc) · 15 KB
/
DnsBatch.java
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* 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 java.net.HttpURLConnection.HTTP_NOT_FOUND;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.services.dns.model.Change;
import com.google.api.services.dns.model.ChangesListResponse;
import com.google.api.services.dns.model.ManagedZone;
import com.google.api.services.dns.model.ManagedZonesListResponse;
import com.google.api.services.dns.model.Project;
import com.google.api.services.dns.model.ResourceRecordSet;
import com.google.api.services.dns.model.ResourceRecordSetsListResponse;
import com.google.cloud.Page;
import com.google.cloud.PageImpl;
import com.google.cloud.dns.spi.DnsRpc;
import com.google.cloud.dns.spi.RpcBatch;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import java.util.List;
import java.util.Map;
/**
* A batch of operations to be submitted to Google Cloud DNS using a single RPC request.
*/
public class DnsBatch {
private RpcBatch batch;
private final DnsRpc dnsRpc;
private final DnsOptions options;
DnsBatch(DnsOptions options) {
this.options = options;
this.dnsRpc = options.rpc();
this.batch = dnsRpc.createBatch();
}
@VisibleForTesting
Object batch() {
return batch;
}
@VisibleForTesting
DnsRpc dnsRpc() {
return dnsRpc;
}
@VisibleForTesting
DnsOptions options() {
return options;
}
/**
* Adds a request representing the "list zones" operation to this batch. The {@code options} can
* be used to restrict the fields returned or provide page size limits in the same way as for
* {@link Dns#listZones(Dns.ZoneListOption...)}. Calling {@link DnsBatchResult#get()} on the
* return value yields a page of zones if successful and throws a {@link DnsException} otherwise.
*/
public DnsBatchResult<Page<Zone>> listZones(Dns.ZoneListOption... options) {
DnsBatchResult<Page<Zone>> result = new DnsBatchResult<>();
Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options);
RpcBatch.Callback<ManagedZonesListResponse> callback =
createListZonesCallback(result, optionMap);
batch.addListZones(callback, optionMap);
return result;
}
/**
* Adds a request representing the "create zone" operation to this batch. The {@code options} can
* be used to restrict the fields returned in the same way as for {@link Dns#create(ZoneInfo,
* Dns.ZoneOption...)}. Calling {@link DnsBatchResult#get()} on the return value yields the
* created {@link Zone} if successful and throws a {@link DnsException} otherwise.
*/
public DnsBatchResult<Zone> createZone(ZoneInfo zone, Dns.ZoneOption... options) {
DnsBatchResult<Zone> result = new DnsBatchResult<>();
// todo this can cause misleading report of a failure, intended to be fixed within #924
RpcBatch.Callback<ManagedZone> callback = createZoneCallback(this.options, result, true);
Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options);
batch.addCreateZone(zone.toPb(), callback, optionMap);
return result;
}
/**
* Adds a request representing the "delete zone" operation to this batch. Calling {@link
* DnsBatchResult#get()} on the return value yields {@code true} upon successful deletion, {@code
* false} if the zone was not found, or throws a {@link DnsException} if the operation failed.
*/
public DnsBatchResult<Boolean> deleteZone(String zoneName) {
DnsBatchResult<Boolean> result = new DnsBatchResult<>();
RpcBatch.Callback<Void> callback = createDeleteZoneCallback(result);
batch.addDeleteZone(zoneName, callback);
return result;
}
/**
* Adds a request representing the "get zone" operation to this batch. The {@code options} can be
* used to restrict the fields returned in the same way as for {@link Dns#getZone(String,
* Dns.ZoneOption...)}. Calling {@link DnsBatchResult#get()} on the return value yields the
* requested {@link Zone} if successful, {@code null} if no such zone exists, or throws a
* {@link DnsException} if the operation failed.
*/
public DnsBatchResult<Zone> getZone(String zoneName, Dns.ZoneOption... options) {
DnsBatchResult<Zone> result = new DnsBatchResult<>();
RpcBatch.Callback<ManagedZone> callback = createZoneCallback(this.options, result, true);
Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options);
batch.addGetZone(zoneName, callback, optionMap);
return result;
}
/**
* Adds a request representing the "get project" operation to this batch. The {@code options} can
* be used to restrict the fields returned in the same way as for {@link
* Dns#getProject(Dns.ProjectOption...)}. Calling {@link DnsBatchResult#get()} on the return value
* yields the created {@link ProjectInfo} if successful and throws a {@link DnsException} if the
* operation failed.
*/
public DnsBatchResult<ProjectInfo> getProject(Dns.ProjectOption... options) {
DnsBatchResult<ProjectInfo> result = new DnsBatchResult<>();
RpcBatch.Callback<Project> callback = createProjectCallback(result);
Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options);
batch.addGetProject(callback, optionMap);
return result;
}
/**
* Adds a request representing the "list record sets" operation in the zone specified by {@code
* zoneName} to this batch. The {@code options} can be used to restrict the fields returned or
* provide page size limits in the same way as for {@link Dns#listRecordSets(String,
* Dns.RecordSetListOption...)}. Calling {@link DnsBatchResult#get()} on the return value yields a
* page of record sets if successful and throws a {@link DnsException} if the operation failed or
* the zone does not exist.
*/
public DnsBatchResult<Page<RecordSet>> listRecordSets(String zoneName,
Dns.RecordSetListOption... options) {
DnsBatchResult<Page<RecordSet>> result = new DnsBatchResult<>();
Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options);
RpcBatch.Callback<ResourceRecordSetsListResponse> callback =
createListRecordSetsCallback(zoneName, result, optionMap);
batch.addListRecordSets(zoneName, callback, optionMap);
return result;
}
/**
* Adds a request representing the "list change requests" operation in the zone specified by
* {@code zoneName} to this batch. The {@code options} can be used to restrict the fields returned
* or provide page size limits in the same way as for {@link Dns#listChangeRequests(String,
* Dns.ChangeRequestListOption...)}. Calling {@link DnsBatchResult#get()} on the return value
* yields a page of change requests if successful and throws a {@link DnsException} if the
* operation failed or the zone does not exist.
*/
public DnsBatchResult<Page<ChangeRequest>> listChangeRequests(String zoneName,
Dns.ChangeRequestListOption... options) {
DnsBatchResult<Page<ChangeRequest>> result = new DnsBatchResult<>();
Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options);
RpcBatch.Callback<ChangesListResponse> callback =
createListChangeRequestsCallback(zoneName, result, optionMap);
batch.addListChangeRequests(zoneName, callback, optionMap);
return result;
}
/**
* Adds a request representing the "get change request" operation for the zone specified by {@code
* zoneName} to this batch. The {@code options} can be used to restrict the fields returned in the
* same way as for {@link Dns#getChangeRequest(String, String, Dns.ChangeRequestOption...)}.
* Calling {@link DnsBatchResult#get()} on the return value yields the requested {@link
* ChangeRequest} if successful, {@code null} if the change request does not exist, or throws a
* {@link DnsException} if the operation failed or the zone does not exist.
*/
public DnsBatchResult<ChangeRequest> getChangeRequest(String zoneName, String changeRequestId,
Dns.ChangeRequestOption... options) {
DnsBatchResult<ChangeRequest> result = new DnsBatchResult<>();
RpcBatch.Callback<Change> callback = createChangeRequestCallback(zoneName, result, true);
Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options);
batch.addGetChangeRequest(zoneName, changeRequestId, callback, optionMap);
return result;
}
/**
* Adds a request representing the "apply change request" operation to the zone specified by
* {@code zoneName} to this batch. The {@code options} can be used to restrict the fields returned
* in the same way as for {@link Dns#applyChangeRequest(String, ChangeRequestInfo,
* Dns.ChangeRequestOption...)}. Calling {@link DnsBatchResult#get()} on the return value yields
* the created {@link ChangeRequest} if successful, {@code null} if the change request does not
* exist, or throws a {@link DnsException} if the operation failed or the zone does not exist.
*/
public DnsBatchResult<ChangeRequest> applyChangeRequest(String zoneName,
ChangeRequestInfo changeRequest, Dns.ChangeRequestOption... options) {
DnsBatchResult<ChangeRequest> result = new DnsBatchResult<>();
RpcBatch.Callback<Change> callback = createChangeRequestCallback(zoneName, result, false);
Map<DnsRpc.Option, ?> optionMap = DnsImpl.optionMap(options);
batch.addApplyChangeRequest(zoneName, changeRequest.toPb(), callback, optionMap);
return result;
}
/**
* Submits this batch for processing using a single HTTP request.
*/
public void submit() {
batch.submit();
}
private RpcBatch.Callback<ManagedZonesListResponse> createListZonesCallback(
final DnsBatchResult<Page<Zone>> result, final Map<DnsRpc.Option, ?> optionMap) {
return new RpcBatch.Callback<ManagedZonesListResponse>() {
@Override
public void onSuccess(ManagedZonesListResponse response) {
List<ManagedZone> zones = response.getManagedZones();
Page<Zone> zonePage = new PageImpl<>(
new DnsImpl.ZonePageFetcher(options, response.getNextPageToken(), optionMap),
response.getNextPageToken(), zones == null ? ImmutableList.<Zone>of()
: Iterables.transform(zones, DnsImpl.zoneFromPb(options)));
result.success(zonePage);
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
result.error(new DnsException(googleJsonError, true));
}
};
}
private RpcBatch.Callback<Void> createDeleteZoneCallback(final DnsBatchResult<Boolean> result) {
return new RpcBatch.Callback<Void>() {
@Override
public void onSuccess(Void response) {
result.success(true);
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
DnsException serviceException = new DnsException(googleJsonError, false);
if (serviceException.code() == HTTP_NOT_FOUND) {
result.success(false);
} else {
result.error(serviceException);
}
}
};
}
/**
* A joint callback for both "get zone" and "create zone" operations.
*/
private RpcBatch.Callback<ManagedZone> createZoneCallback(final DnsOptions serviceOptions,
final DnsBatchResult<Zone> result, final boolean idempotent) {
return new RpcBatch.Callback<ManagedZone>() {
@Override
public void onSuccess(ManagedZone response) {
result.success(response == null ? null : Zone.fromPb(serviceOptions.service(), response));
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
result.error(new DnsException(googleJsonError, idempotent));
}
};
}
private RpcBatch.Callback<Project> createProjectCallback(
final DnsBatchResult<ProjectInfo> result) {
return new RpcBatch.Callback<Project>() {
@Override
public void onSuccess(Project response) {
result.success(response == null ? null : ProjectInfo.fromPb(response));
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
result.error(new DnsException(googleJsonError, true));
}
};
}
private RpcBatch.Callback<ResourceRecordSetsListResponse> createListRecordSetsCallback(
final String zoneName, final DnsBatchResult<Page<RecordSet>> result,
final Map<DnsRpc.Option, ?> optionMap) {
return new RpcBatch.Callback<ResourceRecordSetsListResponse>() {
@Override
public void onSuccess(ResourceRecordSetsListResponse response) {
List<ResourceRecordSet> recordSets = response.getRrsets();
Page<RecordSet> page = new PageImpl<>(
new DnsImpl.RecordSetPageFetcher(zoneName, options, response.getNextPageToken(),
optionMap),
response.getNextPageToken(), recordSets == null ? ImmutableList.<RecordSet>of()
: Iterables.transform(recordSets, RecordSet.FROM_PB_FUNCTION));
result.success(page);
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
result.error(new DnsException(googleJsonError, true));
}
};
}
private RpcBatch.Callback<ChangesListResponse> createListChangeRequestsCallback(
final String zoneName, final DnsBatchResult<Page<ChangeRequest>> result,
final Map<DnsRpc.Option, ?> optionMap) {
return new RpcBatch.Callback<ChangesListResponse>() {
@Override
public void onSuccess(ChangesListResponse response) {
List<Change> changes = response.getChanges();
Page<ChangeRequest> page = new PageImpl<>(
new DnsImpl.ChangeRequestPageFetcher(zoneName, options, response.getNextPageToken(),
optionMap),
response.getNextPageToken(), changes == null ? ImmutableList.<ChangeRequest>of()
: Iterables.transform(changes, ChangeRequest.fromPbFunction(options.service(),
zoneName)));
result.success(page);
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
result.error(new DnsException(googleJsonError, true));
}
};
}
/**
* A joint callback for both "get change request" and "create change request" operations.
*/
private RpcBatch.Callback<Change> createChangeRequestCallback(final String zoneName,
final DnsBatchResult<ChangeRequest> result, final boolean idempotent) {
return new RpcBatch.Callback<Change>() {
@Override
public void onSuccess(Change response) {
result.success(response == null ? null : ChangeRequest.fromPb(options.service(),
zoneName, response));
}
@Override
public void onFailure(GoogleJsonError googleJsonError) {
result.error(new DnsException(googleJsonError, idempotent));
}
};
}
}