From 8fd946c93d11ea77eb9afeed5e688260844f5a8a Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Fri, 25 Apr 2014 15:23:04 +0200 Subject: [PATCH] [JCLOUDS-550] fix for obsolete machineTypes --- .../GoogleComputeEngineServiceAdapter.java | 9 +++- .../InstanceInZoneToNodeMetadata.java | 3 +- .../domain/MachineType.java | 38 +++++++++++++---- .../GoogleComputeEngineServiceLiveTest.java | 42 +++++++++++++++++++ 4 files changed, 81 insertions(+), 11 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 3fdd0845aa..3a26d3420f 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 @@ -32,7 +32,6 @@ import static org.jclouds.googlecomputeengine.GoogleComputeEngineConstants.OPERA import static org.jclouds.googlecomputeengine.domain.Instance.NetworkInterface.AccessConfig.Type; import static org.jclouds.googlecomputeengine.predicates.InstancePredicates.isBootDisk; import static org.jclouds.util.Predicates2.retry; - import java.net.URI; import java.util.List; import java.util.Map; @@ -260,6 +259,12 @@ public class GoogleComputeEngineServiceAdapter implements ComputeServiceAdapter< builder.addAll(api.getMachineTypeApiForProject(userProject.get()) .listInZone(zone.getId()) .concat() + .filter(new Predicate() { + @Override + public boolean apply(MachineType input) { + return !input.getDeprecated().isPresent(); + } + }) .transform(new Function() { @Override @@ -285,7 +290,7 @@ public class GoogleComputeEngineServiceAdapter implements ComputeServiceAdapter< public Image getImage(String id) { return Objects.firstNonNull(api.getImageApiForProject(userProject.get()).get(id), Objects.firstNonNull(api.getImageApiForProject(DEBIAN_PROJECT).get(id), - api.getImageApiForProject(CENTOS_PROJECT).get(id))); + api.getImageApiForProject(CENTOS_PROJECT).get(id))); } diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/InstanceInZoneToNodeMetadata.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/InstanceInZoneToNodeMetadata.java index 704e9ef1a5..c1ddea1d68 100644 --- a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/InstanceInZoneToNodeMetadata.java +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/InstanceInZoneToNodeMetadata.java @@ -98,8 +98,7 @@ public class InstanceInZoneToNodeMetadata implements Function deprecated; @ConstructorProperties({ "id", "creationTimestamp", "selfLink", "name", "description", "guestCpus", "memoryMb", - "imageSpaceGb", "scratchDisks", "maximumPersistentDisks", "maximumPersistentDisksSizeGb", "zone" + "imageSpaceGb", "scratchDisks", "maximumPersistentDisks", "maximumPersistentDisksSizeGb", "zone", + "deprecated" }) private MachineType(String id, Date creationTimestamp, URI selfLink, String name, String description, int guestCpus, int memoryMb, int imageSpaceGb, List scratchDisks, - int maximumPersistentDisks, long maximumPersistentDisksSizeGb, String zone) { + int maximumPersistentDisks, long maximumPersistentDisksSizeGb, String zone, + @Nullable Deprecated deprecated) { super(Kind.MACHINE_TYPE, id, creationTimestamp, selfLink, name, description); this.guestCpus = checkNotNull(guestCpus, "guestCpus of %s", name); this.memoryMb = checkNotNull(memoryMb, "memoryMb of %s", name); @@ -60,6 +66,7 @@ public final class MachineType extends Resource { this.maximumPersistentDisks = checkNotNull(maximumPersistentDisks, "maximumPersistentDisks of %s", name); this.maximumPersistentDisksSizeGb = maximumPersistentDisksSizeGb; this.zone = checkNotNull(zone, "zone of %s", name); + this.deprecated = fromNullable(deprecated); } /** @@ -111,6 +118,13 @@ public final class MachineType extends Resource { return zone; } + /** + * @return the deprecation information for this machine type + */ + public Optional getDeprecated() { + return deprecated; + } + /** * {@inheritDoc} */ @@ -135,7 +149,8 @@ public final class MachineType extends Resource { .add("scratchDisks", scratchDisks) .add("maximumPersistentDisks", maximumPersistentDisks) .add("maximumPersistentDisksSizeGb", maximumPersistentDisksSizeGb) - .add("zone", zone); + .add("zone", zone) + .add("deprecated", deprecated.orNull()); } /** @@ -163,6 +178,7 @@ public final class MachineType extends Resource { private Integer maximumPersistentDisks; private Long maximumPersistentDisksSizeGb; private String zone; + private Deprecated deprecated; /** * @see MachineType#getGuestCpus() @@ -228,6 +244,14 @@ public final class MachineType extends Resource { return this; } + /** + * @see MachineType#getDeprecated() + */ + public Builder deprecated(Deprecated deprecated) { + this.deprecated = deprecated; + return this; + } + @Override protected Builder self() { return this; @@ -236,15 +260,15 @@ public final class MachineType extends Resource { public MachineType build() { return new MachineType(id, creationTimestamp, selfLink, name, description, guestCpus, memoryMb, imageSpaceGb, scratchDisks.build(), maximumPersistentDisks, maximumPersistentDisksSizeGb, - zone); + zone, deprecated); } public Builder fromMachineType(MachineType in) { return super.fromResource(in).memoryMb(in.getMemoryMb()).imageSpaceGb(in.getImageSpaceGb()).scratchDisks(in .getScratchDisks()).maximumPersistentDisks(in.getMaximumPersistentDisks()) - .maximumPersistentDisksSizeGb(in.getMaximumPersistentDisksSizeGb()).zone(in - .getZone()); + .maximumPersistentDisksSizeGb(in.getMaximumPersistentDisksSizeGb()).zone(in.getZone()) + .deprecated(in.getDeprecated().orNull()); } } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceLiveTest.java index 24e5c892d7..2b5bdcd89d 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceLiveTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/GoogleComputeEngineServiceLiveTest.java @@ -16,23 +16,65 @@ */ package org.jclouds.googlecomputeengine.compute; +import static com.google.common.collect.Iterables.contains; +import static org.jclouds.oauth.v2.OAuthTestUtils.setCredentialFromPemFile; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; +import java.util.Properties; +import java.util.Set; +import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.internal.BaseComputeServiceLiveTest; +import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; +import org.jclouds.googlecomputeengine.config.UserProject; +import org.jclouds.googlecomputeengine.domain.MachineType; import org.jclouds.sshj.config.SshjSshClientModule; import org.testng.annotations.Test; +import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.inject.Key; import com.google.inject.Module; +import com.google.inject.TypeLiteral; @Test(groups = "live", singleThreaded = true) public class GoogleComputeEngineServiceLiveTest extends BaseComputeServiceLiveTest { + protected static final String DEFAULT_ZONE_NAME = "us-central1-a"; + public GoogleComputeEngineServiceLiveTest() { provider = "google-compute-engine"; } + @Override + protected Properties setupProperties() { + Properties props = super.setupProperties(); + setCredentialFromPemFile(props, provider + ".credential"); + return props; + } + + public void testListHardwareProfiles() throws Exception { + GoogleComputeEngineApi api = client.getContext().unwrapApi(GoogleComputeEngineApi.class); + Supplier userProject = context.utils().injector().getInstance(Key.get(new TypeLiteral>() { + }, UserProject.class)); + ImmutableSet.Builder deprecatedMachineTypes = ImmutableSet.builder(); + for (MachineType machine : api.getMachineTypeApiForProject(userProject.get()) + .listInZone(DEFAULT_ZONE_NAME).concat()) { + if (machine.getDeprecated().isPresent()) { + deprecatedMachineTypes.add(machine.getId()); + } + } + ImmutableSet deprecatedMachineTypeIds = deprecatedMachineTypes.build(); + Set hardwareProfiles = client.listHardwareProfiles(); + System.out.println(hardwareProfiles.size()); + for (Hardware hardwareProfile : hardwareProfiles) { + System.out.println(hardwareProfile); + assertFalse(contains(deprecatedMachineTypeIds, hardwareProfile.getId())); + } + } + /** * Nodes may have additional metadata entries (particularly they may have an "sshKeys" entry) */