1616
1717package com .google .gcloud .dns ;
1818
19- import static com .google .common .base .Preconditions .checkArgument ;
2019import static com .google .common .base .Preconditions .checkNotNull ;
2120
2221import com .google .gcloud .Page ;
2322
2423import java .io .Serializable ;
25- import java .math .BigInteger ;
2624
2725/**
2826 * A Google Cloud DNS Zone object.
@@ -39,7 +37,7 @@ public class Zone implements Serializable {
3937
4038 // TODO(mderka) Zone and zoneInfo to be merged. Opened issue #605.
4139
42- private static final long serialVersionUID = - 4012581571095484813L ;
40+ private static final long serialVersionUID = 6847890192129375500L ;
4341 private final ZoneInfo zoneInfo ;
4442 private final Dns dns ;
4543
@@ -68,166 +66,81 @@ public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... option
6866 }
6967
7068 /**
71- * Constructs a {@code Zone} object that contains information received from the Google Cloud DNS
72- * service for the provided {@code zoneId}.
73- *
74- * @param zoneId ID of the zone to be searched for
75- * @param options optional restriction on what fields should be returned by the service
76- * @return zone object containing zone's information or {@code null} if not not found
77- * @throws DnsException upon failure
78- */
79- public static Zone get (Dns dnsService , BigInteger zoneId , Dns .ZoneOption ... options ) {
80- checkNotNull (zoneId );
81- checkNotNull (dnsService );
82- ZoneInfo zoneInfo = dnsService .getZone (zoneId , options );
83- return zoneInfo == null ? null : new Zone (dnsService , zoneInfo );
84- }
85-
86- /**
87- * Retrieves the latest information about the zone. The method first attempts to retrieve the zone
88- * by ID and if not set or zone is not found, it searches by name.
69+ * Retrieves the latest information about the zone. The method retrieves the zone by name which
70+ * must always be initialized.
8971 *
9072 * @param options optional restriction on what fields should be fetched
9173 * @return zone object containing updated information or {@code null} if not not found
9274 * @throws DnsException upon failure
93- * @throws NullPointerException if both zone ID and name are not initialized
9475 */
9576 public Zone reload (Dns .ZoneOption ... options ) {
96- checkNameOrIdNotNull ();
97- Zone zone = null ;
98- if (zoneInfo .id () != null ) {
99- zone = Zone .get (dns , zoneInfo .id (), options );
100- }
101- if (zone == null && zoneInfo .name () != null ) {
102- // zone was not found by id or id is not set at all
103- zone = Zone .get (dns , zoneInfo .name (), options );
104- }
105- return zone ;
77+ return Zone .get (dns , zoneInfo .name (), options );
10678 }
10779
10880 /**
109- * Deletes the zone. The method first attempts to delete the zone by ID. If the zone is not found
110- * or id is not set, it attempts to delete by name.
81+ * Deletes the zone. The method first deletes the zone by name which must always be initialized.
11182 *
11283 * @return {@code true} is zone was found and deleted and {@code false} otherwise
11384 * @throws DnsException upon failure
114- * @throws NullPointerException if both zone ID and name are not initialized
11585 */
11686 public boolean delete () {
117- checkNameOrIdNotNull ();
118- boolean deleted = false ;
119- if (zoneInfo .id () != null ) {
120- deleted = dns .delete (zoneInfo .id ());
121- }
122- if (!deleted && zoneInfo .name () != null ) {
123- // zone was not found by id or id is not set at all
124- deleted = dns .delete (zoneInfo .name ());
125- }
126- return deleted ;
87+ return dns .delete (zoneInfo .name ());
12788 }
12889
12990 /**
130- * Lists all {@link DnsRecord}s associated with this zone. First searches for zone by ID and if
131- * not found then by name .
91+ * Lists all {@link DnsRecord}s associated with this zone. The method searches for zone by name
92+ * which must always be initialized .
13293 *
13394 * @param options optional restriction on listing and fields of {@link DnsRecord}s returned
13495 * @return a page of DNS records
13596 * @throws DnsException upon failure or if the zone is not found
136- * @throws NullPointerException if both zone ID and name are not initialized
13797 */
13898 public Page <DnsRecord > listDnsRecords (Dns .DnsRecordListOption ... options ) {
139- checkNameOrIdNotNull ();
140- Page <DnsRecord > page = null ;
141- if (zoneInfo .id () != null ) {
142- page = dns .listDnsRecords (zoneInfo .id (), options );
143- }
144- if (page == null && zoneInfo .name () != null ) {
145- // zone was not found by id or id is not set at all
146- page = dns .listDnsRecords (zoneInfo .name (), options );
147- }
148- return page ;
99+ return dns .listDnsRecords (zoneInfo .name (), options );
149100 }
150101
151102 /**
152- * Submits {@link ChangeRequest} to the service for it to applied to this zone. First searches for
153- * the zone by ID and if not found then by name. Returns a {@link ChangeRequest} with
154- * server-assigned ID or {@code null} if the zone was not found.
103+ * Submits {@link ChangeRequest} to the service for it to applied to this zone. The method
104+ * searches for zone by name which must always be initialized.
155105 *
156106 * @param options optional restriction on what fields of {@link ChangeRequest} should be returned
157107 * @return ChangeRequest with server-assigned ID
158108 * @throws DnsException upon failure or if the zone is not found
159- * @throws NullPointerException if both zone ID and name are not initialized
160109 */
161110 public ChangeRequest applyChangeRequest (ChangeRequest changeRequest ,
162111 Dns .ChangeRequestOption ... options ) {
163- checkNameOrIdNotNull ();
164112 checkNotNull (changeRequest );
165- ChangeRequest updated = null ;
166- if (zoneInfo .id () != null ) {
167- updated = dns .applyChangeRequest (zoneInfo .id (), changeRequest , options );
168- }
169- if (updated == null && zoneInfo .name () != null ) {
170- // zone was not found by id or id is not set at all
171- updated = dns .applyChangeRequest (zoneInfo .name (), changeRequest , options );
172- }
173- return updated ;
113+ return dns .applyChangeRequest (zoneInfo .name (), changeRequest , options );
174114 }
175115
176116 /**
177117 * Retrieves an updated information about a change request previously submitted to be applied to
178- * this zone. First searches for the zone by ID and if not found then by name. Returns a {@link
179- * ChangeRequest} if found and {@code null} is the zone or the change request was not found.
118+ * this zone. The method searches for zone by name which must always be initialized. Returns a
119+ * {@link ChangeRequest} if and {@code null} if the change request was not found. Throws {@link
120+ * DnsException} if the zone is not found.
180121 *
181122 * @param options optional restriction on what fields of {@link ChangeRequest} should be returned
182123 * @return updated ChangeRequest
183124 * @throws DnsException upon failure or if the zone is not found
184- * @throws NullPointerException if both zone ID and name are not initialized
185125 * @throws NullPointerException if the change request does not have initialized id
186126 */
187127 public ChangeRequest getChangeRequest (String changeRequestId ,
188128 Dns .ChangeRequestOption ... options ) {
189- checkNameOrIdNotNull ();
190129 checkNotNull (changeRequestId );
191- ChangeRequest updated = null ;
192- if (zoneInfo .id () != null ) {
193- updated = dns .getChangeRequest (zoneInfo .id (), changeRequestId , options );
194- }
195- if (updated == null && zoneInfo .name () != null ) {
196- // zone was not found by id or id is not set at all
197- updated = dns .getChangeRequest (zoneInfo .name (), changeRequestId , options );
198- }
199- return updated ;
130+ return dns .getChangeRequest (zoneInfo .name (), changeRequestId , options );
200131 }
201132
202133 /**
203- * Retrieves all change requests for this zone. First searches for the zone by ID and if not found
204- * then by name . Returns a page of {@link ChangeRequest}s or {@code null} if the zone is not
205- * found.
134+ * Retrieves all change requests for this zone. The method searches for zone by name which must
135+ * always be initialized . Returns a page of {@link ChangeRequest}s. Throws a {@link DnsException}
136+ * if the zone is not found.
206137 *
207138 * @param options optional restriction on listing and fields to be returned
208139 * @return a page of change requests
209140 * @throws DnsException upon failure or if the zone is not found
210- * @throws NullPointerException if both zone ID and name are not initialized
211141 */
212142 public Page <ChangeRequest > listChangeRequests (Dns .ChangeRequestListOption ... options ) {
213- checkNameOrIdNotNull ();
214- Page <ChangeRequest > changeRequests = null ;
215- if (zoneInfo .id () != null ) {
216- changeRequests = dns .listChangeRequests (zoneInfo .id (), options );
217- }
218- if (changeRequests == null && zoneInfo .name () != null ) {
219- // zone was not found by id or id is not set at all
220- changeRequests = dns .listChangeRequests (zoneInfo .name (), options );
221- }
222- return changeRequests ;
223- }
224-
225- /**
226- * Check that at least one of name and ID are initialized and throw and exception if not.
227- */
228- private void checkNameOrIdNotNull () {
229- checkArgument (zoneInfo != null && (zoneInfo .id () != null || zoneInfo .name () != null ),
230- "Both zoneInfo.id and zoneInfo.name are null. This is should never happen." );
143+ return dns .listChangeRequests (zoneInfo .name (), options );
231144 }
232145
233146 /**
0 commit comments