Skip to content

Commit

Permalink
Change Tuple<X,Y> into ListResult<X>, added NAME option.
Browse files Browse the repository at this point in the history
  • Loading branch information
mderka committed Feb 4, 2016
1 parent 896de75 commit a729444
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public static DnsRecordListOption pageSize(int pageSize) {
* Restricts the list to only DNS records with this fully qualified domain name.
*/
public static DnsRecordListOption dnsName(String dnsName) {
return new DnsRecordListOption(DnsRpc.Option.DNS_NAME, dnsName);
return new DnsRecordListOption(DnsRpc.Option.NAME, dnsName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.google.gcloud.spi;

import static com.google.gcloud.spi.DnsRpc.ListResult.of;
import static com.google.gcloud.spi.DnsRpc.Option.DNS_NAME;
import static com.google.gcloud.spi.DnsRpc.Option.NAME;
import static com.google.gcloud.spi.DnsRpc.Option.DNS_TYPE;
import static com.google.gcloud.spi.DnsRpc.Option.FIELDS;
import static com.google.gcloud.spi.DnsRpc.Option.PAGE_SIZE;
Expand Down Expand Up @@ -30,7 +32,7 @@
*/
public class DefaultDnsRpc implements DnsRpc {

private static final String SORTING_KEY = "changeSequence";
private static final String SORT_BY = "changeSequence";
private final Dns dns;
private final DnsOptions options;

Expand Down Expand Up @@ -77,17 +79,15 @@ public ManagedZone getZone(String zoneName, Map<Option, ?> options) throws DnsEx
}

@Override
public Tuple<String, Iterable<ManagedZone>> listZones(Map<Option, ?> options)
throws DnsException {
public ListResult<ManagedZone> listZones(Map<Option, ?> options) throws DnsException {
// fields, page token, page size
try {
ManagedZonesListResponse zoneList = dns.managedZones().list(this.options.projectId())
.setFields(FIELDS.getString(options))
.setMaxResults(PAGE_SIZE.getInt(options))
.setPageToken(PAGE_TOKEN.getString(options))
.execute();
return Tuple.<String, Iterable<ManagedZone>>of(zoneList.getNextPageToken(),
zoneList.getManagedZones());
return of(zoneList.getNextPageToken(), zoneList.getManagedZones());
} catch (IOException ex) {
throw translate(ex);
}
Expand All @@ -108,20 +108,19 @@ public boolean deleteZone(String zoneName) throws DnsException {
}

@Override
public Tuple<String, Iterable<ResourceRecordSet>> listDnsRecords(String zoneName,
Map<Option, ?> options) throws DnsException {
public ListResult<ResourceRecordSet> listDnsRecords(String zoneName, Map<Option, ?> options)
throws DnsException {
// options are fields, page token, dns name, type
try {
ResourceRecordSetsListResponse response = dns.resourceRecordSets()
.list(this.options.projectId(), zoneName)
.setFields(FIELDS.getString(options))
.setPageToken(PAGE_TOKEN.getString(options))
.setMaxResults(PAGE_SIZE.getInt(options))
.setName(DNS_NAME.getString(options))
.setName(NAME.getString(options))
.setType(DNS_TYPE.getString(options))
.execute();
return Tuple.<String, Iterable<ResourceRecordSet>>of(response.getNextPageToken(),
response.getRrsets());
return of(response.getNextPageToken(), response.getRrsets());
} catch (IOException ex) {
throw translate(ex);
}
Expand Down Expand Up @@ -159,8 +158,7 @@ public Change getChangeRequest(String zoneName, String changeRequestId, Map<Opti
} catch (IOException ex) {
DnsException serviceException = translate(ex);
if (serviceException.code() == HTTP_NOT_FOUND) {
// message format "The 'parameters.changeId' resource named '4' does not exist."
if (serviceException.getMessage().contains("changeId")) {
if (serviceException.location().equals("entity.parameters.changeId")) {
// the change id was not found, but the zone exists
return null;
}
Expand All @@ -171,7 +169,7 @@ public Change getChangeRequest(String zoneName, String changeRequestId, Map<Opti
}

@Override
public Tuple<String, Iterable<Change>> listChangeRequests(String zoneName, Map<Option, ?> options)
public ListResult<Change> listChangeRequests(String zoneName, Map<Option, ?> options)
throws DnsException {
// options are fields, page token, page size, sort order
try {
Expand All @@ -181,10 +179,10 @@ public Tuple<String, Iterable<Change>> listChangeRequests(String zoneName, Map<O
.setPageToken(PAGE_TOKEN.getString(options));
if (SORTING_ORDER.getString(options) != null) {
// todo check and change if more sorting options are implemented, issue #604
request = request.setSortBy(SORTING_KEY).setSortOrder(SORTING_ORDER.getString(options));
request = request.setSortBy(SORT_BY).setSortOrder(SORTING_ORDER.getString(options));
}
ChangesListResponse response = request.execute();
return Tuple.<String, Iterable<Change>>of(response.getNextPageToken(), response.getChanges());
return ListResult.of(response.getNextPageToken(), response.getChanges());
} catch (IOException ex) {
throw translate(ex);
}
Expand Down
44 changes: 23 additions & 21 deletions gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.api.services.dns.model.ManagedZone;
import com.google.api.services.dns.model.Project;
import com.google.api.services.dns.model.ResourceRecordSet;
import com.google.common.collect.ImmutableList;
import com.google.gcloud.dns.DnsException;

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

enum Option {
FIELDS("fields"),
PAGE_SIZE("maxSize"),
PAGE_SIZE("maxResults"),
PAGE_TOKEN("pageToken"),
DNS_NAME("dnsName"),
NAME("name"),
DNS_TYPE("type"),
SORTING_ORDER("sortOrder");

Expand Down Expand Up @@ -58,26 +60,26 @@ Integer getInt(Map<Option, ?> options) {
}
}

class Tuple<X, Y> {
class ListResult<T> {

private final X x;
private final Y y;
private final Iterable<T> results;
private final String pageToken;

private Tuple(X x, Y y) {
this.x = x;
this.y = y;
public ListResult(String pageToken, Iterable<T> results) {
this.results = ImmutableList.copyOf(results);
this.pageToken = pageToken;
}

public static <X, Y> Tuple<X, Y> of(X x, Y y) {
return new Tuple<>(x, y);
public static <T> ListResult<T> of(String pageToken, Iterable<T> list) {
return new ListResult<>(pageToken, list);
}

public X x() {
return x;
public Iterable<T> results() {
return results;
}

public Y y() {
return y;
public String pageToken() {
return pageToken;
}
}

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

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

/**
* Returns information about the current project.
*
*
* @param options a map of options for the service call
* @return up-to-date project information
* @throws DnsException upon failure or if the project is not found
*/
Project getProject(Map<Option, ?> options) throws DnsException;

/**
* Applies change request to a zone.
* Applies change request to a zone.
*
* @param zoneName the name of a zone to which the {@code Change} should be applied
* @param changeRequest change to be applied
* @param options a map of options for the service call
* @return updated change object with server-assigned ID
* @throws DnsException upon failure or if zone was not found
*/
Change applyChangeRequest(String zoneName, Change changeRequest, Map<Option, ?> options)
Change applyChangeRequest(String zoneName, Change changeRequest, Map<Option, ?> options)
throws DnsException;

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

/**
Expand All @@ -166,6 +168,6 @@ Change getChangeRequest(String zoneName, String changeRequestId, Map<Option, ?>
* @param options a map of options for the service call
* @throws DnsException upon failure or if zone was not found
*/
Tuple<String, Iterable<Change>> listChangeRequests(String zoneName, Map<Option, ?> options)
ListResult<Change> listChangeRequests(String zoneName, Map<Option, ?> options)
throws DnsException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void testDnsRecordListOption() {
String dnsName = "some name";
Dns.DnsRecordListOption dnsRecordListOption = Dns.DnsRecordListOption.dnsName(dnsName);
assertEquals(dnsName, dnsRecordListOption.value());
assertEquals(DnsRpc.Option.DNS_NAME, dnsRecordListOption.rpcOption());
assertEquals(DnsRpc.Option.NAME, dnsRecordListOption.rpcOption());
// page token
dnsRecordListOption = Dns.DnsRecordListOption.pageToken(PAGE_TOKEN);
assertEquals(PAGE_TOKEN, dnsRecordListOption.value());
Expand Down

0 comments on commit a729444

Please sign in to comment.