Skip to content

Commit 35dbb34

Browse files
committed
Merge pull request #616 from mderka/dns-options
Completes DnsRpc interface by adding methods and doc. Implements DefaultDnsRpc.
2 parents e3686e3 + 3ae0f21 commit 35dbb34

6 files changed

Lines changed: 317 additions & 9 deletions

File tree

gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public static DnsRecordListOption pageSize(int pageSize) {
230230
* Restricts the list to only DNS records with this fully qualified domain name.
231231
*/
232232
public static DnsRecordListOption dnsName(String dnsName) {
233-
return new DnsRecordListOption(DnsRpc.Option.DNS_NAME, dnsName);
233+
return new DnsRecordListOption(DnsRpc.Option.NAME, dnsName);
234234
}
235235

236236
/**

gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public class DnsException extends BaseServiceException {
2727

2828
private static final long serialVersionUID = 490302380416260252L;
2929

30-
public DnsException(IOException exception, boolean idempotent) {
31-
super(exception, idempotent);
30+
public DnsException(IOException exception) {
31+
super(exception, true);
3232
}
3333

3434
//TODO(mderka) Add translation and retry functionality. Created issue #593.

gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.ImmutableSet;
2020
import com.google.gcloud.ServiceOptions;
21+
import com.google.gcloud.spi.DefaultDnsRpc;
2122
import com.google.gcloud.spi.DnsRpc;
2223
import com.google.gcloud.spi.DnsRpcFactory;
2324

@@ -46,8 +47,7 @@ public static class DefaultDnsRpcFactory implements DnsRpcFactory {
4647

4748
@Override
4849
public DnsRpc create(DnsOptions options) {
49-
// TODO(mderka) Implement when DefaultDnsRpc is available. Created issue #595.
50-
return null;
50+
return new DefaultDnsRpc(options);
5151
}
5252
}
5353

@@ -80,7 +80,7 @@ protected DnsFactory defaultServiceFactory() {
8080
@SuppressWarnings("unchecked")
8181
@Override
8282
protected DnsRpcFactory defaultRpcFactory() {
83-
return null;
83+
return DefaultDnsRpcFactory.INSTANCE;
8484
}
8585

8686
@Override
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package com.google.gcloud.spi;
2+
3+
import static com.google.gcloud.spi.DnsRpc.ListResult.of;
4+
import static com.google.gcloud.spi.DnsRpc.Option.DNS_NAME;
5+
import static com.google.gcloud.spi.DnsRpc.Option.NAME;
6+
import static com.google.gcloud.spi.DnsRpc.Option.DNS_TYPE;
7+
import static com.google.gcloud.spi.DnsRpc.Option.FIELDS;
8+
import static com.google.gcloud.spi.DnsRpc.Option.PAGE_SIZE;
9+
import static com.google.gcloud.spi.DnsRpc.Option.PAGE_TOKEN;
10+
import static com.google.gcloud.spi.DnsRpc.Option.SORTING_ORDER;
11+
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
12+
13+
import com.google.api.client.http.HttpRequestInitializer;
14+
import com.google.api.client.http.HttpTransport;
15+
import com.google.api.client.json.jackson.JacksonFactory;
16+
import com.google.api.services.dns.Dns;
17+
import com.google.api.services.dns.model.Change;
18+
import com.google.api.services.dns.model.ChangesListResponse;
19+
import com.google.api.services.dns.model.ManagedZone;
20+
import com.google.api.services.dns.model.ManagedZonesListResponse;
21+
import com.google.api.services.dns.model.Project;
22+
import com.google.api.services.dns.model.ResourceRecordSet;
23+
import com.google.api.services.dns.model.ResourceRecordSetsListResponse;
24+
import com.google.gcloud.dns.DnsException;
25+
import com.google.gcloud.dns.DnsOptions;
26+
27+
import java.io.IOException;
28+
import java.util.Map;
29+
30+
/**
31+
* A default implementation of the DnsRpc interface.
32+
*/
33+
public class DefaultDnsRpc implements DnsRpc {
34+
35+
private static final String SORT_BY = "changeSequence";
36+
private final Dns dns;
37+
private final DnsOptions options;
38+
39+
private static DnsException translate(IOException exception) {
40+
return new DnsException(exception);
41+
}
42+
43+
/**
44+
* Constructs an instance of this rpc client with provided {@link DnsOptions}.
45+
*/
46+
public DefaultDnsRpc(DnsOptions options) {
47+
HttpTransport transport = options.httpTransportFactory().create();
48+
HttpRequestInitializer initializer = options.httpRequestInitializer();
49+
this.dns = new Dns.Builder(transport, new JacksonFactory(), initializer)
50+
.setRootUrl(options.host())
51+
.setApplicationName(options.applicationName())
52+
.build();
53+
this.options = options;
54+
}
55+
56+
@Override
57+
public ManagedZone create(ManagedZone zone) throws DnsException {
58+
try {
59+
return dns.managedZones().create(this.options.projectId(), zone).execute();
60+
} catch (IOException ex) {
61+
throw translate(ex);
62+
}
63+
}
64+
65+
@Override
66+
public ManagedZone getZone(String zoneName, Map<Option, ?> options) throws DnsException {
67+
// just fields option
68+
try {
69+
return dns.managedZones().get(this.options.projectId(), zoneName)
70+
.setFields(FIELDS.getString(options))
71+
.execute();
72+
} catch (IOException ex) {
73+
DnsException serviceException = translate(ex);
74+
if (serviceException.code() == HTTP_NOT_FOUND) {
75+
return null;
76+
}
77+
throw serviceException;
78+
}
79+
}
80+
81+
@Override
82+
public ListResult<ManagedZone> listZones(Map<Option, ?> options) throws DnsException {
83+
// fields, page token, page size
84+
try {
85+
ManagedZonesListResponse zoneList = dns.managedZones().list(this.options.projectId())
86+
.setFields(FIELDS.getString(options))
87+
.setMaxResults(PAGE_SIZE.getInt(options))
88+
.setDnsName(DNS_NAME.getString(options))
89+
.setPageToken(PAGE_TOKEN.getString(options))
90+
.execute();
91+
return of(zoneList.getNextPageToken(), zoneList.getManagedZones());
92+
} catch (IOException ex) {
93+
throw translate(ex);
94+
}
95+
}
96+
97+
@Override
98+
public boolean deleteZone(String zoneName) throws DnsException {
99+
try {
100+
dns.managedZones().delete(this.options.projectId(), zoneName).execute();
101+
return true;
102+
} catch (IOException ex) {
103+
DnsException serviceException = translate(ex);
104+
if (serviceException.code() == HTTP_NOT_FOUND) {
105+
return false;
106+
}
107+
throw serviceException;
108+
}
109+
}
110+
111+
@Override
112+
public ListResult<ResourceRecordSet> listDnsRecords(String zoneName, Map<Option, ?> options)
113+
throws DnsException {
114+
// options are fields, page token, dns name, type
115+
try {
116+
ResourceRecordSetsListResponse response = dns.resourceRecordSets()
117+
.list(this.options.projectId(), zoneName)
118+
.setFields(FIELDS.getString(options))
119+
.setPageToken(PAGE_TOKEN.getString(options))
120+
.setMaxResults(PAGE_SIZE.getInt(options))
121+
.setName(NAME.getString(options))
122+
.setType(DNS_TYPE.getString(options))
123+
.execute();
124+
return of(response.getNextPageToken(), response.getRrsets());
125+
} catch (IOException ex) {
126+
throw translate(ex);
127+
}
128+
}
129+
130+
@Override
131+
public Project getProject(Map<Option, ?> options) throws DnsException {
132+
try {
133+
return dns.projects().get(this.options.projectId())
134+
.setFields(FIELDS.getString(options)).execute();
135+
} catch (IOException ex) {
136+
throw translate(ex);
137+
}
138+
}
139+
140+
@Override
141+
public Change applyChangeRequest(String zoneName, Change changeRequest, Map<Option, ?> options)
142+
throws DnsException {
143+
try {
144+
return dns.changes().create(this.options.projectId(), zoneName, changeRequest)
145+
.setFields(FIELDS.getString(options))
146+
.execute();
147+
} catch (IOException ex) {
148+
throw translate(ex);
149+
}
150+
}
151+
152+
@Override
153+
public Change getChangeRequest(String zoneName, String changeRequestId, Map<Option, ?> options)
154+
throws DnsException {
155+
try {
156+
return dns.changes().get(this.options.projectId(), zoneName, changeRequestId)
157+
.setFields(FIELDS.getString(options))
158+
.execute();
159+
} catch (IOException ex) {
160+
DnsException serviceException = translate(ex);
161+
if (serviceException.code() == HTTP_NOT_FOUND) {
162+
if (serviceException.location().equals("entity.parameters.changeId")) {
163+
// the change id was not found, but the zone exists
164+
return null;
165+
}
166+
// the zone does not exist, so throw an exception
167+
}
168+
throw serviceException;
169+
}
170+
}
171+
172+
@Override
173+
public ListResult<Change> listChangeRequests(String zoneName, Map<Option, ?> options)
174+
throws DnsException {
175+
// options are fields, page token, page size, sort order
176+
try {
177+
Dns.Changes.List request = dns.changes().list(this.options.projectId(), zoneName)
178+
.setFields(FIELDS.getString(options))
179+
.setMaxResults(PAGE_SIZE.getInt(options))
180+
.setPageToken(PAGE_TOKEN.getString(options));
181+
if (SORTING_ORDER.getString(options) != null) {
182+
// todo check and change if more sorting options are implemented, issue #604
183+
request = request.setSortBy(SORT_BY).setSortOrder(SORTING_ORDER.getString(options));
184+
}
185+
ChangesListResponse response = request.execute();
186+
return ListResult.of(response.getNextPageToken(), response.getChanges());
187+
} catch (IOException ex) {
188+
throw translate(ex);
189+
}
190+
}
191+
}

gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@
1616

1717
package com.google.gcloud.spi;
1818

19+
import com.google.api.services.dns.model.Change;
20+
import com.google.api.services.dns.model.ManagedZone;
21+
import com.google.api.services.dns.model.Project;
22+
import com.google.api.services.dns.model.ResourceRecordSet;
23+
import com.google.common.collect.ImmutableList;
24+
import com.google.gcloud.dns.DnsException;
25+
1926
import java.util.Map;
2027

2128
public interface DnsRpc {
2229

2330
enum Option {
2431
FIELDS("fields"),
25-
PAGE_SIZE("maxSize"),
32+
PAGE_SIZE("maxResults"),
2633
PAGE_TOKEN("pageToken"),
2734
DNS_NAME("dnsName"),
35+
NAME("name"),
2836
DNS_TYPE("type"),
2937
SORTING_ORDER("sortOrder");
3038

@@ -52,5 +60,114 @@ Integer getInt(Map<Option, ?> options) {
5260
}
5361
}
5462

55-
//TODO(mderka) add supported operations. Created issue #594.
63+
class ListResult<T> {
64+
65+
private final Iterable<T> results;
66+
private final String pageToken;
67+
68+
public ListResult(String pageToken, Iterable<T> results) {
69+
this.results = ImmutableList.copyOf(results);
70+
this.pageToken = pageToken;
71+
}
72+
73+
public static <T> ListResult<T> of(String pageToken, Iterable<T> list) {
74+
return new ListResult<>(pageToken, list);
75+
}
76+
77+
public Iterable<T> results() {
78+
return results;
79+
}
80+
81+
public String pageToken() {
82+
return pageToken;
83+
}
84+
}
85+
86+
/**
87+
* Creates a new zone.
88+
*
89+
* @param zone a zone to be created
90+
* @return Updated {@code ManagedZone} object
91+
* @throws DnsException upon failure
92+
*/
93+
ManagedZone create(ManagedZone zone) throws DnsException;
94+
95+
/**
96+
* Retrieves and returns an existing zone.
97+
*
98+
* @param zoneName name of the zone to be returned
99+
* @param options a map of options for the service call
100+
* @return a zone or {@code null} if not found
101+
* @throws DnsException upon failure
102+
*/
103+
ManagedZone getZone(String zoneName, Map<Option, ?> options) throws DnsException;
104+
105+
/**
106+
* Lists the zones that exist within the project.
107+
*
108+
* @param options a map of options for the service call
109+
* @throws DnsException upon failure
110+
*/
111+
ListResult<ManagedZone> listZones(Map<Option, ?> options) throws DnsException;
112+
113+
/**
114+
* Deletes the zone identified by the name.
115+
*
116+
* @return {@code true} if the zone was deleted and {@code false} otherwise
117+
* @throws DnsException upon failure
118+
*/
119+
boolean deleteZone(String zoneName) throws DnsException;
120+
121+
/**
122+
* Lists DNS records for a given zone.
123+
*
124+
* @param zoneName name of the zone to be listed
125+
* @param options a map of options for the service call
126+
* @throws DnsException upon failure or if zone was not found
127+
*/
128+
ListResult<ResourceRecordSet> listDnsRecords(String zoneName, Map<Option, ?> options)
129+
throws DnsException;
130+
131+
/**
132+
* Returns information about the current project.
133+
*
134+
* @param options a map of options for the service call
135+
* @return up-to-date project information
136+
* @throws DnsException upon failure or if the project is not found
137+
*/
138+
Project getProject(Map<Option, ?> options) throws DnsException;
139+
140+
/**
141+
* Applies change request to a zone.
142+
*
143+
* @param zoneName the name of a zone to which the {@code Change} should be applied
144+
* @param changeRequest change to be applied
145+
* @param options a map of options for the service call
146+
* @return updated change object with server-assigned ID
147+
* @throws DnsException upon failure or if zone was not found
148+
*/
149+
Change applyChangeRequest(String zoneName, Change changeRequest, Map<Option, ?> options)
150+
throws DnsException;
151+
152+
/**
153+
* Returns an existing change request.
154+
*
155+
* @param zoneName the name of a zone to which the {@code Change} was be applied
156+
* @param changeRequestId the unique id assigned to the change by the server
157+
* @param options a map of options for the service call
158+
* @return up-to-date change object or {@code null} if change was not found
159+
* @throws DnsException upon failure or if zone was not found
160+
*/
161+
Change getChangeRequest(String zoneName, String changeRequestId, Map<Option, ?> options)
162+
throws DnsException;
163+
164+
/**
165+
* List existing change requests for a zone.
166+
*
167+
* @param zoneName the name of a zone to which the {@code Change}s were be applied
168+
* @param options a map of options for the service call
169+
* @throws DnsException upon failure or if zone was not found
170+
*/
171+
ListResult<Change> listChangeRequests(String zoneName, Map<Option, ?> options)
172+
throws DnsException;
56173
}

gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void testDnsRecordListOption() {
3434
String dnsName = "some name";
3535
Dns.DnsRecordListOption dnsRecordListOption = Dns.DnsRecordListOption.dnsName(dnsName);
3636
assertEquals(dnsName, dnsRecordListOption.value());
37-
assertEquals(DnsRpc.Option.DNS_NAME, dnsRecordListOption.rpcOption());
37+
assertEquals(DnsRpc.Option.NAME, dnsRecordListOption.rpcOption());
3838
// page token
3939
dnsRecordListOption = Dns.DnsRecordListOption.pageToken(PAGE_TOKEN);
4040
assertEquals(PAGE_TOKEN, dnsRecordListOption.value());

0 commit comments

Comments
 (0)