diff --git a/apis/cloudsigma/src/main/java/org/jclouds/cloudsigma/compute/config/CloudSigmaComputeServiceContextModule.java b/apis/cloudsigma/src/main/java/org/jclouds/cloudsigma/compute/config/CloudSigmaComputeServiceContextModule.java index 035ac36391..d178d08db4 100644 --- a/apis/cloudsigma/src/main/java/org/jclouds/cloudsigma/compute/config/CloudSigmaComputeServiceContextModule.java +++ b/apis/cloudsigma/src/main/java/org/jclouds/cloudsigma/compute/config/CloudSigmaComputeServiceContextModule.java @@ -30,7 +30,6 @@ import org.jclouds.cloudsigma.compute.functions.ParseOsFamilyVersion64BitFromIma import org.jclouds.cloudsigma.compute.functions.PreinstalledDiskToImage; import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata; import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata.DeviceToVolume; -import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata.FindImageForId; import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata.GetImageIdFromServer; import org.jclouds.cloudsigma.compute.options.CloudSigmaTemplateOptions; import org.jclouds.cloudsigma.domain.Device; @@ -86,8 +85,6 @@ public class CloudSigmaComputeServiceContextModule extends }).to(DeviceToVolume.class); bind(new TypeLiteral>() { }).to(GetImageIdFromServer.class); - bind(new TypeLiteral>() { - }).to(FindImageForId.class); bind(new TypeLiteral>() { }).to(ParseOsFamilyVersion64BitFromImageName.class); bind(TemplateBuilder.class) diff --git a/apis/cloudsigma/src/main/java/org/jclouds/cloudsigma/compute/functions/ParseOsFamilyVersion64BitFromImageName.java b/apis/cloudsigma/src/main/java/org/jclouds/cloudsigma/compute/functions/ParseOsFamilyVersion64BitFromImageName.java index e99a86104d..e89e29be8f 100644 --- a/apis/cloudsigma/src/main/java/org/jclouds/cloudsigma/compute/functions/ParseOsFamilyVersion64BitFromImageName.java +++ b/apis/cloudsigma/src/main/java/org/jclouds/cloudsigma/compute/functions/ParseOsFamilyVersion64BitFromImageName.java @@ -75,10 +75,8 @@ public class ParseOsFamilyVersion64BitFromImageName implements Function getImageIdFromServer; - private final Function findImageForId; + private final Supplier> images; private final Supplier locationSupplier; private final Function deviceToVolume; private final GroupNamingConvention nodeNamingConvention; @Inject - ServerInfoToNodeMetadata(Function getImageIdFromServer, Function findImageForId, + ServerInfoToNodeMetadata(Function getImageIdFromServer, @Memoized Supplier> images, Function deviceToVolume, Supplier locationSupplier, GroupNamingConvention.Factory namingConvention) { this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); this.deviceToVolume = checkNotNull(deviceToVolume, "deviceToVolume"); - this.findImageForId = checkNotNull(findImageForId, "findImageForId"); + this.images = checkNotNull(images, "images"); this.getImageIdFromServer = checkNotNull(getImageIdFromServer, "getImageIdFromServer"); } @@ -96,9 +98,9 @@ public class ServerInfoToNodeMetadata implements Function image = FluentIterable.from(images.get()).firstMatch(idEquals(imageId)); + if (image.isPresent()) { + builder.operatingSystem(image.get().getOperatingSystem()); } } builder.hardware(new HardwareBuilder().ids(from.getUuid()).hypervisor("kvm") @@ -175,19 +177,4 @@ public class ServerInfoToNodeMetadata implements Function { - - @Inject - public FindImageForId(@Memoized Supplier> images) { - super(images); - } - - @Override - public boolean matches(String from, Image input) { - return input.getProviderId().equals(from); - } - } - } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/functions/TemplateToImage.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/functions/TemplateToImage.java index f3c1ba4614..f492220b73 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/functions/TemplateToImage.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/functions/TemplateToImage.java @@ -19,6 +19,7 @@ package org.jclouds.cloudstack.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.location.predicates.LocationPredicates.idEquals; import java.util.Set; @@ -26,28 +27,28 @@ import javax.inject.Inject; import javax.inject.Singleton; import org.jclouds.cloudstack.domain.Template; -import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.Image.Status; import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.OperatingSystem; -import org.jclouds.compute.domain.Image.Status; import org.jclouds.domain.Location; import com.google.common.base.Function; import com.google.common.base.Supplier; +import com.google.common.collect.FluentIterable; /** */ @Singleton public class TemplateToImage implements Function { - private final FindLocationForTemplate findLocationForTemplate; + private final Supplier> locations; private final Function templateToOperatingSystem; @Inject - public TemplateToImage(FindLocationForTemplate findLocationForTemplate, + public TemplateToImage(@Memoized Supplier> locations, Function templateToOperatingSystem) { - this.findLocationForTemplate = checkNotNull(findLocationForTemplate, "findLocationForTemplate"); + this.locations = checkNotNull(locations, "locations"); this.templateToOperatingSystem = checkNotNull(templateToOperatingSystem, "templateToOperatingSystem"); } @@ -61,24 +62,10 @@ public class TemplateToImage implements Function { .description(template.getDisplayText()).operatingSystem(os); if (!template.isCrossZones()) - builder.location(findLocationForTemplate.apply(template)); + builder.location(FluentIterable.from(locations.get()).firstMatch(idEquals(template.getZoneId())).orNull()); //TODO: implement status mapping!!! builder.status(Status.AVAILABLE); return builder.build(); } - - @Singleton - public static class FindLocationForTemplate extends FindResourceInSet { - - @Inject - public FindLocationForTemplate(@Memoized Supplier> location) { - super(location); - } - - @Override - public boolean matches(Template from, Location input) { - return input.getId().equals(from.getZoneId()); - } - } } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadata.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadata.java index c0aaadf428..237ebf682b 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadata.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadata.java @@ -22,6 +22,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; import static com.google.common.collect.Sets.newHashSet; +import static org.jclouds.location.predicates.LocationPredicates.idEquals; import static org.jclouds.util.InetAddresses2.isPrivateIPAddress; import java.util.Map; @@ -33,15 +34,13 @@ import javax.inject.Singleton; import org.jclouds.cloudstack.domain.IPForwardingRule; import org.jclouds.cloudstack.domain.NIC; import org.jclouds.cloudstack.domain.VirtualMachine; -import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; -import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.Processor; -import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.domain.Location; import org.jclouds.rest.ResourceNotFoundException; @@ -52,6 +51,7 @@ import com.google.common.base.Predicate; import com.google.common.base.Supplier; import com.google.common.base.Throwables; import com.google.common.cache.LoadingCache; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; @@ -75,25 +75,25 @@ public class VirtualMachineToNodeMetadata implements Function> locations; + private final Supplier> images; private final LoadingCache> getIPForwardingRulesByVirtualMachine; private final GroupNamingConvention nodeNamingConvention; @Inject - VirtualMachineToNodeMetadata(FindLocationForVirtualMachine findLocationForVirtualMachine, - FindImageForVirtualMachine findImageForVirtualMachine, + VirtualMachineToNodeMetadata(@Memoized Supplier> locations, + @Memoized Supplier> images, LoadingCache> getIPForwardingRulesByVirtualMachine, GroupNamingConvention.Factory namingConvention) { this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); - this.findLocationForVirtualMachine = checkNotNull(findLocationForVirtualMachine, "findLocationForVirtualMachine"); - this.findImageForVirtualMachine = checkNotNull(findImageForVirtualMachine, "findImageForVirtualMachine"); + this.locations = checkNotNull(locations, "locations"); + this.images = checkNotNull(images, "images"); this.getIPForwardingRulesByVirtualMachine = checkNotNull(getIPForwardingRulesByVirtualMachine, "getIPForwardingRulesByVirtualMachine"); } @Override - public NodeMetadata apply(VirtualMachine from) { + public NodeMetadata apply(final VirtualMachine from) { // convert the result object to a jclouds NodeMetadata NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.ids(from.getId() + ""); @@ -105,9 +105,16 @@ public class VirtualMachineToNodeMetadata implements Function() { + @Override + public boolean apply(Image input) { + return input.getProviderId().equals(from.getTemplateId() + "") + // either location free image (location is null) or in the same zone as the VM + && (input.getLocation() == null || input.getId().equals(from.getZoneId() + "")); + } + }).orNull(); if (image != null) { builder.imageId(image.getId()); builder.operatingSystem(image.getOperatingSystem()); @@ -170,50 +177,4 @@ public class VirtualMachineToNodeMetadata implements Function { - - @Inject - public FindLocationForVirtualMachine(@Memoized Supplier> location) { - super(location); - } - - @Override - public boolean matches(VirtualMachine from, Location input) { - return input.getId().equals(from.getZoneId()); - } - } - - @Singleton - public static class FindHardwareForVirtualMachine extends FindResourceInSet { - - @Inject - public FindHardwareForVirtualMachine(@Memoized Supplier> location) { - super(location); - } - - @Override - public boolean matches(VirtualMachine from, Hardware input) { - return input.getProviderId().equals(from.getServiceOfferingId()); - } - } - - @Singleton - public static class FindImageForVirtualMachine extends FindResourceInSet { - - @Inject - public FindImageForVirtualMachine(@Memoized Supplier> location) { - super(location); - } - - @Override - public boolean matches(VirtualMachine from, Image input) { - return input.getProviderId().equals(from.getTemplateId() + "") - // either location free image (location is null) - // or in the same zone as the VM - && (input.getLocation() == null || input.getId().equals(from.getZoneId() + "")); - } - } - } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/TemplateToImageTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/TemplateToImageTest.java index 413f608004..a22387ab1b 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/TemplateToImageTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/TemplateToImageTest.java @@ -22,12 +22,11 @@ import static org.testng.AssertJUnit.assertEquals; import java.util.Set; -import org.jclouds.cloudstack.compute.functions.TemplateToImage.FindLocationForTemplate; import org.jclouds.cloudstack.domain.Template; import org.jclouds.cloudstack.parse.ListTemplatesResponseTest; import org.jclouds.compute.domain.Image; -import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.Image.Status; +import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.domain.Location; import org.testng.annotations.Test; @@ -46,8 +45,7 @@ public class TemplateToImageTest { .> ofInstance(ImmutableSet. of(ZoneToLocationTest.one, ZoneToLocationTest.two)); - static TemplateToImage function = new TemplateToImage(new FindLocationForTemplate(locationSupplier), - TemplateToOperatingSystemTest.function); + static TemplateToImage function = new TemplateToImage(locationSupplier, TemplateToOperatingSystemTest.function); // location free image static Image one = new ImageBuilder().id("2").providerId("2").name("CentOS 5.3(64-bit) no GUI (XenServer)") diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadataTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadataTest.java index ca89526200..a434ab23b5 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadataTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadataTest.java @@ -23,8 +23,6 @@ import static org.testng.Assert.assertEquals; import java.net.UnknownHostException; import java.util.Set; -import org.jclouds.cloudstack.compute.functions.VirtualMachineToNodeMetadata.FindImageForVirtualMachine; -import org.jclouds.cloudstack.compute.functions.VirtualMachineToNodeMetadata.FindLocationForVirtualMachine; import org.jclouds.cloudstack.domain.GuestIPType; import org.jclouds.cloudstack.domain.IPForwardingRule; import org.jclouds.cloudstack.domain.NIC; @@ -35,8 +33,8 @@ import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeMetadata.Status; +import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.domain.Location; @@ -67,17 +65,14 @@ public class VirtualMachineToNodeMetadataTest { Supplier> imageSupplier = Suppliers.> ofInstance(ImmutableSet .of(TemplateToImageTest.one, TemplateToImageTest.two)); - VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine( - locationSupplier), new FindImageForVirtualMachine( - imageSupplier), CacheBuilder.newBuilder().> build( - new CacheLoader>() { - - @Override - public Set load(String arg0) throws Exception { - return ImmutableSet.of(IPForwardingRule.builder().id("1234l").IPAddress("1.1.1.1").build()); - } - - }), namingConvention); + VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier, + CacheBuilder.newBuilder().> build( + new CacheLoader>() { + @Override + public Set load(String arg0) throws Exception { + return ImmutableSet.of(IPForwardingRule.builder().id("1234l").IPAddress("1.1.1.1").build()); + } + }), namingConvention); // notice if we've already parsed this properly here, we can rely on it. VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0); @@ -104,16 +99,14 @@ public class VirtualMachineToNodeMetadataTest { Supplier> imageSupplier = Suppliers.> ofInstance(ImmutableSet .of(TemplateToImageTest.one, TemplateToImageTest.two)); - VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine( - locationSupplier), new FindImageForVirtualMachine( - imageSupplier), CacheBuilder.newBuilder().> build( - new CacheLoader>() { - @Override - public Set load(String arg0) throws Exception { - return ImmutableSet.of(); - } - - }), namingConvention); + VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier, + CacheBuilder.newBuilder().> build( + new CacheLoader>() { + @Override + public Set load(String arg0) throws Exception { + return ImmutableSet.of(); + } + }), namingConvention); VirtualMachine guest =VirtualMachine.builder() .id("54") @@ -166,17 +159,16 @@ public class VirtualMachineToNodeMetadataTest { Supplier> imageSupplier = Suppliers.> ofInstance(ImmutableSet .of(TemplateToImageTest.one, TemplateToImageTest.two)); - VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine( - locationSupplier), new FindImageForVirtualMachine( - imageSupplier), CacheBuilder.newBuilder().> build( - new CacheLoader>() { + VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier, + CacheBuilder.newBuilder().> build( + new CacheLoader>() { - @Override - public Set load(String arg0) throws Exception { - throw new ResourceNotFoundException("no ip forwarding rule for: " + arg0); - } + @Override + public Set load(String arg0) throws Exception { + throw new ResourceNotFoundException("no ip forwarding rule for: " + arg0); + } - }), namingConvention); + }), namingConvention); // notice if we've already parsed this properly here, we can rely on it. VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0); diff --git a/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/config/ElasticStackComputeServiceContextModule.java b/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/config/ElasticStackComputeServiceContextModule.java index 680240b049..37af4b2344 100644 --- a/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/config/ElasticStackComputeServiceContextModule.java +++ b/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/config/ElasticStackComputeServiceContextModule.java @@ -37,10 +37,9 @@ import org.jclouds.domain.Location; import org.jclouds.elasticstack.ElasticStackClient; import org.jclouds.elasticstack.compute.ElasticStackComputeServiceAdapter; import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata; -import org.jclouds.elasticstack.compute.functions.WellKnownImageToImage; import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata.DeviceToVolume; -import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata.FindImageForId; import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata.GetImageIdFromServer; +import org.jclouds.elasticstack.compute.functions.WellKnownImageToImage; import org.jclouds.elasticstack.domain.Device; import org.jclouds.elasticstack.domain.DriveInfo; import org.jclouds.elasticstack.domain.Server; @@ -88,8 +87,6 @@ public class ElasticStackComputeServiceContextModule extends }).to(DeviceToVolume.class); bind(new TypeLiteral>() { }).to(GetImageIdFromServer.class); - bind(new TypeLiteral>() { - }).to(FindImageForId.class); bind(new TypeLiteral>() { }).to(WellKnownImageToImage.class); } diff --git a/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/functions/ServerInfoToNodeMetadata.java b/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/functions/ServerInfoToNodeMetadata.java index 27f648f928..1707b2197e 100644 --- a/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/functions/ServerInfoToNodeMetadata.java +++ b/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/functions/ServerInfoToNodeMetadata.java @@ -19,6 +19,7 @@ package org.jclouds.elasticstack.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.compute.predicates.ImagePredicates.idEquals; import java.util.Map; import java.util.Set; @@ -27,16 +28,15 @@ import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Singleton; -import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.Processor; import org.jclouds.compute.domain.Volume; import org.jclouds.compute.domain.VolumeBuilder; -import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.domain.Location; import org.jclouds.elasticstack.domain.Device; @@ -47,8 +47,10 @@ import org.jclouds.elasticstack.domain.ServerStatus; import org.jclouds.logging.Logger; import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Supplier; import com.google.common.cache.LoadingCache; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -70,19 +72,19 @@ public class ServerInfoToNodeMetadata implements Function getImageIdFromServer; - private final Function findImageForId; + private final Supplier> images; private final Supplier locationSupplier; private final Function deviceToVolume; private final GroupNamingConvention nodeNamingConvention; @Inject - ServerInfoToNodeMetadata(Function getImageIdFromServer, Function findImageForId, + ServerInfoToNodeMetadata(Function getImageIdFromServer, @Memoized Supplier> images, Function deviceToVolume, Supplier locationSupplier, GroupNamingConvention.Factory namingConvention) { this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); this.deviceToVolume = checkNotNull(deviceToVolume, "deviceToVolume"); - this.findImageForId = checkNotNull(findImageForId, "findImageForId"); + this.images = checkNotNull(images, "images"); this.getImageIdFromServer = checkNotNull(getImageIdFromServer, "getImageIdFromServer"); } @@ -97,9 +99,9 @@ public class ServerInfoToNodeMetadata implements Function image = FluentIterable.from(images.get()).firstMatch(idEquals(imageId)); + if (image.isPresent()) { + builder.operatingSystem(image.get().getOperatingSystem()); } } builder.hardware(new HardwareBuilder().ids(from.getUuid()).hypervisor("kvm") @@ -177,18 +179,4 @@ public class ServerInfoToNodeMetadata implements Function { - - @Inject - public FindImageForId(@Memoized Supplier> images) { - super(images); - } - - @Override - public boolean matches(String from, Image input) { - return input.getProviderId().equals(from); - } - } - } diff --git a/core/src/main/java/org/jclouds/collect/FindResourceInSet.java b/core/src/main/java/org/jclouds/collect/FindResourceInSet.java deleted file mode 100644 index 8cb8cab775..0000000000 --- a/core/src/main/java/org/jclouds/collect/FindResourceInSet.java +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you 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 - * - * http://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 org.jclouds.collect; - -import static com.google.common.base.Preconditions.checkNotNull; - -import java.util.NoSuchElementException; -import java.util.Set; - -import javax.annotation.Resource; -import javax.inject.Inject; -import javax.inject.Singleton; - -import org.jclouds.logging.Logger; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.base.Supplier; -import com.google.common.collect.Iterables; - -/** - * @author Adrian Cole - */ -@Singleton -public abstract class FindResourceInSet implements Function { - @Resource - protected Logger logger = Logger.NULL; - - private final Supplier> set; - - @Inject - public FindResourceInSet(@Memoized Supplier> set) { - this.set = checkNotNull(set, "set"); - } - - public abstract boolean matches(F from, T input); - - public T apply(final F from) { - try { - return Iterables.find(set.get(), new Predicate() { - - @Override - public boolean apply(T input) { - return matches(from, input); - } - - }); - } catch (NoSuchElementException e) { - logger.trace("could not find a match in set for %s", from); - } - return null; - } -} diff --git a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/compute/functions/VMToNodeMetadata.java b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/compute/functions/VMToNodeMetadata.java index 83bc5a5603..a2e37b146e 100644 --- a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/compute/functions/VMToNodeMetadata.java +++ b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/compute/functions/VMToNodeMetadata.java @@ -20,6 +20,7 @@ package org.jclouds.savvis.vpdc.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Predicates.not; +import static com.google.common.collect.FluentIterable.from; import static com.google.common.collect.Iterables.filter; import java.util.Map; @@ -28,14 +29,14 @@ import java.util.Set; import javax.inject.Inject; import javax.inject.Singleton; -import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; import org.jclouds.compute.domain.CIMOperatingSystem; import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeMetadata.Status; +import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.domain.Location; +import org.jclouds.location.predicates.LocationPredicates; import org.jclouds.savvis.vpdc.domain.VM; import org.jclouds.savvis.vpdc.util.Utils; import org.jclouds.util.InetAddresses2.IsPrivateIPAddress; @@ -57,14 +58,14 @@ public class VMToNodeMetadata implements Function { Status.UNRECOGNIZED).put(VM.Status.UNKNOWN, Status.UNRECOGNIZED).put(VM.Status.SUSPENDED, Status.SUSPENDED).put(VM.Status.UNRESOLVED, Status.PENDING).build(); - private final FindLocationForVM findLocationForVM; + private final Supplier> locations; private final GroupNamingConvention nodeNamingConvention; @Inject - VMToNodeMetadata(FindLocationForVM findLocationForVM, + VMToNodeMetadata(@Memoized Supplier> locations, GroupNamingConvention.Factory namingConvention) { this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); - this.findLocationForVM = checkNotNull(findLocationForVM, "findLocationForVM"); + this.locations = checkNotNull(locations, "locations"); } @Override @@ -72,7 +73,8 @@ public class VMToNodeMetadata implements Function { NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.ids(from.getHref().toASCIIString()); builder.name(from.getName()); - builder.location(findLocationForVM.apply(from)); + String locationId = Iterables.get(from.getNetworkSection().getNetworks(), 0).getName(); + builder.location(from(locations.get()).firstMatch(LocationPredicates.idEquals(locationId)).orNull()); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getName())); try { builder.operatingSystem(CIMOperatingSystem.toComputeOs(from.getOperatingSystemSection())); @@ -87,18 +89,4 @@ public class VMToNodeMetadata implements Function { builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE)); return builder.build(); } - - @Singleton - public static class FindLocationForVM extends FindResourceInSet { - - @Inject - public FindLocationForVM(@Memoized Supplier> hardware) { - super(hardware); - } - - @Override - public boolean matches(VM from, Location input) { - return input.getId().equals(Iterables.get(from.getNetworkSection().getNetworks(), 0).getName()); - } - } } diff --git a/labs/smartos-ssh/src/main/java/org/jclouds/smartos/compute/functions/VMToNodeMetadata.java b/labs/smartos-ssh/src/main/java/org/jclouds/smartos/compute/functions/VMToNodeMetadata.java index b32c32e783..516ea9221e 100644 --- a/labs/smartos-ssh/src/main/java/org/jclouds/smartos/compute/functions/VMToNodeMetadata.java +++ b/labs/smartos-ssh/src/main/java/org/jclouds/smartos/compute/functions/VMToNodeMetadata.java @@ -19,6 +19,7 @@ package org.jclouds.smartos.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.collect.FluentIterable.from; import java.util.Map; import java.util.Set; @@ -26,16 +27,18 @@ import java.util.Set; import javax.inject.Inject; import javax.inject.Singleton; -import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.functions.GroupNamingConvention; +import org.jclouds.compute.predicates.HardwarePredicates; +import org.jclouds.compute.predicates.ImagePredicates; import org.jclouds.domain.Credentials; import org.jclouds.domain.Location; import org.jclouds.domain.LoginCredentials; +import org.jclouds.location.predicates.LocationPredicates; import org.jclouds.smartos.compute.domain.VM; import com.google.common.base.Function; @@ -56,21 +59,21 @@ public class VMToNodeMetadata implements Function { .put(VM.State.INCOMPLETE, NodeMetadata.Status.PENDING)// .build(); - private final FindHardwareForServer findHardwareForServer; - private final FindLocationForServer findLocationForServer; - private final FindImageForServer findImageForServer; + private final Supplier> hardware; + private final Supplier> locations; + private final Supplier> images; private final Map credentialStore; private final GroupNamingConvention nodeNamingConvention; @Inject - VMToNodeMetadata(Map credentialStore, FindHardwareForServer findHardwareForServer, - FindLocationForServer findLocationForServer, FindImageForServer findImageForServer, - GroupNamingConvention.Factory namingConvention) { + VMToNodeMetadata(Map credentialStore, @Memoized Supplier> hardware, + @Memoized Supplier> locations, @Memoized Supplier> images, + GroupNamingConvention.Factory namingConvention) { this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.credentialStore = checkNotNull(credentialStore, "credentialStore"); - this.findHardwareForServer = checkNotNull(findHardwareForServer, "findHardwareForServer"); - this.findLocationForServer = checkNotNull(findLocationForServer, "findLocationForServer"); - this.findImageForServer = checkNotNull(findImageForServer, "findImageForServer"); + this.hardware = checkNotNull(hardware, "hardware"); + this.locations = checkNotNull(locations, "locations"); + this.images = checkNotNull(images, "images"); } @Override @@ -79,13 +82,13 @@ public class VMToNodeMetadata implements Function { NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.ids(from.getUuid() + ""); builder.name(from.getAlias()); - builder.location(findLocationForServer.apply(from)); + builder.location(from(locations.get()).firstMatch(LocationPredicates.idEquals(from.getUuid() + "")).orNull()); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getType())); builder.imageId(from.getType() + ""); - Image image = findImageForServer.apply(from); + Image image = from(images.get()).firstMatch(ImagePredicates.idEquals(from.getUuid() + "")).orNull(); if (image != null) builder.operatingSystem(image.getOperatingSystem()); - builder.hardware(findHardwareForServer.apply(from)); + builder.hardware(from(hardware.get()).firstMatch(HardwarePredicates.idEquals(from.getUuid() + "")).orNull()); builder.status(serverStatusToNodeStatus.get(from.getState())); try { if (from.getState() == VM.State.RUNNING) { @@ -102,46 +105,4 @@ public class VMToNodeMetadata implements Function { builder.credentials(LoginCredentials.fromCredentials(credentialStore.get(from.getUuid() + ""))); return builder.build(); } - - @Singleton - public static class FindHardwareForServer extends FindResourceInSet { - - @Inject - public FindHardwareForServer(@Memoized Supplier> hardware) { - super(hardware); - } - - @Override - public boolean matches(VM from, Hardware input) { - return input.getProviderId().equals(from.getUuid() + ""); - } - } - - @Singleton - public static class FindImageForServer extends FindResourceInSet { - - @Inject - public FindImageForServer(@Memoized Supplier> hardware) { - super(hardware); - } - - @Override - public boolean matches(VM from, Image input) { - return input.getProviderId().equals(from.getUuid() + ""); - } - } - - @Singleton - public static class FindLocationForServer extends FindResourceInSet { - - @Inject - public FindLocationForServer(@Memoized Supplier> hardware) { - super(hardware); - } - - @Override - public boolean matches(VM from, Location input) { - return input.getId().equals(from.getUuid() + ""); - } - } } diff --git a/providers/glesys/src/main/java/org/jclouds/glesys/compute/GleSYSComputeServiceAdapter.java b/providers/glesys/src/main/java/org/jclouds/glesys/compute/GleSYSComputeServiceAdapter.java index 91f488da6c..915bfdc269 100644 --- a/providers/glesys/src/main/java/org/jclouds/glesys/compute/GleSYSComputeServiceAdapter.java +++ b/providers/glesys/src/main/java/org/jclouds/glesys/compute/GleSYSComputeServiceAdapter.java @@ -38,7 +38,6 @@ import javax.inject.Named; import javax.inject.Singleton; import org.jclouds.Constants; -import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; import org.jclouds.compute.ComputeService; import org.jclouds.compute.ComputeServiceAdapter; @@ -66,12 +65,12 @@ import org.jclouds.glesys.options.DestroyServerOptions; import org.jclouds.location.predicates.LocationPredicates; import org.jclouds.logging.Logger; import org.jclouds.predicates.RetryablePredicate; -import org.jclouds.util.Iterables2; import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Predicate; import com.google.common.base.Supplier; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; @@ -154,20 +153,6 @@ public class GleSYSComputeServiceAdapter implements ComputeServiceAdapter { - - @Inject - public FindLocationForServerSpec(@Memoized Supplier> location) { - super(location); - } - - @Override - public boolean matches(ServerSpec from, Location input) { - return input.getId().equals(from.getDatacenter()); - } - } - @Override public Iterable listHardwareProfiles() { Set locationsSet = locations.get(); @@ -224,27 +209,24 @@ public class GleSYSComputeServiceAdapter implements ComputeServiceAdapter listNodes() { - return Iterables2.concreteCopy(transformParallel(api.getServerApi().list(), new Function>() { + return transformParallel(api.getServerApi().list(), new Function>() { @Override public Future apply(Server from) { return aapi.getServerApi().get(from.getId()); } - }, userThreads, null, logger, "server details")); + }, userThreads, null, logger, "server details"); } @Override public Set listLocations() { - return ImmutableSet.copyOf(Iterables.concat(Iterables.transform(api.getServerApi() - .getAllowedArgumentsForCreateByPlatform().values(), - new Function>() { - + return FluentIterable.from(api.getServerApi().getAllowedArgumentsForCreateByPlatform().values()) + .transformAndConcat(new Function>() { @Override public Set apply(AllowedArgumentsForCreateServer arg0) { return arg0.getDataCenters(); } - - }))); + }).toSet(); } @Override diff --git a/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/FindLocationForServerSpec.java b/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/FindLocationForServerSpec.java deleted file mode 100644 index e5aaa71f04..0000000000 --- a/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/FindLocationForServerSpec.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you 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 - * - * http://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 org.jclouds.glesys.compute.functions; - -import java.util.Set; - -import javax.inject.Inject; -import javax.inject.Singleton; - -import org.jclouds.collect.FindResourceInSet; -import org.jclouds.collect.Memoized; -import org.jclouds.domain.Location; -import org.jclouds.glesys.domain.ServerSpec; - -import com.google.common.base.Supplier; - -/** - * @author Adrian Cole - */ -@Singleton -public class FindLocationForServerSpec extends FindResourceInSet { - - @Inject - public FindLocationForServerSpec(@Memoized Supplier> location) { - super(location); - } - - @Override - public boolean matches(ServerSpec from, Location input) { - return input.getId().equals(from.getDatacenter()); - } -} diff --git a/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/ServerDetailsToNodeMetadata.java b/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/ServerDetailsToNodeMetadata.java index f86f20aaab..e31aaec72f 100644 --- a/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/ServerDetailsToNodeMetadata.java +++ b/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/ServerDetailsToNodeMetadata.java @@ -24,6 +24,7 @@ import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.io.BaseEncoding.base16; import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromCommaDelimitedValue; +import static org.jclouds.location.predicates.LocationPredicates.idEquals; import java.util.Map; import java.util.NoSuchElementException; @@ -34,7 +35,6 @@ import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; -import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.Image; @@ -59,6 +59,7 @@ import com.google.common.base.Predicates; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.base.Supplier; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; @@ -78,7 +79,7 @@ public class ServerDetailsToNodeMetadata implements Function> images; - protected final FindLocationForServerDetails findLocationForServerDetails; + protected final Supplier> locations; protected final GroupNamingConvention nodeNamingConvention; private static class FindImageForServer implements Predicate { @@ -95,11 +96,10 @@ public class ServerDetailsToNodeMetadata implements Function> images, - GroupNamingConvention.Factory namingConvention) { + ServerDetailsToNodeMetadata(@Memoized Supplier> locations, + @Memoized Supplier> images, GroupNamingConvention.Factory namingConvention) { this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); - this.findLocationForServerDetails = checkNotNull(findLocationForServerDetails, "findLocationForServerDetails"); + this.locations = checkNotNull(locations, "locations"); this.images = checkNotNull(images, "images"); } @@ -109,7 +109,7 @@ public class ServerDetailsToNodeMetadata implements Function { - - @Inject - public FindLocationForServerDetails(@Memoized Supplier> location) { - super(location); - } - - @Override - public boolean matches(ServerDetails from, Location input) { - return input.getId().equals(from.getDatacenter()); - } - } } diff --git a/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/ServerSpecToHardware.java b/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/ServerSpecToHardware.java index 330bf259d8..bee9df96cf 100644 --- a/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/ServerSpecToHardware.java +++ b/providers/glesys/src/main/java/org/jclouds/glesys/compute/functions/ServerSpecToHardware.java @@ -19,10 +19,14 @@ package org.jclouds.glesys.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.location.predicates.LocationPredicates.idEquals; + +import java.util.Set; import javax.inject.Inject; import javax.inject.Singleton; +import org.jclouds.collect.Memoized; import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.Processor; @@ -33,6 +37,8 @@ import org.jclouds.domain.Location; import org.jclouds.glesys.domain.ServerSpec; import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; /** @@ -42,16 +48,16 @@ import com.google.common.collect.ImmutableList; @Singleton public class ServerSpecToHardware implements Function { - private final FindLocationForServerSpec findLocationForServerSpec; + private final Supplier> locations; @Inject - ServerSpecToHardware(FindLocationForServerSpec findLocationForServerSpec) { - this.findLocationForServerSpec = checkNotNull(findLocationForServerSpec, "findLocationForServerSpec"); + ServerSpecToHardware(@Memoized Supplier> locations) { + this.locations = checkNotNull(locations, "locations"); } @Override public Hardware apply(ServerSpec spec) { - Location location = findLocationForServerSpec.apply(spec); + Location location = FluentIterable.from(locations.get()).firstMatch(idEquals(spec.getDatacenter())).orNull(); assert location != null : String.format("no location matched ServerSpec %s", spec); return new HardwareBuilder().ids(spec.toString()).ram(spec.getMemorySizeMB()).processors( ImmutableList.of(new Processor(spec.getCpuCores(), 1.0))).volumes( diff --git a/providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadata.java b/providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadata.java index b5a6bd8ee5..d7b8d280df 100644 --- a/providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadata.java +++ b/providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadata.java @@ -19,6 +19,7 @@ package org.jclouds.softlayer.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.collect.FluentIterable.from; import java.util.Map; import java.util.Set; @@ -26,17 +27,16 @@ import java.util.Set; import javax.inject.Inject; import javax.inject.Singleton; -import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeMetadata.Status; +import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.domain.Location; +import org.jclouds.location.predicates.LocationPredicates; import org.jclouds.softlayer.SoftLayerClient; -import org.jclouds.softlayer.domain.Datacenter; import org.jclouds.softlayer.domain.ProductItem; import org.jclouds.softlayer.domain.ProductOrder; import org.jclouds.softlayer.domain.VirtualGuest; @@ -59,19 +59,19 @@ public class VirtualGuestToNodeMetadata implements Function> locations; + private final GetHardwareForVirtualGuest hardware; + private final GetImageForVirtualGuest images; private final GroupNamingConvention nodeNamingConvention; @Inject - VirtualGuestToNodeMetadata(FindLocationForVirtualGuest findLocationForVirtualGuest, - GetHardwareForVirtualGuest getHardwareForVirtualGuest, GetImageForVirtualGuest getImageForVirtualGuest, + VirtualGuestToNodeMetadata(@Memoized Supplier> locations, + GetHardwareForVirtualGuest hardware, GetImageForVirtualGuest images, GroupNamingConvention.Factory namingConvention) { this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); - this.findLocationForVirtualGuest = checkNotNull(findLocationForVirtualGuest, "findLocationForVirtualGuest"); - this.getHardwareForVirtualGuest = checkNotNull(getHardwareForVirtualGuest, "getHardwareForVirtualGuest"); - this.getImageForVirtualGuest = checkNotNull(getImageForVirtualGuest, "getImageForVirtualGuest"); + this.locations = checkNotNull(locations, "locations"); + this.hardware = checkNotNull(hardware, "hardware"); + this.images = checkNotNull(images, "images"); } @Override @@ -81,18 +81,18 @@ public class VirtualGuestToNodeMetadata implements Function { - - @Inject - public FindLocationForVirtualGuest(@Memoized Supplier> location) { - super(location); - } - - @Override - public boolean matches(VirtualGuest from, Location input) { - Datacenter dc = from.getDatacenter(); - if (dc == null) - return false; - return input.getId().equals(Integer.toString(dc.getId())); - } - } - @Singleton public static class GetHardwareForVirtualGuest { diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadataTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadataTest.java index fa8be24ad2..d8403b1c36 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadataTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadataTest.java @@ -28,13 +28,12 @@ import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.OperatingSystem; -import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.domain.Location; import org.jclouds.softlayer.SoftLayerClient; -import org.jclouds.softlayer.compute.functions.VirtualGuestToNodeMetadata.FindLocationForVirtualGuest; import org.jclouds.softlayer.domain.VirtualGuest; import org.jclouds.softlayer.parse.ParseBadVirtualGuest; import org.jclouds.softlayer.parse.ParseVirtualGuestHaltedTest; @@ -67,8 +66,8 @@ public class VirtualGuestToNodeMetadataTest { Supplier> locationSupplier = Suppliers.> ofInstance(ImmutableSet . of(expectedLocation)); - VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( - locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); + VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata( + locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); @@ -94,8 +93,8 @@ public class VirtualGuestToNodeMetadataTest { Supplier> locationSupplier = Suppliers.> ofInstance(ImmutableSet . of()); - VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( - locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); + VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier, + new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); @@ -119,8 +118,8 @@ public class VirtualGuestToNodeMetadataTest { Supplier> locationSupplier = Suppliers.> ofInstance(ImmutableSet . of(expectedLocation)); - VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( - locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); + VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier, + new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); @@ -147,8 +146,8 @@ public class VirtualGuestToNodeMetadataTest { Supplier> locationSupplier = Suppliers.> ofInstance(ImmutableSet . of(expectedLocation)); - VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( - locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); + VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier, + new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); @@ -175,8 +174,8 @@ public class VirtualGuestToNodeMetadataTest { Supplier> locationSupplier = Suppliers.> ofInstance(ImmutableSet . of(expectedLocation)); - VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( - locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); + VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier, + new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); diff --git a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/compute/functions/ServerToNodeMetadata.java b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/compute/functions/ServerToNodeMetadata.java index 7415bc785a..4467b19947 100644 --- a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/compute/functions/ServerToNodeMetadata.java +++ b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/compute/functions/ServerToNodeMetadata.java @@ -19,6 +19,7 @@ package org.jclouds.servermanager.compute.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.collect.FluentIterable.from; import java.util.Map; import java.util.Set; @@ -26,17 +27,19 @@ import java.util.Set; import javax.inject.Inject; import javax.inject.Singleton; -import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeMetadata.Status; +import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.functions.GroupNamingConvention; +import org.jclouds.compute.predicates.HardwarePredicates; +import org.jclouds.compute.predicates.ImagePredicates; import org.jclouds.domain.Credentials; import org.jclouds.domain.Location; import org.jclouds.domain.LoginCredentials; +import org.jclouds.location.predicates.LocationPredicates; import org.jclouds.servermanager.Server; import com.google.common.base.Function; @@ -57,21 +60,21 @@ public class ServerToNodeMetadata implements Function { .put(Server.Status.UNRECOGNIZED, Status.UNRECOGNIZED)// .build(); - private final FindHardwareForServer findHardwareForServer; - private final FindLocationForServer findLocationForServer; - private final FindImageForServer findImageForServer; + private final Supplier> hardware; + private final Supplier> locations; + private final Supplier> images; private final Map credentialStore; private final GroupNamingConvention nodeNamingConvention; @Inject - ServerToNodeMetadata(Map credentialStore, FindHardwareForServer findHardwareForServer, - FindLocationForServer findLocationForServer, FindImageForServer findImageForServer, + ServerToNodeMetadata(Map credentialStore, @Memoized Supplier> hardware, + @Memoized Supplier> locations, @Memoized Supplier> images, GroupNamingConvention.Factory namingConvention) { this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.credentialStore = checkNotNull(credentialStore, "credentialStore"); - this.findHardwareForServer = checkNotNull(findHardwareForServer, "findHardwareForServer"); - this.findLocationForServer = checkNotNull(findLocationForServer, "findLocationForServer"); - this.findImageForServer = checkNotNull(findImageForServer, "findImageForServer"); + this.hardware = checkNotNull(hardware, "hardware"); + this.locations = checkNotNull(locations, "locations"); + this.images = checkNotNull(images, "images"); } @Override @@ -80,59 +83,17 @@ public class ServerToNodeMetadata implements Function { NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.ids(from.id + ""); builder.name(from.name); - builder.location(findLocationForServer.apply(from)); + builder.location(from(locations.get()).firstMatch(LocationPredicates.idEquals(from.datacenter)).orNull()); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.name)); builder.imageId(from.imageId + ""); - Image image = findImageForServer.apply(from); + Image image = from(images.get()).firstMatch(ImagePredicates.idEquals(from.imageId + "")).orNull(); if (image != null) builder.operatingSystem(image.getOperatingSystem()); - builder.hardware(findHardwareForServer.apply(from)); + builder.hardware(from(hardware.get()).firstMatch(HardwarePredicates.idEquals(from.hardwareId + "")).orNull()); builder.status(serverStatusToNodeStatus.get(from.status)); builder.publicAddresses(ImmutableSet. of(from.publicAddress)); builder.privateAddresses(ImmutableSet. of(from.privateAddress)); builder.credentials(LoginCredentials.fromCredentials(credentialStore.get(from.id + ""))); return builder.build(); } - - @Singleton - public static class FindHardwareForServer extends FindResourceInSet { - - @Inject - public FindHardwareForServer(@Memoized Supplier> hardware) { - super(hardware); - } - - @Override - public boolean matches(Server from, Hardware input) { - return input.getProviderId().equals(from.hardwareId + ""); - } - } - - @Singleton - public static class FindImageForServer extends FindResourceInSet { - - @Inject - public FindImageForServer(@Memoized Supplier> hardware) { - super(hardware); - } - - @Override - public boolean matches(Server from, Image input) { - return input.getProviderId().equals(from.imageId + ""); - } - } - - @Singleton - public static class FindLocationForServer extends FindResourceInSet { - - @Inject - public FindLocationForServer(@Memoized Supplier> hardware) { - super(hardware); - } - - @Override - public boolean matches(Server from, Location input) { - return input.getId().equals(from.datacenter + ""); - } - } }