forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDnsImpl.java
More file actions
401 lines (364 loc) · 15.4 KB
/
DnsImpl.java
File metadata and controls
401 lines (364 loc) · 15.4 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* 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.checkArgument;
import static com.google.gcloud.RetryHelper.RetryHelperException;
import static com.google.gcloud.RetryHelper.runWithRetries;
import static com.google.gcloud.dns.ChangeRequest.fromPb;
import com.google.api.client.googleapis.batch.BatchRequest;
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.http.HttpHeaders;
import com.google.api.services.dns.model.Change;
import com.google.api.services.dns.model.ManagedZone;
import com.google.api.services.dns.model.ManagedZonesListResponse;
import com.google.api.services.dns.model.ResourceRecordSet;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.gcloud.BaseService;
import com.google.gcloud.Page;
import com.google.gcloud.PageImpl;
import com.google.gcloud.RetryHelper;
import com.google.gcloud.dns.spi.DnsRpc;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* A default implementation of Dns.
*/
final class DnsImpl extends BaseService<DnsOptions> implements Dns {
private final DnsRpc dnsRpc;
private static class ZonePageFetcher implements PageImpl.NextPageFetcher<Zone> {
private static final long serialVersionUID = 2158209410430566961L;
private final Map<DnsRpc.Option, ?> requestOptions;
private final DnsOptions serviceOptions;
ZonePageFetcher(DnsOptions serviceOptions, String cursor,
Map<DnsRpc.Option, ?> optionMap) {
this.requestOptions =
PageImpl.nextRequestOptions(DnsRpc.Option.PAGE_TOKEN, cursor, optionMap);
this.serviceOptions = serviceOptions;
}
@Override
public Page<Zone> nextPage() {
return listZones(serviceOptions, requestOptions);
}
}
private static class ChangeRequestPageFetcher implements PageImpl.NextPageFetcher<ChangeRequest> {
private static final long serialVersionUID = 4473265130673029139L;
private final String zoneName;
private final Map<DnsRpc.Option, ?> requestOptions;
private final DnsOptions serviceOptions;
ChangeRequestPageFetcher(String zoneName, DnsOptions serviceOptions, String cursor,
Map<DnsRpc.Option, ?> optionMap) {
this.zoneName = zoneName;
this.requestOptions =
PageImpl.nextRequestOptions(DnsRpc.Option.PAGE_TOKEN, cursor, optionMap);
this.serviceOptions = serviceOptions;
}
@Override
public Page<ChangeRequest> nextPage() {
return listChangeRequests(zoneName, serviceOptions, requestOptions);
}
}
private static class DnsRecordPageFetcher implements PageImpl.NextPageFetcher<DnsRecord> {
private static final long serialVersionUID = -6039369212511530846L;
private final Map<DnsRpc.Option, ?> requestOptions;
private final DnsOptions serviceOptions;
private final String zoneName;
DnsRecordPageFetcher(String zoneName, DnsOptions serviceOptions, String cursor,
Map<DnsRpc.Option, ?> optionMap) {
this.zoneName = zoneName;
this.requestOptions =
PageImpl.nextRequestOptions(DnsRpc.Option.PAGE_TOKEN, cursor, optionMap);
this.serviceOptions = serviceOptions;
}
@Override
public Page<DnsRecord> nextPage() {
return listDnsRecords(zoneName, serviceOptions, requestOptions);
}
}
DnsImpl(DnsOptions options) {
super(options);
dnsRpc = options.rpc();
}
@Override
public Page<Zone> listZones(ZoneListOption... options) {
return listZones(options(), optionMap(options));
}
private static Page<Zone> listZones(final DnsOptions serviceOptions,
final Map<DnsRpc.Option, ?> optionsMap) {
// define transformation function
// this differs from the other list operations since zone is functional and requires dns service
Function<ManagedZone, Zone> pbToZoneFunction = new Function<ManagedZone, Zone>() {
@Override
public Zone apply(
com.google.api.services.dns.model.ManagedZone zonePb) {
return Zone.fromPb(serviceOptions.service(), zonePb);
}
};
try {
// get a list of managed zones
final DnsRpc rpc = serviceOptions.rpc();
DnsRpc.ListResult<ManagedZone> result =
runWithRetries(new Callable<DnsRpc.ListResult<ManagedZone>>() {
@Override
public DnsRpc.ListResult<ManagedZone> call() {
return rpc.listZones(optionsMap);
}
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
String cursor = result.pageToken();
// transform that list into zone objects
Iterable<Zone> zones = result.results() == null
? ImmutableList.<Zone>of() : Iterables.transform(result.results(), pbToZoneFunction);
return new PageImpl<>(new ZonePageFetcher(serviceOptions, cursor, optionsMap),
cursor, zones);
} catch (RetryHelperException e) {
throw DnsException.translateAndThrow(e);
}
}
@Override
public Page<ChangeRequest> listChangeRequests(String zoneName,
ChangeRequestListOption... options) {
return listChangeRequests(zoneName, options(), optionMap(options));
}
private static Page<ChangeRequest> listChangeRequests(final String zoneName,
final DnsOptions serviceOptions, final Map<DnsRpc.Option, ?> optionsMap) {
try {
// get a list of changes
final DnsRpc rpc = serviceOptions.rpc();
DnsRpc.ListResult<Change> result = runWithRetries(new Callable<DnsRpc.ListResult<Change>>() {
@Override
public DnsRpc.ListResult<Change> call() {
return rpc.listChangeRequests(zoneName, optionsMap);
}
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
String cursor = result.pageToken();
// transform that list into change request objects
Iterable<ChangeRequest> changes = result.results() == null
? ImmutableList.<ChangeRequest>of()
: Iterables.transform(result.results(), ChangeRequest.FROM_PB_FUNCTION);
return new PageImpl<>(new ChangeRequestPageFetcher(zoneName, serviceOptions, cursor,
optionsMap), cursor, changes);
} catch (RetryHelperException e) {
throw DnsException.translateAndThrow(e);
}
}
@Override
public Page<DnsRecord> listDnsRecords(String zoneName, DnsRecordListOption... options) {
return listDnsRecords(zoneName, options(), optionMap(options));
}
private static Page<DnsRecord> listDnsRecords(final String zoneName,
final DnsOptions serviceOptions, final Map<DnsRpc.Option, ?> optionsMap) {
try {
// get a list of resource record sets
final DnsRpc rpc = serviceOptions.rpc();
DnsRpc.ListResult<ResourceRecordSet> result = runWithRetries(
new Callable<DnsRpc.ListResult<ResourceRecordSet>>() {
@Override
public DnsRpc.ListResult<ResourceRecordSet> call() {
return rpc.listDnsRecords(zoneName, optionsMap);
}
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
String cursor = result.pageToken();
// transform that list into dns records
Iterable<DnsRecord> records = result.results() == null
? ImmutableList.<DnsRecord>of()
: Iterables.transform(result.results(), DnsRecord.FROM_PB_FUNCTION);
return new PageImpl<>(new DnsRecordPageFetcher(zoneName, serviceOptions, cursor, optionsMap),
cursor, records);
} catch (RetryHelperException e) {
throw DnsException.translateAndThrow(e);
}
}
@Override
public Zone create(final ZoneInfo zoneInfo, Dns.ZoneOption... options) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.dns.model.ManagedZone answer = runWithRetries(
new Callable<com.google.api.services.dns.model.ManagedZone>() {
@Override
public com.google.api.services.dns.model.ManagedZone call() {
return dnsRpc.create(zoneInfo.toPb(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : Zone.fromPb(this, answer);
} catch (RetryHelper.RetryHelperException ex) {
throw DnsException.translateAndThrow(ex);
}
}
@Override
public Zone getZone(final String zoneName, Dns.ZoneOption... options) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.dns.model.ManagedZone answer = runWithRetries(
new Callable<com.google.api.services.dns.model.ManagedZone>() {
@Override
public com.google.api.services.dns.model.ManagedZone call() {
return dnsRpc.getZone(zoneName, optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : Zone.fromPb(this, answer);
} catch (RetryHelper.RetryHelperException ex) {
throw DnsException.translateAndThrow(ex);
}
}
@Override
public boolean delete(final String zoneName) {
try {
return runWithRetries(new Callable<Boolean>() {
@Override
public Boolean call() {
return dnsRpc.deleteZone(zoneName);
}
}, options().retryParams(), EXCEPTION_HANDLER);
} catch (RetryHelper.RetryHelperException ex) {
throw DnsException.translateAndThrow(ex);
}
}
@Override
public ProjectInfo getProject(Dns.ProjectOption... fields) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(fields);
try {
com.google.api.services.dns.model.Project answer = runWithRetries(
new Callable<com.google.api.services.dns.model.Project>() {
@Override
public com.google.api.services.dns.model.Project call() {
return dnsRpc.getProject(optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : ProjectInfo.fromPb(answer); // should never be null
} catch (RetryHelper.RetryHelperException ex) {
throw DnsException.translateAndThrow(ex);
}
}
@Override
public ChangeRequest applyChangeRequest(final String zoneName, final ChangeRequest changeRequest,
Dns.ChangeRequestOption... options) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.dns.model.Change answer =
runWithRetries(
new Callable<com.google.api.services.dns.model.Change>() {
@Override
public com.google.api.services.dns.model.Change call() {
return dnsRpc.applyChangeRequest(zoneName, changeRequest.toPb(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : fromPb(answer); // should never be null
} catch (RetryHelper.RetryHelperException ex) {
throw DnsException.translateAndThrow(ex);
}
}
@Override
public ChangeRequest getChangeRequest(final String zoneName, final String changeRequestId,
Dns.ChangeRequestOption... options) {
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.dns.model.Change answer =
runWithRetries(
new Callable<com.google.api.services.dns.model.Change>() {
@Override
public com.google.api.services.dns.model.Change call() {
return dnsRpc.getChangeRequest(zoneName, changeRequestId, optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : fromPb(answer);
} catch (RetryHelper.RetryHelperException ex) {
throw DnsException.translateAndThrow(ex);
}
}
@Override
public void submitBatch(DnsBatch toSubmit) {
try {
BatchRequest batchRequest = prepareBatch(toSubmit);
batchRequest.execute();
} catch (IOException ex) {
throw new DnsException(ex);
}
}
/**
* Since {@code BatchRequest} is a final class, it cannot be mocked with easy mock and the call of
* {@code execute()} cannot be tested. Thus, most of the functionality of {@link
* #submitBatch(DnsBatch)} is extracted to this method which does not make the call so it does not
* communicate with the service.
*/
BatchRequest prepareBatch(DnsBatch toSubmit) throws IOException {
BatchRequest batch = null;
for (Map.Entry<DnsBatch.Request, DnsBatch.Callback> entry : toSubmit.requests().entrySet()) {
DnsBatch.Request request = entry.getKey();
DnsBatch.Callback callback = entry.getValue();
switch (request.operation()) {
case LIST_ZONES:
JsonBatchCallback rpcCallback = listZonesCallback(callback, request);
batch =
dnsRpc.enqueueListZones(batch, request, rpcCallback, optionMap(request.options()));
break;
default:
// todo(mderka) implement the rest of the operations
throw new UnsupportedOperationException("Not implemented yet");
}
}
return batch;
}
@Override
public DnsBatch batch() {
return new DnsBatch(this);
}
private JsonBatchCallback listZonesCallback(final DnsBatch.Callback callback,
final DnsBatch.Request request) {
return new JsonBatchCallback<ManagedZonesListResponse>() {
@Override
public void onFailure(GoogleJsonError error, HttpHeaders httpHeaders) {
if (callback != null) {
callback.error(new DnsException(error), request);
}
}
@Override
public void onSuccess(ManagedZonesListResponse zoneList, HttpHeaders httpHeaders) {
if (callback != null) {
DnsRpc.ListResult<ManagedZone> listResult =
DnsRpc.ListResult.of(zoneList.getNextPageToken(), zoneList.getManagedZones());
String cursor = listResult.pageToken();
Iterable<Zone> zones = listResult.results() == null ? ImmutableList.<Zone>of()
: Iterables.transform(listResult.results(), new Function<ManagedZone, Zone>() {
@Override
public Zone apply(ManagedZone managedZone) {
return new Zone(options().service(),
new ZoneInfo.BuilderImpl(ZoneInfo.fromPb(managedZone)));
}
}
);
Page<Zone> page = new PageImpl<>(
new ZonePageFetcher(options(), cursor, optionMap(request.options())), cursor, zones);
callback.success(page, request);
}
}
};
}
private Map<DnsRpc.Option, ?> optionMap(AbstractOption... options) {
Map<DnsRpc.Option, Object> temp = Maps.newEnumMap(DnsRpc.Option.class);
if (options != null) {
for (AbstractOption option : options) {
Object prev = temp.put(option.rpcOption(), option.value());
checkArgument(prev == null, "Duplicate option %s", option);
}
}
return ImmutableMap.copyOf(temp);
}
}