Skip to content

Commit

Permalink
Merge pull request #620 from mderka/dns-options
Browse files Browse the repository at this point in the history
Avoids exposing zone id
  • Loading branch information
mderka committed Feb 5, 2016
2 parents 35dbb34 + d722635 commit b128548
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 693 deletions.
90 changes: 9 additions & 81 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.gcloud.spi.DnsRpc;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Set;

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

static String selector(ZoneField... fields) {
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
fieldStrings.add(ZONE_ID.selector());
fieldStrings.add(NAME.selector());
for (ZoneField field : fields) {
fieldStrings.add(field.selector());
}
Expand All @@ -105,9 +103,8 @@ static String selector(ZoneField... fields) {
* The fields of a DNS record.
*
* <p>These values can be used to specify the fields to include in a partial response when calling
* {@link Dns#listDnsRecords(BigInteger, DnsRecordListOption...)} or {@link
* Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if not
* selected.
* {@link Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if
* not selected.
*/
enum DnsRecordField {
DNS_RECORDS("rrdatas"),
Expand Down Expand Up @@ -139,8 +136,7 @@ static String selector(DnsRecordField... fields) {
* The fields of a change request.
*
* <p>These values can be used to specify the fields to include in a partial response when calling
* {@link Dns#applyChangeRequest(BigInteger, ChangeRequest, ChangeRequestOption...)} or {@link
* Dns#applyChangeRequest(String, ChangeRequest, ChangeRequestOption...)} The ID is always
* {@link Dns#applyChangeRequest(String, ChangeRequest, ChangeRequestOption...)} The ID is always
* returned even if not selected.
*/
enum ChangeRequestField {
Expand Down Expand Up @@ -425,14 +421,15 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
*
* <p>Returns {@link ZoneInfo} object representing the new zone's information. In addition to the
* name, dns name and description (supplied by the user within the {@code zoneInfo} parameter),
* the returned object will include the following read-only fields supplied by the server:
* creation time, id, and list of name servers.
* the returned object can include the following read-only fields supplied by the server: creation
* time, id, and list of name servers. The returned fields can be optionally restricted by
* specifying {@link ZoneOption}s.
*
* @throws DnsException upon failure
* @see <a href="https://cloud.google.com/dns/api/v1/managedZones/create">Cloud DNS Managed Zones:
* create</a>
*/
ZoneInfo create(ZoneInfo zoneInfo);
ZoneInfo create(ZoneInfo zoneInfo, ZoneOption... options);

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

/**
* Returns the zone by the specified zone id. Returns {@code null} if the zone is not found. The
* returned fields can be optionally restricted by specifying {@link ZoneOption}s.
*
* @throws DnsException upon failure
* @see <a href="https://cloud.google.com/dns/api/v1/managedZones/get">Cloud DNS Managed Zones:
* get</a>
*/
ZoneInfo getZone(BigInteger zoneId, ZoneOption... options);

/**
* Lists the zones inside the project.
*
Expand All @@ -479,17 +466,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
*/
boolean delete(String zoneName); // delete does not admit any options

/**
* Deletes an existing zone identified by id. Returns {@code true} if the zone was successfully
* deleted and {@code false} otherwise.
*
* @return {@code true} if zone was found and deleted and {@code false} otherwise
* @throws DnsException upon failure
* @see <a href="https://cloud.google.com/dns/api/v1/managedZones/delete">Cloud DNS Managed Zones:
* delete</a>
*/
boolean delete(BigInteger zoneId); // delete does not admit any options

/**
* Lists the DNS records in the zone identified by name.
*
Expand All @@ -502,18 +478,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
*/
Page<DnsRecord> listDnsRecords(String zoneName, DnsRecordListOption... options);

/**
* Lists the DNS records in the zone identified by ID.
*
* <p>The fields to be returned, page size and page tokens can be specified using {@link
* DnsRecordListOption}s.
*
* @throws DnsException upon failure or if the zone cannot be found
* @see <a href="https://cloud.google.com/dns/api/v1/resourceRecordSets/list">Cloud DNS
* ResourceRecordSets: list</a>
*/
Page<DnsRecord> listDnsRecords(BigInteger zoneId, DnsRecordListOption... options);

/**
* Retrieves the information about the current project. The returned fields can be optionally
* restricted by specifying {@link ProjectOption}s.
Expand All @@ -523,18 +487,6 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
*/
ProjectInfo getProjectInfo(ProjectOption... fields);

/**
* Submits a change request for the specified zone. The returned object contains the following
* read-only fields supplied by the server: id, start time and status. time, id, and list of name
* servers. The fields to be returned can be selected by {@link ChangeRequestOption}s.
*
* @return the new {@link ChangeRequest} or {@code null} if zone is not found
* @throws DnsException upon failure
* @see <a href="https://cloud.google.com/dns/api/v1/changes/create">Cloud DNS Changes: create</a>
*/
ChangeRequest applyChangeRequest(BigInteger zoneId, ChangeRequest changeRequest,
ChangeRequestOption... options);

/**
* Submits a change request for the specified zone. The returned object contains the following
* read-only fields supplied by the server: id, start time and status. time, id, and list of name
Expand All @@ -547,18 +499,6 @@ ChangeRequest applyChangeRequest(BigInteger zoneId, ChangeRequest changeRequest,
ChangeRequest applyChangeRequest(String zoneName, ChangeRequest changeRequest,
ChangeRequestOption... options);

/**
* Retrieves updated information about a change request previously submitted for a zone identified
* by ID. Returns {@code null} if the request cannot be found and throws an exception if the zone
* does not exist. The fields to be returned using can be specified using {@link
* ChangeRequestOption}s.
*
* @throws DnsException upon failure or if the zone cannot be found
* @see <a href="https://cloud.google.com/dns/api/v1/changes/get">Cloud DNS Chages: get</a>
*/
ChangeRequest getChangeRequest(BigInteger zoneId, String changeRequestId,
ChangeRequestOption... options);

/**
* Retrieves updated information about a change request previously submitted for a zone identified
* by ID. Returns {@code null} if the request cannot be found and throws an exception if the zone
Expand All @@ -571,18 +511,6 @@ ChangeRequest getChangeRequest(BigInteger zoneId, String changeRequestId,
ChangeRequest getChangeRequest(String zoneName, String changeRequestId,
ChangeRequestOption... options);

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

/**
* Lists the change requests for the zone identified by name that were submitted to the service.
*
Expand Down
125 changes: 17 additions & 108 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package com.google.gcloud.dns;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.gcloud.Page;

import java.io.Serializable;
import java.math.BigInteger;

/**
* A Google Cloud DNS Zone object.
Expand All @@ -39,7 +37,7 @@ public class Zone implements Serializable {

// TODO(mderka) Zone and zoneInfo to be merged. Opened issue #605.

private static final long serialVersionUID = -4012581571095484813L;
private static final long serialVersionUID = 6847890192129375500L;
private final ZoneInfo zoneInfo;
private final Dns dns;

Expand Down Expand Up @@ -68,166 +66,77 @@ public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... option
}

/**
* Constructs a {@code Zone} object that contains information received from the Google Cloud DNS
* service for the provided {@code zoneId}.
*
* @param zoneId ID of the zone to be searched for
* @param options optional restriction on what fields should be returned by the service
* @return zone object containing zone's information or {@code null} if not not found
* @throws DnsException upon failure
*/
public static Zone get(Dns dnsService, BigInteger zoneId, Dns.ZoneOption... options) {
checkNotNull(zoneId);
checkNotNull(dnsService);
ZoneInfo zoneInfo = dnsService.getZone(zoneId, options);
return zoneInfo == null ? null : new Zone(dnsService, zoneInfo);
}

/**
* Retrieves the latest information about the zone. The method first attempts to retrieve the zone
* by ID and if not set or zone is not found, it searches by name.
* Retrieves the latest information about the zone. The method retrieves the zone by name.
*
* @param options optional restriction on what fields should be fetched
* @return zone object containing updated information or {@code null} if not not found
* @throws DnsException upon failure
* @throws NullPointerException if both zone ID and name are not initialized
*/
public Zone reload(Dns.ZoneOption... options) {
checkNameOrIdNotNull();
Zone zone = null;
if (zoneInfo.id() != null) {
zone = Zone.get(dns, zoneInfo.id(), options);
}
if (zone == null && zoneInfo.name() != null) {
// zone was not found by id or id is not set at all
zone = Zone.get(dns, zoneInfo.name(), options);
}
return zone;
return Zone.get(dns, zoneInfo.name(), options);
}

/**
* Deletes the zone. The method first attempts to delete the zone by ID. If the zone is not found
* or id is not set, it attempts to delete by name.
* Deletes the zone. The method deletes the zone by name.
*
* @return {@code true} is zone was found and deleted and {@code false} otherwise
* @throws DnsException upon failure
* @throws NullPointerException if both zone ID and name are not initialized
*/
public boolean delete() {
checkNameOrIdNotNull();
boolean deleted = false;
if (zoneInfo.id() != null) {
deleted = dns.delete(zoneInfo.id());
}
if (!deleted && zoneInfo.name() != null) {
// zone was not found by id or id is not set at all
deleted = dns.delete(zoneInfo.name());
}
return deleted;
return dns.delete(zoneInfo.name());
}

/**
* Lists all {@link DnsRecord}s associated with this zone. First searches for zone by ID and if
* not found then by name.
* Lists all {@link DnsRecord}s associated with this zone. The method searches for zone by name.
*
* @param options optional restriction on listing and fields of {@link DnsRecord}s returned
* @return a page of DNS records
* @throws DnsException upon failure or if the zone is not found
* @throws NullPointerException if both zone ID and name are not initialized
*/
public Page<DnsRecord> listDnsRecords(Dns.DnsRecordListOption... options) {
checkNameOrIdNotNull();
Page<DnsRecord> page = null;
if (zoneInfo.id() != null) {
page = dns.listDnsRecords(zoneInfo.id(), options);
}
if (page == null && zoneInfo.name() != null) {
// zone was not found by id or id is not set at all
page = dns.listDnsRecords(zoneInfo.name(), options);
}
return page;
return dns.listDnsRecords(zoneInfo.name(), options);
}

/**
* Submits {@link ChangeRequest} to the service for it to applied to this zone. First searches for
* the zone by ID and if not found then by name. Returns a {@link ChangeRequest} with
* server-assigned ID or {@code null} if the zone was not found.
* Submits {@link ChangeRequest} to the service for it to applied to this zone. The method
* searches for zone by name.
*
* @param options optional restriction on what fields of {@link ChangeRequest} should be returned
* @return ChangeRequest with server-assigned ID
* @throws DnsException upon failure or if the zone is not found
* @throws NullPointerException if both zone ID and name are not initialized
*/
public ChangeRequest applyChangeRequest(ChangeRequest changeRequest,
Dns.ChangeRequestOption... options) {
checkNameOrIdNotNull();
checkNotNull(changeRequest);
ChangeRequest updated = null;
if (zoneInfo.id() != null) {
updated = dns.applyChangeRequest(zoneInfo.id(), changeRequest, options);
}
if (updated == null && zoneInfo.name() != null) {
// zone was not found by id or id is not set at all
updated = dns.applyChangeRequest(zoneInfo.name(), changeRequest, options);
}
return updated;
return dns.applyChangeRequest(zoneInfo.name(), changeRequest, options);
}

/**
* Retrieves an updated information about a change request previously submitted to be applied to
* this zone. First searches for the zone by ID and if not found then by name. Returns a {@link
* ChangeRequest} if found and {@code null} is the zone or the change request was not found.
* this zone. Returns a {@link ChangeRequest} or {@code null} if the change request was not found.
* Throws {@link DnsException} if the zone is not found.
*
* @param options optional restriction on what fields of {@link ChangeRequest} should be returned
* @return updated ChangeRequest
* @throws DnsException upon failure or if the zone is not found
* @throws NullPointerException if both zone ID and name are not initialized
* @throws NullPointerException if the change request does not have initialized id
* @throws NullPointerException if {@code changeRequestId} is null
*/
public ChangeRequest getChangeRequest(String changeRequestId,
Dns.ChangeRequestOption... options) {
checkNameOrIdNotNull();
checkNotNull(changeRequestId);
ChangeRequest updated = null;
if (zoneInfo.id() != null) {
updated = dns.getChangeRequest(zoneInfo.id(), changeRequestId, options);
}
if (updated == null && zoneInfo.name() != null) {
// zone was not found by id or id is not set at all
updated = dns.getChangeRequest(zoneInfo.name(), changeRequestId, options);
}
return updated;
return dns.getChangeRequest(zoneInfo.name(), changeRequestId, options);
}

/**
* Retrieves all change requests for this zone. First searches for the zone by ID and if not found
* then by name. Returns a page of {@link ChangeRequest}s or {@code null} if the zone is not
* found.
* Retrieves all change requests for this zone. The method searches for zone by name. Returns a
* page of {@link ChangeRequest}s.
*
* @param options optional restriction on listing and fields to be returned
* @return a page of change requests
* @throws DnsException upon failure or if the zone is not found
* @throws NullPointerException if both zone ID and name are not initialized
*/
public Page<ChangeRequest> listChangeRequests(Dns.ChangeRequestListOption... options) {
checkNameOrIdNotNull();
Page<ChangeRequest> changeRequests = null;
if (zoneInfo.id() != null) {
changeRequests = dns.listChangeRequests(zoneInfo.id(), options);
}
if (changeRequests == null && zoneInfo.name() != null) {
// zone was not found by id or id is not set at all
changeRequests = dns.listChangeRequests(zoneInfo.name(), options);
}
return changeRequests;
}

/**
* Check that at least one of name and ID are initialized and throw and exception if not.
*/
private void checkNameOrIdNotNull() {
checkArgument(zoneInfo != null && (zoneInfo.id() != null || zoneInfo.name() != null),
"Both zoneInfo.id and zoneInfo.name are null. This is should never happen.");
return dns.listChangeRequests(zoneInfo.name(), options);
}

/**
Expand Down
Loading

0 comments on commit b128548

Please sign in to comment.