Skip to content

Commit 8bac0c0

Browse files
committed
---
yaml --- r: 2967 b: refs/heads/dns-alpha-batch c: d1c4512 h: refs/heads/master i: 2965: c65f286 2963: 03cdd59 2959: 6aba267
1 parent d3293d8 commit 8bac0c0

6 files changed

Lines changed: 728 additions & 5 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ refs/tags/v0.0.11: ffbfba48a6426ff63c08ff2117e58681f251fbf2
1212
refs/tags/v0.0.12: 2fd8066e891fb3dfea69b65f6bf6461db79342b9
1313
refs/heads/compute-alpha: b1274b5bdf4eea955f3588b56378a5ae4ba59cef
1414
refs/heads/dns-alpha: 2f90e7e338349287ace33375896907af0f032ca1
15-
refs/heads/dns-alpha-batch: 5be58b3e91cade1d349014122484a29e254ba8f3
15+
refs/heads/dns-alpha-batch: d1c45124dffd0a5a3bfdc2a4abe5c6483f32c295

branches/dns-alpha-batch/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface Dns extends Service<DnsOptions> {
3636
* The fields of a project.
3737
*
3838
* <p>These values can be used to specify the fields to include in a partial response when calling
39-
* {@link Dns#getProjectInfo(ProjectOption...)}. Project ID is always returned, even if not
39+
* {@link Dns#getProject(ProjectOption...)}. Project ID is always returned, even if not
4040
* specified.
4141
*/
4242
enum ProjectField {
@@ -429,7 +429,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
429429
* @see <a href="https://cloud.google.com/dns/api/v1/managedZones/create">Cloud DNS Managed Zones:
430430
* create</a>
431431
*/
432-
ZoneInfo create(ZoneInfo zoneInfo, ZoneOption... options);
432+
Zone create(ZoneInfo zoneInfo, ZoneOption... options);
433433

434434
/**
435435
* Returns the zone by the specified zone name. Returns {@code null} if the zone is not found. The
@@ -485,7 +485,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
485485
* @throws DnsException upon failure
486486
* @see <a href="https://cloud.google.com/dns/api/v1/projects/get">Cloud DNS Projects: get</a>
487487
*/
488-
ProjectInfo getProjectInfo(ProjectOption... fields);
488+
ProjectInfo getProject(ProjectOption... fields);
489489

490490
/**
491491
* Submits a change request for the specified zone. The returned object contains the following

branches/dns-alpha-batch/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.google.gcloud.dns;
1818

1919
import com.google.gcloud.BaseServiceException;
20+
import com.google.gcloud.RetryHelper.RetryHelperException;
21+
import com.google.gcloud.RetryHelper.RetryInterruptedException;
2022

2123
import java.io.IOException;
2224

@@ -31,5 +33,21 @@ public DnsException(IOException exception) {
3133
super(exception, true);
3234
}
3335

36+
public DnsException(int code, String message) {
37+
super(code, message, null, true);
38+
}
39+
40+
/**
41+
* Translate RetryHelperException to the DnsException that caused the error. This method will
42+
* always throw an exception.
43+
*
44+
* @throws DnsException when {@code ex} was caused by a {@code DnsException}
45+
* @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException}
46+
*/
47+
static DnsException translateAndThrow(RetryHelperException ex) {
48+
BaseServiceException.translateAndPropagateIfPossible(ex);
49+
throw new DnsException(UNKNOWN_CODE, ex.getMessage());
50+
}
51+
3452
//TODO(mderka) Add translation and retry functionality. Created issue #593.
3553
}
Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
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.checkArgument;
20+
import static com.google.gcloud.RetryHelper.RetryHelperException;
21+
import static com.google.gcloud.RetryHelper.runWithRetries;
22+
import static com.google.gcloud.dns.ChangeRequest.fromPb;
23+
24+
import com.google.api.services.dns.model.Change;
25+
import com.google.api.services.dns.model.ManagedZone;
26+
import com.google.api.services.dns.model.ResourceRecordSet;
27+
import com.google.common.base.Function;
28+
import com.google.common.collect.ImmutableList;
29+
import com.google.common.collect.ImmutableMap;
30+
import com.google.common.collect.Iterables;
31+
import com.google.common.collect.Maps;
32+
import com.google.gcloud.BaseService;
33+
import com.google.gcloud.Page;
34+
import com.google.gcloud.PageImpl;
35+
import com.google.gcloud.RetryHelper;
36+
import com.google.gcloud.spi.DnsRpc;
37+
38+
import java.util.Map;
39+
import java.util.concurrent.Callable;
40+
41+
/**
42+
* A default implementation of Dns.
43+
*/
44+
final class DnsImpl extends BaseService<DnsOptions> implements Dns {
45+
46+
private final DnsRpc dnsRpc;
47+
48+
private static class ZonePageFetcher implements PageImpl.NextPageFetcher<Zone> {
49+
50+
private static final long serialVersionUID = 2158209410430566961L;
51+
private final Map<DnsRpc.Option, ?> requestOptions;
52+
private final DnsOptions serviceOptions;
53+
54+
ZonePageFetcher(DnsOptions serviceOptions, String cursor,
55+
Map<DnsRpc.Option, ?> optionMap) {
56+
this.requestOptions =
57+
PageImpl.nextRequestOptions(DnsRpc.Option.PAGE_TOKEN, cursor, optionMap);
58+
this.serviceOptions = serviceOptions;
59+
}
60+
61+
@Override
62+
public Page<Zone> nextPage() {
63+
return listZones(serviceOptions, requestOptions);
64+
}
65+
}
66+
67+
private static class ChangeRequestPageFetcher implements PageImpl.NextPageFetcher<ChangeRequest> {
68+
69+
private static final Function<Change, ChangeRequest> PB_TO_CHANGE_REQUEST =
70+
new Function<Change, ChangeRequest>() {
71+
@Override
72+
public ChangeRequest apply(com.google.api.services.dns.model.Change changePb) {
73+
return fromPb(changePb);
74+
}
75+
};
76+
private static final long serialVersionUID = -8737501076674042014L;
77+
private final String zoneName;
78+
private final Map<DnsRpc.Option, ?> requestOptions;
79+
private final DnsOptions serviceOptions;
80+
81+
ChangeRequestPageFetcher(String zoneName, DnsOptions serviceOptions, String cursor,
82+
Map<DnsRpc.Option, ?> optionMap) {
83+
this.zoneName = zoneName;
84+
this.requestOptions =
85+
PageImpl.nextRequestOptions(DnsRpc.Option.PAGE_TOKEN, cursor, optionMap);
86+
this.serviceOptions = serviceOptions;
87+
}
88+
89+
@Override
90+
public Page<ChangeRequest> nextPage() {
91+
return listChangeRequests(zoneName, serviceOptions, requestOptions, PB_TO_CHANGE_REQUEST);
92+
}
93+
}
94+
95+
private static class DnsRecordPageFetcher implements PageImpl.NextPageFetcher<DnsRecord> {
96+
97+
private static final Function<ResourceRecordSet, DnsRecord> PB_TO_DNS_RECORD =
98+
new Function<ResourceRecordSet, DnsRecord>() {
99+
@Override
100+
public DnsRecord apply(com.google.api.services.dns.model.ResourceRecordSet pb) {
101+
return DnsRecord.fromPb(pb);
102+
}
103+
};
104+
private static final long serialVersionUID = 670996349097667660L;
105+
private final Map<DnsRpc.Option, ?> requestOptions;
106+
private final DnsOptions serviceOptions;
107+
private final String zoneName;
108+
109+
DnsRecordPageFetcher(String zoneName, DnsOptions serviceOptions, String cursor,
110+
Map<DnsRpc.Option, ?> optionMap) {
111+
this.zoneName = zoneName;
112+
this.requestOptions =
113+
PageImpl.nextRequestOptions(DnsRpc.Option.PAGE_TOKEN, cursor, optionMap);
114+
this.serviceOptions = serviceOptions;
115+
}
116+
117+
@Override
118+
public Page<DnsRecord> nextPage() {
119+
return listDnsRecords(zoneName, serviceOptions, requestOptions, PB_TO_DNS_RECORD);
120+
}
121+
}
122+
123+
private static Page<Zone> listZones(final DnsOptions serviceOptions,
124+
final Map<DnsRpc.Option, ?> optionsMap) {
125+
// define transformation function
126+
// this differs from the other list operations since zone is functional and requires dns service
127+
Function<ManagedZone, Zone> pbToZoneFunction = new Function<ManagedZone, Zone>() {
128+
@Override
129+
public Zone apply(
130+
com.google.api.services.dns.model.ManagedZone zonePb) {
131+
return new Zone(serviceOptions.service(), ZoneInfo.fromPb(zonePb));
132+
}
133+
};
134+
try {
135+
// get a list of managed zones
136+
DnsRpc.ListResult<ManagedZone> result =
137+
runWithRetries(new Callable<DnsRpc.ListResult<ManagedZone>>() {
138+
@Override
139+
public DnsRpc.ListResult<ManagedZone> call() {
140+
return serviceOptions.rpc().listZones(optionsMap);
141+
}
142+
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
143+
String cursor = result.pageToken();
144+
// transform that list into zone objects
145+
Iterable<Zone> zones = result.results() == null
146+
? ImmutableList.<Zone>of() : Iterables.transform(result.results(), pbToZoneFunction);
147+
return new PageImpl<>(new ZonePageFetcher(serviceOptions, cursor, optionsMap),
148+
cursor, zones);
149+
} catch (RetryHelperException e) {
150+
throw DnsException.translateAndThrow(e);
151+
}
152+
}
153+
154+
private static Page<ChangeRequest> listChangeRequests(final String zoneName,
155+
final DnsOptions serviceOptions, final Map<DnsRpc.Option, ?> optionsMap,
156+
Function<Change, ChangeRequest> TRANSFORM_FUNCTION) {
157+
try {
158+
// get a list of changes
159+
DnsRpc.ListResult<Change> result = runWithRetries(new Callable<DnsRpc.ListResult<Change>>() {
160+
@Override
161+
public DnsRpc.ListResult<Change> call() {
162+
return serviceOptions.rpc().listChangeRequests(zoneName, optionsMap);
163+
}
164+
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
165+
String cursor = result.pageToken();
166+
// transform that list into change request objects
167+
Iterable<ChangeRequest> changes = result.results() == null
168+
? ImmutableList.<ChangeRequest>of()
169+
: Iterables.transform(result.results(), TRANSFORM_FUNCTION);
170+
return new PageImpl<>(new ChangeRequestPageFetcher(zoneName, serviceOptions, cursor,
171+
optionsMap), cursor, changes);
172+
} catch (RetryHelperException e) {
173+
throw DnsException.translateAndThrow(e);
174+
}
175+
}
176+
177+
private static Page<DnsRecord> listDnsRecords(final String zoneName,
178+
final DnsOptions serviceOptions, final Map<DnsRpc.Option, ?> optionsMap,
179+
Function<ResourceRecordSet, DnsRecord> TRANSFORM_FUNCTION) {
180+
try {
181+
// get a list of resource record sets
182+
DnsRpc.ListResult<ResourceRecordSet> result = runWithRetries(
183+
new Callable<DnsRpc.ListResult<ResourceRecordSet>>() {
184+
@Override
185+
public DnsRpc.ListResult<ResourceRecordSet> call() {
186+
return serviceOptions.rpc().listDnsRecords(zoneName, optionsMap);
187+
}
188+
}, serviceOptions.retryParams(), EXCEPTION_HANDLER);
189+
String cursor = result.pageToken();
190+
// transform that list into dns records
191+
Iterable<DnsRecord> records = result.results() == null
192+
? ImmutableList.<DnsRecord>of()
193+
: Iterables.transform(result.results(), TRANSFORM_FUNCTION);
194+
return new PageImpl<>(new DnsRecordPageFetcher(zoneName, serviceOptions, cursor, optionsMap),
195+
cursor, records);
196+
} catch (RetryHelperException e) {
197+
throw DnsException.translateAndThrow(e);
198+
}
199+
}
200+
201+
DnsImpl(DnsOptions options) {
202+
super(options);
203+
dnsRpc = options.rpc();
204+
}
205+
206+
@Override
207+
public Page<Zone> listZones(ZoneListOption... options) {
208+
return listZones(options(), optionMap(options));
209+
}
210+
211+
@Override
212+
public Page<ChangeRequest> listChangeRequests(String zoneName,
213+
ChangeRequestListOption... options) {
214+
return listChangeRequests(zoneName, options(), optionMap(options),
215+
ChangeRequestPageFetcher.PB_TO_CHANGE_REQUEST);
216+
}
217+
218+
@Override
219+
public Page<DnsRecord> listDnsRecords(String zoneName, DnsRecordListOption... options) {
220+
return listDnsRecords(zoneName, options(), optionMap(options),
221+
DnsRecordPageFetcher.PB_TO_DNS_RECORD);
222+
}
223+
224+
@Override
225+
public Zone create(final ZoneInfo zoneInfo, Dns.ZoneOption... options) {
226+
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
227+
try {
228+
com.google.api.services.dns.model.ManagedZone answer = runWithRetries(
229+
new Callable<com.google.api.services.dns.model.ManagedZone>() {
230+
@Override
231+
public com.google.api.services.dns.model.ManagedZone call() {
232+
return dnsRpc.create(zoneInfo.toPb(), optionsMap);
233+
}
234+
}, options().retryParams(), EXCEPTION_HANDLER);
235+
return answer == null ? null : Zone.fromPb(this, answer);
236+
} catch (RetryHelper.RetryHelperException ex) {
237+
throw DnsException.translateAndThrow(ex);
238+
}
239+
}
240+
241+
@Override
242+
public Zone getZone(final String zoneName, Dns.ZoneOption... options) {
243+
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
244+
try {
245+
com.google.api.services.dns.model.ManagedZone answer = runWithRetries(
246+
new Callable<com.google.api.services.dns.model.ManagedZone>() {
247+
@Override
248+
public com.google.api.services.dns.model.ManagedZone call() {
249+
return dnsRpc.getZone(zoneName, optionsMap);
250+
}
251+
}, options().retryParams(), EXCEPTION_HANDLER);
252+
return answer == null ? null : Zone.fromPb(this, answer);
253+
} catch (RetryHelper.RetryHelperException ex) {
254+
throw DnsException.translateAndThrow(ex);
255+
}
256+
}
257+
258+
@Override
259+
public boolean delete(final String zoneName) {
260+
try {
261+
return runWithRetries(new Callable<Boolean>() {
262+
@Override
263+
public Boolean call() {
264+
return dnsRpc.deleteZone(zoneName);
265+
}
266+
}, options().retryParams(), EXCEPTION_HANDLER);
267+
} catch (RetryHelper.RetryHelperException ex) {
268+
throw DnsException.translateAndThrow(ex);
269+
}
270+
}
271+
272+
@Override
273+
public ProjectInfo getProject(Dns.ProjectOption... fields) {
274+
final Map<DnsRpc.Option, ?> optionsMap = optionMap(fields);
275+
try {
276+
com.google.api.services.dns.model.Project answer = runWithRetries(
277+
new Callable<com.google.api.services.dns.model.Project>() {
278+
@Override
279+
public com.google.api.services.dns.model.Project call() {
280+
return dnsRpc.getProject(optionsMap);
281+
}
282+
}, options().retryParams(), EXCEPTION_HANDLER);
283+
return answer == null ? null : ProjectInfo.fromPb(answer); // should never be null
284+
} catch (RetryHelper.RetryHelperException ex) {
285+
throw DnsException.translateAndThrow(ex);
286+
}
287+
}
288+
289+
@Override
290+
public ChangeRequest applyChangeRequest(final String zoneName, final ChangeRequest changeRequest,
291+
Dns.ChangeRequestOption... options) {
292+
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
293+
try {
294+
com.google.api.services.dns.model.Change answer =
295+
runWithRetries(
296+
new Callable<com.google.api.services.dns.model.Change>() {
297+
@Override
298+
public com.google.api.services.dns.model.Change call() {
299+
return dnsRpc.applyChangeRequest(zoneName, changeRequest.toPb(), optionsMap);
300+
}
301+
}, options().retryParams(), EXCEPTION_HANDLER);
302+
return answer == null ? null : fromPb(answer); // should never be null
303+
} catch (RetryHelper.RetryHelperException ex) {
304+
throw DnsException.translateAndThrow(ex);
305+
}
306+
}
307+
308+
@Override
309+
public ChangeRequest getChangeRequest(final String zoneName, final String changeRequestId,
310+
Dns.ChangeRequestOption... options) {
311+
final Map<DnsRpc.Option, ?> optionsMap = optionMap(options);
312+
try {
313+
com.google.api.services.dns.model.Change answer =
314+
runWithRetries(
315+
new Callable<com.google.api.services.dns.model.Change>() {
316+
@Override
317+
public com.google.api.services.dns.model.Change call() {
318+
return dnsRpc.getChangeRequest(zoneName, changeRequestId, optionsMap);
319+
}
320+
}, options().retryParams(), EXCEPTION_HANDLER);
321+
return answer == null ? null : fromPb(answer); // should never be null
322+
} catch (RetryHelper.RetryHelperException ex) {
323+
throw DnsException.translateAndThrow(ex);
324+
}
325+
}
326+
327+
private Map<DnsRpc.Option, ?> optionMap(AbstractOption... options) {
328+
Map<DnsRpc.Option, Object> temp = Maps.newEnumMap(DnsRpc.Option.class);
329+
for (AbstractOption option : options) {
330+
Object prev = temp.put(option.rpcOption(), option.value());
331+
checkArgument(prev == null, "Duplicate option %s", option);
332+
}
333+
return ImmutableMap.copyOf(temp);
334+
}
335+
}

0 commit comments

Comments
 (0)