Skip to content

Commit b128548

Browse files
committed
Merge pull request #620 from mderka/dns-options
Avoids exposing zone id
2 parents 35dbb34 + d722635 commit b128548

8 files changed

Lines changed: 234 additions & 693 deletions

File tree

gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java

Lines changed: 9 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.gcloud.spi.DnsRpc;
2424

2525
import java.io.Serializable;
26-
import java.math.BigInteger;
2726
import java.util.Set;
2827

2928
/**
@@ -69,8 +68,7 @@ static String selector(ProjectField... fields) {
6968
* The fields of a zone.
7069
*
7170
* <p>These values can be used to specify the fields to include in a partial response when calling
72-
* {@link Dns#getZone(BigInteger, ZoneOption...)} or {@link Dns#getZone(String, ZoneOption...)}.
73-
* The ID is always returned, even if not specified.
71+
* {@link Dns#getZone(String, ZoneOption...)}. The name is always returned, even if not specified.
7472
*/
7573
enum ZoneField {
7674
CREATION_TIME("creationTime"),
@@ -93,7 +91,7 @@ String selector() {
9391

9492
static String selector(ZoneField... fields) {
9593
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
96-
fieldStrings.add(ZONE_ID.selector());
94+
fieldStrings.add(NAME.selector());
9795
for (ZoneField field : fields) {
9896
fieldStrings.add(field.selector());
9997
}
@@ -105,9 +103,8 @@ static String selector(ZoneField... fields) {
105103
* The fields of a DNS record.
106104
*
107105
* <p>These values can be used to specify the fields to include in a partial response when calling
108-
* {@link Dns#listDnsRecords(BigInteger, DnsRecordListOption...)} or {@link
109-
* Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if not
110-
* selected.
106+
* {@link Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if
107+
* not selected.
111108
*/
112109
enum DnsRecordField {
113110
DNS_RECORDS("rrdatas"),
@@ -139,8 +136,7 @@ static String selector(DnsRecordField... fields) {
139136
* The fields of a change request.
140137
*
141138
* <p>These values can be used to specify the fields to include in a partial response when calling
142-
* {@link Dns#applyChangeRequest(BigInteger, ChangeRequest, ChangeRequestOption...)} or {@link
143-
* Dns#applyChangeRequest(String, ChangeRequest, ChangeRequestOption...)} The ID is always
139+
* {@link Dns#applyChangeRequest(String, ChangeRequest, ChangeRequestOption...)} The ID is always
144140
* returned even if not selected.
145141
*/
146142
enum ChangeRequestField {
@@ -425,14 +421,15 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
425421
*
426422
* <p>Returns {@link ZoneInfo} object representing the new zone's information. In addition to the
427423
* name, dns name and description (supplied by the user within the {@code zoneInfo} parameter),
428-
* the returned object will include the following read-only fields supplied by the server:
429-
* creation time, id, and list of name servers.
424+
* the returned object can include the following read-only fields supplied by the server: creation
425+
* time, id, and list of name servers. The returned fields can be optionally restricted by
426+
* specifying {@link ZoneOption}s.
430427
*
431428
* @throws DnsException upon failure
432429
* @see <a href="https://cloud.google.com/dns/api/v1/managedZones/create">Cloud DNS Managed Zones:
433430
* create</a>
434431
*/
435-
ZoneInfo create(ZoneInfo zoneInfo);
432+
ZoneInfo create(ZoneInfo zoneInfo, ZoneOption... options);
436433

437434
/**
438435
* Returns the zone by the specified zone name. Returns {@code null} if the zone is not found. The
@@ -444,16 +441,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
444441
*/
445442
ZoneInfo getZone(String zoneName, ZoneOption... options);
446443

447-
/**
448-
* Returns the zone by the specified zone id. Returns {@code null} if the zone is not found. The
449-
* returned fields can be optionally restricted by specifying {@link ZoneOption}s.
450-
*
451-
* @throws DnsException upon failure
452-
* @see <a href="https://cloud.google.com/dns/api/v1/managedZones/get">Cloud DNS Managed Zones:
453-
* get</a>
454-
*/
455-
ZoneInfo getZone(BigInteger zoneId, ZoneOption... options);
456-
457444
/**
458445
* Lists the zones inside the project.
459446
*
@@ -479,17 +466,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
479466
*/
480467
boolean delete(String zoneName); // delete does not admit any options
481468

482-
/**
483-
* Deletes an existing zone identified by id. Returns {@code true} if the zone was successfully
484-
* deleted and {@code false} otherwise.
485-
*
486-
* @return {@code true} if zone was found and deleted and {@code false} otherwise
487-
* @throws DnsException upon failure
488-
* @see <a href="https://cloud.google.com/dns/api/v1/managedZones/delete">Cloud DNS Managed Zones:
489-
* delete</a>
490-
*/
491-
boolean delete(BigInteger zoneId); // delete does not admit any options
492-
493469
/**
494470
* Lists the DNS records in the zone identified by name.
495471
*
@@ -502,18 +478,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
502478
*/
503479
Page<DnsRecord> listDnsRecords(String zoneName, DnsRecordListOption... options);
504480

505-
/**
506-
* Lists the DNS records in the zone identified by ID.
507-
*
508-
* <p>The fields to be returned, page size and page tokens can be specified using {@link
509-
* DnsRecordListOption}s.
510-
*
511-
* @throws DnsException upon failure or if the zone cannot be found
512-
* @see <a href="https://cloud.google.com/dns/api/v1/resourceRecordSets/list">Cloud DNS
513-
* ResourceRecordSets: list</a>
514-
*/
515-
Page<DnsRecord> listDnsRecords(BigInteger zoneId, DnsRecordListOption... options);
516-
517481
/**
518482
* Retrieves the information about the current project. The returned fields can be optionally
519483
* restricted by specifying {@link ProjectOption}s.
@@ -523,18 +487,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
523487
*/
524488
ProjectInfo getProjectInfo(ProjectOption... fields);
525489

526-
/**
527-
* Submits a change request for the specified zone. The returned object contains the following
528-
* read-only fields supplied by the server: id, start time and status. time, id, and list of name
529-
* servers. The fields to be returned can be selected by {@link ChangeRequestOption}s.
530-
*
531-
* @return the new {@link ChangeRequest} or {@code null} if zone is not found
532-
* @throws DnsException upon failure
533-
* @see <a href="https://cloud.google.com/dns/api/v1/changes/create">Cloud DNS Changes: create</a>
534-
*/
535-
ChangeRequest applyChangeRequest(BigInteger zoneId, ChangeRequest changeRequest,
536-
ChangeRequestOption... options);
537-
538490
/**
539491
* Submits a change request for the specified zone. The returned object contains the following
540492
* read-only fields supplied by the server: id, start time and status. time, id, and list of name
@@ -547,18 +499,6 @@ ChangeRequest applyChangeRequest(BigInteger zoneId, ChangeRequest changeRequest,
547499
ChangeRequest applyChangeRequest(String zoneName, ChangeRequest changeRequest,
548500
ChangeRequestOption... options);
549501

550-
/**
551-
* Retrieves updated information about a change request previously submitted for a zone identified
552-
* by ID. Returns {@code null} if the request cannot be found and throws an exception if the zone
553-
* does not exist. The fields to be returned using can be specified using {@link
554-
* ChangeRequestOption}s.
555-
*
556-
* @throws DnsException upon failure or if the zone cannot be found
557-
* @see <a href="https://cloud.google.com/dns/api/v1/changes/get">Cloud DNS Chages: get</a>
558-
*/
559-
ChangeRequest getChangeRequest(BigInteger zoneId, String changeRequestId,
560-
ChangeRequestOption... options);
561-
562502
/**
563503
* Retrieves updated information about a change request previously submitted for a zone identified
564504
* by ID. Returns {@code null} if the request cannot be found and throws an exception if the zone
@@ -571,18 +511,6 @@ ChangeRequest getChangeRequest(BigInteger zoneId, String changeRequestId,
571511
ChangeRequest getChangeRequest(String zoneName, String changeRequestId,
572512
ChangeRequestOption... options);
573513

574-
/**
575-
* Lists the change requests for the zone identified by ID that were submitted to the service.
576-
*
577-
* <p>The sorting order for changes (based on when they were received by the server), fields to be
578-
* returned, page size and page token can be specified using {@link ChangeRequestListOption}s.
579-
*
580-
* @return A page of change requests
581-
* @throws DnsException upon failure or if the zone cannot be found
582-
* @see <a href="https://cloud.google.com/dns/api/v1/changes/list">Cloud DNS Chages: list</a>
583-
*/
584-
Page<ChangeRequest> listChangeRequests(BigInteger zoneId, ChangeRequestListOption... options);
585-
586514
/**
587515
* Lists the change requests for the zone identified by name that were submitted to the service.
588516
*

gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java

Lines changed: 17 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616

1717
package com.google.gcloud.dns;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
2019
import static com.google.common.base.Preconditions.checkNotNull;
2120

2221
import com.google.gcloud.Page;
2322

2423
import 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,77 @@ 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.
8970
*
9071
* @param options optional restriction on what fields should be fetched
9172
* @return zone object containing updated information or {@code null} if not not found
9273
* @throws DnsException upon failure
93-
* @throws NullPointerException if both zone ID and name are not initialized
9474
*/
9575
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;
76+
return Zone.get(dns, zoneInfo.name(), options);
10677
}
10778

10879
/**
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.
80+
* Deletes the zone. The method deletes the zone by name.
11181
*
11282
* @return {@code true} is zone was found and deleted and {@code false} otherwise
11383
* @throws DnsException upon failure
114-
* @throws NullPointerException if both zone ID and name are not initialized
11584
*/
11685
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;
86+
return dns.delete(zoneInfo.name());
12787
}
12888

12989
/**
130-
* Lists all {@link DnsRecord}s associated with this zone. First searches for zone by ID and if
131-
* not found then by name.
90+
* Lists all {@link DnsRecord}s associated with this zone. The method searches for zone by name.
13291
*
13392
* @param options optional restriction on listing and fields of {@link DnsRecord}s returned
13493
* @return a page of DNS records
13594
* @throws DnsException upon failure or if the zone is not found
136-
* @throws NullPointerException if both zone ID and name are not initialized
13795
*/
13896
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;
97+
return dns.listDnsRecords(zoneInfo.name(), options);
14998
}
15099

151100
/**
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.
101+
* Submits {@link ChangeRequest} to the service for it to applied to this zone. The method
102+
* searches for zone by name.
155103
*
156104
* @param options optional restriction on what fields of {@link ChangeRequest} should be returned
157105
* @return ChangeRequest with server-assigned ID
158106
* @throws DnsException upon failure or if the zone is not found
159-
* @throws NullPointerException if both zone ID and name are not initialized
160107
*/
161108
public ChangeRequest applyChangeRequest(ChangeRequest changeRequest,
162109
Dns.ChangeRequestOption... options) {
163-
checkNameOrIdNotNull();
164110
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;
111+
return dns.applyChangeRequest(zoneInfo.name(), changeRequest, options);
174112
}
175113

176114
/**
177115
* 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.
116+
* this zone. Returns a {@link ChangeRequest} or {@code null} if the change request was not found.
117+
* Throws {@link DnsException} if the zone is not found.
180118
*
181119
* @param options optional restriction on what fields of {@link ChangeRequest} should be returned
182120
* @return updated ChangeRequest
183121
* @throws DnsException upon failure or if the zone is not found
184-
* @throws NullPointerException if both zone ID and name are not initialized
185-
* @throws NullPointerException if the change request does not have initialized id
122+
* @throws NullPointerException if {@code changeRequestId} is null
186123
*/
187124
public ChangeRequest getChangeRequest(String changeRequestId,
188125
Dns.ChangeRequestOption... options) {
189-
checkNameOrIdNotNull();
190126
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;
127+
return dns.getChangeRequest(zoneInfo.name(), changeRequestId, options);
200128
}
201129

202130
/**
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.
131+
* Retrieves all change requests for this zone. The method searches for zone by name. Returns a
132+
* page of {@link ChangeRequest}s.
206133
*
207134
* @param options optional restriction on listing and fields to be returned
208135
* @return a page of change requests
209136
* @throws DnsException upon failure or if the zone is not found
210-
* @throws NullPointerException if both zone ID and name are not initialized
211137
*/
212138
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.");
139+
return dns.listChangeRequests(zoneInfo.name(), options);
231140
}
232141

233142
/**

0 commit comments

Comments
 (0)