Skip to content

Commit 3ae0f21

Browse files
committed
Change Tuple<X,Y> into ListResult<X>, added NAME option.
1 parent 896de75 commit 3ae0f21

4 files changed

Lines changed: 39 additions & 38 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/spi/DefaultDnsRpc.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.google.gcloud.spi;
22

3+
import static com.google.gcloud.spi.DnsRpc.ListResult.of;
34
import static com.google.gcloud.spi.DnsRpc.Option.DNS_NAME;
5+
import static com.google.gcloud.spi.DnsRpc.Option.NAME;
46
import static com.google.gcloud.spi.DnsRpc.Option.DNS_TYPE;
57
import static com.google.gcloud.spi.DnsRpc.Option.FIELDS;
68
import static com.google.gcloud.spi.DnsRpc.Option.PAGE_SIZE;
@@ -30,7 +32,7 @@
3032
*/
3133
public class DefaultDnsRpc implements DnsRpc {
3234

33-
private static final String SORTING_KEY = "changeSequence";
35+
private static final String SORT_BY = "changeSequence";
3436
private final Dns dns;
3537
private final DnsOptions options;
3638

@@ -77,17 +79,16 @@ public ManagedZone getZone(String zoneName, Map<Option, ?> options) throws DnsEx
7779
}
7880

7981
@Override
80-
public Tuple<String, Iterable<ManagedZone>> listZones(Map<Option, ?> options)
81-
throws DnsException {
82+
public ListResult<ManagedZone> listZones(Map<Option, ?> options) throws DnsException {
8283
// fields, page token, page size
8384
try {
8485
ManagedZonesListResponse zoneList = dns.managedZones().list(this.options.projectId())
8586
.setFields(FIELDS.getString(options))
8687
.setMaxResults(PAGE_SIZE.getInt(options))
88+
.setDnsName(DNS_NAME.getString(options))
8789
.setPageToken(PAGE_TOKEN.getString(options))
8890
.execute();
89-
return Tuple.<String, Iterable<ManagedZone>>of(zoneList.getNextPageToken(),
90-
zoneList.getManagedZones());
91+
return of(zoneList.getNextPageToken(), zoneList.getManagedZones());
9192
} catch (IOException ex) {
9293
throw translate(ex);
9394
}
@@ -108,20 +109,19 @@ public boolean deleteZone(String zoneName) throws DnsException {
108109
}
109110

110111
@Override
111-
public Tuple<String, Iterable<ResourceRecordSet>> listDnsRecords(String zoneName,
112-
Map<Option, ?> options) throws DnsException {
112+
public ListResult<ResourceRecordSet> listDnsRecords(String zoneName, Map<Option, ?> options)
113+
throws DnsException {
113114
// options are fields, page token, dns name, type
114115
try {
115116
ResourceRecordSetsListResponse response = dns.resourceRecordSets()
116117
.list(this.options.projectId(), zoneName)
117118
.setFields(FIELDS.getString(options))
118119
.setPageToken(PAGE_TOKEN.getString(options))
119120
.setMaxResults(PAGE_SIZE.getInt(options))
120-
.setName(DNS_NAME.getString(options))
121+
.setName(NAME.getString(options))
121122
.setType(DNS_TYPE.getString(options))
122123
.execute();
123-
return Tuple.<String, Iterable<ResourceRecordSet>>of(response.getNextPageToken(),
124-
response.getRrsets());
124+
return of(response.getNextPageToken(), response.getRrsets());
125125
} catch (IOException ex) {
126126
throw translate(ex);
127127
}
@@ -159,8 +159,7 @@ public Change getChangeRequest(String zoneName, String changeRequestId, Map<Opti
159159
} catch (IOException ex) {
160160
DnsException serviceException = translate(ex);
161161
if (serviceException.code() == HTTP_NOT_FOUND) {
162-
// message format "The 'parameters.changeId' resource named '4' does not exist."
163-
if (serviceException.getMessage().contains("changeId")) {
162+
if (serviceException.location().equals("entity.parameters.changeId")) {
164163
// the change id was not found, but the zone exists
165164
return null;
166165
}
@@ -171,7 +170,7 @@ public Change getChangeRequest(String zoneName, String changeRequestId, Map<Opti
171170
}
172171

173172
@Override
174-
public Tuple<String, Iterable<Change>> listChangeRequests(String zoneName, Map<Option, ?> options)
173+
public ListResult<Change> listChangeRequests(String zoneName, Map<Option, ?> options)
175174
throws DnsException {
176175
// options are fields, page token, page size, sort order
177176
try {
@@ -181,10 +180,10 @@ public Tuple<String, Iterable<Change>> listChangeRequests(String zoneName, Map<O
181180
.setPageToken(PAGE_TOKEN.getString(options));
182181
if (SORTING_ORDER.getString(options) != null) {
183182
// todo check and change if more sorting options are implemented, issue #604
184-
request = request.setSortBy(SORTING_KEY).setSortOrder(SORTING_ORDER.getString(options));
183+
request = request.setSortBy(SORT_BY).setSortOrder(SORTING_ORDER.getString(options));
185184
}
186185
ChangesListResponse response = request.execute();
187-
return Tuple.<String, Iterable<Change>>of(response.getNextPageToken(), response.getChanges());
186+
return ListResult.of(response.getNextPageToken(), response.getChanges());
188187
} catch (IOException ex) {
189188
throw translate(ex);
190189
}

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.api.services.dns.model.ManagedZone;
2121
import com.google.api.services.dns.model.Project;
2222
import com.google.api.services.dns.model.ResourceRecordSet;
23+
import com.google.common.collect.ImmutableList;
2324
import com.google.gcloud.dns.DnsException;
2425

2526
import java.util.Map;
@@ -28,9 +29,10 @@ public interface DnsRpc {
2829

2930
enum Option {
3031
FIELDS("fields"),
31-
PAGE_SIZE("maxSize"),
32+
PAGE_SIZE("maxResults"),
3233
PAGE_TOKEN("pageToken"),
3334
DNS_NAME("dnsName"),
35+
NAME("name"),
3436
DNS_TYPE("type"),
3537
SORTING_ORDER("sortOrder");
3638

@@ -58,26 +60,26 @@ Integer getInt(Map<Option, ?> options) {
5860
}
5961
}
6062

61-
class Tuple<X, Y> {
63+
class ListResult<T> {
6264

63-
private final X x;
64-
private final Y y;
65+
private final Iterable<T> results;
66+
private final String pageToken;
6567

66-
private Tuple(X x, Y y) {
67-
this.x = x;
68-
this.y = y;
68+
public ListResult(String pageToken, Iterable<T> results) {
69+
this.results = ImmutableList.copyOf(results);
70+
this.pageToken = pageToken;
6971
}
7072

71-
public static <X, Y> Tuple<X, Y> of(X x, Y y) {
72-
return new Tuple<>(x, y);
73+
public static <T> ListResult<T> of(String pageToken, Iterable<T> list) {
74+
return new ListResult<>(pageToken, list);
7375
}
7476

75-
public X x() {
76-
return x;
77+
public Iterable<T> results() {
78+
return results;
7779
}
7880

79-
public Y y() {
80-
return y;
81+
public String pageToken() {
82+
return pageToken;
8183
}
8284
}
8385

@@ -106,7 +108,7 @@ public Y y() {
106108
* @param options a map of options for the service call
107109
* @throws DnsException upon failure
108110
*/
109-
Tuple<String, Iterable<ManagedZone>> listZones(Map<Option, ?> options) throws DnsException;
111+
ListResult<ManagedZone> listZones(Map<Option, ?> options) throws DnsException;
110112

111113
/**
112114
* Deletes the zone identified by the name.
@@ -123,28 +125,28 @@ public Y y() {
123125
* @param options a map of options for the service call
124126
* @throws DnsException upon failure or if zone was not found
125127
*/
126-
Tuple<String, Iterable<ResourceRecordSet>> listDnsRecords(String zoneName,
127-
Map<Option, ?> options) throws DnsException;
128+
ListResult<ResourceRecordSet> listDnsRecords(String zoneName, Map<Option, ?> options)
129+
throws DnsException;
128130

129131
/**
130132
* Returns information about the current project.
131-
*
133+
*
132134
* @param options a map of options for the service call
133135
* @return up-to-date project information
134136
* @throws DnsException upon failure or if the project is not found
135137
*/
136138
Project getProject(Map<Option, ?> options) throws DnsException;
137139

138140
/**
139-
* Applies change request to a zone.
141+
* Applies change request to a zone.
140142
*
141143
* @param zoneName the name of a zone to which the {@code Change} should be applied
142144
* @param changeRequest change to be applied
143145
* @param options a map of options for the service call
144146
* @return updated change object with server-assigned ID
145147
* @throws DnsException upon failure or if zone was not found
146148
*/
147-
Change applyChangeRequest(String zoneName, Change changeRequest, Map<Option, ?> options)
149+
Change applyChangeRequest(String zoneName, Change changeRequest, Map<Option, ?> options)
148150
throws DnsException;
149151

150152
/**
@@ -156,7 +158,7 @@ Change applyChangeRequest(String zoneName, Change changeRequest, Map<Option, ?>
156158
* @return up-to-date change object or {@code null} if change was not found
157159
* @throws DnsException upon failure or if zone was not found
158160
*/
159-
Change getChangeRequest(String zoneName, String changeRequestId, Map<Option, ?> options)
161+
Change getChangeRequest(String zoneName, String changeRequestId, Map<Option, ?> options)
160162
throws DnsException;
161163

162164
/**
@@ -166,6 +168,6 @@ Change getChangeRequest(String zoneName, String changeRequestId, Map<Option, ?>
166168
* @param options a map of options for the service call
167169
* @throws DnsException upon failure or if zone was not found
168170
*/
169-
Tuple<String, Iterable<Change>> listChangeRequests(String zoneName, Map<Option, ?> options)
171+
ListResult<Change> listChangeRequests(String zoneName, Map<Option, ?> options)
170172
throws DnsException;
171173
}

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)