From f5a5d869feb058e8b113e6f6f84d4ecb3eb4cff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Wojtun=CC=81?= Date: Wed, 22 Feb 2017 08:16:10 +0100 Subject: [PATCH] Wrap Image with Optional in DiskURIToImage --- .../GoogleComputeEngineServiceAdapter.java | 5 +++-- ...ogleComputeEngineServiceContextModule.java | 7 ++++--- .../functions/InstanceToNodeMetadata.java | 11 ++++++---- .../compute/loaders/DiskURIToImage.java | 9 ++++++--- .../GoogleComputeEngineServiceMockTest.java | 20 +++++++++++++++++++ .../functions/InstanceToNodeMetadataTest.java | 5 +++-- .../disk_get_with_source_snapshot.json | 12 +++++++++++ 7 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 providers/google-compute-engine/src/test/resources/disk_get_with_source_snapshot.json 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 99ec4e7e39..fe54bdf04b 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 @@ -62,6 +62,7 @@ import org.jclouds.googlecomputeengine.features.InstanceApi; import org.jclouds.location.suppliers.all.JustProvider; import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Splitter; import com.google.common.base.Strings; @@ -96,7 +97,7 @@ public final class GoogleComputeEngineServiceAdapter private final Function, String> windowsPasswordGenerator; private final FirewallTagNamingConvention.Factory firewallTagNamingConvention; private final List imageProjects; - private final LoadingCache diskURIToImage; + private final LoadingCache> diskURIToImage; @Inject GoogleComputeEngineServiceAdapter(JustProvider justProvider, GoogleComputeEngineApi api, Predicate> operationDone, @@ -105,7 +106,7 @@ public final class GoogleComputeEngineServiceAdapter Resources resources, FirewallTagNamingConvention.Factory firewallTagNamingConvention, @Named(IMAGE_PROJECTS) String imageProjects, - LoadingCache diskURIToImage) { + LoadingCache> diskURIToImage) { this.justProvider = justProvider; this.api = api; this.operationDone = operationDone; diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/config/GoogleComputeEngineServiceContextModule.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/config/GoogleComputeEngineServiceContextModule.java index 10c4d6e989..6a9772e068 100644 --- a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/config/GoogleComputeEngineServiceContextModule.java +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/config/GoogleComputeEngineServiceContextModule.java @@ -67,6 +67,7 @@ import org.jclouds.googlecomputeengine.domain.Operation; import org.jclouds.location.suppliers.ImplicitLocationSupplier; import org.jclouds.location.suppliers.implicit.FirstZone; +import com.google.common.base.Optional; import com.google.common.base.Function; import com.google.common.base.Functions; import com.google.common.base.Predicate; @@ -134,7 +135,7 @@ public final class GoogleComputeEngineServiceContextModule bind(FirewallTagNamingConvention.Factory.class).in(Scopes.SINGLETON); - bind(new TypeLiteral>() { + bind(new TypeLiteral>>() { }).to(DiskURIToImage.class); bindHttpApi(binder(), Resources.class); @@ -202,8 +203,8 @@ public final class GoogleComputeEngineServiceContextModule @Provides @Singleton - protected LoadingCache diskURIToImageMap( - CacheLoader in) { + protected LoadingCache> diskURIToImageMap( + CacheLoader> in) { return CacheBuilder.newBuilder().build(in); } diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/InstanceToNodeMetadata.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/InstanceToNodeMetadata.java index 7e7407ebe1..c531f13d1c 100644 --- a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/InstanceToNodeMetadata.java +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/InstanceToNodeMetadata.java @@ -17,6 +17,7 @@ package org.jclouds.googlecomputeengine.compute.functions; import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Splitter; import com.google.common.base.Supplier; import com.google.common.cache.LoadingCache; @@ -44,13 +45,13 @@ public final class InstanceToNodeMetadata implements Function toPortableNodeStatus; private final GroupNamingConvention nodeNamingConvention; - private final LoadingCache diskURIToImage; + private final LoadingCache> diskURIToImage; private final Supplier> hardwares; private final Supplier> locationsByUri; @Inject InstanceToNodeMetadata(Map toPortableNodeStatus, GroupNamingConvention.Factory namingConvention, - LoadingCache diskURIToImage, + LoadingCache> diskURIToImage, @Memoized Supplier> hardwares, @Memoized Supplier> locationsByUri) { this.toPortableNodeStatus = toPortableNodeStatus; @@ -74,7 +75,9 @@ public final class InstanceToNodeMetadata implements Function image = diskURIToImage.getUnchecked(diskSource); Hardware hardware; if (isCustomMachineTypeURI(input.machineType())) { @@ -89,7 +92,7 @@ public final class InstanceToNodeMetadata implements Function { +public class DiskURIToImage extends CacheLoader> { @Resource protected Logger logger = Logger.NULL; @@ -42,10 +43,12 @@ public class DiskURIToImage extends CacheLoader { } @Override - public Image load(URI key) throws ExecutionException { + public Optional load(URI key) throws ExecutionException { try { Disk disk = resources.disk(key); - return resources.image(disk.sourceImage()); + URI sourceImage = disk.sourceImage(); + Image image = sourceImage != null ? resources.image(sourceImage) : null; + return Optional.fromNullable(image); } catch (Exception e) { throw new ExecutionException(message(key, e), e); } 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 4eb7aef112..279f09d24f 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 @@ -148,6 +148,26 @@ public class GoogleComputeEngineServiceMockTest extends BaseGoogleComputeEngineA assertSent(server, "GET", "/projects/party/aggregated/machineTypes"); } + + public void listNodesWithSnapshotSource() throws Exception { + server.enqueue(aggregatedListWithInstanceNetworkAndStatus("test-0", "test-network", RUNNING)); + server.enqueue(singleRegionSingleZoneResponse()); + server.enqueue(jsonResponse("/disk_get_with_source_snapshot.json")); + server.enqueue(jsonResponse("/aggregated_machinetype_list.json")); + + Set nodes = computeService().listNodes(); + assertEquals(nodes.size(), 1); + NodeMetadata node = (NodeMetadata) nodes.iterator().next(); + String imageId = node.getImageId(); + assertEquals(imageId, null); + 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/aggregated/machineTypes"); + } + + + public void createNodeWhenFirewallDoesNotExist() throws Exception { server.enqueue(singleRegionSingleZoneResponse()); server.enqueue(jsonResponse("/image_list.json")); diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/functions/InstanceToNodeMetadataTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/functions/InstanceToNodeMetadataTest.java index 93ee6c56ff..e2d679b7f8 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/functions/InstanceToNodeMetadataTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/functions/InstanceToNodeMetadataTest.java @@ -18,6 +18,7 @@ package org.jclouds.googlecomputeengine.compute.functions; import com.google.common.base.Function; import com.google.common.base.Functions; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; @@ -176,8 +177,8 @@ public class InstanceToNodeMetadataTest { } }; - Map imageMap = ImmutableMap.of(instance.disks().get(0).source(), - new ParseImageTest().expected()); + Map> imageMap = ImmutableMap.of(instance.disks().get(0).source(), + Optional.of(new ParseImageTest().expected())); return new InstanceToNodeMetadata( ImmutableMap.builder() diff --git a/providers/google-compute-engine/src/test/resources/disk_get_with_source_snapshot.json b/providers/google-compute-engine/src/test/resources/disk_get_with_source_snapshot.json new file mode 100644 index 0000000000..cf5db84a52 --- /dev/null +++ b/providers/google-compute-engine/src/test/resources/disk_get_with_source_snapshot.json @@ -0,0 +1,12 @@ +{ + "kind": "compute#disk", + "id": "13050421646334304115", + "creationTimestamp": "2012-11-25T01:38:48.306", + "selfLink": "https://www.googleapis.com/compute/v1/projects/party/zones/us-central1-a/disks/test", + "name": "test", + "sizeGb": "1", + "zone": "https://www.googleapis.com/compute/v1/projects/party/zones/us-central1-a", + "status": "READY", + "type": "https://www.googleapis.com/compute/v1/projects/studied-point-720/zones/us-central1-a/diskTypes/pd-standard", + "sourceSnapshot": "https://www.googleapis.com/compute/v1/projects/party/global/snapshots/snapshot" +} \ No newline at end of file