transitioned from FindResourceInSet to FluentIterable

This commit is contained in:
Adrian Cole 2012-12-31 22:11:34 -08:00
parent 0ee2c56668
commit e1afa8c516
19 changed files with 165 additions and 511 deletions

View File

@ -30,7 +30,6 @@ import org.jclouds.cloudsigma.compute.functions.ParseOsFamilyVersion64BitFromIma
import org.jclouds.cloudsigma.compute.functions.PreinstalledDiskToImage; import org.jclouds.cloudsigma.compute.functions.PreinstalledDiskToImage;
import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata; import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata;
import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata.DeviceToVolume; 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.functions.ServerInfoToNodeMetadata.GetImageIdFromServer;
import org.jclouds.cloudsigma.compute.options.CloudSigmaTemplateOptions; import org.jclouds.cloudsigma.compute.options.CloudSigmaTemplateOptions;
import org.jclouds.cloudsigma.domain.Device; import org.jclouds.cloudsigma.domain.Device;
@ -86,8 +85,6 @@ public class CloudSigmaComputeServiceContextModule extends
}).to(DeviceToVolume.class); }).to(DeviceToVolume.class);
bind(new TypeLiteral<Function<Server, String>>() { bind(new TypeLiteral<Function<Server, String>>() {
}).to(GetImageIdFromServer.class); }).to(GetImageIdFromServer.class);
bind(new TypeLiteral<Function<String, Image>>() {
}).to(FindImageForId.class);
bind(new TypeLiteral<Function<String, OsFamilyVersion64Bit>>() { bind(new TypeLiteral<Function<String, OsFamilyVersion64Bit>>() {
}).to(ParseOsFamilyVersion64BitFromImageName.class); }).to(ParseOsFamilyVersion64BitFromImageName.class);
bind(TemplateBuilder.class) bind(TemplateBuilder.class)

View File

@ -75,10 +75,8 @@ public class ParseOsFamilyVersion64BitFromImageName implements Function<String,
Matcher matcher = PATTERN.matcher(input); Matcher matcher = PATTERN.matcher(input);
if (matcher.find()) { if (matcher.find()) {
OsFamily fam = OsFamily.fromValue(matcher.group(1).toLowerCase()); OsFamily fam = OsFamily.fromValue(matcher.group(1).toLowerCase());
switch (fam) { if (fam == OsFamily.UNRECOGNIZED)
case UNRECOGNIZED: return new OsFamilyVersion64Bit(OsFamily.UNRECOGNIZED, null, is64Bit);
return new OsFamilyVersion64Bit(OsFamily.UNRECOGNIZED, null, is64Bit);
}
return new OsFamilyVersion64Bit(fam, ComputeServiceUtils.parseVersionOrReturnEmptyString(fam, matcher return new OsFamilyVersion64Bit(fam, ComputeServiceUtils.parseVersionOrReturnEmptyString(fam, matcher
.group(2), osVersionMap), is64Bit); .group(2), osVersionMap), is64Bit);
} else { } else {

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudsigma.compute.functions; package org.jclouds.cloudsigma.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.predicates.ImagePredicates.idEquals;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -32,7 +33,6 @@ import org.jclouds.cloudsigma.domain.DriveInfo;
import org.jclouds.cloudsigma.domain.Server; import org.jclouds.cloudsigma.domain.Server;
import org.jclouds.cloudsigma.domain.ServerInfo; import org.jclouds.cloudsigma.domain.ServerInfo;
import org.jclouds.cloudsigma.domain.ServerStatus; import org.jclouds.cloudsigma.domain.ServerStatus;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
@ -47,8 +47,10 @@ import org.jclouds.domain.Location;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -70,19 +72,19 @@ public class ServerInfoToNodeMetadata implements Function<ServerInfo, NodeMetada
.build(); .build();
private final Function<Server, String> getImageIdFromServer; private final Function<Server, String> getImageIdFromServer;
private final Function<String, Image> findImageForId; private final Supplier<Set<? extends Image>> images;
private final Supplier<Location> locationSupplier; private final Supplier<Location> locationSupplier;
private final Function<Device, Volume> deviceToVolume; private final Function<Device, Volume> deviceToVolume;
private final GroupNamingConvention nodeNamingConvention; private final GroupNamingConvention nodeNamingConvention;
@Inject @Inject
ServerInfoToNodeMetadata(Function<Server, String> getImageIdFromServer, Function<String, Image> findImageForId, ServerInfoToNodeMetadata(Function<Server, String> getImageIdFromServer, @Memoized Supplier<Set<? extends Image>> images,
Function<Device, Volume> deviceToVolume, Supplier<Location> locationSupplier, Function<Device, Volume> deviceToVolume, Supplier<Location> locationSupplier,
GroupNamingConvention.Factory namingConvention) { GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier");
this.deviceToVolume = checkNotNull(deviceToVolume, "deviceToVolume"); this.deviceToVolume = checkNotNull(deviceToVolume, "deviceToVolume");
this.findImageForId = checkNotNull(findImageForId, "findImageForId"); this.images = checkNotNull(images, "images");
this.getImageIdFromServer = checkNotNull(getImageIdFromServer, "getImageIdFromServer"); this.getImageIdFromServer = checkNotNull(getImageIdFromServer, "getImageIdFromServer");
} }
@ -96,9 +98,9 @@ public class ServerInfoToNodeMetadata implements Function<ServerInfo, NodeMetada
String imageId = getImageIdFromServer.apply(from); String imageId = getImageIdFromServer.apply(from);
if (imageId != null) { if (imageId != null) {
Image image = findImageForId.apply(imageId); Optional<? extends Image> image = FluentIterable.from(images.get()).firstMatch(idEquals(imageId));
if (image != null) { if (image.isPresent()) {
builder.operatingSystem(image.getOperatingSystem()); builder.operatingSystem(image.get().getOperatingSystem());
} }
} }
builder.hardware(new HardwareBuilder().ids(from.getUuid()).hypervisor("kvm") builder.hardware(new HardwareBuilder().ids(from.getUuid()).hypervisor("kvm")
@ -175,19 +177,4 @@ public class ServerInfoToNodeMetadata implements Function<ServerInfo, NodeMetada
return imageId; return imageId;
} }
} }
@Singleton
public static class FindImageForId extends FindResourceInSet<String, Image> {
@Inject
public FindImageForId(@Memoized Supplier<Set<? extends Image>> images) {
super(images);
}
@Override
public boolean matches(String from, Image input) {
return input.getProviderId().equals(from);
}
}
} }

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudstack.compute.functions; package org.jclouds.cloudstack.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.location.predicates.LocationPredicates.idEquals;
import java.util.Set; import java.util.Set;
@ -26,28 +27,28 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.cloudstack.domain.Template; import org.jclouds.cloudstack.domain.Template;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.Image.Status;
import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.Image.Status;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
/** /**
*/ */
@Singleton @Singleton
public class TemplateToImage implements Function<Template, Image> { public class TemplateToImage implements Function<Template, Image> {
private final FindLocationForTemplate findLocationForTemplate; private final Supplier<Set<? extends Location>> locations;
private final Function<Template, OperatingSystem> templateToOperatingSystem; private final Function<Template, OperatingSystem> templateToOperatingSystem;
@Inject @Inject
public TemplateToImage(FindLocationForTemplate findLocationForTemplate, public TemplateToImage(@Memoized Supplier<Set<? extends Location>> locations,
Function<Template, OperatingSystem> templateToOperatingSystem) { Function<Template, OperatingSystem> templateToOperatingSystem) {
this.findLocationForTemplate = checkNotNull(findLocationForTemplate, "findLocationForTemplate"); this.locations = checkNotNull(locations, "locations");
this.templateToOperatingSystem = checkNotNull(templateToOperatingSystem, "templateToOperatingSystem"); this.templateToOperatingSystem = checkNotNull(templateToOperatingSystem, "templateToOperatingSystem");
} }
@ -61,24 +62,10 @@ public class TemplateToImage implements Function<Template, Image> {
.description(template.getDisplayText()).operatingSystem(os); .description(template.getDisplayText()).operatingSystem(os);
if (!template.isCrossZones()) if (!template.isCrossZones())
builder.location(findLocationForTemplate.apply(template)); builder.location(FluentIterable.from(locations.get()).firstMatch(idEquals(template.getZoneId())).orNull());
//TODO: implement status mapping!!! //TODO: implement status mapping!!!
builder.status(Status.AVAILABLE); builder.status(Status.AVAILABLE);
return builder.build(); return builder.build();
} }
@Singleton
public static class FindLocationForTemplate extends FindResourceInSet<Template, Location> {
@Inject
public FindLocationForTemplate(@Memoized Supplier<Set<? extends Location>> location) {
super(location);
}
@Override
public boolean matches(Template from, Location input) {
return input.getId().equals(from.getZoneId());
}
}
} }

View File

@ -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.filter;
import static com.google.common.collect.Iterables.transform; import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Sets.newHashSet; import static com.google.common.collect.Sets.newHashSet;
import static org.jclouds.location.predicates.LocationPredicates.idEquals;
import static org.jclouds.util.InetAddresses2.isPrivateIPAddress; import static org.jclouds.util.InetAddresses2.isPrivateIPAddress;
import java.util.Map; import java.util.Map;
@ -33,15 +34,13 @@ import javax.inject.Singleton;
import org.jclouds.cloudstack.domain.IPForwardingRule; import org.jclouds.cloudstack.domain.IPForwardingRule;
import org.jclouds.cloudstack.domain.NIC; import org.jclouds.cloudstack.domain.NIC;
import org.jclouds.cloudstack.domain.VirtualMachine; import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.Processor; import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.rest.ResourceNotFoundException; 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.Supplier;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -75,25 +75,25 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
.put(VirtualMachine.State.SHUTDOWNED, Status.PENDING) .put(VirtualMachine.State.SHUTDOWNED, Status.PENDING)
.put(VirtualMachine.State.UNRECOGNIZED, Status.UNRECOGNIZED).build(); .put(VirtualMachine.State.UNRECOGNIZED, Status.UNRECOGNIZED).build();
private final FindLocationForVirtualMachine findLocationForVirtualMachine; private final Supplier<Set<? extends Location>> locations;
private final FindImageForVirtualMachine findImageForVirtualMachine; private final Supplier<Set<? extends Image>> images;
private final LoadingCache<String, Set<IPForwardingRule>> getIPForwardingRulesByVirtualMachine; private final LoadingCache<String, Set<IPForwardingRule>> getIPForwardingRulesByVirtualMachine;
private final GroupNamingConvention nodeNamingConvention; private final GroupNamingConvention nodeNamingConvention;
@Inject @Inject
VirtualMachineToNodeMetadata(FindLocationForVirtualMachine findLocationForVirtualMachine, VirtualMachineToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
FindImageForVirtualMachine findImageForVirtualMachine, @Memoized Supplier<Set<? extends Image>> images,
LoadingCache<String, Set<IPForwardingRule>> getIPForwardingRulesByVirtualMachine, LoadingCache<String, Set<IPForwardingRule>> getIPForwardingRulesByVirtualMachine,
GroupNamingConvention.Factory namingConvention) { GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.findLocationForVirtualMachine = checkNotNull(findLocationForVirtualMachine, "findLocationForVirtualMachine"); this.locations = checkNotNull(locations, "locations");
this.findImageForVirtualMachine = checkNotNull(findImageForVirtualMachine, "findImageForVirtualMachine"); this.images = checkNotNull(images, "images");
this.getIPForwardingRulesByVirtualMachine = checkNotNull(getIPForwardingRulesByVirtualMachine, this.getIPForwardingRulesByVirtualMachine = checkNotNull(getIPForwardingRulesByVirtualMachine,
"getIPForwardingRulesByVirtualMachine"); "getIPForwardingRulesByVirtualMachine");
} }
@Override @Override
public NodeMetadata apply(VirtualMachine from) { public NodeMetadata apply(final VirtualMachine from) {
// convert the result object to a jclouds NodeMetadata // convert the result object to a jclouds NodeMetadata
NodeMetadataBuilder builder = new NodeMetadataBuilder(); NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getId() + ""); builder.ids(from.getId() + "");
@ -105,9 +105,16 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
// we set displayName to the same value as name, but this could be wrong // we set displayName to the same value as name, but this could be wrong
// on hosts not started with jclouds // on hosts not started with jclouds
builder.hostname(from.getDisplayName()); builder.hostname(from.getDisplayName());
builder.location(findLocationForVirtualMachine.apply(from)); builder.location(FluentIterable.from(locations.get()).firstMatch(idEquals(from.getZoneId())).orNull());
builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getDisplayName())); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getDisplayName()));
Image image = findImageForVirtualMachine.apply(from); Image image = FluentIterable.from(images.get()).firstMatch(new Predicate<Image>() {
@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) { if (image != null) {
builder.imageId(image.getId()); builder.imageId(image.getId());
builder.operatingSystem(image.getOperatingSystem()); builder.operatingSystem(image.getOperatingSystem());
@ -170,50 +177,4 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
} }
return builder.privateAddresses(privateAddresses).publicAddresses(publicAddresses).build(); return builder.privateAddresses(privateAddresses).publicAddresses(publicAddresses).build();
} }
@Singleton
public static class FindLocationForVirtualMachine extends FindResourceInSet<VirtualMachine, Location> {
@Inject
public FindLocationForVirtualMachine(@Memoized Supplier<Set<? extends Location>> location) {
super(location);
}
@Override
public boolean matches(VirtualMachine from, Location input) {
return input.getId().equals(from.getZoneId());
}
}
@Singleton
public static class FindHardwareForVirtualMachine extends FindResourceInSet<VirtualMachine, Hardware> {
@Inject
public FindHardwareForVirtualMachine(@Memoized Supplier<Set<? extends Hardware>> location) {
super(location);
}
@Override
public boolean matches(VirtualMachine from, Hardware input) {
return input.getProviderId().equals(from.getServiceOfferingId());
}
}
@Singleton
public static class FindImageForVirtualMachine extends FindResourceInSet<VirtualMachine, Image> {
@Inject
public FindImageForVirtualMachine(@Memoized Supplier<Set<? extends Image>> 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() + ""));
}
}
} }

View File

@ -22,12 +22,11 @@ import static org.testng.AssertJUnit.assertEquals;
import java.util.Set; import java.util.Set;
import org.jclouds.cloudstack.compute.functions.TemplateToImage.FindLocationForTemplate;
import org.jclouds.cloudstack.domain.Template; import org.jclouds.cloudstack.domain.Template;
import org.jclouds.cloudstack.parse.ListTemplatesResponseTest; import org.jclouds.cloudstack.parse.ListTemplatesResponseTest;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.Image.Status; import org.jclouds.compute.domain.Image.Status;
import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -46,8 +45,7 @@ public class TemplateToImageTest {
.<Set<? extends Location>> ofInstance(ImmutableSet.<Location> of(ZoneToLocationTest.one, .<Set<? extends Location>> ofInstance(ImmutableSet.<Location> of(ZoneToLocationTest.one,
ZoneToLocationTest.two)); ZoneToLocationTest.two));
static TemplateToImage function = new TemplateToImage(new FindLocationForTemplate(locationSupplier), static TemplateToImage function = new TemplateToImage(locationSupplier, TemplateToOperatingSystemTest.function);
TemplateToOperatingSystemTest.function);
// location free image // location free image
static Image one = new ImageBuilder().id("2").providerId("2").name("CentOS 5.3(64-bit) no GUI (XenServer)") static Image one = new ImageBuilder().id("2").providerId("2").name("CentOS 5.3(64-bit) no GUI (XenServer)")

View File

@ -23,8 +23,6 @@ import static org.testng.Assert.assertEquals;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Set; 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.GuestIPType;
import org.jclouds.cloudstack.domain.IPForwardingRule; import org.jclouds.cloudstack.domain.IPForwardingRule;
import org.jclouds.cloudstack.domain.NIC; 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.HardwareBuilder;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
@ -67,17 +65,14 @@ public class VirtualMachineToNodeMetadataTest {
Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image>of(TemplateToImageTest.one, TemplateToImageTest.two)); .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine( VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
locationSupplier), new FindImageForVirtualMachine( CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
imageSupplier), CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build( new CacheLoader<String, Set<IPForwardingRule>>() {
new CacheLoader<String, Set<IPForwardingRule>>() { @Override
public Set<IPForwardingRule> load(String arg0) throws Exception {
@Override return ImmutableSet.of(IPForwardingRule.builder().id("1234l").IPAddress("1.1.1.1").build());
public Set<IPForwardingRule> load(String arg0) throws Exception { }
return ImmutableSet.of(IPForwardingRule.builder().id("1234l").IPAddress("1.1.1.1").build()); }), namingConvention);
}
}), namingConvention);
// notice if we've already parsed this properly here, we can rely on it. // notice if we've already parsed this properly here, we can rely on it.
VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0); VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0);
@ -104,16 +99,14 @@ public class VirtualMachineToNodeMetadataTest {
Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image>of(TemplateToImageTest.one, TemplateToImageTest.two)); .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine( VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
locationSupplier), new FindImageForVirtualMachine( CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
imageSupplier), CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build( new CacheLoader<String, Set<IPForwardingRule>>() {
new CacheLoader<String, Set<IPForwardingRule>>() { @Override
@Override public Set<IPForwardingRule> load(String arg0) throws Exception {
public Set<IPForwardingRule> load(String arg0) throws Exception { return ImmutableSet.of();
return ImmutableSet.of(); }
} }), namingConvention);
}), namingConvention);
VirtualMachine guest =VirtualMachine.builder() VirtualMachine guest =VirtualMachine.builder()
.id("54") .id("54")
@ -166,17 +159,16 @@ public class VirtualMachineToNodeMetadataTest {
Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image>of(TemplateToImageTest.one, TemplateToImageTest.two)); .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine( VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
locationSupplier), new FindImageForVirtualMachine( CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
imageSupplier), CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build( new CacheLoader<String, Set<IPForwardingRule>>() {
new CacheLoader<String, Set<IPForwardingRule>>() {
@Override @Override
public Set<IPForwardingRule> load(String arg0) throws Exception { public Set<IPForwardingRule> load(String arg0) throws Exception {
throw new ResourceNotFoundException("no ip forwarding rule for: " + arg0); 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. // notice if we've already parsed this properly here, we can rely on it.
VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0); VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0);

View File

@ -37,10 +37,9 @@ import org.jclouds.domain.Location;
import org.jclouds.elasticstack.ElasticStackClient; import org.jclouds.elasticstack.ElasticStackClient;
import org.jclouds.elasticstack.compute.ElasticStackComputeServiceAdapter; import org.jclouds.elasticstack.compute.ElasticStackComputeServiceAdapter;
import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata; 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.DeviceToVolume;
import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata.FindImageForId;
import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata.GetImageIdFromServer; 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.Device;
import org.jclouds.elasticstack.domain.DriveInfo; import org.jclouds.elasticstack.domain.DriveInfo;
import org.jclouds.elasticstack.domain.Server; import org.jclouds.elasticstack.domain.Server;
@ -88,8 +87,6 @@ public class ElasticStackComputeServiceContextModule extends
}).to(DeviceToVolume.class); }).to(DeviceToVolume.class);
bind(new TypeLiteral<Function<Server, String>>() { bind(new TypeLiteral<Function<Server, String>>() {
}).to(GetImageIdFromServer.class); }).to(GetImageIdFromServer.class);
bind(new TypeLiteral<Function<String, Image>>() {
}).to(FindImageForId.class);
bind(new TypeLiteral<Function<DriveInfo, Image>>() { bind(new TypeLiteral<Function<DriveInfo, Image>>() {
}).to(WellKnownImageToImage.class); }).to(WellKnownImageToImage.class);
} }

View File

@ -19,6 +19,7 @@
package org.jclouds.elasticstack.compute.functions; package org.jclouds.elasticstack.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.predicates.ImagePredicates.idEquals;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -27,16 +28,15 @@ import javax.annotation.Resource;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.Processor; import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Volume; import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.VolumeBuilder; import org.jclouds.compute.domain.VolumeBuilder;
import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.elasticstack.domain.Device; import org.jclouds.elasticstack.domain.Device;
@ -47,8 +47,10 @@ import org.jclouds.elasticstack.domain.ServerStatus;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -70,19 +72,19 @@ public class ServerInfoToNodeMetadata implements Function<ServerInfo, NodeMetada
.build(); .build();
private final Function<Server, String> getImageIdFromServer; private final Function<Server, String> getImageIdFromServer;
private final Function<String, Image> findImageForId; private final Supplier<Set<? extends Image>> images;
private final Supplier<Location> locationSupplier; private final Supplier<Location> locationSupplier;
private final Function<Device, Volume> deviceToVolume; private final Function<Device, Volume> deviceToVolume;
private final GroupNamingConvention nodeNamingConvention; private final GroupNamingConvention nodeNamingConvention;
@Inject @Inject
ServerInfoToNodeMetadata(Function<Server, String> getImageIdFromServer, Function<String, Image> findImageForId, ServerInfoToNodeMetadata(Function<Server, String> getImageIdFromServer, @Memoized Supplier<Set<? extends Image>> images,
Function<Device, Volume> deviceToVolume, Supplier<Location> locationSupplier, Function<Device, Volume> deviceToVolume, Supplier<Location> locationSupplier,
GroupNamingConvention.Factory namingConvention) { GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier");
this.deviceToVolume = checkNotNull(deviceToVolume, "deviceToVolume"); this.deviceToVolume = checkNotNull(deviceToVolume, "deviceToVolume");
this.findImageForId = checkNotNull(findImageForId, "findImageForId"); this.images = checkNotNull(images, "images");
this.getImageIdFromServer = checkNotNull(getImageIdFromServer, "getImageIdFromServer"); this.getImageIdFromServer = checkNotNull(getImageIdFromServer, "getImageIdFromServer");
} }
@ -97,9 +99,9 @@ public class ServerInfoToNodeMetadata implements Function<ServerInfo, NodeMetada
builder.userMetadata(from.getUserMetadata()); builder.userMetadata(from.getUserMetadata());
String imageId = getImageIdFromServer.apply(from); String imageId = getImageIdFromServer.apply(from);
if (imageId != null) { if (imageId != null) {
Image image = findImageForId.apply(imageId); Optional<? extends Image> image = FluentIterable.from(images.get()).firstMatch(idEquals(imageId));
if (image != null) { if (image.isPresent()) {
builder.operatingSystem(image.getOperatingSystem()); builder.operatingSystem(image.get().getOperatingSystem());
} }
} }
builder.hardware(new HardwareBuilder().ids(from.getUuid()).hypervisor("kvm") builder.hardware(new HardwareBuilder().ids(from.getUuid()).hypervisor("kvm")
@ -177,18 +179,4 @@ public class ServerInfoToNodeMetadata implements Function<ServerInfo, NodeMetada
} }
} }
@Singleton
public static class FindImageForId extends FindResourceInSet<String, Image> {
@Inject
public FindImageForId(@Memoized Supplier<Set<? extends Image>> images) {
super(images);
}
@Override
public boolean matches(String from, Image input) {
return input.getProviderId().equals(from);
}
}
} }

View File

@ -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<F, T> implements Function<F, T> {
@Resource
protected Logger logger = Logger.NULL;
private final Supplier<Set<? extends T>> set;
@Inject
public FindResourceInSet(@Memoized Supplier<Set<? extends T>> 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<T>() {
@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;
}
}

View File

@ -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.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.not; 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 static com.google.common.collect.Iterables.filter;
import java.util.Map; import java.util.Map;
@ -28,14 +29,14 @@ import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.CIMOperatingSystem; import org.jclouds.compute.domain.CIMOperatingSystem;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.location.predicates.LocationPredicates;
import org.jclouds.savvis.vpdc.domain.VM; import org.jclouds.savvis.vpdc.domain.VM;
import org.jclouds.savvis.vpdc.util.Utils; import org.jclouds.savvis.vpdc.util.Utils;
import org.jclouds.util.InetAddresses2.IsPrivateIPAddress; import org.jclouds.util.InetAddresses2.IsPrivateIPAddress;
@ -57,14 +58,14 @@ public class VMToNodeMetadata implements Function<VM, NodeMetadata> {
Status.UNRECOGNIZED).put(VM.Status.UNKNOWN, Status.UNRECOGNIZED).put(VM.Status.SUSPENDED, Status.UNRECOGNIZED).put(VM.Status.UNKNOWN, Status.UNRECOGNIZED).put(VM.Status.SUSPENDED,
Status.SUSPENDED).put(VM.Status.UNRESOLVED, Status.PENDING).build(); Status.SUSPENDED).put(VM.Status.UNRESOLVED, Status.PENDING).build();
private final FindLocationForVM findLocationForVM; private final Supplier<Set<? extends Location>> locations;
private final GroupNamingConvention nodeNamingConvention; private final GroupNamingConvention nodeNamingConvention;
@Inject @Inject
VMToNodeMetadata(FindLocationForVM findLocationForVM, VMToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
GroupNamingConvention.Factory namingConvention) { GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.findLocationForVM = checkNotNull(findLocationForVM, "findLocationForVM"); this.locations = checkNotNull(locations, "locations");
} }
@Override @Override
@ -72,7 +73,8 @@ public class VMToNodeMetadata implements Function<VM, NodeMetadata> {
NodeMetadataBuilder builder = new NodeMetadataBuilder(); NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getHref().toASCIIString()); builder.ids(from.getHref().toASCIIString());
builder.name(from.getName()); 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())); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getName()));
try { try {
builder.operatingSystem(CIMOperatingSystem.toComputeOs(from.getOperatingSystemSection())); builder.operatingSystem(CIMOperatingSystem.toComputeOs(from.getOperatingSystemSection()));
@ -87,18 +89,4 @@ public class VMToNodeMetadata implements Function<VM, NodeMetadata> {
builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE)); builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));
return builder.build(); return builder.build();
} }
@Singleton
public static class FindLocationForVM extends FindResourceInSet<VM, Location> {
@Inject
public FindLocationForVM(@Memoized Supplier<Set<? extends Location>> hardware) {
super(hardware);
}
@Override
public boolean matches(VM from, Location input) {
return input.getId().equals(Iterables.get(from.getNetworkSection().getNetworks(), 0).getName());
}
}
} }

View File

@ -19,6 +19,7 @@
package org.jclouds.smartos.compute.functions; package org.jclouds.smartos.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.FluentIterable.from;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -26,16 +27,18 @@ import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.functions.GroupNamingConvention; 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.Credentials;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.domain.LoginCredentials; import org.jclouds.domain.LoginCredentials;
import org.jclouds.location.predicates.LocationPredicates;
import org.jclouds.smartos.compute.domain.VM; import org.jclouds.smartos.compute.domain.VM;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -56,21 +59,21 @@ public class VMToNodeMetadata implements Function<VM, NodeMetadata> {
.put(VM.State.INCOMPLETE, NodeMetadata.Status.PENDING)// .put(VM.State.INCOMPLETE, NodeMetadata.Status.PENDING)//
.build(); .build();
private final FindHardwareForServer findHardwareForServer; private final Supplier<Set<? extends Hardware>> hardware;
private final FindLocationForServer findLocationForServer; private final Supplier<Set<? extends Location>> locations;
private final FindImageForServer findImageForServer; private final Supplier<Set<? extends Image>> images;
private final Map<String, Credentials> credentialStore; private final Map<String, Credentials> credentialStore;
private final GroupNamingConvention nodeNamingConvention; private final GroupNamingConvention nodeNamingConvention;
@Inject @Inject
VMToNodeMetadata(Map<String, Credentials> credentialStore, FindHardwareForServer findHardwareForServer, VMToNodeMetadata(Map<String, Credentials> credentialStore, @Memoized Supplier<Set<? extends Hardware>> hardware,
FindLocationForServer findLocationForServer, FindImageForServer findImageForServer, @Memoized Supplier<Set<? extends Location>> locations, @Memoized Supplier<Set<? extends Image>> images,
GroupNamingConvention.Factory namingConvention) { GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.credentialStore = checkNotNull(credentialStore, "credentialStore"); this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.findHardwareForServer = checkNotNull(findHardwareForServer, "findHardwareForServer"); this.hardware = checkNotNull(hardware, "hardware");
this.findLocationForServer = checkNotNull(findLocationForServer, "findLocationForServer"); this.locations = checkNotNull(locations, "locations");
this.findImageForServer = checkNotNull(findImageForServer, "findImageForServer"); this.images = checkNotNull(images, "images");
} }
@Override @Override
@ -79,13 +82,13 @@ public class VMToNodeMetadata implements Function<VM, NodeMetadata> {
NodeMetadataBuilder builder = new NodeMetadataBuilder(); NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getUuid() + ""); builder.ids(from.getUuid() + "");
builder.name(from.getAlias()); 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.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getType()));
builder.imageId(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) if (image != null)
builder.operatingSystem(image.getOperatingSystem()); 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())); builder.status(serverStatusToNodeStatus.get(from.getState()));
try { try {
if (from.getState() == VM.State.RUNNING) { if (from.getState() == VM.State.RUNNING) {
@ -102,46 +105,4 @@ public class VMToNodeMetadata implements Function<VM, NodeMetadata> {
builder.credentials(LoginCredentials.fromCredentials(credentialStore.get(from.getUuid() + ""))); builder.credentials(LoginCredentials.fromCredentials(credentialStore.get(from.getUuid() + "")));
return builder.build(); return builder.build();
} }
@Singleton
public static class FindHardwareForServer extends FindResourceInSet<VM, Hardware> {
@Inject
public FindHardwareForServer(@Memoized Supplier<Set<? extends Hardware>> hardware) {
super(hardware);
}
@Override
public boolean matches(VM from, Hardware input) {
return input.getProviderId().equals(from.getUuid() + "");
}
}
@Singleton
public static class FindImageForServer extends FindResourceInSet<VM, Image> {
@Inject
public FindImageForServer(@Memoized Supplier<Set<? extends Image>> hardware) {
super(hardware);
}
@Override
public boolean matches(VM from, Image input) {
return input.getProviderId().equals(from.getUuid() + "");
}
}
@Singleton
public static class FindLocationForServer extends FindResourceInSet<VM, Location> {
@Inject
public FindLocationForServer(@Memoized Supplier<Set<? extends Location>> hardware) {
super(hardware);
}
@Override
public boolean matches(VM from, Location input) {
return input.getId().equals(from.getUuid() + "");
}
}
} }

View File

@ -38,7 +38,6 @@ import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.ComputeService; import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.ComputeServiceAdapter;
@ -66,12 +65,12 @@ import org.jclouds.glesys.options.DestroyServerOptions;
import org.jclouds.location.predicates.LocationPredicates; import org.jclouds.location.predicates.LocationPredicates;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.predicates.RetryablePredicate; import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.util.Iterables2;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -154,20 +153,6 @@ public class GleSYSComputeServiceAdapter implements ComputeServiceAdapter<Server
return UUID.randomUUID().toString().replace("-",""); return UUID.randomUUID().toString().replace("-","");
} }
@Singleton
public static class FindLocationForServerSpec extends FindResourceInSet<ServerSpec, Location> {
@Inject
public FindLocationForServerSpec(@Memoized Supplier<Set<? extends Location>> location) {
super(location);
}
@Override
public boolean matches(ServerSpec from, Location input) {
return input.getId().equals(from.getDatacenter());
}
}
@Override @Override
public Iterable<Hardware> listHardwareProfiles() { public Iterable<Hardware> listHardwareProfiles() {
Set<? extends Location> locationsSet = locations.get(); Set<? extends Location> locationsSet = locations.get();
@ -224,27 +209,24 @@ public class GleSYSComputeServiceAdapter implements ComputeServiceAdapter<Server
@Override @Override
public Iterable<ServerDetails> listNodes() { public Iterable<ServerDetails> listNodes() {
return Iterables2.concreteCopy(transformParallel(api.getServerApi().list(), new Function<Server, Future<? extends ServerDetails>>() { return transformParallel(api.getServerApi().list(), new Function<Server, Future<? extends ServerDetails>>() {
@Override @Override
public Future<ServerDetails> apply(Server from) { public Future<ServerDetails> apply(Server from) {
return aapi.getServerApi().get(from.getId()); return aapi.getServerApi().get(from.getId());
} }
}, userThreads, null, logger, "server details")); }, userThreads, null, logger, "server details");
} }
@Override @Override
public Set<String> listLocations() { public Set<String> listLocations() {
return ImmutableSet.copyOf(Iterables.concat(Iterables.transform(api.getServerApi() return FluentIterable.from(api.getServerApi().getAllowedArgumentsForCreateByPlatform().values())
.getAllowedArgumentsForCreateByPlatform().values(), .transformAndConcat(new Function<AllowedArgumentsForCreateServer, Set<String>>() {
new Function<AllowedArgumentsForCreateServer, Set<String>>() {
@Override @Override
public Set<String> apply(AllowedArgumentsForCreateServer arg0) { public Set<String> apply(AllowedArgumentsForCreateServer arg0) {
return arg0.getDataCenters(); return arg0.getDataCenters();
} }
}).toSet();
})));
} }
@Override @Override

View File

@ -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<ServerSpec, Location> {
@Inject
public FindLocationForServerSpec(@Memoized Supplier<Set<? extends Location>> location) {
super(location);
}
@Override
public boolean matches(ServerSpec from, Location input) {
return input.getId().equals(from.getDatacenter());
}
}

View File

@ -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.base.Strings.isNullOrEmpty;
import static com.google.common.io.BaseEncoding.base16; import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromCommaDelimitedValue; import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromCommaDelimitedValue;
import static org.jclouds.location.predicates.LocationPredicates.idEquals;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -34,7 +35,6 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image; 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.Splitter;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -78,7 +79,7 @@ public class ServerDetailsToNodeMetadata implements Function<ServerDetails, Node
.put(ServerDetails.State.UNRECOGNIZED, Status.UNRECOGNIZED).build(); .put(ServerDetails.State.UNRECOGNIZED, Status.UNRECOGNIZED).build();
protected final Supplier<Set<? extends Image>> images; protected final Supplier<Set<? extends Image>> images;
protected final FindLocationForServerDetails findLocationForServerDetails; protected final Supplier<Set<? extends Location>> locations;
protected final GroupNamingConvention nodeNamingConvention; protected final GroupNamingConvention nodeNamingConvention;
private static class FindImageForServer implements Predicate<Image> { private static class FindImageForServer implements Predicate<Image> {
@ -95,11 +96,10 @@ public class ServerDetailsToNodeMetadata implements Function<ServerDetails, Node
} }
@Inject @Inject
ServerDetailsToNodeMetadata(FindLocationForServerDetails findLocationForServerDetails, ServerDetailsToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
@Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Image>> images, GroupNamingConvention.Factory namingConvention) {
GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.findLocationForServerDetails = checkNotNull(findLocationForServerDetails, "findLocationForServerDetails"); this.locations = checkNotNull(locations, "locations");
this.images = checkNotNull(images, "images"); this.images = checkNotNull(images, "images");
} }
@ -109,7 +109,7 @@ public class ServerDetailsToNodeMetadata implements Function<ServerDetails, Node
builder.ids(from.getId() + ""); builder.ids(from.getId() + "");
builder.name(from.getHostname()); builder.name(from.getHostname());
builder.hostname(from.getHostname()); builder.hostname(from.getHostname());
Location location = findLocationForServerDetails.apply(from); Location location = FluentIterable.from(locations.get()).firstMatch(idEquals(from.getDatacenter())).orNull();
checkState(location != null, "no location matched ServerDetails %s", from); checkState(location != null, "no location matched ServerDetails %s", from);
builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getHostname())); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getHostname()));
@ -148,18 +148,4 @@ public class ServerDetailsToNodeMetadata implements Function<ServerDetails, Node
} }
return null; return null;
} }
@Singleton
public static class FindLocationForServerDetails extends FindResourceInSet<ServerDetails, Location> {
@Inject
public FindLocationForServerDetails(@Memoized Supplier<Set<? extends Location>> location) {
super(location);
}
@Override
public boolean matches(ServerDetails from, Location input) {
return input.getId().equals(from.getDatacenter());
}
}
} }

View File

@ -19,10 +19,14 @@
package org.jclouds.glesys.compute.functions; package org.jclouds.glesys.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull; 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.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Processor; import org.jclouds.compute.domain.Processor;
@ -33,6 +37,8 @@ import org.jclouds.domain.Location;
import org.jclouds.glesys.domain.ServerSpec; import org.jclouds.glesys.domain.ServerSpec;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
/** /**
@ -42,16 +48,16 @@ import com.google.common.collect.ImmutableList;
@Singleton @Singleton
public class ServerSpecToHardware implements Function<ServerSpec, Hardware> { public class ServerSpecToHardware implements Function<ServerSpec, Hardware> {
private final FindLocationForServerSpec findLocationForServerSpec; private final Supplier<Set<? extends Location>> locations;
@Inject @Inject
ServerSpecToHardware(FindLocationForServerSpec findLocationForServerSpec) { ServerSpecToHardware(@Memoized Supplier<Set<? extends Location>> locations) {
this.findLocationForServerSpec = checkNotNull(findLocationForServerSpec, "findLocationForServerSpec"); this.locations = checkNotNull(locations, "locations");
} }
@Override @Override
public Hardware apply(ServerSpec spec) { 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); assert location != null : String.format("no location matched ServerSpec %s", spec);
return new HardwareBuilder().ids(spec.toString()).ram(spec.getMemorySizeMB()).processors( return new HardwareBuilder().ids(spec.toString()).ram(spec.getMemorySizeMB()).processors(
ImmutableList.of(new Processor(spec.getCpuCores(), 1.0))).volumes( ImmutableList.of(new Processor(spec.getCpuCores(), 1.0))).volumes(

View File

@ -19,6 +19,7 @@
package org.jclouds.softlayer.compute.functions; package org.jclouds.softlayer.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.FluentIterable.from;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -26,17 +27,16 @@ import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.location.predicates.LocationPredicates;
import org.jclouds.softlayer.SoftLayerClient; import org.jclouds.softlayer.SoftLayerClient;
import org.jclouds.softlayer.domain.Datacenter;
import org.jclouds.softlayer.domain.ProductItem; import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductOrder; import org.jclouds.softlayer.domain.ProductOrder;
import org.jclouds.softlayer.domain.VirtualGuest; import org.jclouds.softlayer.domain.VirtualGuest;
@ -59,19 +59,19 @@ public class VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMe
.put(VirtualGuest.State.PAUSED, Status.SUSPENDED).put(VirtualGuest.State.RUNNING, Status.RUNNING) .put(VirtualGuest.State.PAUSED, Status.SUSPENDED).put(VirtualGuest.State.RUNNING, Status.RUNNING)
.put(VirtualGuest.State.UNRECOGNIZED, Status.UNRECOGNIZED).build(); .put(VirtualGuest.State.UNRECOGNIZED, Status.UNRECOGNIZED).build();
private final FindLocationForVirtualGuest findLocationForVirtualGuest; private final Supplier<Set<? extends Location>> locations;
private final GetHardwareForVirtualGuest getHardwareForVirtualGuest; private final GetHardwareForVirtualGuest hardware;
private final GetImageForVirtualGuest getImageForVirtualGuest; private final GetImageForVirtualGuest images;
private final GroupNamingConvention nodeNamingConvention; private final GroupNamingConvention nodeNamingConvention;
@Inject @Inject
VirtualGuestToNodeMetadata(FindLocationForVirtualGuest findLocationForVirtualGuest, VirtualGuestToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
GetHardwareForVirtualGuest getHardwareForVirtualGuest, GetImageForVirtualGuest getImageForVirtualGuest, GetHardwareForVirtualGuest hardware, GetImageForVirtualGuest images,
GroupNamingConvention.Factory namingConvention) { GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.findLocationForVirtualGuest = checkNotNull(findLocationForVirtualGuest, "findLocationForVirtualGuest"); this.locations = checkNotNull(locations, "locations");
this.getHardwareForVirtualGuest = checkNotNull(getHardwareForVirtualGuest, "getHardwareForVirtualGuest"); this.hardware = checkNotNull(hardware, "hardware");
this.getImageForVirtualGuest = checkNotNull(getImageForVirtualGuest, "getImageForVirtualGuest"); this.images = checkNotNull(images, "images");
} }
@Override @Override
@ -81,18 +81,18 @@ public class VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMe
builder.ids(from.getId() + ""); builder.ids(from.getId() + "");
builder.name(from.getHostname()); builder.name(from.getHostname());
builder.hostname(from.getHostname()); builder.hostname(from.getHostname());
builder.location(findLocationForVirtualGuest.apply(from)); if (from.getDatacenter() != null)
builder.location(from(locations.get()).firstMatch(
LocationPredicates.idEquals(from.getDatacenter().getId() + "")).orNull());
builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getHostname())); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getHostname()));
Image image = getImageForVirtualGuest.getImage(from); Image image = images.getImage(from);
if (image != null) { if (image != null) {
builder.imageId(image.getId()); builder.imageId(image.getId());
builder.operatingSystem(image.getOperatingSystem()); builder.operatingSystem(image.getOperatingSystem());
} }
Hardware hardware = getHardwareForVirtualGuest.getHardware(from); builder.hardware(hardware.getHardware(from));
if (hardware != null)
builder.hardware(hardware);
builder.status(serverStateToNodeStatus.get(from.getPowerState().getKeyName())); builder.status(serverStateToNodeStatus.get(from.getPowerState().getKeyName()));
@ -104,23 +104,6 @@ public class VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMe
return builder.build(); return builder.build();
} }
@Singleton
public static class FindLocationForVirtualGuest extends FindResourceInSet<VirtualGuest, Location> {
@Inject
public FindLocationForVirtualGuest(@Memoized Supplier<Set<? extends Location>> 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 @Singleton
public static class GetHardwareForVirtualGuest { public static class GetHardwareForVirtualGuest {

View File

@ -28,13 +28,12 @@ import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.softlayer.SoftLayerClient; import org.jclouds.softlayer.SoftLayerClient;
import org.jclouds.softlayer.compute.functions.VirtualGuestToNodeMetadata.FindLocationForVirtualGuest;
import org.jclouds.softlayer.domain.VirtualGuest; import org.jclouds.softlayer.domain.VirtualGuest;
import org.jclouds.softlayer.parse.ParseBadVirtualGuest; import org.jclouds.softlayer.parse.ParseBadVirtualGuest;
import org.jclouds.softlayer.parse.ParseVirtualGuestHaltedTest; import org.jclouds.softlayer.parse.ParseVirtualGuestHaltedTest;
@ -67,8 +66,8 @@ public class VirtualGuestToNodeMetadataTest {
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(expectedLocation)); .<Location> of(expectedLocation));
VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(
locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
NodeMetadata node = parser.apply(guest); NodeMetadata node = parser.apply(guest);
@ -94,8 +93,8 @@ public class VirtualGuestToNodeMetadataTest {
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of()); .<Location> of());
VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier,
locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
NodeMetadata node = parser.apply(guest); NodeMetadata node = parser.apply(guest);
@ -119,8 +118,8 @@ public class VirtualGuestToNodeMetadataTest {
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(expectedLocation)); .<Location> of(expectedLocation));
VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier,
locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
NodeMetadata node = parser.apply(guest); NodeMetadata node = parser.apply(guest);
@ -147,8 +146,8 @@ public class VirtualGuestToNodeMetadataTest {
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(expectedLocation)); .<Location> of(expectedLocation));
VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier,
locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
NodeMetadata node = parser.apply(guest); NodeMetadata node = parser.apply(guest);
@ -175,8 +174,8 @@ public class VirtualGuestToNodeMetadataTest {
Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(expectedLocation)); .<Location> of(expectedLocation));
VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(new FindLocationForVirtualGuest( VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier,
locationSupplier), new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
NodeMetadata node = parser.apply(guest); NodeMetadata node = parser.apply(guest);

View File

@ -19,6 +19,7 @@
package org.jclouds.servermanager.compute.functions; package org.jclouds.servermanager.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.FluentIterable.from;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -26,17 +27,19 @@ import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.functions.GroupNamingConvention; 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.Credentials;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.domain.LoginCredentials; import org.jclouds.domain.LoginCredentials;
import org.jclouds.location.predicates.LocationPredicates;
import org.jclouds.servermanager.Server; import org.jclouds.servermanager.Server;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -57,21 +60,21 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
.put(Server.Status.UNRECOGNIZED, Status.UNRECOGNIZED)// .put(Server.Status.UNRECOGNIZED, Status.UNRECOGNIZED)//
.build(); .build();
private final FindHardwareForServer findHardwareForServer; private final Supplier<Set<? extends Hardware>> hardware;
private final FindLocationForServer findLocationForServer; private final Supplier<Set<? extends Location>> locations;
private final FindImageForServer findImageForServer; private final Supplier<Set<? extends Image>> images;
private final Map<String, Credentials> credentialStore; private final Map<String, Credentials> credentialStore;
private final GroupNamingConvention nodeNamingConvention; private final GroupNamingConvention nodeNamingConvention;
@Inject @Inject
ServerToNodeMetadata(Map<String, Credentials> credentialStore, FindHardwareForServer findHardwareForServer, ServerToNodeMetadata(Map<String, Credentials> credentialStore, @Memoized Supplier<Set<? extends Hardware>> hardware,
FindLocationForServer findLocationForServer, FindImageForServer findImageForServer, @Memoized Supplier<Set<? extends Location>> locations, @Memoized Supplier<Set<? extends Image>> images,
GroupNamingConvention.Factory namingConvention) { GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix(); this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.credentialStore = checkNotNull(credentialStore, "credentialStore"); this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.findHardwareForServer = checkNotNull(findHardwareForServer, "findHardwareForServer"); this.hardware = checkNotNull(hardware, "hardware");
this.findLocationForServer = checkNotNull(findLocationForServer, "findLocationForServer"); this.locations = checkNotNull(locations, "locations");
this.findImageForServer = checkNotNull(findImageForServer, "findImageForServer"); this.images = checkNotNull(images, "images");
} }
@Override @Override
@ -80,59 +83,17 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
NodeMetadataBuilder builder = new NodeMetadataBuilder(); NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.id + ""); builder.ids(from.id + "");
builder.name(from.name); 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.group(nodeNamingConvention.groupInUniqueNameOrNull(from.name));
builder.imageId(from.imageId + ""); builder.imageId(from.imageId + "");
Image image = findImageForServer.apply(from); Image image = from(images.get()).firstMatch(ImagePredicates.idEquals(from.imageId + "")).orNull();
if (image != null) if (image != null)
builder.operatingSystem(image.getOperatingSystem()); 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.status(serverStatusToNodeStatus.get(from.status));
builder.publicAddresses(ImmutableSet.<String> of(from.publicAddress)); builder.publicAddresses(ImmutableSet.<String> of(from.publicAddress));
builder.privateAddresses(ImmutableSet.<String> of(from.privateAddress)); builder.privateAddresses(ImmutableSet.<String> of(from.privateAddress));
builder.credentials(LoginCredentials.fromCredentials(credentialStore.get(from.id + ""))); builder.credentials(LoginCredentials.fromCredentials(credentialStore.get(from.id + "")));
return builder.build(); return builder.build();
} }
@Singleton
public static class FindHardwareForServer extends FindResourceInSet<Server, Hardware> {
@Inject
public FindHardwareForServer(@Memoized Supplier<Set<? extends Hardware>> hardware) {
super(hardware);
}
@Override
public boolean matches(Server from, Hardware input) {
return input.getProviderId().equals(from.hardwareId + "");
}
}
@Singleton
public static class FindImageForServer extends FindResourceInSet<Server, Image> {
@Inject
public FindImageForServer(@Memoized Supplier<Set<? extends Image>> hardware) {
super(hardware);
}
@Override
public boolean matches(Server from, Image input) {
return input.getProviderId().equals(from.imageId + "");
}
}
@Singleton
public static class FindLocationForServer extends FindResourceInSet<Server, Location> {
@Inject
public FindLocationForServer(@Memoized Supplier<Set<? extends Location>> hardware) {
super(hardware);
}
@Override
public boolean matches(Server from, Location input) {
return input.getId().equals(from.datacenter + "");
}
}
} }