1515 */
1616package com .google .gcloud .dns ;
1717
18+ import static com .google .common .base .Preconditions .checkArgument ;
1819import static com .google .common .base .Preconditions .checkNotNull ;
1920
2021import com .google .common .collect .ImmutableList ;
2122
23+ import java .io .Serializable ;
24+
2225import java .util .LinkedList ;
2326import java .util .List ;
27+ import java .util .Objects ;
2428
2529/**
2630 * A class that represents Google Cloud DNS record set.
2731 *
28- * <p>
29- * A unit of data that will be returned by the DNS servers.
32+ * <p> A unit of data that will be returned by the DNS servers.
3033 *
31- * @see <a href="https://cloud.google.com/dns/api/v1/resourceRecordSets">Google
32- * Cloud DNS documentation</a>
34+ * @see <a href="https://cloud.google.com/dns/api/v1/resourceRecordSets">Google Cloud DNS
35+ * documentation</a>
3336 */
34- public class DnsRecord {
37+ public class DnsRecord implements Serializable {
3538
36- private String name ;
37- private List <String > rrdatas = new LinkedList <>();
38- private Integer ttl = 86400 ; // the default ttl of 24 hours
39- private DnsRecordType type ;
40- private String parentName ;
41- private Long parentId ;
39+ private static final long serialVersionUID = 2016011914302204L ;
40+ private final String name ;
41+ private final List <String > rrdatas ;
42+ private final Integer ttl ;
43+ private final DnsRecordType type ;
44+ private final String zoneName ;
45+ private final Long zoneId ;
4246
4347 /**
4448 * A private constructor. Obtain an instance using {@link DnsRecord#Builder}.
4549 */
4650 private DnsRecord () {
51+ this .name = null ;
52+ this .rrdatas = null ;
53+ this .ttl = null ;
54+ this .type = null ;
55+ this .zoneName = null ;
56+ this .zoneId = null ;
4757 }
4858
4959 DnsRecord (Builder builder ) {
5060 this .name = builder .name ;
5161 this .rrdatas = ImmutableList .copyOf (builder .rrdatas );
5262 this .ttl = builder .ttl ;
5363 this .type = builder .type ;
54- this .parentName = builder .parentName ;
55- this .parentId = builder .parentId ;
64+ this .zoneName = builder .zoneName ;
65+ this .zoneId = builder .zoneId ;
5666 }
5767
5868 /**
5969 * Enum for the DNS record types supported by Cloud DNS.
6070 *
61- * <p>
62- * Google Cloud DNS currently supports records of type A, AAAA, CNAME, MX
63- * NAPTR, NS, PTR, SOA, SPF, SRV, TXT.
71+ * <p> Google Cloud DNS currently supports records of type A, AAAA, CNAME, MX NAPTR, NS, PTR, SOA,
72+ * SPF, SRV, TXT.
6473 *
65- * @see
66- * <a href="https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types">Cloud
67- * DNS supported record types</a>
74+ * @see <a href="https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types">Cloud DNS
75+ * supported record types</a>
6876 */
6977 public enum DnsRecordType {
70- A ("A" ),
71- AAAA ("AAAA" ),
72- CNAME ("CNAME" ),
73- MX ("MX" ),
74- NAPTR ("NAPTR" ),
75- NS ("NS" ),
76- PTR ("PTR" ),
77- SOA ("SOA" ),
78- SPF ("SPF" ),
79- SRV ("SRV" ),
80- TXT ("TXT" );
81-
82- private final String type ;
83-
84- private DnsRecordType (String type ) {
85- this .type = type ;
86- }
78+ A ,
79+ AAAA ,
80+ CNAME ,
81+ MX ,
82+ NAPTR ,
83+ NS ,
84+ PTR ,
85+ SOA ,
86+ SPF ,
87+ SRV ,
88+ TXT ;
8789 }
8890
8991 public static class Builder {
9092
9193 private List <String > rrdatas = new LinkedList <>();
9294 private String name ;
93- private Integer ttl = 86400 ; // default ttl of 24 hours
95+ private Integer ttl ;
9496 private DnsRecordType type ;
95- private String parentName ;
96- private Long parentId ;
97+ private String zoneName ;
98+ private Long zoneId ;
9799
98100 private Builder () {
99101 }
100102
101103 /**
102- * Creates a builder and pre-populates attributes with the values from the
103- * provided DnsRecord instance.
104+ * Creates a builder and pre-populates attributes with the values from the provided DnsRecord
105+ * instance.
104106 */
105107 public Builder (DnsRecord record ) {
106108 this .name = record .name ;
107109 this .ttl = record .ttl ;
108110 this .type = record .type ;
109- this .parentId = record .parentId ;
110- this .parentName = record .parentName ;
111+ this .zoneId = record .zoneId ;
112+ this .zoneName = record .zoneName ;
111113 this .rrdatas .addAll (record .rrdatas );
112114 }
113115
114116 /**
115- * Adds a record to the record set.
117+ * Adds a record to the record set. The records should be as defined in RFC 1035 (section 5) and
118+ * RFC 1034 (section 3.6.1). Examples of records are available in Google DNS documentation.
116119 *
117- * <p>
118- * The records should be as defined in RFC 1035 (section 5) and RFC 1034
119- * (section 3.6.1). Examples of records are available in
120- * <a href="https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types">Cloud
121- * DNS documentation</a>.
120+ * @see <a href="https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types">Google
121+ * DNS documentation </a>.
122122 */
123123 public Builder add (String record ) {
124124 this .rrdatas .add (checkNotNull (record ));
@@ -134,25 +134,19 @@ public Builder name(String name) {
134134 }
135135
136136 /**
137- * Sets the number of seconds that this record can be cached by resolvers.
138- * This number must be non-negative.
137+ * Sets the number of seconds that this record can be cached by resolvers. This number must be
138+ * non-negative.
139139 *
140140 * @param ttl A non-negative number of seconds
141141 */
142142 public Builder ttl (int ttl ) {
143- // change only if
144- if (ttl < 0 ) {
145- throw new IllegalArgumentException (
146- "TTL cannot be negative. The supplied value was " + ttl + "."
147- );
148- }
143+ checkArgument (ttl >= 0 , "TTL cannot be negative. The supplied value was " + ttl + "." );
149144 this .ttl = ttl ;
150145 return this ;
151146 }
152147
153148 /**
154- * The identifier of a supported record type, for example, A, AAAA, MX, TXT,
155- * and so on.
149+ * The identifier of a supported record type, for example, A, AAAA, MX, TXT, and so on.
156150 */
157151 public Builder type (DnsRecordType type ) {
158152 this .type = checkNotNull (type );
@@ -171,8 +165,8 @@ public DnsRecord build() {
171165 */
172166 public Builder managedZone (ManagedZoneInfo parent ) {
173167 checkNotNull (parent );
174- this .parentId = parent .id ();
175- this .parentName = parent .name ();
168+ this .zoneId = parent .id ();
169+ this .zoneName = parent .name ();
176170 return this ;
177171 }
178172 }
@@ -192,9 +186,7 @@ public static Builder builder() {
192186 }
193187
194188 /**
195- * Get user assigned name of this DNS record.
196- *
197- * TODO: is this field mandatory?
189+ * Get the mandatory user assigned name of this DNS record.
198190 */
199191 public String name () {
200192 return name ;
@@ -204,16 +196,12 @@ public String name() {
204196 * Returns a list of DNS record stored in this record set.
205197 */
206198 public List <String > rrdatas () {
207- return rrdatas ;
199+ return ImmutableList . copyOf ( rrdatas ) ;
208200 }
209201
210202 /**
211- * Returns the number of seconds that this ResourceRecordSet can be cached by
212- * resolvers.
213- *
214- * <p>
215- * This number is provided by the user. If this values is not set, we use
216- * default of 86400.
203+ * Returns the number of seconds that this ResourceRecordSet can be cached by resolvers. This
204+ * number is provided by the user.
217205 */
218206 public Integer ttl () {
219207 return ttl ;
@@ -227,28 +215,56 @@ public DnsRecordType type() {
227215 }
228216
229217 /**
230- * Returns name of the managed zone that this record belongs to.
231- *
232- * <p>
233- * The name of the managed zone is provided by the user when the managed zone
234- * is created. It is unique within a project. If this DNS record is not
235- * associated with a managed zone, this returns null.
218+ * Returns name of the managed zone that this record belongs to. The name of the managed zone is
219+ * provided by the user when the managed zone is created. It is unique within a project. If this
220+ * DNS record is not associated with a managed zone, this returns null.
236221 */
237- public String parentName () {
238- return parentName ;
222+ public String zoneName () {
223+ return zoneName ;
239224 }
240225
241226 /**
242227 * Returns name of the managed zone that this record belongs to.
243228 *
244- * <p>
245- * The id of the managed zone is determined by the server when the managed
246- * zone is created. It is a read only value. If this DNS record is not
247- * associated with a managed zone, or if the id of the managed zone was not
248- * loaded from the cloud service, this returns null.
229+ * <p> The id of the managed zone is determined by the server when the managed zone is created. It
230+ * is a read only value. If this DNS record is not associated with a managed zone, or if the id of
231+ * the managed zone was not loaded from the cloud service, this returns null.
249232 */
250- public Long parentId () {
251- return parentId ;
233+ public Long zoneId () {
234+ return zoneId ;
235+ }
236+
237+ @ Override
238+ public int hashCode () {
239+ return Objects .hash (name , rrdatas , ttl , type , zoneName , zoneId );
240+ }
241+
242+ @ Override
243+ public boolean equals (Object obj ) {
244+ if (obj instanceof DnsRecord ) {
245+ DnsRecord other = (DnsRecord ) obj ;
246+ return zoneId == other .zoneId ()
247+ && zoneName == other .zoneName
248+ && this .toRRSet ().equals (other .toRRSet ());
249+ }
250+ return false ;
251+ }
252+
253+ com .google .api .services .dns .model .ResourceRecordSet toRRSet () {
254+ com .google .api .services .dns .model .ResourceRecordSet rrset =
255+ new com .google .api .services .dns .model .ResourceRecordSet ();
256+ rrset .setName (name );
257+ rrset .setRrdatas (this .rrdatas ());
258+ rrset .setTtl (ttl );
259+ rrset .setType (type == null ? null : type .name ());
260+ return rrset ;
261+ }
262+
263+ @ Override
264+ public String toString () {
265+ return "DnsRecord{" + "name=" + name + ", rrdatas=" + rrdatas
266+ + ", ttl=" + ttl + ", type=" + type + ", zoneName="
267+ + zoneName + ", zoneId=" + zoneId + '}' ;
252268 }
253269
254270}
0 commit comments