1 /* 2 * Copyright 2016 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.cloud.dns; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 21 import com.google.api.gax.paging.Page; 22 import com.google.api.services.dns.model.ManagedZone; 23 import java.io.IOException; 24 import java.io.ObjectInputStream; 25 import java.util.List; 26 import java.util.Map; 27 import java.util.Objects; 28 29 /** 30 * A Google Cloud DNS Zone object. 31 * 32 * <p>A zone is the container for all of your record sets that share the same DNS name prefix, for 33 * example, example.com. Zones are automatically assigned a set of name servers when they are 34 * created to handle responding to DNS queries for that zone. A zone has quotas for the number of 35 * record sets that it can include. 36 * 37 * @see <a href="https://cloud.google.com/dns/zones/">Google Cloud DNS managed zone 38 * documentation</a> 39 */ 40 public class Zone extends ZoneInfo { 41 42 private static final long serialVersionUID = -5817771337847861598L; 43 private final DnsOptions options; 44 private transient Dns dns; 45 46 /** Builder for {@code Zone}. */ 47 public static class Builder extends ZoneInfo.Builder { 48 private final Dns dns; 49 private final ZoneInfo.BuilderImpl infoBuilder; 50 Builder(Zone zone)51 private Builder(Zone zone) { 52 this.dns = zone.dns; 53 this.infoBuilder = new ZoneInfo.BuilderImpl(zone); 54 } 55 56 @Override setName(String name)57 public Builder setName(String name) { 58 infoBuilder.setName(name); 59 return this; 60 } 61 62 @Override setGeneratedId(String generatedId)63 Builder setGeneratedId(String generatedId) { 64 infoBuilder.setGeneratedId(generatedId); 65 return this; 66 } 67 68 @Override setCreationTimeMillis(long creationTimeMillis)69 Builder setCreationTimeMillis(long creationTimeMillis) { 70 infoBuilder.setCreationTimeMillis(creationTimeMillis); 71 return this; 72 } 73 74 @Override setDnsName(String dnsName)75 public Builder setDnsName(String dnsName) { 76 infoBuilder.setDnsName(dnsName); 77 return this; 78 } 79 80 @Override setDescription(String description)81 public Builder setDescription(String description) { 82 infoBuilder.setDescription(description); 83 return this; 84 } 85 86 @Override setNameServerSet(String nameServerSet)87 Builder setNameServerSet(String nameServerSet) { 88 infoBuilder.setNameServerSet(nameServerSet); 89 return this; 90 } 91 92 @Override setNameServers(List<String> nameServers)93 Builder setNameServers(List<String> nameServers) { 94 infoBuilder.setNameServers(nameServers); // infoBuilder makes a copy 95 return this; 96 } 97 98 @Override setDnsSecConfig(DnsSecConfig dnsSecConfig)99 public Builder setDnsSecConfig(DnsSecConfig dnsSecConfig) { 100 infoBuilder.setDnsSecConfig(dnsSecConfig); 101 return this; 102 } 103 104 @Override setLabels(Map<String, String> labels)105 public Builder setLabels(Map<String, String> labels) { 106 infoBuilder.setLabels(labels); 107 return this; 108 } 109 110 @Override build()111 public Zone build() { 112 return new Zone(dns, infoBuilder); 113 } 114 } 115 Zone(Dns dns, ZoneInfo.BuilderImpl infoBuilder)116 Zone(Dns dns, ZoneInfo.BuilderImpl infoBuilder) { 117 super(infoBuilder); 118 this.dns = dns; 119 this.options = dns.getOptions(); 120 } 121 122 @Override toBuilder()123 public Builder toBuilder() { 124 return new Builder(this); 125 } 126 127 /** 128 * Retrieves the latest information about the zone. The method retrieves the zone by name. 129 * 130 * @param options optional restriction on what fields should be fetched 131 * @return zone object containing updated information or {@code null} if not not found 132 * @throws DnsException upon failure 133 */ reload(Dns.ZoneOption... options)134 public Zone reload(Dns.ZoneOption... options) { 135 return dns.getZone(getName(), options); 136 } 137 138 /** 139 * Deletes the zone. The method deletes the zone by name. 140 * 141 * @return {@code true} is zone was found and deleted and {@code false} otherwise 142 * @throws DnsException upon failure 143 */ delete()144 public boolean delete() { 145 return dns.delete(getName()); 146 } 147 148 /** 149 * Lists all {@link RecordSet}s associated with this zone. The method searches for zone by name. 150 * 151 * @param options optional restriction on listing and fields of {@link RecordSet}s returned 152 * @return a page of record sets 153 * @throws DnsException upon failure or if the zone is not found 154 */ listRecordSets(Dns.RecordSetListOption... options)155 public Page<RecordSet> listRecordSets(Dns.RecordSetListOption... options) { 156 return dns.listRecordSets(getName(), options); 157 } 158 159 /** 160 * Submits {@link ChangeRequestInfo} to the service for it to applied to this zone. The method 161 * searches for zone by name. 162 * 163 * @param options optional restriction on what fields of {@link ChangeRequest} should be returned 164 * @return ChangeRequest with server-assigned ID 165 * @throws DnsException upon failure or if the zone is not found 166 */ applyChangeRequest( ChangeRequestInfo changeRequest, Dns.ChangeRequestOption... options)167 public ChangeRequest applyChangeRequest( 168 ChangeRequestInfo changeRequest, Dns.ChangeRequestOption... options) { 169 checkNotNull(changeRequest); 170 return dns.applyChangeRequest(getName(), changeRequest, options); 171 } 172 173 /** 174 * Retrieves an updated information about a change request previously submitted to be applied to 175 * this zone. Returns a {@link ChangeRequest} or {@code null} if the change request was not found. 176 * Throws {@link DnsException} if the zone is not found. 177 * 178 * @param options optional restriction on what fields of {@link ChangeRequest} should be returned 179 * @return updated ChangeRequest 180 * @throws DnsException upon failure or if the zone is not found 181 * @throws NullPointerException if {@code changeRequestId} is null 182 */ getChangeRequest( String changeRequestId, Dns.ChangeRequestOption... options)183 public ChangeRequest getChangeRequest( 184 String changeRequestId, Dns.ChangeRequestOption... options) { 185 checkNotNull(changeRequestId); 186 return dns.getChangeRequest(getName(), changeRequestId, options); 187 } 188 189 /** 190 * Retrieves all change requests for this zone. The method searches for zone by name. Returns a 191 * page of {@link ChangeRequest}s. 192 * 193 * @param options optional restriction on listing and fields to be returned 194 * @return a page of change requests 195 * @throws DnsException upon failure or if the zone is not found 196 */ listChangeRequests(Dns.ChangeRequestListOption... options)197 public Page<ChangeRequest> listChangeRequests(Dns.ChangeRequestListOption... options) { 198 return dns.listChangeRequests(getName(), options); 199 } 200 201 /** Returns the {@link Dns} service object associated with this zone. */ getDns()202 public Dns getDns() { 203 return this.dns; 204 } 205 206 @Override equals(Object obj)207 public final boolean equals(Object obj) { 208 if (obj == this) { 209 return true; 210 } 211 if (obj == null || !obj.getClass().equals(Zone.class)) { 212 return false; 213 } 214 Zone other = (Zone) obj; 215 return Objects.equals(toPb(), other.toPb()) && Objects.equals(options, other.options); 216 } 217 218 @Override hashCode()219 public final int hashCode() { 220 return Objects.hash(super.hashCode(), options); 221 } 222 readObject(ObjectInputStream stream)223 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 224 stream.defaultReadObject(); 225 this.dns = options.getService(); 226 } 227 fromPb(Dns dns, ManagedZone zone)228 static Zone fromPb(Dns dns, ManagedZone zone) { 229 ZoneInfo info = ZoneInfo.fromPb(zone); 230 return new Zone(dns, new ZoneInfo.BuilderImpl(info)); 231 } 232 } 233