Skip to content

Commit

Permalink
Makes Zone subclass of ZoneInfo. Fixes googleapis#605.
Browse files Browse the repository at this point in the history
  • Loading branch information
mderka committed Feb 7, 2016
1 parent d722635 commit 5be58b3
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) {
* @see <a href="https://cloud.google.com/dns/api/v1/managedZones/get">Cloud DNS Managed Zones:
* get</a>
*/
ZoneInfo getZone(String zoneName, ZoneOption... options);
Zone getZone(String zoneName, ZoneOption... options);

/**
* Lists the zones inside the project.
Expand Down
121 changes: 98 additions & 23 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 @@ -20,7 +20,8 @@

import com.google.gcloud.Page;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
* A Google Cloud DNS Zone object.
Expand All @@ -33,20 +34,87 @@
* @see <a href="https://cloud.google.com/dns/zones/">Google Cloud DNS managed zone
* documentation</a>
*/
public class Zone implements Serializable {
public class Zone extends ZoneInfo {

// TODO(mderka) Zone and zoneInfo to be merged. Opened issue #605.
private static final long serialVersionUID = 564454483894599281L;
private transient Dns dns;

private static final long serialVersionUID = 6847890192129375500L;
private final ZoneInfo zoneInfo;
private final Dns dns;
/**
* Builder for {@code Zone}.
*/
public static class Builder extends ZoneInfo.Builder {
private final Dns dns;
private final ZoneInfo.BuilderImpl infoBuilder;

private Builder(Zone zone) {
this.dns = zone.dns;
this.infoBuilder = new ZoneInfo.BuilderImpl(zone);
}

@Override
public Builder name(String name) {
infoBuilder.name(name);
return this;
}

@Override
Builder id(String id) {
infoBuilder.id(id);
return this;
}

@Override
Builder creationTimeMillis(long creationTimeMillis) {
infoBuilder.creationTimeMillis(creationTimeMillis);
return this;
}

@Override
public Builder dnsName(String dnsName) {
infoBuilder.dnsName(dnsName);
return this;
}

@Override
public Builder description(String description) {
infoBuilder.description(description);
return this;
}

@Override
public Builder nameServerSet(String nameServerSet) {
infoBuilder.nameServerSet(nameServerSet);
return this;
}

@Override
Builder nameServers(List<String> nameServers) {
infoBuilder.nameServers(nameServers); // infoBuilder makes a copy
return this;
}

@Override
public Zone build() {
return new Zone(dns, infoBuilder);
}
}

Zone(Dns dns, ZoneInfo.BuilderImpl infoBuilder) {
super(infoBuilder);
this.dns = dns;
}

@Override
public Builder toBuilder() {
return new Builder(this);
}

/**
* Constructs a {@code Zone} object that contains the given {@code zoneInfo}.
*/
public Zone(Dns dns, ZoneInfo zoneInfo) {
this.zoneInfo = checkNotNull(zoneInfo);
this.dns = checkNotNull(dns);
super(new BuilderImpl(zoneInfo));
this.dns = dns;
}

/**
Expand All @@ -61,8 +129,7 @@ public Zone(Dns dns, ZoneInfo zoneInfo) {
public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... options) {
checkNotNull(zoneName);
checkNotNull(dnsService);
ZoneInfo zoneInfo = dnsService.getZone(zoneName, options);
return zoneInfo == null ? null : new Zone(dnsService, zoneInfo);
return dnsService.getZone(zoneName, options);
}

/**
Expand All @@ -73,7 +140,7 @@ public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... option
* @throws DnsException upon failure
*/
public Zone reload(Dns.ZoneOption... options) {
return Zone.get(dns, zoneInfo.name(), options);
return dns.getZone(name(), options);
}

/**
Expand All @@ -83,7 +150,7 @@ public Zone reload(Dns.ZoneOption... options) {
* @throws DnsException upon failure
*/
public boolean delete() {
return dns.delete(zoneInfo.name());
return dns.delete(name());
}

/**
Expand All @@ -94,7 +161,7 @@ public boolean delete() {
* @throws DnsException upon failure or if the zone is not found
*/
public Page<DnsRecord> listDnsRecords(Dns.DnsRecordListOption... options) {
return dns.listDnsRecords(zoneInfo.name(), options);
return dns.listDnsRecords(name(), options);
}

/**
Expand All @@ -108,7 +175,7 @@ public Page<DnsRecord> listDnsRecords(Dns.DnsRecordListOption... options) {
public ChangeRequest applyChangeRequest(ChangeRequest changeRequest,
Dns.ChangeRequestOption... options) {
checkNotNull(changeRequest);
return dns.applyChangeRequest(zoneInfo.name(), changeRequest, options);
return dns.applyChangeRequest(name(), changeRequest, options);
}

/**
Expand All @@ -124,7 +191,7 @@ public ChangeRequest applyChangeRequest(ChangeRequest changeRequest,
public ChangeRequest getChangeRequest(String changeRequestId,
Dns.ChangeRequestOption... options) {
checkNotNull(changeRequestId);
return dns.getChangeRequest(zoneInfo.name(), changeRequestId, options);
return dns.getChangeRequest(name(), changeRequestId, options);
}

/**
Expand All @@ -136,14 +203,7 @@ public ChangeRequest getChangeRequest(String changeRequestId,
* @throws DnsException upon failure or if the zone is not found
*/
public Page<ChangeRequest> listChangeRequests(Dns.ChangeRequestListOption... options) {
return dns.listChangeRequests(zoneInfo.name(), options);
}

/**
* Returns the {@link ZoneInfo} object containing information about this zone.
*/
public ZoneInfo info() {
return zoneInfo;
return dns.listChangeRequests(name(), options);
}

/**
Expand All @@ -152,4 +212,19 @@ public ZoneInfo info() {
public Dns dns() {
return this.dns;
}

@Override
public boolean equals(Object obj) {
return obj instanceof Zone && Objects.equals(toPb(), ((Zone) obj).toPb());
}

@Override
public int hashCode() {
return super.hashCode();
}

static Zone fromPb(Dns dns, com.google.api.services.dns.model.ManagedZone zone) {
ZoneInfo info = ZoneInfo.fromPb(zone);
return new Zone(dns, info);
}
}
98 changes: 62 additions & 36 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,55 @@ public class ZoneInfo implements Serializable {
private final List<String> nameServers;

/**
* A builder for {@code ZoneInfo}.
* Builder for {@code ZoneInfo}.
*/
public static class Builder {
public abstract static class Builder {
/**
* Sets a mandatory user-provided name for the zone. It must be unique within the project.
*/
public abstract Builder name(String name);

/**
* Sets an id for the zone which is assigned to the zone by the server.
*/
abstract Builder id(String id);

/**
* Sets the time when this zone was created.
*/
abstract Builder creationTimeMillis(long creationTimeMillis);

/**
* Sets a mandatory DNS name of this zone, for instance "example.com.".
*/
public abstract Builder dnsName(String dnsName);

/**
* Sets a mandatory description for this zone. The value is a string of at most 1024 characters
* which has no effect on the zone's function.
*/
public abstract Builder description(String description);

/**
* Optionally specifies the NameServerSet for this zone. A NameServerSet is a set of DNS name
* servers that all host the same zones. Most users will not need to specify this value.
*/
public abstract Builder nameServerSet(String nameServerSet);
// todo(mderka) add more to the doc when questions are answered by the service owner

/**
* Sets a list of servers that hold the information about the zone. This information is provided
* by Google Cloud DNS and is read only.
*/
abstract Builder nameServers(List<String> nameServers);

/**
* Builds the instance of {@code ZoneInfo} based on the information set by this builder.
*/
public abstract ZoneInfo build();
}

public static class BuilderImpl extends Builder {
private String name;
private String id;
private Long creationTimeMillis;
Expand All @@ -60,14 +106,14 @@ public static class Builder {
private String nameServerSet;
private List<String> nameServers = new LinkedList<>();

private Builder(String name) {
private BuilderImpl(String name) {
this.name = checkNotNull(name);
}

/**
* Creates a builder from an existing ZoneInfo object.
*/
Builder(ZoneInfo info) {
BuilderImpl(ZoneInfo info) {
this.name = info.name;
this.id = info.id;
this.creationTimeMillis = info.creationTimeMillis;
Expand All @@ -77,76 +123,56 @@ private Builder(String name) {
this.nameServers.addAll(info.nameServers);
}

/**
* Sets a mandatory user-provided name for the zone. It must be unique within the project.
*/
@Override
public Builder name(String name) {
this.name = checkNotNull(name);
return this;
}

/**
* Sets an id for the zone which is assigned to the zone by the server.
*/
@Override
Builder id(String id) {
this.id = id;
return this;
}

/**
* Sets the time when this zone was created.
*/
@Override
Builder creationTimeMillis(long creationTimeMillis) {
this.creationTimeMillis = creationTimeMillis;
return this;
}

/**
* Sets a mandatory DNS name of this zone, for instance "example.com.".
*/
@Override
public Builder dnsName(String dnsName) {
this.dnsName = checkNotNull(dnsName);
return this;
}

/**
* Sets a mandatory description for this zone. The value is a string of at most 1024 characters
* which has no effect on the zone's function.
*/
@Override
public Builder description(String description) {
this.description = checkNotNull(description);
return this;
}

/**
* Optionally specifies the NameServerSet for this zone. A NameServerSet is a set of DNS name
* servers that all host the same zones. Most users will not need to specify this value.
*/
@Override
public Builder nameServerSet(String nameServerSet) {
// todo(mderka) add more to the doc when questions are answered by the service owner
this.nameServerSet = checkNotNull(nameServerSet);
return this;
}

/**
* Sets a list of servers that hold the information about the zone. This information is provided
* by Google Cloud DNS and is read only.
*/
@Override
Builder nameServers(List<String> nameServers) {
checkNotNull(nameServers);
this.nameServers = Lists.newLinkedList(nameServers);
return this;
}

/**
* Builds the instance of {@code ZoneInfo} based on the information set by this builder.
*/
@Override
public ZoneInfo build() {
return new ZoneInfo(this);
}
}

private ZoneInfo(Builder builder) {
ZoneInfo(BuilderImpl builder) {
this.name = builder.name;
this.id = builder.id;
this.creationTimeMillis = builder.creationTimeMillis;
Expand All @@ -160,7 +186,7 @@ private ZoneInfo(Builder builder) {
* Returns a builder for {@code ZoneInfo} with an assigned {@code name}.
*/
public static Builder builder(String name) {
return new Builder(name);
return new BuilderImpl(name);
}

/**
Expand Down Expand Up @@ -217,7 +243,7 @@ public List<String> nameServers() {
* Returns a builder for {@code ZoneInfo} prepopulated with the metadata of this zone.
*/
public Builder toBuilder() {
return new Builder(this);
return new BuilderImpl(this);
}

com.google.api.services.dns.model.ManagedZone toPb() {
Expand All @@ -240,7 +266,7 @@ com.google.api.services.dns.model.ManagedZone toPb() {
}

static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) {
Builder builder = new Builder(pb.getName());
Builder builder = new BuilderImpl(pb.getName());
if (pb.getDescription() != null) {
builder.description(pb.getDescription());
}
Expand Down
Loading

0 comments on commit 5be58b3

Please sign in to comment.