Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.2k
Completes DnsRpc interface by adding methods and doc. Implements DefaultDnsRpc.#616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
0338eadbc4b820896de753ae0f21File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| 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; | ||
| 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 static final String SORT_BY = "changeSequence"; | ||
| 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) { | ||
| DnsException serviceException = translate(ex); | ||
| if (serviceException.code() == HTTP_NOT_FOUND) { | ||
| return null; | ||
| } | ||
| throw serviceException; | ||
| } | ||
| } | ||
| @Override | ||
| 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)) | ||
| .setDnsName(DNS_NAME.getString(options)) | ||
| .setPageToken(PAGE_TOKEN.getString(options)) | ||
| .execute(); | ||
| return 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 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(NAME.getString(options)) | ||
| .setType(DNS_TYPE.getString(options)) | ||
| .execute(); | ||
| return 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); | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } | ||
| @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) { | ||
| DnsException serviceException = translate(ex); | ||
| if (serviceException.code() == HTTP_NOT_FOUND) { | ||
| if (serviceException.location().equals("entity.parameters.changeId")) { | ||
| // the change id was not found, but the zone exists | ||
| return null; | ||
| } | ||
| // the zone does not exist, so throw an exception | ||
| } | ||
| throw serviceException; | ||
| } | ||
| } | ||
| @Override | ||
| public ListResult<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) { | ||
| // todo check and change if more sorting options are implemented, issue #604 | ||
| request = request.setSortBy(SORT_BY).setSortOrder(SORTING_ORDER.getString(options)); | ||
| } | ||
| ChangesListResponse response = request.execute(); | ||
| return ListResult.of(response.getNextPageToken(), response.getChanges()); | ||
| } catch (IOException ex) { | ||
| throw translate(ex); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,15 +16,23 @@ | ||
| package com.google.gcloud.spi; | ||
| import com.google.api.services.dns.model.Change; | ||
| 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; | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| import java.util.Map; | ||
| 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"); | ||
| @@ -52,5 +60,114 @@ Integer getInt(Map<Option, ?> options) { | ||
| } | ||
| } | ||
| //TODO(mderka) add supported operations. Created issue #594. | ||
| class ListResult<T> { | ||
| private final Iterable<T> results; | ||
| private final String pageToken; | ||
| public ListResult(String pageToken, Iterable<T> results) { | ||
| this.results = ImmutableList.copyOf(results); | ||
| this.pageToken = pageToken; | ||
| } | ||
| public static <T> ListResult<T> of(String pageToken, Iterable<T> list) { | ||
| return new ListResult<>(pageToken, list); | ||
| } | ||
| public Iterable<T> results() { | ||
| return results; | ||
| } | ||
| public String pageToken() { | ||
| return pageToken; | ||
| } | ||
| } | ||
| /** | ||
| * Creates a new zone. | ||
| * | ||
| * @param zone a zone to be created | ||
| * @return Updated {@code ManagedZone} object | ||
| * @throws DnsException upon failure | ||
| */ | ||
| ManagedZone create(ManagedZone zone) throws DnsException; | ||
| /** | ||
| * Retrieves and returns an existing zone. | ||
| * | ||
| * @param zoneName name of the zone to be returned | ||
| * @param options a map of options for the service call | ||
| * @return a zone or {@code null} if not found | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| * @throws DnsException upon failure | ||
| */ | ||
| ManagedZone getZone(String zoneName, Map<Option, ?> options) throws DnsException; | ||
| /** | ||
| * Lists the zones that exist within the project. | ||
| * | ||
| * @param options a map of options for the service call | ||
| * @throws DnsException upon failure | ||
| */ | ||
| ListResult<ManagedZone> listZones(Map<Option, ?> options) throws DnsException; | ||
| /** | ||
| * Deletes the zone identified by the name. | ||
| * | ||
| * @return {@code true} if the zone was deleted and {@code false} otherwise | ||
| * @throws DnsException upon failure | ||
| */ | ||
| boolean deleteZone(String zoneName) throws DnsException; | ||
| /** | ||
| * Lists DNS records for a given zone. | ||
| * | ||
| * @param zoneName name of the zone to be listed | ||
| * @param options a map of options for the service call | ||
| * @throws DnsException upon failure or if zone was not found | ||
| */ | ||
| 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; | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * 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) | ||
| throws DnsException; | ||
| /** | ||
| * Returns an existing change request. | ||
| * | ||
| * @param zoneName the name of a zone to which the {@code Change} was be applied | ||
| * @param changeRequestId the unique id assigned to the change by the server | ||
| * @param options a map of options for the service call | ||
| * @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) | ||
| throws DnsException; | ||
| /** | ||
| * List existing change requests for a zone. | ||
| * | ||
| * @param zoneName the name of a zone to which the {@code Change}s were be applied | ||
| * @param options a map of options for the service call | ||
| * @throws DnsException upon failure or if zone was not found | ||
| */ | ||
| ListResult<Change> listChangeRequests(String zoneName, Map<Option, ?> options) | ||
| throws DnsException; | ||
| } | ||
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.