1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617package com.google.gcloud.dns;
1718
1819import static com.google.common.base.Preconditions.checkArgument;
1920import static com.google.common.base.Preconditions.checkNotNull;
2021
2122import com.google.common.base.MoreObjects;
2223import com.google.common.collect.ImmutableList;
24+ import com.google.common.collect.Lists;
2325
2426import java.io.Serializable;
2527
3032/**
3133 * A class that represents Google Cloud DNS record set.
3234 *
33- * <p>A unit of data that will be returned by the DNS servers.
35+ * <p>A DnsRecord is the unit of data that will be returned by the DNS servers upon a DNS request
36+ * for a specific domain. The DnsRecord holds the current state of the DNS records that make up a
37+ * managed zone. You can read the records but you do not modify them directly. Rather, you edit
38+ * the records in a managed zone by creating a {@link ChangeRequest}.
3439 *
3540 * @see <a href="https://cloud.google.com/dns/api/v1/resourceRecordSets">Google Cloud DNS
3641 * documentation</a>
@@ -42,26 +47,6 @@ public class DnsRecord implements Serializable {
4247 private final List<String> rrdatas;
4348 private final Integer ttl;
4449 private final DnsRecordType type;
45- private final String zoneName;
46- private final Long zoneId;
47-
48- private DnsRecord() {
49- this.name = null;
50- this.rrdatas = null;
51- this.ttl = null;
52- this.type = null;
53- this.zoneName = null;
54- this.zoneId = null;
55- }
56-
57- DnsRecord(Builder builder) {
58- this.name = builder.name;
59- this.rrdatas = ImmutableList.copyOf(builder.rrdatas);
60- this.ttl = builder.ttl;
61- this.type = builder.type;
62- this.zoneName = builder.zoneName;
63- this.zoneId = builder.zoneId;
64- }
6550
6651 /**
6752 * Enum for the DNS record types supported by Cloud DNS.
@@ -73,16 +58,51 @@ private DnsRecord() {
7358 * supported record types</a>
7459 */
7560 public enum DnsRecordType {
61+ /**
62+ * Address record, which is used to map host names to their IPv4 address.
63+ */
7664 A,
65+ /**
66+ * IPv6 Address record, which is used to map host names to their IPv6 address.
67+ */
7768 AAAA,
69+ /**
70+ * Canonical name record, which is used to alias names.
71+ */
7872 CNAME,
73+ /**
74+ * Mail exchange record, which is used in routing requests to mail servers.
75+ */
7976 MX,
77+ /**
78+ * Naming authority pointer record, defined by RFC3403.
79+ */
8080 NAPTR,
81+ /**
82+ * Name server record, which delegates a DNS zone to an authoritative server.
83+ */
8184 NS,
85+ /**
86+ * Pointer record, which is often used for reverse DNS lookups.
87+ */
8288 PTR,
89+ /**
90+ * Start of authority record, which specifies authoritative information about a DNS zone.
91+ */
8392 SOA,
93+ /**
94+ * Sender policy framework record, which is used in email validation systems.
95+ */
8496 SPF,
97+ /**
98+ * Service locator record, which is used by some voice over IP, instant messaging protocols and
99+ * other applications.
100+ */
85101 SRV,
102+ /**
103+ * Text record, which can contain arbitrary text and can also be used to define machine readable
104+ * data such as security or abuse prevention information.
105+ */
86106 TXT
87107 }
88108
@@ -92,8 +112,6 @@ public static class Builder {
92112 private String name;
93113 private Integer ttl;
94114 private DnsRecordType type;
95- private String zoneName;
96- private Long zoneId;
97115
98116 private Builder() {
99117 }
@@ -102,12 +120,10 @@ private Builder() {
102120 * Creates a builder and pre-populates attributes with the values from the provided DnsRecord
103121 * instance.
104122 */
105- public Builder(DnsRecord record) {
123+ private Builder(DnsRecord record) {
106124 this.name = record.name;
107125 this.ttl = record.ttl;
108126 this.type = record.type;
109- this.zoneId = record.zoneId;
110- this.zoneName = record.zoneName;
111127 this.rrdatas.addAll(record.rrdatas);
112128 }
113129
@@ -118,11 +134,46 @@ public Builder(DnsRecord record) {
118134 * @see <a href="https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types">Google
119135 * DNS documentation </a>.
120136 */
121- public Builder add (String record) {
137+ public Builder addRecord (String record) {
122138 this.rrdatas.add(checkNotNull(record));
123139 return this;
124140 }
125141
142+ /**
143+ * Removes a record from the set. An exact match is required.
144+ */
145+ public Builder removerRecord(String record) {
146+ this.rrdatas.remove(checkNotNull(record));
147+ return this;
148+ }
149+
150+ /**
151+ * Removes a record on the given index from the set.
152+ */
153+ public Builder removerRecord(int index) {
154+ checkArgument(index >= 0 && index < this.rrdatas.size(), "The index is out of bounds. An " +
155+ "integer between 0 and " + (this.rrdatas.size() - 1) + " is required. The provided " +
156+ "value was " + index + ".");
157+ this.rrdatas.remove(index);
158+ return this;
159+ }
160+
161+ /**
162+ * Removes all the records.
163+ */
164+ public Builder clearRecords() {
165+ this.rrdatas.clear();
166+ return this;
167+ }
168+
169+ /**
170+ * Replaces the current records with the provided list of records.
171+ */
172+ public Builder records(List<String> records) {
173+ this.rrdatas = Lists.newLinkedList(checkNotNull(records));
174+ return this;
175+ }
176+
126177 /**
127178 * Sets name for this DNS record set. For example, www.example.com.
128179 */
@@ -157,26 +208,13 @@ public Builder type(DnsRecordType type) {
157208 public DnsRecord build() {
158209 return new DnsRecord(this);
159210 }
211+ }
160212
161- /**
162- * Sets references to the managed zone that this DNS record belongs to.
163- *
164- * todo(mderka): consider if this method is needed; may not be possible when listing records
165- */
166- Builder managedZone(ManagedZoneInfo parent) {
167- checkNotNull(parent);
168- this.zoneId = parent.id();
169- this.zoneName = parent.name();
170- return this;
171- }
172-
173- /**
174- * Sets name reference to the managed zone that this DNS record belongs to.
175- */
176- Builder managedZone(String managedZoneName) {
177- this.zoneName = checkNotNull(managedZoneName);
178- return this;
179- }
213+ DnsRecord(Builder builder) {
214+ this.name = builder.name;
215+ this.rrdatas = ImmutableList.copyOf(builder.rrdatas);
216+ this.ttl = builder.ttl;
217+ this.type = builder.type;
180218 }
181219
182220 /**
@@ -187,7 +225,7 @@ public Builder toBuilder() {
187225 }
188226
189227 /**
190- * Creates an empty builder
228+ * Creates an empty builder.
191229 */
192230 public static Builder builder() {
193231 return new Builder();
@@ -203,13 +241,12 @@ public String name() {
203241 /**
204242 * Returns a list of DNS record stored in this record set.
205243 */
206- public List<String> rrdatas () {
244+ public List<String> records () {
207245 return rrdatas;
208246 }
209247
210248 /**
211- * Returns the number of seconds that this DnsResource can be cached by resolvers. This number is
212- * provided by the user.
249+ * Returns the number of seconds that this DnsResource can be cached by resolvers.
213250 */
214251 public Integer ttl() {
215252 return ttl;
@@ -222,44 +259,21 @@ public DnsRecordType type() {
222259 return type;
223260 }
224261
225- /**
226- * Returns name of the managed zone that this record belongs to. The name of the managed zone is
227- * provided by the user when the managed zone is created. It is unique within a project. If this
228- * DNS record is not associated with a managed zone, this returns null.
229- */
230- public String zoneName() {
231- return zoneName;
232- }
233-
234- /**
235- * Returns id of the managed zone that this record belongs to.
236- *
237- * <p>The id of the managed zone is determined by the server when the managed zone is created. It
238- * is a read only value. If this DNS record is not associated with a managed zone, or if the id of
239- * the managed zone was not loaded from the cloud service, this returns null.
240- */
241- public Long zoneId() {
242- return zoneId;
243- }
244-
245262 @Override
246263 public int hashCode() {
247- return Objects.hash(name, rrdatas, ttl, type, zoneName, zoneId );
264+ return Objects.hash(name, rrdatas, ttl, type);
248265 }
249266
250267 @Override
251268 public boolean equals(Object obj) {
252- return (obj instanceof DnsRecord) && Objects.equals(this.toPb(), ((DnsRecord) obj).toPb())
253- && this.zoneId().equals(((DnsRecord) obj).zoneId())
254- && this.zoneName().equals(((DnsRecord) obj).zoneName());
255-
269+ return (obj instanceof DnsRecord) && Objects.equals(this.toPb(), ((DnsRecord) obj).toPb());
256270 }
257271
258272 com.google.api.services.dns.model.ResourceRecordSet toPb() {
259273 com.google.api.services.dns.model.ResourceRecordSet pb =
260274 new com.google.api.services.dns.model.ResourceRecordSet();
261275 pb.setName(this.name());
262- pb.setRrdatas(this.rrdatas ());
276+ pb.setRrdatas(this.records ());
263277 pb.setTtl(this.ttl());
264278 pb.setType(this.type() == null ? null : this.type().name());
265279 return pb;
@@ -269,11 +283,9 @@ com.google.api.services.dns.model.ResourceRecordSet toPb() {
269283 public String toString() {
270284 return MoreObjects.toStringHelper(this)
271285 .add("name", name())
272- .add("rrdatas", rrdatas ())
286+ .add("rrdatas", records ())
273287 .add("ttl", ttl())
274288 .add("type", type())
275- .add("zoneName", zoneName())
276- .add("zoneId", zoneId())
277289 .toString();
278290 }
279291
0 commit comments