diff options
author | Harald Musum <musum@verizonmedia.com> | 2019-05-14 12:00:54 +0200 |
---|---|---|
committer | Harald Musum <musum@verizonmedia.com> | 2019-05-14 12:00:54 +0200 |
commit | b4415a853699322f051e0d88c6592a34bc8d56d3 (patch) | |
tree | fff3ace08647ede1806d58c6dc0add0b2486f15e /config-provisioning/src | |
parent | df5d867f2222cdd90897cfb84156982a8309db3b (diff) |
Add methods for creating ZoneId with system
Support deserializing zone id which includes cloud and system
Diffstat (limited to 'config-provisioning/src')
-rw-r--r-- | config-provisioning/src/main/java/com/yahoo/config/provision/zone/ZoneId.java | 41 | ||||
-rw-r--r-- | config-provisioning/src/test/java/com/yahoo/config/provision/ZoneIdTest.java | 57 |
2 files changed, 92 insertions, 6 deletions
diff --git a/config-provisioning/src/main/java/com/yahoo/config/provision/zone/ZoneId.java b/config-provisioning/src/main/java/com/yahoo/config/provision/zone/ZoneId.java index 7eeae10eeb8..d839c65574e 100644 --- a/config-provisioning/src/main/java/com/yahoo/config/provision/zone/ZoneId.java +++ b/config-provisioning/src/main/java/com/yahoo/config/provision/zone/ZoneId.java @@ -4,6 +4,7 @@ package com.yahoo.config.provision.zone; import com.yahoo.config.provision.CloudName; import com.yahoo.config.provision.Environment; import com.yahoo.config.provision.RegionName; +import com.yahoo.config.provision.SystemName; import java.util.Objects; @@ -17,24 +18,32 @@ import java.util.Objects; public class ZoneId { // TODO: Replace usages of environment + region with usages of this. + // TODO: Remove static factory methods not specifying cloud and system + private final Environment environment; private final RegionName region; private final CloudName cloud; + private final SystemName system; - private ZoneId(Environment environment, RegionName region, CloudName cloud) { + private ZoneId(Environment environment, RegionName region, CloudName cloud, SystemName system) { this.environment = Objects.requireNonNull(environment, "environment must be non-null"); this.region = Objects.requireNonNull(region, "region must be non-null"); this.cloud = Objects.requireNonNull(cloud, "cloud must be non-null"); + this.system = Objects.requireNonNull(system, "system must be non-null"); } private ZoneId(Environment environment, RegionName region) { - this(environment, region, CloudName.defaultName()); + this(environment, region, CloudName.defaultName(), SystemName.defaultSystem()); } public static ZoneId from(Environment environment, RegionName region) { return new ZoneId(environment, region); } + public static ZoneId from(Environment environment, RegionName region, CloudName cloud, SystemName system) { + return new ZoneId(environment, region, cloud, system); + } + public static ZoneId from(String environment, String region) { return from(Environment.from(environment), RegionName.from(region)); } @@ -42,15 +51,26 @@ public class ZoneId { /** Create from a serialised ZoneId. Inverse of {@code ZoneId.value()}. */ public static ZoneId from(String value) { String[] parts = value.split("\\."); - return from(parts[0], parts[1]); + switch (parts.length) { + case 2: + return from(parts[0], parts[1]); + case 4: + return from(parts[2], parts[3], parts[0], parts[1]); + default: + throw new IllegalArgumentException("Cannot deserialize zone id '" + value + "'"); + } } public static ZoneId from(Environment environment, RegionName region, CloudName cloud) { - return new ZoneId(environment, region, cloud); + return new ZoneId(environment, region, cloud, SystemName.defaultSystem()); } public static ZoneId from(String environment, String region, String cloud) { - return new ZoneId(Environment.from(environment), RegionName.from(region), CloudName.from(cloud)); + return new ZoneId(Environment.from(environment), RegionName.from(region), CloudName.from(cloud), SystemName.defaultSystem()); + } + + public static ZoneId from(String environment, String region, String cloud, String system) { + return new ZoneId(Environment.from(environment), RegionName.from(region), CloudName.from(cloud), SystemName.from(system)); } public Environment environment() { @@ -65,17 +85,26 @@ public class ZoneId { return cloud; } + public SystemName system() { + return system; + } + /** Returns the serialised value of this. Inverse of {@code ZoneId.from(String value)}. */ public String value() { return environment + "." + region; + // TODO: Change to the below when there only methods use constructor including cloud and system are used and + // all serialized values contain cloud and system + // return cloud + "." + system + "." + environment + "." + region; } @Override public String toString() { - return "zone " + value() + " in " + cloud; + // TODO: Use value() instead for last parts of statement + return "zone " + cloud + "." + system + "." + environment + "." + region; } @Override + // TODO: Update to check cloud and system when everyone use methods that specify cloud and system public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; diff --git a/config-provisioning/src/test/java/com/yahoo/config/provision/ZoneIdTest.java b/config-provisioning/src/test/java/com/yahoo/config/provision/ZoneIdTest.java new file mode 100644 index 00000000000..a546d429291 --- /dev/null +++ b/config-provisioning/src/test/java/com/yahoo/config/provision/ZoneIdTest.java @@ -0,0 +1,57 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.provision; + +import com.yahoo.config.provision.zone.ZoneId; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; + +/** + * @author hmusum + */ +public class ZoneIdTest { + + private static final Environment environment = Environment.prod; + private static final RegionName region = RegionName.from("moon-dark-side-1"); + private static final CloudName cloud = CloudName.from("aws"); + private static final SystemName system = SystemName.Public; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void testCreatingZoneId() { + ZoneId zoneId = ZoneId.from(environment, region); + assertEquals(region, zoneId.region()); + assertEquals(environment, zoneId.environment()); + assertEquals(CloudName.defaultName(), zoneId.cloud()); + assertEquals(SystemName.defaultSystem(), zoneId.system()); + + ZoneId zoneIdWithCloudAndSystem = ZoneId.from(environment, region, cloud, system); + assertEquals(region, zoneIdWithCloudAndSystem.region()); + assertEquals(environment, zoneIdWithCloudAndSystem.environment()); + assertEquals(cloud, zoneIdWithCloudAndSystem.cloud()); + assertEquals(system, zoneIdWithCloudAndSystem.system()); + } + + @Test + public void testSerializingAndDeserializing() { + ZoneId zoneId = ZoneId.from(environment, region); + assertEquals(environment.value() + "." + region.value(), zoneId.value()); + assertEquals(ZoneId.from(zoneId.value()), zoneId); + + ZoneId zoneIdWithCloudAndSystem = ZoneId.from(environment, region, cloud, system); + assertEquals(environment.value() + "." + region.value(), zoneIdWithCloudAndSystem.value()); + assertEquals(ZoneId.from(zoneIdWithCloudAndSystem.value()), zoneIdWithCloudAndSystem); + // TODO: Expect cloud and system to be part of deserialized value when the new format is supported everywhere + //assertEquals(cloud.value() + "." + system.name() + "." + environment.value() + "." + region.value() , zoneId.value()); + + String serializedZoneId = "some.illegal.value"; + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Cannot deserialize zone id '" + serializedZoneId + "'"); + ZoneId.from(serializedZoneId); + } + +} |