Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
feat: add supports of labels while creating managed zone (#289)
Browse files Browse the repository at this point in the history
* feat: add supports of labels while creating manazed zone

* fix: package import and code cleanup

* chore: fix compilation error from merge

* chore: fix lint

Co-authored-by: Jeff Ching <chingor@google.com>
  • Loading branch information
athakor and chingor13 committed Dec 28, 2020
1 parent b3bdbd1 commit 47b687f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
9 changes: 9 additions & 0 deletions clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- see https://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html -->
<differences>
<difference>
<className>com/google/cloud/dns/ZoneInfo$Builder</className>
<method>com.google.cloud.dns.ZoneInfo$Builder setLabels(java.util.Map)</method>
<differenceType>7013</differenceType>
</difference>
</differences>
3 changes: 2 additions & 1 deletion src/main/java/com/google/cloud/dns/Dns.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ enum ZoneField implements FieldSelector {
NAME("name"),
NAME_SERVER_SET("nameServerSet"),
NAME_SERVERS("nameServers"),
DNSSEC("dnssecConfig");
DNSSEC("dnssecConfig"),
LABELS("labels");

static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(NAME);

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/google/cloud/dns/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
Expand Down Expand Up @@ -100,6 +101,12 @@ public Builder setDnsSecConfig(DnsSecConfig dnsSecConfig) {
return this;
}

@Override
public Builder setLabels(Map<String, String> labels) {
infoBuilder.setLabels(labels);
return this;
}

@Override
public Zone build() {
return new Zone(dns, infoBuilder);
Expand Down
44 changes: 43 additions & 1 deletion src/main/java/com/google/cloud/dns/ZoneInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.transform;

import com.google.api.client.util.Data;
import com.google.api.services.dns.model.DnsKeySpec;
import com.google.api.services.dns.model.ManagedZone;
import com.google.api.services.dns.model.ManagedZoneDnsSecConfig;
Expand All @@ -27,9 +28,11 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.threeten.bp.Instant;
Expand All @@ -56,6 +59,7 @@ public class ZoneInfo implements Serializable {
private final String nameServerSet;
private final List<String> nameServers;
private final DnsSecConfig dnsSecConfig;
private final Map<String, String> labels;

/** This class represents the DNS key spec. */
public static class KeySpec {
Expand Down Expand Up @@ -379,6 +383,11 @@ public Builder setDnsSecConfig(DnsSecConfig dnsSecConfig) {
return this;
}

/** Sets the label of this zone. */
public Builder setLabels(Map<String, String> labels) {
return this;
}

/** Builds the instance of {@code ZoneInfo} based on the information set by this builder. */
public abstract ZoneInfo build();
}
Expand All @@ -392,6 +401,7 @@ static class BuilderImpl extends Builder {
private String nameServerSet;
private List<String> nameServers;
private DnsSecConfig dnsSecConfig;
private Map<String, String> labels;

private BuilderImpl(String name) {
this.name = checkNotNull(name);
Expand All @@ -409,6 +419,7 @@ private BuilderImpl(String name) {
this.nameServers = ImmutableList.copyOf(info.nameServers);
}
this.dnsSecConfig = info.dnsSecConfig;
this.labels = info.labels;
}

@Override
Expand Down Expand Up @@ -460,6 +471,23 @@ public Builder setDnsSecConfig(DnsSecConfig dnsSecConfig) {
return this;
}

@Override
public Builder setLabels(Map<String, String> labels) {
if (labels != null) {
this.labels =
Maps.transformValues(
labels,
new Function<String, String>() {
@Override
public String apply(String input) {
// replace null values with empty strings
return input == null ? Data.<String>nullOf(String.class) : input;
}
});
}
return this;
}

@Override
public ZoneInfo build() {
return new ZoneInfo(this);
Expand All @@ -476,6 +504,7 @@ public ZoneInfo build() {
this.nameServers =
builder.nameServers == null ? null : ImmutableList.copyOf(builder.nameServers);
this.dnsSecConfig = builder.dnsSecConfig;
this.labels = builder.labels;
}

/**
Expand Down Expand Up @@ -523,6 +552,11 @@ public String getNameServerSet() {
return nameServerSet;
}

/** Returns the labels for this zone. */
public Map<String, String> getLabels() {
return labels;
}

/**
* The nameservers that the zone should be delegated to. This is defined by the Google DNS cloud.
*/
Expand Down Expand Up @@ -556,6 +590,9 @@ ManagedZone toPb() {
if (this.dnsSecConfig != null) {
pb.setDnssecConfig(this.dnsSecConfig.toPb());
}
if (this.getLabels() != null) {
pb.setLabels(labels);
}
return pb;
}

Expand Down Expand Up @@ -583,6 +620,9 @@ static ZoneInfo fromPb(ManagedZone pb) {
if (pb.getDnssecConfig() != null) {
builder.setDnsSecConfig(DnsSecConfig.fromPb(pb.getDnssecConfig()));
}
if (pb.getLabels() != null) {
builder.setLabels(pb.getLabels());
}
return builder.build();
}

Expand All @@ -604,7 +644,8 @@ public int hashCode() {
description,
nameServerSet,
nameServers,
dnsSecConfig);
dnsSecConfig,
labels);
}

@Override
Expand All @@ -618,6 +659,7 @@ public String toString() {
.add("nameServers", getNameServers())
.add("creationTimeMillis", getCreationTimeMillis())
.add("dnsSecConfig", getDnsSecConfig())
.add("labels", getLabels())
.toString();
}
}
6 changes: 6 additions & 0 deletions src/test/java/com/google/cloud/dns/ZoneInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

import com.google.api.services.dns.model.ManagedZone;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.junit.Test;

public class ZoneInfoTest {
Expand All @@ -40,6 +42,8 @@ public class ZoneInfoTest {
private static final String NS2 = "name server 2";
private static final String NS3 = "name server 3";
private static final List<String> NAME_SERVERS = ImmutableList.of(NS1, NS2, NS3);
private static final Map<String, String> LABELS =
ImmutableMap.of("label1", "value1", "label2", "value2");
private static final String ALGORITHM = "rsasha256";
private static final String KEY_TYPE1 = "zoneSigning";
private static final String KEY_TYPE2 = "keySigning";
Expand Down Expand Up @@ -75,6 +79,7 @@ public class ZoneInfoTest {
.setNameServerSet(NAME_SERVER_SET)
.setNameServers(NAME_SERVERS)
.setDnsSecConfig(DNS_SEC_CONFIG)
.setLabels(LABELS)
.build();

@Test
Expand Down Expand Up @@ -102,6 +107,7 @@ public void testBuilder() {
assertEquals(DESCRIPTION, INFO.getDescription());
assertEquals(DNS_NAME, INFO.getDnsName());
assertEquals(DNS_SEC_CONFIG, INFO.getDnsSecConfig());
assertEquals(LABELS, INFO.getLabels());

ZoneInfo.DnsSecConfig config = INFO.getDnsSecConfig();
assertEquals(DEFAULT_KEY_SPECS, config.getDefaultKeySpecs());
Expand Down
20 changes: 18 additions & 2 deletions src/test/java/com/google/cloud/dns/it/ITDnsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
import com.google.cloud.dns.Zone;
import com.google.cloud.dns.ZoneInfo;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
Expand All @@ -64,6 +66,8 @@ public class ITDnsTest {
private static final String ZONE_DNS_NAME1 = ZONE_NAME1 + ".com.";
private static final String ZONE_DNS_EMPTY_DESCRIPTION = ZONE_NAME_EMPTY_DESCRIPTION + ".com.";
private static final String ZONE_DNS_NAME_NO_PERIOD = ZONE_NAME1 + ".com";
private static final Map<String, String> LABELS =
ImmutableMap.of("label1", "value1", "label2", "value2");

private static final String ALGORITHM = "rsasha256";
private static final String KEY_TYPE1 = "zoneSigning";
Expand Down Expand Up @@ -97,14 +101,14 @@ public class ITDnsTest {
.setDnsName(ZONE_DNS_EMPTY_DESCRIPTION)
.setDescription(ZONE_DESCRIPTION1)
.setDnsSecConfig(DNS_SEC_CONFIG)
.setLabels(LABELS)
.build();
private static final ZoneInfo ZONE_DNSSEC =
ZoneInfo.newBuilder(ZONE_NAME1)
.setDnsName(ZONE_DNS_NAME1)
.setDescription(ZONE_DESCRIPTION1)
.setDnsSecConfig(DNS_SEC_CONFIG)
.build();

private static final ZoneInfo ZONE_EMPTY_DESCRIPTION =
ZoneInfo.of(ZONE_NAME_EMPTY_DESCRIPTION, ZONE_DNS_NAME1, ZONE_DESCRIPTION1);
private static final ZoneInfo ZONE_NAME_ERROR =
Expand Down Expand Up @@ -212,6 +216,7 @@ public void testCreateValidZone() {
assertEquals(ZONE1.getDescription(), created.getDescription());
assertEquals(ZONE1.getDnsName(), created.getDnsName());
assertEquals(ZONE1.getName(), created.getName());
assertEquals(ZONE1.getLabels(), created.getLabels());
assertNotNull(created.getCreationTimeMillis());
assertNotNull(created.getNameServers());
assertNull(created.getNameServerSet());
Expand Down Expand Up @@ -489,6 +494,15 @@ public void testGetZone() {
assertFalse(created.getNameServers().isEmpty());
assertNull(created.getNameServerSet());
assertNull(created.getGeneratedId());
created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.LABELS));
assertEquals(ZONE1.getName(), created.getName()); // always returned
assertNull(created.getCreationTimeMillis());
assertNull(created.getDnsName());
assertNull(created.getDescription());
assertTrue(created.getNameServers().isEmpty());
assertNull(created.getNameServerSet());
assertEquals(LABELS, created.getLabels());
assertNull(created.getGeneratedId());
created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.ZONE_ID));
assertEquals(ZONE1.getName(), created.getName()); // always returned
assertNull(created.getCreationTimeMillis());
Expand Down Expand Up @@ -517,14 +531,16 @@ public void testGetZone() {
ZoneField.ZONE_ID,
ZoneField.NAME_SERVERS,
ZoneField.NAME_SERVER_SET,
ZoneField.DESCRIPTION));
ZoneField.DESCRIPTION,
ZoneField.LABELS));
assertEquals(ZONE1.getName(), created.getName()); // always returned
assertNull(created.getCreationTimeMillis());
assertNull(created.getDnsName());
assertEquals(ZONE1.getDescription(), created.getDescription());
assertFalse(created.getNameServers().isEmpty());
assertNull(created.getNameServerSet()); // we did not set it
assertNotNull(created.getGeneratedId());
assertEquals(ZONE1.getLabels(), created.getLabels());
} finally {
DNS.delete(ZONE1.getName());
}
Expand Down

0 comments on commit 47b687f

Please sign in to comment.