From 9dd50b39c94aa71eafbde4dfd651d81b7d5d9a6b Mon Sep 17 00:00:00 2001 From: Ignasi Barrera Date: Tue, 23 Oct 2018 22:04:36 +0200 Subject: [PATCH] JCLOUDS-1463: Do not return MachineTypes from zones that are not available for deploy --- .../GoogleComputeEngineServiceAdapter.java | 12 +++++++++++- .../GoogleComputeEngineServiceMockTest.java | 18 +++++++++++++++++- .../resources/aggregated_machinetype_list.json | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceAdapter.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceAdapter.java index c5b31273a9..a1a0d2251e 100644 --- a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceAdapter.java +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceAdapter.java @@ -18,12 +18,15 @@ package org.jclouds.googlecomputeengine.compute; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.collect.Iterables.contains; import static com.google.common.collect.Iterables.filter; +import static com.google.common.collect.Iterables.transform; import static java.lang.String.format; import static org.jclouds.googlecloud.internal.ListPages.concat; import static org.jclouds.googlecomputeengine.compute.domain.internal.RegionAndName.fromRegionAndName; import static org.jclouds.googlecomputeengine.compute.strategy.CreateNodesWithGroupEncodedIntoNameThenAddToSet.nameFromNetworkString; import static org.jclouds.googlecomputeengine.config.GoogleComputeEngineProperties.IMAGE_PROJECTS; +import static org.jclouds.location.predicates.LocationPredicates.isZone; import java.net.URI; import java.util.List; @@ -216,10 +219,17 @@ public final class GoogleComputeEngineServiceAdapter } @Override public Iterable listHardwareProfiles() { + // JCLOUDS-1463: Only return the machine types that belong to zones that are actually available + final Iterable zones = transform(filter(listLocations(), isZone()), new Function() { + public String apply(Location input) { + return input.getId(); + } + }); + return filter(concat(api.aggregatedList().machineTypes()), new Predicate() { @Override public boolean apply(MachineType input) { - return input.deprecated() == null; + return input.deprecated() == null && contains(zones, input.zone()); } }); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceMockTest.java index a81b17522a..6e1c11b834 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceMockTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceMockTest.java @@ -47,6 +47,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/image_list.json")); server.enqueue(jsonResponse("/image_list_debian.json")); // per IMAGE_PROJECTS = "debian-cloud" + server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/aggregated_machinetype_list.json")); ComputeService computeService = computeService(); @@ -60,7 +61,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA Hardware fastest = computeService.templateBuilder().fastest().build().getHardware(); assertNotNull(fastest); - assertEquals(computeService.listHardwareProfiles().size(), 3); + assertEquals(computeService.listHardwareProfiles().size(), 2); Template toMatch = computeService.templateBuilder().imageId(template.getImage().getId()).build(); assertEquals(toMatch.getImage(), template.getImage()); @@ -68,6 +69,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/global/images"); assertSent(server, "GET", "/projects/debian-cloud/global/images"); + assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/aggregated/machineTypes"); } @@ -76,6 +78,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/disk_get_with_source_image.json")); server.enqueue(jsonResponse("/image_get_for_source_image.json")); + server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/aggregated_machinetype_list.json")); // Why are we getting machineTypes to delete an instance? server.enqueue(jsonResponse("/operation.json")); // instance delete server.enqueue(jsonResponse("/zone_operation.json")); @@ -92,6 +95,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/zones/us-central1-a/disks/test"); assertSent(server, "GET", "/projects/debian-cloud/global/images/debian-7-wheezy-v20140718"); + assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/aggregated/machineTypes"); // Why are we getting machineTypes to delete an instance? assertSent(server, "DELETE", "/jclouds/zones/us-central1-a/instances/test-delete-1"); // instance delete assertSent(server, "GET", "/projects/party/zones/us-central1-a/operations/operation-1354084865060"); @@ -133,6 +137,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/disk_get_with_source_image.json")); server.enqueue(jsonResponse("/image_get_for_source_image.json")); + server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/aggregated_machinetype_list.json")); Set nodes = computeService().listNodes(); @@ -144,6 +149,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/zones/us-central1-a/disks/test"); assertSent(server, "GET", "/projects/debian-cloud/global/images/debian-7-wheezy-v20140718"); + assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/aggregated/machineTypes"); } @@ -152,6 +158,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA server.enqueue(aggregatedListWithInstanceNetworkAndStatus("test-0", "test-network", RUNNING)); server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/disk_get_with_source_snapshot.json")); + server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/aggregated_machinetype_list.json")); Set nodes = computeService().listNodes(); @@ -162,6 +169,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA assertSent(server, "GET", "/projects/party/aggregated/instances"); assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/zones/us-central1-a/disks/test"); + assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/aggregated/machineTypes"); } @@ -171,6 +179,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/image_list.json")); server.enqueue(jsonResponse("/image_list_debian.json")); // per IMAGE_PROJECTS = "debian-cloud" + server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/aggregated_machinetype_list.json")); server.enqueue(new MockResponse().setResponseCode(404)); // Get Subnet server.enqueue(jsonResponse("/network_get_default.json")); @@ -198,6 +207,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/global/images"); assertSent(server, "GET", "/projects/debian-cloud/global/images"); + assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/aggregated/machineTypes"); assertSent(server, "GET", "/projects/party/regions/us-central1/subnetworks/default"); assertSent(server, "GET", "/projects/party/global/networks/default"); @@ -219,6 +229,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/image_list.json")); server.enqueue(jsonResponse("/image_list_debian.json")); // per IMAGE_PROJECTS = "debian-cloud" + server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/aggregated_machinetype_list.json")); server.enqueue(new MockResponse().setResponseCode(404)); // Get Subnet server.enqueue(jsonResponse("/network_get_default.json")); @@ -249,6 +260,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/global/images"); assertSent(server, "GET", "/projects/debian-cloud/global/images"); + assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/aggregated/machineTypes"); assertSent(server, "GET", "/projects/party/regions/us-central1/subnetworks/default"); assertSent(server, "GET", "/projects/party/global/networks/default"); @@ -271,6 +283,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/image_list.json")); server.enqueue(jsonResponse("/image_list_debian.json")); // per IMAGE_PROJECTS = "debian-cloud" + server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/aggregated_machinetype_list.json")); server.enqueue(jsonResponse("/subnetwork_get.json")); server.enqueue(jsonResponse("/network_get.json")); @@ -301,6 +314,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/global/images"); assertSent(server, "GET", "/projects/debian-cloud/global/images"); + assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/aggregated/machineTypes"); assertSent(server, "GET", "/projects/party/regions/us-central1/subnetworks/jclouds-test"); assertSent(server, "GET", "/projects/party/global/networks/mynetwork"); @@ -323,6 +337,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/image_list.json")); server.enqueue(jsonResponse("/image_list_debian.json")); // per IMAGE_PROJECTS = "debian-cloud" + server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/aggregated_machinetype_list.json")); server.enqueue(jsonResponse("/subnetwork_get.json")); server.enqueue(jsonResponse("/network_get.json")); @@ -353,6 +368,7 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/global/images"); assertSent(server, "GET", "/projects/debian-cloud/global/images"); + assertSent(server, "GET", "/projects/party/regions"); assertSent(server, "GET", "/projects/party/aggregated/machineTypes"); assertSent(server, "GET", "/projects/party/regions/us-central1/subnetworks/jclouds-test"); assertSent(server, "GET", "/projects/party/global/networks/mynetwork"); diff --git a/providers/google-compute-engine/src/test/resources/aggregated_machinetype_list.json b/providers/google-compute-engine/src/test/resources/aggregated_machinetype_list.json index f9ae87cf6a..c23c3ac729 100644 --- a/providers/google-compute-engine/src/test/resources/aggregated_machinetype_list.json +++ b/providers/google-compute-engine/src/test/resources/aggregated_machinetype_list.json @@ -50,7 +50,7 @@ ], "maximumPersistentDisks": 16, "maximumPersistentDisksSizeGb": "1024", - "zone": "us-central1-a" + "zone": "asia-east2-a" } ] }