forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements DefaultDnsRpc. Progress in googleapis#595.
- Loading branch information
Showing
3 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
gcloud-java-dns/src/main/java/com/google/gcloud/spi/DefaultDnsRpc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package com.google.gcloud.spi; | ||
|
||
import static com.google.gcloud.spi.DnsRpc.Option.DNS_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; | ||
import static com.google.gcloud.spi.DnsRpc.Option.PAGE_TOKEN; | ||
import static com.google.gcloud.spi.DnsRpc.Option.SORTING_ORDER; | ||
import static java.net.HttpURLConnection.HTTP_NOT_FOUND; | ||
|
||
import com.google.api.client.http.HttpRequestInitializer; | ||
import com.google.api.client.http.HttpTransport; | ||
import com.google.api.client.json.jackson.JacksonFactory; | ||
import com.google.api.services.dns.Dns; | ||
import com.google.api.services.dns.model.Change; | ||
import com.google.api.services.dns.model.ChangesListResponse; | ||
import com.google.api.services.dns.model.ManagedZone; | ||
import com.google.api.services.dns.model.ManagedZonesListResponse; | ||
import com.google.api.services.dns.model.Project; | ||
import com.google.api.services.dns.model.ResourceRecordSet; | ||
import com.google.api.services.dns.model.ResourceRecordSetsListResponse; | ||
import com.google.gcloud.dns.DnsException; | ||
import com.google.gcloud.dns.DnsOptions; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* A default implementation of the DnsRpc interface. | ||
*/ | ||
public class DefaultDnsRpc implements DnsRpc { | ||
|
||
private final Dns dns; | ||
private final DnsOptions options; | ||
|
||
private static DnsException translate(IOException exception) { | ||
return new DnsException(exception); | ||
} | ||
|
||
/** | ||
* Constructs an instance of this rpc client with provided {@link DnsOptions}. | ||
*/ | ||
public DefaultDnsRpc(DnsOptions options) { | ||
HttpTransport transport = options.httpTransportFactory().create(); | ||
HttpRequestInitializer initializer = options.httpRequestInitializer(); | ||
this.dns = new Dns.Builder(transport, new JacksonFactory(), initializer) | ||
.setRootUrl(options.host()) | ||
.setApplicationName(options.applicationName()) | ||
.build(); | ||
this.options = options; | ||
} | ||
|
||
@Override | ||
public ManagedZone create(ManagedZone zone) throws DnsException { | ||
try { | ||
return dns.managedZones().create(this.options.projectId(), zone).execute(); | ||
} catch (IOException ex) { | ||
throw translate(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public ManagedZone getZone(String zoneName, Map<Option, ?> options) throws DnsException { | ||
// just fields option | ||
try { | ||
return dns.managedZones().get(this.options.projectId(), zoneName) | ||
.setFields(FIELDS.getString(options)).execute(); | ||
} catch (IOException ex) { | ||
throw translate(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public Tuple<String, Iterable<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()); | ||
} catch (IOException ex) { | ||
throw translate(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean deleteZone(String zoneName) throws DnsException { | ||
try { | ||
dns.managedZones().delete(this.options.projectId(), zoneName).execute(); | ||
return true; | ||
} catch (IOException ex) { | ||
DnsException serviceException = translate(ex); | ||
if (serviceException.code() == HTTP_NOT_FOUND) { | ||
return false; | ||
} | ||
throw serviceException; | ||
} | ||
} | ||
|
||
@Override | ||
public Tuple<String, Iterable<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)) | ||
.setType(DNS_TYPE.getString(options)) | ||
.execute(); | ||
return Tuple.<String, Iterable<ResourceRecordSet>>of(response.getNextPageToken(), | ||
response.getRrsets()); | ||
} catch (IOException ex) { | ||
throw translate(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public Project getProject(Map<Option, ?> options) throws DnsException { | ||
try { | ||
return dns.projects().get(this.options.projectId()) | ||
.setFields(FIELDS.getString(options)).execute(); | ||
} catch (IOException ex) { | ||
throw translate(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public Change applyChangeRequest(String zoneName, Change changeRequest, Map<Option, ?> options) | ||
throws DnsException { | ||
try { | ||
return dns.changes().create(this.options.projectId(), zoneName, changeRequest) | ||
.setFields(FIELDS.getString(options)) | ||
.execute(); | ||
} catch (IOException ex) { | ||
throw translate(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public Change getChangeRequest(String zoneName, String changeRequestId, Map<Option, ?> options) | ||
throws DnsException { | ||
try { | ||
return dns.changes().get(this.options.projectId(), zoneName, changeRequestId) | ||
.setFields(FIELDS.getString(options)) | ||
.execute(); | ||
} catch (IOException ex) { | ||
throw translate(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public Tuple<String, Iterable<Change>> listChangeRequests(String zoneName, Map<Option, ?> options) | ||
throws DnsException { | ||
// options are fields, page token, page size, sort order | ||
try { | ||
Dns.Changes.List request = dns.changes().list(this.options.projectId(), zoneName) | ||
.setFields(FIELDS.getString(options)) | ||
.setMaxResults(PAGE_SIZE.getInt(options)) | ||
.setPageToken(PAGE_TOKEN.getString(options)); | ||
if (SORTING_ORDER.getString(options) != null) { | ||
// this needs to be checked and changed if more sorting options are implemented, issue #604 | ||
String key = "changeSequence"; | ||
request = request.setSortBy(key).setSortOrder(SORTING_ORDER.getString(options)); | ||
} | ||
ChangesListResponse response = request.execute(); | ||
return Tuple.<String, Iterable<Change>>of(response.getNextPageToken(), response.getChanges()); | ||
} catch (IOException ex) { | ||
throw translate(ex); | ||
} | ||
} | ||
} |