Merge pull request #1130 from jclouds/remove-findresourceinset

Remove findresourceinset
This commit is contained in:
Adrian Cole 2012-12-31 23:58:42 -08:00
commit f9cbaa0973
21 changed files with 232 additions and 484 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.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<Function<Server, String>>() {
}).to(GetImageIdFromServer.class);
bind(new TypeLiteral<Function<String, Image>>() {
}).to(FindImageForId.class);
bind(new TypeLiteral<Function<String, OsFamilyVersion64Bit>>() {
}).to(ParseOsFamilyVersion64BitFromImageName.class);
bind(TemplateBuilder.class)

View File

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

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudsigma.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;
@ -32,7 +33,6 @@ import org.jclouds.cloudsigma.domain.DriveInfo;
import org.jclouds.cloudsigma.domain.Server;
import org.jclouds.cloudsigma.domain.ServerInfo;
import org.jclouds.cloudsigma.domain.ServerStatus;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image;
@ -47,8 +47,10 @@ import org.jclouds.domain.Location;
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<ServerInfo, NodeMetada
.build();
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 Function<Device, Volume> deviceToVolume;
private final GroupNamingConvention nodeNamingConvention;
@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,
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<ServerInfo, NodeMetada
String imageId = getImageIdFromServer.apply(from);
if (imageId != null) {
Image image = findImageForId.apply(imageId);
if (image != null) {
builder.operatingSystem(image.getOperatingSystem());
Optional<? extends Image> 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<ServerInfo, NodeMetada
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;
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<Template, Image> {
private final FindLocationForTemplate findLocationForTemplate;
private final Supplier<Set<? extends Location>> locations;
private final Function<Template, OperatingSystem> templateToOperatingSystem;
@Inject
public TemplateToImage(FindLocationForTemplate findLocationForTemplate,
public TemplateToImage(@Memoized Supplier<Set<? extends Location>> locations,
Function<Template, OperatingSystem> 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<Template, Image> {
.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<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.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<VirtualMachine, No
.put(VirtualMachine.State.SHUTDOWNED, Status.PENDING)
.put(VirtualMachine.State.UNRECOGNIZED, Status.UNRECOGNIZED).build();
private final FindLocationForVirtualMachine findLocationForVirtualMachine;
private final FindImageForVirtualMachine findImageForVirtualMachine;
private final Supplier<Set<? extends Location>> locations;
private final Supplier<Set<? extends Image>> images;
private final LoadingCache<String, Set<IPForwardingRule>> getIPForwardingRulesByVirtualMachine;
private final GroupNamingConvention nodeNamingConvention;
@Inject
VirtualMachineToNodeMetadata(FindLocationForVirtualMachine findLocationForVirtualMachine,
FindImageForVirtualMachine findImageForVirtualMachine,
VirtualMachineToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
@Memoized Supplier<Set<? extends Image>> images,
LoadingCache<String, Set<IPForwardingRule>> 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<VirtualMachine, No
// we set displayName to the same value as name, but this could be wrong
// on hosts not started with jclouds
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()));
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) {
builder.imageId(image.getId());
builder.operatingSystem(image.getOperatingSystem());
@ -170,50 +177,4 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
}
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 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 {
.<Set<? extends Location>> ofInstance(ImmutableSet.<Location> 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)")

View File

@ -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<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine(
locationSupplier), new FindImageForVirtualMachine(
imageSupplier), CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
new CacheLoader<String, Set<IPForwardingRule>>() {
@Override
public Set<IPForwardingRule> 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().<String, Set<IPForwardingRule>> build(
new CacheLoader<String, Set<IPForwardingRule>>() {
@Override
public Set<IPForwardingRule> 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<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine(
locationSupplier), new FindImageForVirtualMachine(
imageSupplier), CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
new CacheLoader<String, Set<IPForwardingRule>>() {
@Override
public Set<IPForwardingRule> load(String arg0) throws Exception {
return ImmutableSet.of();
}
}), namingConvention);
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
new CacheLoader<String, Set<IPForwardingRule>>() {
@Override
public Set<IPForwardingRule> load(String arg0) throws Exception {
return ImmutableSet.of();
}
}), namingConvention);
VirtualMachine guest =VirtualMachine.builder()
.id("54")
@ -166,17 +159,16 @@ public class VirtualMachineToNodeMetadataTest {
Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(new FindLocationForVirtualMachine(
locationSupplier), new FindImageForVirtualMachine(
imageSupplier), CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
new CacheLoader<String, Set<IPForwardingRule>>() {
VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
new CacheLoader<String, Set<IPForwardingRule>>() {
@Override
public Set<IPForwardingRule> load(String arg0) throws Exception {
throw new ResourceNotFoundException("no ip forwarding rule for: " + arg0);
}
@Override
public Set<IPForwardingRule> 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);

View File

@ -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<Function<Server, String>>() {
}).to(GetImageIdFromServer.class);
bind(new TypeLiteral<Function<String, Image>>() {
}).to(FindImageForId.class);
bind(new TypeLiteral<Function<DriveInfo, Image>>() {
}).to(WellKnownImageToImage.class);
}

View File

@ -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<ServerInfo, NodeMetada
.build();
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 Function<Device, Volume> deviceToVolume;
private final GroupNamingConvention nodeNamingConvention;
@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,
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<ServerInfo, NodeMetada
builder.userMetadata(from.getUserMetadata());
String imageId = getImageIdFromServer.apply(from);
if (imageId != null) {
Image image = findImageForId.apply(imageId);
if (image != null) {
builder.operatingSystem(image.getOperatingSystem());
Optional<? extends Image> 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<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

@ -0,0 +1,51 @@
/**
* 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.compute.predicates;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.compute.domain.Hardware;
import com.google.common.base.Predicate;
/**
* Container for hardware filters (predicates).
*
* This class has static methods that create customized predicates to use with
* {@link org.jclouds.compute.ComputeService}.
*
* @author Adrian Cole
*/
public class HardwarePredicates {
public static Predicate<Hardware> idEquals(final String id) {
checkNotNull(id, "id must be defined");
return new Predicate<Hardware>() {
@Override
public boolean apply(Hardware hardware) {
return id.equals(hardware.getId());
}
@Override
public String toString() {
return "idEquals(" + id + ")";
}
};
}
}

View File

@ -16,33 +16,29 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.glesys.compute.functions;
package org.jclouds.compute.predicates;
import java.util.Set;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.Hardware;
import org.testng.annotations.Test;
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;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
@Singleton
public class FindLocationForServerSpec extends FindResourceInSet<ServerSpec, Location> {
@Test
public class HardwarePredicatesTest {
ComputeService computeService = ContextBuilder.newBuilder("stub").build(ComputeServiceContext.class).getComputeService();
@Inject
public FindLocationForServerSpec(@Memoized Supplier<Set<? extends Location>> location) {
super(location);
public void testHardwareId() {
Hardware first = Iterables.get(computeService.listHardwareProfiles(), 0);
assert HardwarePredicates.idEquals(first.getId()).apply(first);
Hardware second = Iterables.get(computeService.listHardwareProfiles(), 1);
assert !HardwarePredicates.idEquals(first.getId()).apply(second);
}
@Override
public boolean matches(ServerSpec from, Location input) {
return input.getId().equals(from.getDatacenter());
}
}

View File

@ -29,7 +29,6 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
/**
* Tests possible uses of OperatingSystemPredicates
*
* @author Adrian Cole
*/

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.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<VM, NodeMetadata> {
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<Set<? extends Location>> locations;
private final GroupNamingConvention nodeNamingConvention;
@Inject
VMToNodeMetadata(FindLocationForVM findLocationForVM,
VMToNodeMetadata(@Memoized Supplier<Set<? extends Location>> 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<VM, NodeMetadata> {
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<VM, NodeMetadata> {
builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));
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;
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<VM, NodeMetadata> {
.put(VM.State.INCOMPLETE, NodeMetadata.Status.PENDING)//
.build();
private final FindHardwareForServer findHardwareForServer;
private final FindLocationForServer findLocationForServer;
private final FindImageForServer findImageForServer;
private final Supplier<Set<? extends Hardware>> hardware;
private final Supplier<Set<? extends Location>> locations;
private final Supplier<Set<? extends Image>> images;
private final Map<String, Credentials> credentialStore;
private final GroupNamingConvention nodeNamingConvention;
@Inject
VMToNodeMetadata(Map<String, Credentials> credentialStore, FindHardwareForServer findHardwareForServer,
FindLocationForServer findLocationForServer, FindImageForServer findImageForServer,
GroupNamingConvention.Factory namingConvention) {
VMToNodeMetadata(Map<String, Credentials> credentialStore, @Memoized Supplier<Set<? extends Hardware>> hardware,
@Memoized Supplier<Set<? extends Location>> locations, @Memoized Supplier<Set<? extends Image>> 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<VM, NodeMetadata> {
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<VM, NodeMetadata> {
builder.credentials(LoginCredentials.fromCredentials(credentialStore.get(from.getUuid() + "")));
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 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<Server
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
public Iterable<Hardware> listHardwareProfiles() {
Set<? extends Location> locationsSet = locations.get();
@ -224,27 +209,24 @@ public class GleSYSComputeServiceAdapter implements ComputeServiceAdapter<Server
@Override
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
public Future<ServerDetails> apply(Server from) {
return aapi.getServerApi().get(from.getId());
}
}, userThreads, null, logger, "server details"));
}, userThreads, null, logger, "server details");
}
@Override
public Set<String> listLocations() {
return ImmutableSet.copyOf(Iterables.concat(Iterables.transform(api.getServerApi()
.getAllowedArgumentsForCreateByPlatform().values(),
new Function<AllowedArgumentsForCreateServer, Set<String>>() {
return FluentIterable.from(api.getServerApi().getAllowedArgumentsForCreateByPlatform().values())
.transformAndConcat(new Function<AllowedArgumentsForCreateServer, Set<String>>() {
@Override
public Set<String> apply(AllowedArgumentsForCreateServer arg0) {
return arg0.getDataCenters();
}
})));
}).toSet();
}
@Override

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.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<ServerDetails, Node
.put(ServerDetails.State.UNRECOGNIZED, Status.UNRECOGNIZED).build();
protected final Supplier<Set<? extends Image>> images;
protected final FindLocationForServerDetails findLocationForServerDetails;
protected final Supplier<Set<? extends Location>> locations;
protected final GroupNamingConvention nodeNamingConvention;
private static class FindImageForServer implements Predicate<Image> {
@ -95,11 +96,10 @@ public class ServerDetailsToNodeMetadata implements Function<ServerDetails, Node
}
@Inject
ServerDetailsToNodeMetadata(FindLocationForServerDetails findLocationForServerDetails,
@Memoized Supplier<Set<? extends Image>> images,
GroupNamingConvention.Factory namingConvention) {
ServerDetailsToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
@Memoized Supplier<Set<? extends Image>> 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<ServerDetails, Node
builder.ids(from.getId() + "");
builder.name(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);
builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getHostname()));
@ -148,18 +148,4 @@ public class ServerDetailsToNodeMetadata implements Function<ServerDetails, Node
}
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;
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<ServerSpec, Hardware> {
private final FindLocationForServerSpec findLocationForServerSpec;
private final Supplier<Set<? extends Location>> locations;
@Inject
ServerSpecToHardware(FindLocationForServerSpec findLocationForServerSpec) {
this.findLocationForServerSpec = checkNotNull(findLocationForServerSpec, "findLocationForServerSpec");
ServerSpecToHardware(@Memoized Supplier<Set<? extends Location>> 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(

View File

@ -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<VirtualGuest, NodeMe
.put(VirtualGuest.State.PAUSED, Status.SUSPENDED).put(VirtualGuest.State.RUNNING, Status.RUNNING)
.put(VirtualGuest.State.UNRECOGNIZED, Status.UNRECOGNIZED).build();
private final FindLocationForVirtualGuest findLocationForVirtualGuest;
private final GetHardwareForVirtualGuest getHardwareForVirtualGuest;
private final GetImageForVirtualGuest getImageForVirtualGuest;
private final Supplier<Set<? extends Location>> locations;
private final GetHardwareForVirtualGuest hardware;
private final GetImageForVirtualGuest images;
private final GroupNamingConvention nodeNamingConvention;
@Inject
VirtualGuestToNodeMetadata(FindLocationForVirtualGuest findLocationForVirtualGuest,
GetHardwareForVirtualGuest getHardwareForVirtualGuest, GetImageForVirtualGuest getImageForVirtualGuest,
VirtualGuestToNodeMetadata(@Memoized Supplier<Set<? extends Location>> 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<VirtualGuest, NodeMe
builder.ids(from.getId() + "");
builder.name(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()));
Image image = getImageForVirtualGuest.getImage(from);
Image image = images.getImage(from);
if (image != null) {
builder.imageId(image.getId());
builder.operatingSystem(image.getOperatingSystem());
}
Hardware hardware = getHardwareForVirtualGuest.getHardware(from);
if (hardware != null)
builder.hardware(hardware);
builder.hardware(hardware.getHardware(from));
builder.status(serverStateToNodeStatus.get(from.getPowerState().getKeyName()));
@ -104,23 +104,6 @@ public class VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMe
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
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.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<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> 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<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> 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<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> 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<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> 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<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> 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);

View File

@ -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<Server, NodeMetadata> {
.put(Server.Status.UNRECOGNIZED, Status.UNRECOGNIZED)//
.build();
private final FindHardwareForServer findHardwareForServer;
private final FindLocationForServer findLocationForServer;
private final FindImageForServer findImageForServer;
private final Supplier<Set<? extends Hardware>> hardware;
private final Supplier<Set<? extends Location>> locations;
private final Supplier<Set<? extends Image>> images;
private final Map<String, Credentials> credentialStore;
private final GroupNamingConvention nodeNamingConvention;
@Inject
ServerToNodeMetadata(Map<String, Credentials> credentialStore, FindHardwareForServer findHardwareForServer,
FindLocationForServer findLocationForServer, FindImageForServer findImageForServer,
ServerToNodeMetadata(Map<String, Credentials> credentialStore, @Memoized Supplier<Set<? extends Hardware>> hardware,
@Memoized Supplier<Set<? extends Location>> locations, @Memoized Supplier<Set<? extends Image>> 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<Server, NodeMetadata> {
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.<String> of(from.publicAddress));
builder.privateAddresses(ImmutableSet.<String> of(from.privateAddress));
builder.credentials(LoginCredentials.fromCredentials(credentialStore.get(from.id + "")));
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 + "");
}
}
}