From 32a47d2f4e9d991abe788003d0820fb07940ec4c Mon Sep 17 00:00:00 2001 From: Charles Li Date: Mon, 7 Jan 2019 15:29:49 -0500 Subject: [PATCH 1/6] Created enum Region.java to retrieve Regions without doing request. --- .../java/com/google/cloud/compute/Region.java | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java diff --git a/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java new file mode 100644 index 000000000000..e70aacf9c207 --- /dev/null +++ b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java @@ -0,0 +1,93 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.cloud.compute; + +import java.util.HashMap; +import java.util.Map; +/** + * Enumeration of Regions. + * + * @see Regions and Zones + */ +public enum Region { + ASIA_EAST1("asia-east1", new String[] {"asia-east1-a", "asia-east1-b", "asia-east1-c"}), + ASIA_EAST2("asia-east2", new String[] {"asia-east2-a", "asia-east2-b", "asia-east2-c"}), + ASIA_NORTHEAST1( + "asia-northeast1", + new String[] {"asia-northeast1-a", "asia-northeast1-b", "asia-northeast1-c"}), + ASIA_SOUTH1("asia-south1", new String[] {"asia-south1-a", "asia-south1-b", "asia-south1-c"}), + ASIA_SOUTHEAST1( + "asia-southeast1", + new String[] {"asia-southeast1-a", "asia-southeast1-b", "asia-southeast1-c"}), + AUSTRALIA_SOUTHEAST1( + "australia-southeast1", + new String[] {"australia-southeast1-a", "australia-southeast1-b", "australia-southeast1-c"}), + EUROPE_NORTH1( + "europe-north1", new String[] {"europe-north1-a", "europe-north1-b", "europe-north1-c"}), + EUROPE_WEST1("europe-west1", new String[] {"europe-west1-b", "europe-west1-c", "europe-west1-d"}), + EUROPE_WEST2("europe-west2", new String[] {"europe-west2-a", "europe-west2-b", "europe-west2-c"}), + EUROPE_WEST3("europe-west3", new String[] {"europe-west3-a", "europe-west3-b", "europe-west3-c"}), + EUROPE_WEST4("europe-west4", new String[] {"europe-west4-a", "europe-west4-b", "europe-west4-c"}), + NORTHAMERICA_NORTHEAST1( + "northamerica-northeast1", + new String[] { + "northamerica-northeast1-a", "northamerica-northeast1-b", "northamerica-northeast1-c" + }), + SOUTHAMERICA_EAST1( + "southamerica-east1", + new String[] {"southamerica-east1-a", "southamerica-east1-b", "southamerica-east1-c"}), + US_CENTRAL1( + "us-central1", + new String[] {"us-central1-a", "us-central1-b", "us-central1-c", "us-central1-f"}), + US_EAST1("us-east1", new String[] {"us-east1-b", "us-east1-c", "us-east1-d"}), + US_EAST4("us-east4", new String[] {"us-east4-a", "us-east4-b", "us-east4-c"}), + US_WEST1("us-west1", new String[] {"us-west1-a", "us-west1-b", "us-west1-c"}), + US_WEST2("us-west2", new String[] {"us-west2-a", "us-west2-b", "us-west2-c"}); + + private final String name; + private final String[] zones; + private static final Map REGIONS = new HashMap<>(); + + static { + for (Region region : Region.values()) { + REGIONS.put(region.name, region); + } + } + + private Region(String name, String[] zones) { + this.name = name; + this.zones = zones; + } + /** The name of this region. */ + public String getName() { + return name; + } + /** zones for this region. */ + public String[] getZones() { + return zones; + } + + /** + * Returns a region enum corresponding to the given region name. + * + * @param regionName The name of the region. + * @return Region enum representing the given region name, or a null if there is no Region enum + * that is representing the given region name. + */ + public static Region fromName(String regionName) { + return REGIONS.get(regionName); + } +} From 34fe38664bb9279b525cc18e5f394dec6a849c13 Mon Sep 17 00:00:00 2001 From: Charles Li Date: Tue, 8 Jan 2019 17:09:03 -0500 Subject: [PATCH 2/6] update comments --- .../src/main/java/com/google/cloud/compute/Region.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java index e70aacf9c207..8da655c6dc59 100644 --- a/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java +++ b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java @@ -75,7 +75,7 @@ private Region(String name, String[] zones) { public String getName() { return name; } - /** zones for this region. */ + /** The zones of this region. */ public String[] getZones() { return zones; } From e39a7d1ada591b57a85a92053e8300724f4e6a1e Mon Sep 17 00:00:00 2001 From: Charles Li Date: Wed, 9 Jan 2019 10:13:06 -0500 Subject: [PATCH 3/6] added unit test for Region enum --- .../java/com/google/cloud/compute/Region.java | 1 + .../com/google/cloud/compute/RegionTest.java | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java diff --git a/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java index 8da655c6dc59..b952952547df 100644 --- a/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java +++ b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java @@ -17,6 +17,7 @@ import java.util.HashMap; import java.util.Map; + /** * Enumeration of Regions. * diff --git a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java new file mode 100644 index 000000000000..62d561eec7ba --- /dev/null +++ b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.cloud.compute; + +import org.junit.Assert; +import org.junit.Test; + +public class RegionTest { + @Test + public void fromName_null_returnNull() { + Assert.assertNull(Region.fromName(null)); + } + + @Test + public void fromName_empty_returnNull() { + Assert.assertNull(Region.fromName("")); + } + + @Test + public void fromName_invalid_returnNull() { + Assert.assertNull(Region.fromName("mars")); + } + + @Test + public void fromName_valid_returnRegion() { + Assert.assertEquals(Region.ASIA_EAST1, Region.fromName("asia-east1")); + } +} From 915a99bedb4b2e9ec67b93376444399d315664ca Mon Sep 17 00:00:00 2001 From: Charles Li Date: Thu, 10 Jan 2019 09:58:04 -0500 Subject: [PATCH 4/6] add space between methods and move instance variables after all static blocks. --- .../src/main/java/com/google/cloud/compute/Region.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java index b952952547df..43a9748ee8d7 100644 --- a/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java +++ b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java @@ -58,8 +58,6 @@ public enum Region { US_WEST1("us-west1", new String[] {"us-west1-a", "us-west1-b", "us-west1-c"}), US_WEST2("us-west2", new String[] {"us-west2-a", "us-west2-b", "us-west2-c"}); - private final String name; - private final String[] zones; private static final Map REGIONS = new HashMap<>(); static { @@ -68,14 +66,19 @@ public enum Region { } } + private final String name; + private final String[] zones; + private Region(String name, String[] zones) { this.name = name; this.zones = zones; } + /** The name of this region. */ public String getName() { return name; } + /** The zones of this region. */ public String[] getZones() { return zones; From 26725208c085ac63a0dabffded2d6a3251994e75 Mon Sep 17 00:00:00 2001 From: Charles Li Date: Thu, 10 Jan 2019 12:37:29 -0500 Subject: [PATCH 5/6] update test case name --- .../test/java/com/google/cloud/compute/RegionTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java index 62d561eec7ba..187d6e7cc640 100644 --- a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java +++ b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java @@ -20,22 +20,22 @@ public class RegionTest { @Test - public void fromName_null_returnNull() { + public void fromNameWhenNullReturnNull() { Assert.assertNull(Region.fromName(null)); } @Test - public void fromName_empty_returnNull() { + public void fromNameWhenEmptyReturnNull() { Assert.assertNull(Region.fromName("")); } @Test - public void fromName_invalid_returnNull() { + public void fromNameWhenInvalidReturnNull() { Assert.assertNull(Region.fromName("mars")); } @Test - public void fromName_valid_returnRegion() { + public void fromNameWhenValidReturnRegion() { Assert.assertEquals(Region.ASIA_EAST1, Region.fromName("asia-east1")); } } From 2a2cf8bc85e117a7fdd75a6d1ab30ab98d2c1a62 Mon Sep 17 00:00:00 2001 From: Charles Li Date: Thu, 10 Jan 2019 12:55:44 -0500 Subject: [PATCH 6/6] rename enum Region to Regions for IDE autocomplete and readability purpose. --- .../cloud/compute/{Region.java => Regions.java} | 14 +++++++------- .../compute/{RegionTest.java => RegionsTest.java} | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) rename google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/{Region.java => Regions.java} (88%) rename google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/{RegionTest.java => RegionsTest.java} (78%) diff --git a/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Regions.java similarity index 88% rename from google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java rename to google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Regions.java index 43a9748ee8d7..82c60f800308 100644 --- a/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Region.java +++ b/google-cloud-clients/google-cloud-compute/src/main/java/com/google/cloud/compute/Regions.java @@ -23,7 +23,7 @@ * * @see Regions and Zones */ -public enum Region { +public enum Regions { ASIA_EAST1("asia-east1", new String[] {"asia-east1-a", "asia-east1-b", "asia-east1-c"}), ASIA_EAST2("asia-east2", new String[] {"asia-east2-a", "asia-east2-b", "asia-east2-c"}), ASIA_NORTHEAST1( @@ -58,10 +58,10 @@ public enum Region { US_WEST1("us-west1", new String[] {"us-west1-a", "us-west1-b", "us-west1-c"}), US_WEST2("us-west2", new String[] {"us-west2-a", "us-west2-b", "us-west2-c"}); - private static final Map REGIONS = new HashMap<>(); + private static final Map REGIONS = new HashMap<>(); static { - for (Region region : Region.values()) { + for (Regions region : Regions.values()) { REGIONS.put(region.name, region); } } @@ -69,7 +69,7 @@ public enum Region { private final String name; private final String[] zones; - private Region(String name, String[] zones) { + private Regions(String name, String[] zones) { this.name = name; this.zones = zones; } @@ -85,13 +85,13 @@ public String[] getZones() { } /** - * Returns a region enum corresponding to the given region name. + * Returns a Regions enum corresponding to the given region name. * * @param regionName The name of the region. - * @return Region enum representing the given region name, or a null if there is no Region enum + * @return Regions enum representing the given region name, or a null if there is no Regions enum * that is representing the given region name. */ - public static Region fromName(String regionName) { + public static Regions fromName(String regionName) { return REGIONS.get(regionName); } } diff --git a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionsTest.java similarity index 78% rename from google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java rename to google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionsTest.java index 187d6e7cc640..5ce28eddb0c7 100644 --- a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionTest.java +++ b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/RegionsTest.java @@ -18,24 +18,24 @@ import org.junit.Assert; import org.junit.Test; -public class RegionTest { +public class RegionsTest { @Test public void fromNameWhenNullReturnNull() { - Assert.assertNull(Region.fromName(null)); + Assert.assertNull(Regions.fromName(null)); } @Test public void fromNameWhenEmptyReturnNull() { - Assert.assertNull(Region.fromName("")); + Assert.assertNull(Regions.fromName("")); } @Test public void fromNameWhenInvalidReturnNull() { - Assert.assertNull(Region.fromName("mars")); + Assert.assertNull(Regions.fromName("mars")); } @Test public void fromNameWhenValidReturnRegion() { - Assert.assertEquals(Region.ASIA_EAST1, Region.fromName("asia-east1")); + Assert.assertEquals(Regions.ASIA_EAST1, Regions.fromName("asia-east1")); } }