Skip to content

Commit 59df1eb

Browse files
committed
---
yaml --- r: 3069 b: refs/heads/master c: c364ff3 h: refs/heads/master i: 3067: 9582a22
1 parent eb2aafd commit 59df1eb

3 files changed

Lines changed: 47 additions & 24 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 81a46d8429909998bdbf65cdc0726e9a404d62b1
2+
refs/heads/master: c364ff322187be6404c1a9cd0e1673b8f64a7c35
33
refs/heads/travis: e21ee7b88a5edc3f3d8c71f90c3fc32abf7e8dd6
44
refs/heads/gh-pages: 4e0561bb4504bf647db669a14417b2b2c87ba45d
55
refs/heads/pubsub-alpha: 1a0e970f265af871e02274085b9662b3fe29058b

trunk/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
import java.util.LinkedList;
2828
import java.util.List;
2929
import java.util.Objects;
30+
import java.util.concurrent.TimeUnit;
3031

3132
/**
3233
* A class that represents a Google Cloud DNS record set.
3334
*
3435
* <p>A {@code DnsRecord} is the unit of data that will be returned by the DNS servers upon a DNS
3536
* request for a specific domain. The {@code DnsRecord} holds the current state of the DNS records
36-
* that make up a zone. You can read the records but you cannot modify them directly.
37-
* Rather, you edit the records in a zone by creating a ChangeRequest.
37+
* that make up a zone. You can read the records but you cannot modify them directly. Rather, you
38+
* edit the records in a zone by creating a ChangeRequest.
3839
*
3940
* @see <a href="https://cloud.google.com/dns/api/v1/resourceRecordSets">Google Cloud DNS
4041
* documentation</a>
@@ -44,7 +45,7 @@ public class DnsRecord implements Serializable {
4445
private static final long serialVersionUID = 2016011914302204L;
4546
private final String name;
4647
private final List<String> rrdatas;
47-
private final Integer ttl;
48+
private final Integer ttl; // this is in seconds
4849
private final Type type;
4950

5051
/**
@@ -176,14 +177,23 @@ public Builder name(String name) {
176177
}
177178

178179
/**
179-
* Sets the number of seconds that this record can be cached by resolvers. This number must be
180-
* non-negative.
180+
* Sets the time that this record can be cached by resolvers. This number must be non-negative.
181+
* The maximum duration must be equivalent to at most {@link Integer#MAX_VALUE} seconds.
181182
*
182-
* @param ttl A non-negative number of seconds
183+
* @param duration A non-negative number of time units
184+
* @param unit The unit of the ttl parameter
183185
*/
184-
public Builder ttl(int ttl) {
185-
checkArgument(ttl >= 0, "TTL cannot be negative. The supplied value was %s.", ttl);
186-
this.ttl = ttl;
186+
public Builder ttl(int duration, TimeUnit unit) {
187+
checkArgument(duration >= 0,
188+
"Duration cannot be negative. The supplied value was %s.", duration);
189+
checkNotNull(unit);
190+
// convert to seconds and check that we are not overflowing int
191+
// we cannot do that because pb does not support it
192+
long converted = unit.toSeconds(duration);
193+
checkArgument(converted <= Integer.MAX_VALUE,
194+
"The duration converted to seconds is out of range of int. The value converts to %s sec.",
195+
converted);
196+
ttl = (int) converted;
187197
return this;
188198
}
189199

@@ -278,7 +288,7 @@ static DnsRecord fromPb(com.google.api.services.dns.model.ResourceRecordSet pb)
278288
builder.records(pb.getRrdatas());
279289
}
280290
if (pb.getTtl() != null) {
281-
builder.ttl(pb.getTtl());
291+
builder.ttl(pb.getTtl(), TimeUnit.SECONDS);
282292
}
283293
return builder.build();
284294
}

trunk/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsRecordTest.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,30 @@
1616

1717
package com.google.gcloud.dns;
1818

19+
import static com.google.gcloud.dns.DnsRecord.builder;
1920
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assert.assertNotEquals;
2122
import static org.junit.Assert.assertTrue;
2223
import static org.junit.Assert.fail;
2324

2425
import org.junit.Test;
2526

27+
import java.util.concurrent.TimeUnit;
28+
2629
public class DnsRecordTest {
2730

2831
private static final String NAME = "example.com.";
2932
private static final Integer TTL = 3600;
33+
private static final TimeUnit UNIT = TimeUnit.HOURS;
34+
private static final Integer UNIT_TTL = 1;
3035
private static final DnsRecord.Type TYPE = DnsRecord.Type.AAAA;
31-
private static final DnsRecord record = DnsRecord.builder(NAME, TYPE)
32-
.ttl(TTL)
36+
private static final DnsRecord record = builder(NAME, TYPE)
37+
.ttl(UNIT_TTL, UNIT)
3338
.build();
3439

3540
@Test
3641
public void testDefaultDnsRecord() {
37-
DnsRecord record = DnsRecord.builder(NAME, TYPE).build();
42+
DnsRecord record = builder(NAME, TYPE).build();
3843
assertEquals(0, record.records().size());
3944
assertEquals(TYPE, record.type());
4045
assertEquals(NAME, record.name());
@@ -61,13 +66,21 @@ public void testBuilder() {
6166
@Test
6267
public void testValidTtl() {
6368
try {
64-
DnsRecord.builder(NAME, TYPE).ttl(-1);
69+
builder(NAME, TYPE).ttl(-1, TimeUnit.SECONDS);
6570
fail("A negative value is not acceptable for ttl.");
6671
} catch (IllegalArgumentException e) {
6772
// expected
6873
}
69-
DnsRecord.builder(NAME, TYPE).ttl(0);
70-
DnsRecord.builder(NAME, TYPE).ttl(Integer.MAX_VALUE);
74+
builder(NAME, TYPE).ttl(0, TimeUnit.SECONDS);
75+
builder(NAME, TYPE).ttl(Integer.MAX_VALUE, TimeUnit.SECONDS);
76+
try {
77+
builder(NAME, TYPE).ttl(Integer.MAX_VALUE, TimeUnit.HOURS);
78+
fail("This value is too large for int.");
79+
} catch (IllegalArgumentException e) {
80+
// expected
81+
}
82+
DnsRecord record = DnsRecord.builder(NAME, TYPE).ttl(UNIT_TTL, UNIT).build();
83+
assertEquals(TTL, record.ttl());
7184
}
7285

7386
@Test
@@ -79,7 +92,7 @@ public void testEqualsAndNotEquals() {
7992
String differentName = "totally different name";
8093
clone = record.toBuilder().name(differentName).build();
8194
assertNotEquals(record, clone);
82-
clone = record.toBuilder().ttl(record.ttl() + 1).build();
95+
clone = record.toBuilder().ttl(record.ttl() + 1, TimeUnit.SECONDS).build();
8396
assertNotEquals(record, clone);
8497
clone = record.toBuilder().type(DnsRecord.Type.TXT).build();
8598
assertNotEquals(record, clone);
@@ -95,22 +108,22 @@ public void testSameHashCodeOnEquals() {
95108
@Test
96109
public void testToAndFromPb() {
97110
assertEquals(record, DnsRecord.fromPb(record.toPb()));
98-
DnsRecord partial = DnsRecord.builder(NAME, TYPE).build();
111+
DnsRecord partial = builder(NAME, TYPE).build();
99112
assertEquals(partial, DnsRecord.fromPb(partial.toPb()));
100-
partial = DnsRecord.builder(NAME, TYPE).addRecord("test").build();
113+
partial = builder(NAME, TYPE).addRecord("test").build();
101114
assertEquals(partial, DnsRecord.fromPb(partial.toPb()));
102-
partial = DnsRecord.builder(NAME, TYPE).ttl(15).build();
115+
partial = builder(NAME, TYPE).ttl(15, TimeUnit.SECONDS).build();
103116
assertEquals(partial, DnsRecord.fromPb(partial.toPb()));
104117
}
105118

106119
@Test
107120
public void testToBuilder() {
108121
assertEquals(record, record.toBuilder().build());
109-
DnsRecord partial = DnsRecord.builder(NAME, TYPE).build();
122+
DnsRecord partial = builder(NAME, TYPE).build();
110123
assertEquals(partial, partial.toBuilder().build());
111-
partial = DnsRecord.builder(NAME, TYPE).addRecord("test").build();
124+
partial = builder(NAME, TYPE).addRecord("test").build();
112125
assertEquals(partial, partial.toBuilder().build());
113-
partial = DnsRecord.builder(NAME, TYPE).ttl(15).build();
126+
partial = builder(NAME, TYPE).ttl(15, TimeUnit.SECONDS).build();
114127
assertEquals(partial, partial.toBuilder().build());
115128
}
116129

0 commit comments

Comments
 (0)