diff options
author | Jon Bratseth <bratseth@verizonmedia.com> | 2020-03-12 13:51:47 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@verizonmedia.com> | 2020-03-12 13:51:47 +0100 |
commit | 85e39e75026b09a936516c6234b438069b8b1859 (patch) | |
tree | 02c5c7642f8c45f6340ad8b2495a7ecb96f1d854 /container-core | |
parent | 288ce8de5f5ee49c8c91340a86252b38b16ce6f2 (diff) |
Zone information for containers to replace com.yahoo.config.provision
Diffstat (limited to 'container-core')
4 files changed, 150 insertions, 0 deletions
diff --git a/container-core/src/main/java/ai/vespa/cloud/Environment.java b/container-core/src/main/java/ai/vespa/cloud/Environment.java new file mode 100644 index 00000000000..8f1d9fc962a --- /dev/null +++ b/container-core/src/main/java/ai/vespa/cloud/Environment.java @@ -0,0 +1,13 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.cloud; + +/** + * The environments of a Vespa cloud instance + * + * @author bratseth + */ +public enum Environment { + + dev, perf, test, staging, prod + +} diff --git a/container-core/src/main/java/ai/vespa/cloud/SystemInfo.java b/container-core/src/main/java/ai/vespa/cloud/SystemInfo.java index 21577e17170..6fc0e7e8e8a 100644 --- a/container-core/src/main/java/ai/vespa/cloud/SystemInfo.java +++ b/container-core/src/main/java/ai/vespa/cloud/SystemInfo.java @@ -1,5 +1,32 @@ // Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package ai.vespa.cloud; +import com.google.inject.Inject; +import com.yahoo.cloud.config.ConfigserverConfig; + +/** + * Provides information about the system in which this container is running. + * This is available and can be injected when running in a cloud environment. + * + * @author bratseth + */ public class SystemInfo { + + private final Zone zone; + + /** Do not use */ + @SuppressWarnings("unused") + @Inject + public SystemInfo(ConfigserverConfig config) { + this.zone = new Zone(Environment.valueOf(config.environment()), config.region()); + } + + /** Create an instance for testing */ + public SystemInfo(Zone zone) { + this.zone = zone; + } + + /** Returns the zone this is running in */ + public Zone zone() { return zone; } + } diff --git a/container-core/src/main/java/ai/vespa/cloud/Zone.java b/container-core/src/main/java/ai/vespa/cloud/Zone.java new file mode 100644 index 00000000000..48293aa7908 --- /dev/null +++ b/container-core/src/main/java/ai/vespa/cloud/Zone.java @@ -0,0 +1,61 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.cloud; + +import java.util.Objects; + +/** + * The zone in which a cloud deployment may be running. + * A zone is a combination of an environment and a region. + * + * @author bratseth + */ +public class Zone { + + private final Environment environment; + + private final String region; + + public Zone(Environment environment, String region) { + this.environment = environment; + this.region = region; + } + + public Environment environment() { return environment; } + public String region() { return region; } + + /** Returns the string environment.region */ + @Override + public String toString() { return environment + "." + region; } + + @Override + public int hashCode() { return Objects.hash(environment, region); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if ( ! (o instanceof Zone)) return false; + Zone other = (Zone)o; + return this.environment.equals(other.environment) && this.region.equals(other.region); + } + + /** + * Creates a zone from a string on the form environment.region + * + * @throws IllegalArgumentException if the given string is not a valid zone + */ + public static Zone from(String zoneString) { + String[] parts = zoneString.split("\\."); + if (parts.length != 2) + throw new IllegalArgumentException("A zone string must be on the form [environment].[region], but was '" + zoneString + "'"); + + Environment environment; + try { + environment = Environment.valueOf(parts[0]); + } + catch (IllegalArgumentException e) { + throw new IllegalArgumentException("Invalid zone '" + zoneString + "': No environment named '" + parts[0] + "'"); + } + return new Zone(environment, parts[1]); + } + +} diff --git a/container-core/src/test/java/ai/vespa/cloud/SystemInfoTest.java b/container-core/src/test/java/ai/vespa/cloud/SystemInfoTest.java new file mode 100644 index 00000000000..6bc8b395e00 --- /dev/null +++ b/container-core/src/test/java/ai/vespa/cloud/SystemInfoTest.java @@ -0,0 +1,49 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.cloud; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @author bratseth + */ +public class SystemInfoTest { + + @Test + public void testSystemInfo() { + Zone zone = new Zone(Environment.dev, "us-west-1"); + SystemInfo info = new SystemInfo(zone); + assertEquals(zone, info.zone()); + } + + @Test + public void testZone() { + Zone zone = Zone.from("dev.us-west-1"); + zone = Zone.from(zone.toString()); + assertEquals(Environment.dev, zone.environment()); + assertEquals("us-west-1", zone.region()); + Zone sameZone = Zone.from("dev.us-west-1"); + assertEquals(sameZone.hashCode(), zone.hashCode()); + assertEquals(sameZone, zone); + + try { + Zone.from("invalid"); + fail("Expected exception"); + } + catch (IllegalArgumentException e) { + assertEquals("A zone string must be on the form [environment].[region], but was 'invalid'", + e.getMessage()); + } + + try { + Zone.from("invalid.us-west-1"); + fail("Expected exception"); + } + catch (IllegalArgumentException e) { + assertEquals("Invalid zone 'invalid.us-west-1': No environment named 'invalid'", e.getMessage()); + } + } + +} |