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