Skip to content

Commit

Permalink
Modified ttl to accept time unit. Fixed googleapis#581.
Browse files Browse the repository at this point in the history
  • Loading branch information
mderka committed Jan 28, 2016
1 parent 81a46d8 commit c364ff3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
30 changes: 20 additions & 10 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* 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 zone. You can read the records but you cannot modify them directly.
* Rather, you edit the records in a zone by creating a ChangeRequest.
* that make up a zone. You can read the records but you cannot modify them directly. Rather, you
* edit the records in a zone by creating a ChangeRequest.
*
* @see <a href="https://cloud.google.com/dns/api/v1/resourceRecordSets">Google Cloud DNS
* documentation</a>
Expand All @@ -44,7 +45,7 @@ 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 Integer ttl; // this is in seconds
private final Type type;

/**
Expand Down Expand Up @@ -176,14 +177,23 @@ public Builder name(String name) {
}

/**
* Sets the number of seconds that this record can be cached by resolvers. This number must be
* non-negative.
* Sets the time that this record can be cached by resolvers. This number must be non-negative.
* The maximum duration must be equivalent to at most {@link Integer#MAX_VALUE} seconds.
*
* @param ttl A non-negative number of seconds
* @param duration A non-negative number of time units
* @param unit The unit of the ttl parameter
*/
public Builder ttl(int ttl) {
checkArgument(ttl >= 0, "TTL cannot be negative. The supplied value was %s.", ttl);
this.ttl = ttl;
public Builder ttl(int duration, TimeUnit unit) {
checkArgument(duration >= 0,
"Duration cannot be negative. The supplied value was %s.", duration);
checkNotNull(unit);
// convert to seconds and check that we are not overflowing int
// we cannot do that because pb does not support it
long converted = unit.toSeconds(duration);
checkArgument(converted <= Integer.MAX_VALUE,
"The duration converted to seconds is out of range of int. The value converts to %s sec.",
converted);
ttl = (int) converted;
return this;
}

Expand Down Expand Up @@ -278,7 +288,7 @@ static DnsRecord fromPb(com.google.api.services.dns.model.ResourceRecordSet pb)
builder.records(pb.getRrdatas());
}
if (pb.getTtl() != null) {
builder.ttl(pb.getTtl());
builder.ttl(pb.getTtl(), TimeUnit.SECONDS);
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@

package com.google.gcloud.dns;

import static com.google.gcloud.dns.DnsRecord.builder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class DnsRecordTest {

private static final String NAME = "example.com.";
private static final Integer TTL = 3600;
private static final TimeUnit UNIT = TimeUnit.HOURS;
private static final Integer UNIT_TTL = 1;
private static final DnsRecord.Type TYPE = DnsRecord.Type.AAAA;
private static final DnsRecord record = DnsRecord.builder(NAME, TYPE)
.ttl(TTL)
private static final DnsRecord record = builder(NAME, TYPE)
.ttl(UNIT_TTL, UNIT)
.build();

@Test
public void testDefaultDnsRecord() {
DnsRecord record = DnsRecord.builder(NAME, TYPE).build();
DnsRecord record = builder(NAME, TYPE).build();
assertEquals(0, record.records().size());
assertEquals(TYPE, record.type());
assertEquals(NAME, record.name());
Expand All @@ -61,13 +66,21 @@ public void testBuilder() {
@Test
public void testValidTtl() {
try {
DnsRecord.builder(NAME, TYPE).ttl(-1);
builder(NAME, TYPE).ttl(-1, TimeUnit.SECONDS);
fail("A negative value is not acceptable for ttl.");
} catch (IllegalArgumentException e) {
// expected
}
DnsRecord.builder(NAME, TYPE).ttl(0);
DnsRecord.builder(NAME, TYPE).ttl(Integer.MAX_VALUE);
builder(NAME, TYPE).ttl(0, TimeUnit.SECONDS);
builder(NAME, TYPE).ttl(Integer.MAX_VALUE, TimeUnit.SECONDS);
try {
builder(NAME, TYPE).ttl(Integer.MAX_VALUE, TimeUnit.HOURS);
fail("This value is too large for int.");
} catch (IllegalArgumentException e) {
// expected
}
DnsRecord record = DnsRecord.builder(NAME, TYPE).ttl(UNIT_TTL, UNIT).build();
assertEquals(TTL, record.ttl());
}

@Test
Expand All @@ -79,7 +92,7 @@ public void testEqualsAndNotEquals() {
String differentName = "totally different name";
clone = record.toBuilder().name(differentName).build();
assertNotEquals(record, clone);
clone = record.toBuilder().ttl(record.ttl() + 1).build();
clone = record.toBuilder().ttl(record.ttl() + 1, TimeUnit.SECONDS).build();
assertNotEquals(record, clone);
clone = record.toBuilder().type(DnsRecord.Type.TXT).build();
assertNotEquals(record, clone);
Expand All @@ -95,22 +108,22 @@ public void testSameHashCodeOnEquals() {
@Test
public void testToAndFromPb() {
assertEquals(record, DnsRecord.fromPb(record.toPb()));
DnsRecord partial = DnsRecord.builder(NAME, TYPE).build();
DnsRecord partial = builder(NAME, TYPE).build();
assertEquals(partial, DnsRecord.fromPb(partial.toPb()));
partial = DnsRecord.builder(NAME, TYPE).addRecord("test").build();
partial = builder(NAME, TYPE).addRecord("test").build();
assertEquals(partial, DnsRecord.fromPb(partial.toPb()));
partial = DnsRecord.builder(NAME, TYPE).ttl(15).build();
partial = builder(NAME, TYPE).ttl(15, TimeUnit.SECONDS).build();
assertEquals(partial, DnsRecord.fromPb(partial.toPb()));
}

@Test
public void testToBuilder() {
assertEquals(record, record.toBuilder().build());
DnsRecord partial = DnsRecord.builder(NAME, TYPE).build();
DnsRecord partial = builder(NAME, TYPE).build();
assertEquals(partial, partial.toBuilder().build());
partial = DnsRecord.builder(NAME, TYPE).addRecord("test").build();
partial = builder(NAME, TYPE).addRecord("test").build();
assertEquals(partial, partial.toBuilder().build());
partial = DnsRecord.builder(NAME, TYPE).ttl(15).build();
partial = builder(NAME, TYPE).ttl(15, TimeUnit.SECONDS).build();
assertEquals(partial, partial.toBuilder().build());
}

Expand Down

0 comments on commit c364ff3

Please sign in to comment.