change to use vm -> node mapping

This commit is contained in:
danikov 2012-04-23 17:07:29 +01:00
parent 401d2f675f
commit ef79f94e1d
4 changed files with 27 additions and 29 deletions

View File

@ -29,23 +29,19 @@ import org.jclouds.compute.predicates.ImagePredicates;
import org.jclouds.domain.Location;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.Link;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.VApp;
import org.jclouds.vcloud.director.v1_5.domain.Vm;
import org.jclouds.vcloud.director.v1_5.domain.section.VirtualHardwareSection;
import org.jclouds.vcloud.director.v1_5.functions.SectionForVApp;
import org.jclouds.vcloud.director.v1_5.predicates.LinkPredicates;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.inject.name.Named;
/**
* @author Adrian Cole
*/
public class HardwareForVApp implements Function<VApp, Hardware> {
public class HardwareForVm implements Function<Vm, Hardware> {
@Resource
protected Logger logger = Logger.NULL;
@ -55,7 +51,7 @@ public class HardwareForVApp implements Function<VApp, Hardware> {
private final SectionForVApp<VirtualHardwareSection> findVirtualHardwareSectionForVm;
@Inject
protected HardwareForVApp(Function<Reference, Location> findLocationForResource,
protected HardwareForVm(Function<Reference, Location> findLocationForResource,
VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder,
SectionForVApp<VirtualHardwareSection> findVirtualHardwareSectionForVm) {
this.findLocationForResource = checkNotNull(findLocationForResource, "findLocationForResource");
@ -64,14 +60,13 @@ public class HardwareForVApp implements Function<VApp, Hardware> {
}
@Override
public Hardware apply(VApp from) {
public Hardware apply(Vm from) {
checkNotNull(from, "VApp");
// TODO make this work with composite vApps
Vm vm = from.getChildren().getVms().size() == 0 ? null : Iterables.get(from.getChildren().getVms(), 0);
if (vm == null)
if (from == null)
return null;
VirtualHardwareSection hardware = findVirtualHardwareSectionForVm.apply(vm);
VirtualHardwareSection hardware = findVirtualHardwareSectionForVm.apply(from);
HardwareBuilder builder = rasdToHardwareBuilder.apply(hardware.getItems());
builder.location(findLocationForResource.apply(Iterables.find(checkNotNull(from, "from").getLinks(),
LinkPredicates.typeEquals(VCloudDirectorMediaType.VDC))));

View File

@ -21,9 +21,9 @@ package org.jclouds.vcloud.director.v1_5.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Iterables.filter;
import static org.jclouds.vcloud.director.v1_5.compute.util.VCloudComputeUtils.getCredentialsFrom;
import static org.jclouds.vcloud.director.v1_5.compute.util.VCloudComputeUtils.getIpsFromVApp;
import static org.jclouds.vcloud.director.v1_5.compute.util.VCloudComputeUtils.toComputeOs;
import static org.jclouds.vcloud.director.v1_5.compute.util.VCloudDirectorComputeUtils.getCredentialsFrom;
import static org.jclouds.vcloud.director.v1_5.compute.util.VCloudDirectorComputeUtils.getIpsFromVm;
import static org.jclouds.vcloud.director.v1_5.compute.util.VCloudDirectorComputeUtils.toComputeOs;
import java.util.Map;
import java.util.Set;
@ -42,7 +42,7 @@ import org.jclouds.logging.Logger;
import org.jclouds.util.InetAddresses2.IsPrivateIPAddress;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.ResourceEntity.Status;
import org.jclouds.vcloud.director.v1_5.domain.VApp;
import org.jclouds.vcloud.director.v1_5.domain.Vm;
import org.jclouds.vcloud.director.v1_5.predicates.LinkPredicates;
import com.google.common.base.Function;
@ -52,28 +52,28 @@ import com.google.common.collect.Iterables;
* @author Adrian Cole
*/
@Singleton
public class VAppToNodeMetadata implements Function<VApp, NodeMetadata> {
public class VmToNodeMetadata implements Function<Vm, NodeMetadata> {
@Resource
protected static Logger logger = Logger.NULL;
protected final FindLocationForResource findLocationForResourceInVDC;
protected final Function<VApp, Hardware> hardwareForVApp;
protected final Function<Vm, Hardware> hardwareForVm;
protected final Map<Status, NodeState> vAppStatusToNodeState;
protected final Map<String, Credentials> credentialStore;
protected final GroupNamingConvention nodeNamingConvention;
@Inject
protected VAppToNodeMetadata(Map<Status, NodeState> vAppStatusToNodeState, Map<String, Credentials> credentialStore,
FindLocationForResource findLocationForResourceInVDC, Function<VApp, Hardware> hardwareForVApp,
protected VmToNodeMetadata(Map<Status, NodeState> vAppStatusToNodeState, Map<String, Credentials> credentialStore,
FindLocationForResource findLocationForResourceInVDC, Function<Vm, Hardware> hardwareForVm,
GroupNamingConvention.Factory namingConvention) {
this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
this.hardwareForVApp = checkNotNull(hardwareForVApp, "hardwareForVApp");
this.hardwareForVm = checkNotNull(hardwareForVm, "hardwareForVm");
this.findLocationForResourceInVDC = checkNotNull(findLocationForResourceInVDC, "findLocationForResourceInVDC");
this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.vAppStatusToNodeState = checkNotNull(vAppStatusToNodeState, "vAppStatusToNodeState");
}
public NodeMetadata apply(VApp from) {
public NodeMetadata apply(Vm from) {
NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getHref().toASCIIString());
builder.uri(from.getHref());
@ -82,10 +82,10 @@ public class VAppToNodeMetadata implements Function<VApp, NodeMetadata> {
builder.location(findLocationForResourceInVDC.apply(
Iterables.find(from.getLinks(), LinkPredicates.typeEquals(VCloudDirectorMediaType.VDC))));
builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getName()));
builder.operatingSystem(toComputeOs(from, null));
builder.hardware(hardwareForVApp.apply(from));
builder.operatingSystem(toComputeOs(from));
builder.hardware(hardwareForVm.apply(from));
builder.state(vAppStatusToNodeState.get(from.getStatus()));
Set<String> addresses = getIpsFromVApp(from);
Set<String> addresses = getIpsFromVm(from);
builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE)));
builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));

View File

@ -48,7 +48,7 @@ import com.google.inject.Inject;
*
* @author danikov
*/
public class VCloudComputeUtils {
public class VCloudDirectorComputeUtils {
public static OperatingSystem toComputeOs(VApp vApp, OperatingSystem defaultOs) {
CIMOperatingSystem cimOs = toComputeOs(vApp);
@ -109,8 +109,11 @@ public class VCloudComputeUtils {
// TODO make this work with composite vApps
if (vApp.getChildren().getVms().size() == 0)
return ImmutableSet.of();
return getIpsFromVm(Iterables.get(vApp.getChildren().getVms(), 0));
}
public static Set<String> getIpsFromVm(Vm vm) {
Builder<String> ips = ImmutableSet.builder();
Vm vm = Iterables.get(vApp.getChildren().getVms(), 0);
// TODO: figure out how to differentiate public from private ip addresses
// assumption is that we'll do this from the network object, which may
// have
@ -119,7 +122,7 @@ public class VCloudComputeUtils {
// parsing. At worst, we could have properties set per cloud provider to
// declare the networks which are public, then check against these in
// networkconnection.getNetwork
NetworkConnectionSection networkConnectionSection = findNetworkConnectionSectionForVApp.apply(vApp);
NetworkConnectionSection networkConnectionSection = findNetworkConnectionSectionForVApp.apply(vm);
if (networkConnectionSection != null) {
for (NetworkConnection connection : networkConnectionSection.getNetworkConnections()) {
if (connection.getIpAddress() != null)
@ -128,7 +131,7 @@ public class VCloudComputeUtils {
ips.add(connection.getExternalIpAddress());
}
} else {
for (ResourceAllocationSettingData net : filter(findVirtualHardwareSectionForVApp.apply(vApp).getItems(),
for (ResourceAllocationSettingData net : filter(findVirtualHardwareSectionForVApp.apply(vm).getItems(),
CIMPredicates.resourceTypeIn(ResourceType.ETHERNET_ADAPTER))) {
// FIXME: not yet implemented
// if (net instanceof VCloudNetworkAdapter) {

View File

@ -43,7 +43,7 @@ import org.jclouds.rest.internal.RestContextImpl;
import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminAsyncClient;
import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminClient;
import org.jclouds.vcloud.director.v1_5.annotations.Login;
import org.jclouds.vcloud.director.v1_5.compute.util.VCloudComputeUtils;
import org.jclouds.vcloud.director.v1_5.compute.util.VCloudDirectorComputeUtils;
import org.jclouds.vcloud.director.v1_5.domain.Session;
import org.jclouds.vcloud.director.v1_5.domain.SessionWithToken;
import org.jclouds.vcloud.director.v1_5.features.CatalogAsyncClient;
@ -174,7 +174,7 @@ public class VCloudDirectorRestClientModule extends RestClientModule<VCloudDirec
bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(InvalidateSessionAndRetryOn401AndLogoutOnClose.class);
requestStaticInjection(VCloudComputeUtils.class);
requestStaticInjection(VCloudDirectorComputeUtils.class);
super.configure();
}