Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gcloud dns #559

Merged
merged 8 commits into from
Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions gcloud-java-dns/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java-dns</artifactId>
<packaging>jar</packaging>
<name>GCloud Java DNS</name>
<description>
Java idiomatic client for Google Cloud DNS.
</description>
<parent>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java-pom</artifactId>
<version>0.1.3-SNAPSHOT</version>
</parent>
<properties>
<site.installationModule>gcloud-java-dns</site.installationModule>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-dns</artifactId>
<version>v1-rev7-1.21.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
290 changes: 290 additions & 0 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.gcloud.dns;

This comment was marked as spam.

This comment was marked as spam.


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

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

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

/**
* A class that represents a Google Cloud DNS record set.
*
* <p>A {@code DnsRecord} is the unit of data that will be returned by the DNS servers upon a DNS
* request for a specific domain. The {@code DnsRecord} holds the current state of the DNS records
* that make up a managed zone. You can read the records but you do not modify them directly.

This comment was marked as spam.

This comment was marked as spam.

* Rather, you edit the records in a managed zone by creating a ChangeRequest.
*
* @see <a href="https://cloud.google.com/dns/api/v1/resourceRecordSets">Google Cloud DNS
* documentation</a>
*/
public class DnsRecord implements Serializable {

private static final long serialVersionUID = 2016011914302204L;
private final String name;
private final List<String> rrdatas;
private final Integer ttl;
private final DnsRecordType type;

/**
* Enum for the DNS record types supported by Cloud DNS.
*
* <p>Google Cloud DNS currently supports records of type A, AAAA, CNAME, MX NAPTR, NS, PTR, SOA,
* SPF, SRV, TXT.
*
* @see <a href="https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types">Cloud DNS
* supported record types</a>
*/
public enum DnsRecordType {

This comment was marked as spam.

This comment was marked as spam.

/**
* Address record, which is used to map host names to their IPv4 address.
*/
A,

This comment was marked as spam.

This comment was marked as spam.

/**
* IPv6 Address record, which is used to map host names to their IPv6 address.
*/
AAAA,
/**
* Canonical name record, which is used to alias names.
*/
CNAME,
/**
* Mail exchange record, which is used in routing requests to mail servers.
*/
MX,
/**
* Naming authority pointer record, defined by RFC3403.
*/
NAPTR,
/**
* Name server record, which delegates a DNS zone to an authoritative server.
*/
NS,
/**
* Pointer record, which is often used for reverse DNS lookups.
*/
PTR,
/**
* Start of authority record, which specifies authoritative information about a DNS zone.
*/
SOA,
/**
* Sender policy framework record, which is used in email validation systems.
*/
SPF,
/**
* Service locator record, which is used by some voice over IP, instant messaging protocols and
* other applications.
*/
SRV,
/**
* Text record, which can contain arbitrary text and can also be used to define machine readable
* data such as security or abuse prevention information.
*/
TXT
}

public static class Builder {

This comment was marked as spam.

This comment was marked as spam.


private List<String> rrdatas = new LinkedList<>();
private String name;
private Integer ttl;
private DnsRecordType type;

private Builder() {

This comment was marked as spam.

This comment was marked as spam.

}

/**
* Creates a builder and pre-populates attributes with the values from the provided {@code
* DnsRecord} instance.
*/
private Builder(DnsRecord record) {
this.name = record.name;
this.ttl = record.ttl;
this.type = record.type;
this.rrdatas.addAll(record.rrdatas);
}

/**
* Adds a record to the record set. The records should be as defined in RFC 1035 (section 5) and
* RFC 1034 (section 3.6.1). Examples of records are available in Google DNS documentation.
*
* @see <a href="https://cloud.google.com/dns/what-is-cloud-dns#supported_record_types">Google
* DNS documentation </a>.
*/
public Builder addRecord(String record) {
this.rrdatas.add(checkNotNull(record));
return this;
}

/**
* Removes a record from the set. An exact match is required.
*/
public Builder removeRecord(String record) {

This comment was marked as spam.

This comment was marked as spam.

this.rrdatas.remove(checkNotNull(record));
return this;
}

/**
* Removes all the records.
*/
public Builder clearRecords() {

This comment was marked as spam.

This comment was marked as spam.

this.rrdatas.clear();
return this;
}

/**
* Replaces the current records with the provided list of records.
*/
public Builder records(List<String> records) {
this.rrdatas = Lists.newLinkedList(checkNotNull(records));
return this;
}

/**
* Sets name for this DNS record set. For example, www.example.com.
*/
public Builder name(String name) {
this.name = checkNotNull(name);
return this;
}

/**
* Sets the number of seconds that this record can be cached by resolvers. This number must be
* non-negative.
*
* @param ttl A non-negative number of seconds
*/
public Builder ttl(int ttl) {
checkArgument(ttl >= 0, "TTL cannot be negative. The supplied value was " + ttl + ".");

This comment was marked as spam.

This comment was marked as spam.

this.ttl = ttl;
return this;
}

/**
* The identifier of a supported record type, for example, A, AAAA, MX, TXT, and so on.
*/
public Builder type(DnsRecordType type) {
this.type = checkNotNull(type);
return this;
}

/**
* Builds the DNS record.
*/
public DnsRecord build() {
return new DnsRecord(this);
}
}

DnsRecord(Builder builder) {

This comment was marked as spam.

This comment was marked as spam.

this.name = builder.name;
this.rrdatas = ImmutableList.copyOf(builder.rrdatas);
this.ttl = builder.ttl;
this.type = builder.type;
}

/**
* Creates a builder pre-populated with the attribute values of this instance.
*/
public Builder toBuilder() {
return new Builder(this);
}

/**
* Creates a builder for {@code DnsRecord} with mandatorily set name and type of the record.

This comment was marked as spam.

This comment was marked as spam.

*/
public static Builder builder(String name, DnsRecordType type) {
return new Builder().name(name).type(type);
}

/**
* Get the mandatory user assigned name of this DNS record.

This comment was marked as spam.

This comment was marked as spam.

*/
public String name() {
return name;
}

/**
* Returns a list of DNS record stored in this record set.
*/
public List<String> records() {
return rrdatas;
}

/**

This comment was marked as spam.

This comment was marked as spam.

* Returns the number of seconds that this DnsResource can be cached by resolvers.
*/
public Integer ttl() {
return ttl;
}

/**
* Returns the type of this DNS record.
*/
public DnsRecordType type() {
return type;
}

@Override
public int hashCode() {
return Objects.hash(name, rrdatas, ttl, type);
}

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

com.google.api.services.dns.model.ResourceRecordSet toPb() {
com.google.api.services.dns.model.ResourceRecordSet pb =
new com.google.api.services.dns.model.ResourceRecordSet();

This comment was marked as spam.

This comment was marked as spam.

pb.setName(this.name());

This comment was marked as spam.

This comment was marked as spam.

pb.setRrdatas(this.records());

This comment was marked as spam.

This comment was marked as spam.

pb.setTtl(this.ttl());
pb.setType(this.type() == null ? null : this.type().name());

This comment was marked as spam.

This comment was marked as spam.

return pb;
}

static DnsRecord fromPb(com.google.api.services.dns.model.ResourceRecordSet pb) {
Builder b = builder(pb.getName(), DnsRecordType.valueOf(pb.getType()));
if (pb.getRrdatas() != null) {
b.records(pb.getRrdatas());
}
if (pb.getTtl() != null) {
b.ttl(pb.getTtl());
}
return b.build();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", name())

This comment was marked as spam.

This comment was marked as spam.

.add("rrdatas", records())
.add("ttl", ttl())
.add("type", type())
.toString();
}
}
Loading