Merge pull request #579 from danikov/vcloud-director-endpoints-and-predicates

Issue 830: vCloud director functions, endpoints and predicates
This commit is contained in:
Adrian Cole 2012-04-30 07:59:58 -07:00
commit 6923a11ce7
14 changed files with 924 additions and 20 deletions

View File

@ -0,0 +1,59 @@
/**
* 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.dmtf;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Set;
import org.jclouds.dmtf.cim.ResourceAllocationSettingData;
import org.jclouds.dmtf.cim.ResourceAllocationSettingData.ResourceType;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
/**
*
* @author danikov
*/
public class CIMPredicates {
/**
* Return resource allocations of the specific type.
*
* @param type
* type to match the items
* @return predicate
*/
public static Predicate<ResourceAllocationSettingData> resourceTypeIn(final ResourceType... types) {
checkNotNull(types, "resourceTypes");
final Set<ResourceType> resourceTypes = ImmutableSet.copyOf(types);
return new Predicate<ResourceAllocationSettingData>() {
@Override
public boolean apply(ResourceAllocationSettingData in) {
return resourceTypes.contains(in.getResourceType());
}
@Override
public String toString() {
return "resourceTypeIn(" + resourceTypes + ")";
}
};
}
}

View File

@ -0,0 +1,75 @@
/**
* 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.dmtf.cim.functions;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.transform;
import javax.inject.Singleton;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.internal.VolumeImpl;
import org.jclouds.dmtf.CIMPredicates;
import org.jclouds.dmtf.cim.ResourceAllocationSettingData;
import org.jclouds.dmtf.cim.ResourceAllocationSettingData.ResourceType;
import com.google.common.base.Function;
/**
* @author Adrian Cole
*/
@Singleton
public class HardwareBuilderFromResourceAllocations implements
Function<Iterable<? extends ResourceAllocationSettingData>, HardwareBuilder> {
@Override
public HardwareBuilder apply(Iterable<? extends ResourceAllocationSettingData> from) {
HardwareBuilder builder = new HardwareBuilder();
builder.volumes(transform(filter(from, CIMPredicates.resourceTypeIn(ResourceType.DISK_DRIVE,
ResourceType.BASE_PARTITIONABLE_UNIT, ResourceType.PARTITIONABLE_UNIT)),
new Function<ResourceAllocationSettingData, Volume>() {
@Override
public Volume apply(ResourceAllocationSettingData from) {
return HardwareBuilderFromResourceAllocations.this.apply(from);
}
}));
builder.ram((int) find(from, CIMPredicates.resourceTypeIn(ResourceType.MEMORY)).getVirtualQuantity().longValue());
builder.processors(transform(filter(from, CIMPredicates.resourceTypeIn(ResourceType.PROCESSOR)),
new Function<ResourceAllocationSettingData, Processor>() {
@Override
public Processor apply(ResourceAllocationSettingData arg0) {
return new Processor(arg0.getVirtualQuantity().longValue(), 1);
}
}));
return builder;
}
public Volume apply(ResourceAllocationSettingData from) {
return new VolumeImpl(from.getAddressOnParent() + "", Volume.Type.LOCAL, from.getVirtualQuantity() == null ? null
: from.getVirtualQuantity().longValue() / 1024 / 1024f, null, "0".equals(from.getAddressOnParent())
|| ResourceType.BASE_PARTITIONABLE_UNIT.equals(from.getResourceType()), true);
}
}

View File

@ -0,0 +1,71 @@
/**
* 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.vcloud.director.v1_5.compute.functions;
import java.net.URI;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.collect.Memoized;
import org.jclouds.domain.Location;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
/**
* @author danikov
*/
@Singleton
public class FindLocationForResource implements Function<Reference, Location> {
@Resource
protected Logger logger = Logger.NULL;
final Supplier<Set<? extends Location>> locations;
@Inject
public FindLocationForResource(@Memoized Supplier<Set<? extends Location>> locations) {
this.locations = locations;
}
/**
* searches for a location associated with this resource.
*
* @throws NoSuchElementException
* if not found
*/
public Location apply(Reference resource) {
for (Location input : locations.get()) {
do {
// The "name" isn't always present, ex inside a vApp we have a rel
// link that only includes href and type.
if (URI.create(input.getId()).equals(resource.getHref()))
return input;
} while ((input = input.getParent()) != null);
}
throw new NoSuchElementException(String.format("resource: %s not found in locations: %s", resource,
locations.get()));
}
}

View File

@ -0,0 +1,90 @@
/**
* 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.vcloud.director.v1_5.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.predicates.ImagePredicates;
import org.jclouds.dmtf.ovf.VirtualHardwareSection;
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.VAppTemplate;
import org.jclouds.vcloud.director.v1_5.domain.dmtf.Envelope;
import org.jclouds.vcloud.director.v1_5.predicates.LinkPredicates;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* @author Adrian Cole
*/
@Singleton
public class HardwareForVAppTemplate implements Function<VAppTemplate, Hardware> {
@Resource
protected Logger logger = Logger.NULL;
private final Function<VAppTemplate, Envelope> templateToEnvelope;
private final FindLocationForResource findLocationForResource;
private final VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder;
@Inject
protected HardwareForVAppTemplate(Function<VAppTemplate, Envelope> templateToEnvelope,
FindLocationForResource findLocationForResource,
VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder) {
this.templateToEnvelope = checkNotNull(templateToEnvelope, "templateToEnvelope");
this.findLocationForResource = checkNotNull(findLocationForResource, "findLocationForResource");
this.rasdToHardwareBuilder = checkNotNull(rasdToHardwareBuilder, "rasdToHardwareBuilder");
}
@Override
public Hardware apply(VAppTemplate from) {
checkNotNull(from, "VAppTemplate");
Envelope ovf = templateToEnvelope.apply(from);
if (ovf.getVirtualSystem().getVirtualHardwareSections().size() > 1) {
logger.warn("multiple hardware choices found. using first", ovf);
}
VirtualHardwareSection hardware = Iterables.get(ovf.getVirtualSystem().getVirtualHardwareSections(), 0);
HardwareBuilder builder = rasdToHardwareBuilder.apply(hardware.getItems());
Link vdc = Iterables.find(checkNotNull(from, "from").getLinks(), LinkPredicates.typeEquals(VCloudDirectorMediaType.VDC));
if (vdc != null) {
builder.location(findLocationForResource.apply(vdc));
} else {
// otherwise, it could be in a public catalog, which is not assigned to a VDC
}
builder.ids(from.getHref().toASCIIString()).name(from.getName()).supportsImage(
ImagePredicates.idEquals(from.getHref().toASCIIString()));
builder.hypervisor("VMware");
return builder.build();
}
protected String getName(String name) {
return name;
}
}

View File

@ -0,0 +1,78 @@
/**
* 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.vcloud.director.v1_5.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Resource;
import javax.inject.Inject;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
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.Reference;
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.collect.Iterables;
/**
* @author Adrian Cole
*/
public class HardwareForVm implements Function<Vm, Hardware> {
@Resource
protected Logger logger = Logger.NULL;
private final Function<Reference, Location> findLocationForResource;
private final VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder;
private final SectionForVApp<VirtualHardwareSection> findVirtualHardwareSectionForVm;
@Inject
protected HardwareForVm(Function<Reference, Location> findLocationForResource,
VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder,
SectionForVApp<VirtualHardwareSection> findVirtualHardwareSectionForVm) {
this.findLocationForResource = checkNotNull(findLocationForResource, "findLocationForResource");
this.rasdToHardwareBuilder = checkNotNull(rasdToHardwareBuilder, "rasdToHardwareBuilder");
this.findVirtualHardwareSectionForVm = checkNotNull(findVirtualHardwareSectionForVm, "findVirtualHardwareSectionForVm");
}
@Override
public Hardware apply(Vm from) {
checkNotNull(from, "VApp");
// TODO make this work with composite vApps
if (from == null)
return null;
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))));
builder.ids(from.getHref().toASCIIString()).name(from.getName()).supportsImage(
ImagePredicates.idEquals(from.getHref().toASCIIString()));
builder.hypervisor("VMware");
return builder.build();
}
}

View File

@ -0,0 +1,82 @@
/**
* 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.vcloud.director.v1_5.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.compute.domain.CIMOperatingSystem;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.ovf.Envelope;
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.VAppTemplate;
import org.jclouds.vcloud.director.v1_5.predicates.LinkPredicates;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* @author Adrian Cole
*/
@Singleton
public class ImageForVAppTemplate implements Function<VAppTemplate, Image> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
public Logger logger = Logger.NULL;
private final Function<VAppTemplate, Envelope> templateToEnvelope;
private final FindLocationForResource findLocationForResource;
@Inject
protected ImageForVAppTemplate(Function<VAppTemplate, Envelope> templateToEnvelope,
FindLocationForResource findLocationForResource) {
this.templateToEnvelope = checkNotNull(templateToEnvelope, "templateToEnvelope");
this.findLocationForResource = checkNotNull(findLocationForResource, "findLocationForResource");
}
@Override
public Image apply(VAppTemplate from) {
checkNotNull(from, "VAppTemplate");
Envelope ovf = templateToEnvelope.apply(from);
ImageBuilder builder = new ImageBuilder();
builder.ids(from.getHref().toASCIIString());
builder.uri(from.getHref());
builder.name(from.getName());
Link vdc = Iterables.find(checkNotNull(from, "from").getLinks(), LinkPredicates.typeEquals(VCloudDirectorMediaType.VDC));
if (vdc != null) {
builder.location(findLocationForResource.apply(vdc));
} else {
// otherwise, it could be in a public catalog, which is not assigned to a VDC
}
builder.description(from.getDescription() != null ? from.getDescription() : from.getName());
builder.operatingSystem(CIMOperatingSystem.toComputeOs(ovf));
return builder.build();
}
}

View File

@ -0,0 +1,43 @@
/**
* 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.vcloud.director.v1_5.compute.functions;
import javax.inject.Singleton;
import org.jclouds.compute.domain.Volume;
import org.jclouds.dmtf.cim.ResourceAllocationSettingData;
import org.jclouds.dmtf.cim.functions.HardwareBuilderFromResourceAllocations;
/**
* @author Adrian Cole
*/
@Singleton
public class VCloudHardwareBuilderFromResourceAllocations extends HardwareBuilderFromResourceAllocations {
@Override
public Volume apply(ResourceAllocationSettingData from) {
// FIXME: not yet implemented
// if (from instanceof VCloudHardDisk) {
// VCloudHardDisk vDisk = VCloudHardDisk.class.cast(from);
// return new VolumeImpl(from.getAddressOnParent() + "", Volume.Type.LOCAL, vDisk.getCapacity() / 1024f, null,
// "0".equals(from.getAddressOnParent()), true);
// } else {
return super.apply(from);
// }
}
}

View File

@ -0,0 +1,84 @@
/**
* 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.vcloud.director.v1_5.compute.functions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI;
import java.util.concurrent.ExecutionException;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.dmtf.ovf.NetworkSection;
import org.jclouds.logging.Logger;
import org.jclouds.ovf.Envelope;
import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate;
import org.jclouds.vcloud.director.v1_5.functions.SectionForVAppTemplate;
import com.google.common.base.Function;
import com.google.common.cache.LoadingCache;
/**
* @author Adrian Cole
*/
@Singleton
public class ValidateVAppTemplateAndReturnEnvelopeOrThrowIllegalArgumentException implements
Function<VAppTemplate, Envelope> {
@Resource
protected Logger logger = Logger.NULL;
private final LoadingCache<URI, Envelope> envelopes;
private final SectionForVAppTemplate<NetworkSection> findNetworkSectionForVAppTemplate;
@Inject
protected ValidateVAppTemplateAndReturnEnvelopeOrThrowIllegalArgumentException(LoadingCache<URI, Envelope> envelopes,
SectionForVAppTemplate<NetworkSection> findNetworkSectionForVAppTemplate) {
this.envelopes = checkNotNull(envelopes, "envelopes");
this.findNetworkSectionForVAppTemplate = checkNotNull(findNetworkSectionForVAppTemplate, "findNetworkSectionForVAppTemplate");
}
@Override
public Envelope apply(VAppTemplate from) {
checkArgument(from.getChildren().size() == 1, "multiple vms are not supported: %s", from);
checkArgument(findNetworkSectionForVAppTemplate.apply(from).getNetworks().size() == 1,
"multiple network connections are not supported: %s", from);
checkArgument(from.isOvfDescriptorUploaded(), "ovf descriptor is not uploaded: %s", from);
Envelope ovf = getOVFForVAppTemplateAndValidate(from);
return ovf;
}
private Envelope getOVFForVAppTemplateAndValidate(VAppTemplate from) throws IllegalArgumentException {
Envelope ovf;
try {
ovf = envelopes.get(from.getHref());
checkArgument(ovf.getVirtualSystem().getVirtualHardwareSections().size() > 0,
"no hardware sections exist in ovf %s", ovf);
} catch (ExecutionException e) {
throw new IllegalArgumentException("no ovf envelope found for: " + from, e);
}
return ovf;
}
}

View File

@ -0,0 +1,99 @@
/**
* 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.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.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;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.domain.Credentials;
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.Vm;
import org.jclouds.vcloud.director.v1_5.predicates.LinkPredicates;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* @author Adrian Cole
*/
@Singleton
public class VmToNodeMetadata implements Function<Vm, NodeMetadata> {
@Resource
protected static Logger logger = Logger.NULL;
protected final FindLocationForResource findLocationForResourceInVDC;
protected final Function<Vm, Hardware> hardwareForVm;
protected final Map<Status, NodeState> vAppStatusToNodeState;
protected final Map<String, Credentials> credentialStore;
protected final GroupNamingConvention nodeNamingConvention;
@Inject
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.hardwareForVm = checkNotNull(hardwareForVm, "hardwareForVm");
this.findLocationForResourceInVDC = checkNotNull(findLocationForResourceInVDC, "findLocationForResourceInVDC");
this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.vAppStatusToNodeState = checkNotNull(vAppStatusToNodeState, "vAppStatusToNodeState");
}
public NodeMetadata apply(Vm from) {
NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getHref().toASCIIString());
builder.uri(from.getHref());
builder.name(from.getName());
builder.hostname(from.getName());
builder.location(findLocationForResourceInVDC.apply(
Iterables.find(from.getLinks(), LinkPredicates.typeEquals(VCloudDirectorMediaType.VDC))));
builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getName()));
builder.operatingSystem(toComputeOs(from));
builder.hardware(hardwareForVm.apply(from));
builder.state(vAppStatusToNodeState.get(from.getStatus()));
Set<String> addresses = getIpsFromVm(from);
builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE)));
builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));
// normally, we don't affect the credential store when reading vApps.
// However, login user, etc, is actually in the metadata, so lets see
Credentials fromApi = getCredentialsFrom(from);
if (fromApi != null && !credentialStore.containsKey("node#" + from.getHref().toASCIIString()))
credentialStore.put("node#" + from.getHref().toASCIIString(), fromApi);
return builder.build();
}
}

View File

@ -0,0 +1,150 @@
/**
* 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.vcloud.director.v1_5.compute.util;
import static com.google.common.collect.Iterables.filter;
import java.util.Set;
import org.jclouds.cim.OSType;
import org.jclouds.compute.domain.CIMOperatingSystem;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.dmtf.CIMPredicates;
import org.jclouds.dmtf.cim.ResourceAllocationSettingData;
import org.jclouds.dmtf.cim.ResourceAllocationSettingData.ResourceType;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.vcloud.director.v1_5.domain.VApp;
import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate;
import org.jclouds.vcloud.director.v1_5.domain.Vm;
import org.jclouds.vcloud.director.v1_5.domain.network.NetworkConnection;
import org.jclouds.vcloud.director.v1_5.domain.section.GuestCustomizationSection;
import org.jclouds.vcloud.director.v1_5.domain.section.NetworkConnectionSection;
import org.jclouds.vcloud.director.v1_5.domain.section.OperatingSystemSection;
import org.jclouds.vcloud.director.v1_5.domain.section.VirtualHardwareSection;
import org.jclouds.vcloud.director.v1_5.functions.SectionForVApp;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
/**
*
* @author danikov
*/
public class VCloudDirectorComputeUtils {
public static OperatingSystem toComputeOs(VApp vApp, OperatingSystem defaultOs) {
CIMOperatingSystem cimOs = toComputeOs(vApp);
return cimOs != null ? cimOs : defaultOs;
}
public static CIMOperatingSystem toComputeOs(VApp vApp) {
// TODO we need to change the design so that it doesn't assume single-vms
return vApp.getChildren().getVms().size() > 0 ? toComputeOs(Iterables.get(vApp.getChildren().getVms(), 0)) : null;
}
private static SectionForVApp<OperatingSystemSection> findOperatingSystemSectionForVApp =
new SectionForVApp<OperatingSystemSection>(OperatingSystemSection.class);
public static CIMOperatingSystem toComputeOs(Vm vm) {
return toComputeOs(findOperatingSystemSectionForVApp.apply(vm));
}
public static CIMOperatingSystem toComputeOs(OperatingSystemSection os) {
return new CIMOperatingSystem(OSType.fromValue(os.getId()), "", null, os.getDescription());
}
public static String getVirtualSystemIdentifierOfFirstVMIn(VApp vApp) {
return vApp.getChildren().getVms().size() > 0 ?
getVirtualSystemIdentifierOf(Iterables.get(vApp.getChildren().getVms(), 0)) : null;
}
@Inject private static SectionForVApp<VirtualHardwareSection> findVirtualHardwareSectionForVApp =
new SectionForVApp<VirtualHardwareSection>(VirtualHardwareSection.class);
public static String getVirtualSystemIdentifierOf(Vm vm) {
VirtualHardwareSection virtualHardwareSection = findVirtualHardwareSectionForVApp.apply(vm);
if (virtualHardwareSection != null && virtualHardwareSection != null)
return virtualHardwareSection.getSystem().getVirtualSystemIdentifier();
return null;
}
public static LoginCredentials getCredentialsFrom(VApp vApp) {
return vApp.getChildren().getVms().size() > 0 ?
getCredentialsFrom(Iterables.get(vApp.getChildren().getVms(), 0)) : null;
}
public static LoginCredentials getCredentialsFrom(VAppTemplate vApp) {
return vApp.getChildren().size() > 0 ? getCredentialsFrom(Iterables.get(vApp.getChildren(), 0)) : null;
}
@Inject private static SectionForVApp<GuestCustomizationSection> findGuestCustomizationSectionForVApp =
new SectionForVApp<GuestCustomizationSection>(GuestCustomizationSection.class);
public static LoginCredentials getCredentialsFrom(Vm vm) {
LoginCredentials.Builder builder = LoginCredentials.builder();
GuestCustomizationSection guestCustomizationSection = findGuestCustomizationSectionForVApp.apply(vm);
if (guestCustomizationSection != null)
builder.password(guestCustomizationSection.getAdminPassword());
return builder.build();
}
@Inject private static SectionForVApp<NetworkConnectionSection> findNetworkConnectionSectionForVApp =
new SectionForVApp<NetworkConnectionSection>(NetworkConnectionSection.class);
public static Set<String> getIpsFromVApp(VApp vApp) {
// 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();
// 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
// enough data to tell whether or not it is a public network without
// string
// 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(vm);
if (networkConnectionSection != null) {
for (NetworkConnection connection : networkConnectionSection.getNetworkConnections()) {
if (connection.getIpAddress() != null)
ips.add(connection.getIpAddress());
if (connection.getExternalIpAddress() != null)
ips.add(connection.getExternalIpAddress());
}
} else {
for (ResourceAllocationSettingData net : filter(findVirtualHardwareSectionForVApp.apply(vm).getItems(),
CIMPredicates.resourceTypeIn(ResourceType.ETHERNET_ADAPTER))) {
// FIXME: not yet implemented
// if (net instanceof VCloudNetworkAdapter) {
// VCloudNetworkAdapter vNet = VCloudNetworkAdapter.class.cast(net);
// if (vNet.getIpAddress() != null)
// ips.add(vNet.getIpAddress());
// }
}
}
return ips.build();
}
}

View File

@ -43,6 +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.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;

View File

@ -0,0 +1,47 @@
/*
* 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.vcloud.director.v1_5.functions;
import org.jclouds.dmtf.ovf.SectionType;
import org.jclouds.vcloud.director.v1_5.domain.AbstractVAppType;
import com.google.common.base.Function;
/**
* @author danikov
*/
public class SectionForVApp<S extends SectionType> implements Function<AbstractVAppType, S> {
private final Class<? extends SectionType> sectionType;
public SectionForVApp(Class<S> sectionType) {
this.sectionType = sectionType;
}
@SuppressWarnings("unchecked")
@Override
public S apply(AbstractVAppType from) {
for (SectionType section : from.getSections()) {
if (sectionType.isAssignableFrom(section.getClass())) {
return (S)section;
}
}
return null;
}
}

View File

@ -18,8 +18,6 @@
*/
package org.jclouds.vcloud.director.v1_5.functions;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -27,7 +25,6 @@ import org.jclouds.dmtf.ovf.SectionType;
import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate;
import com.google.common.base.Function;
import com.google.common.collect.Sets;
/**
* @author danikov
@ -45,7 +42,6 @@ public class SectionForVAppTemplate<S extends SectionType> implements Function<V
@SuppressWarnings("unchecked")
@Override
public S apply(VAppTemplate from) {
Set<S> sections = Sets.newLinkedHashSet();
for (SectionType section : from.getSections()) {
if (sectionType.isAssignableFrom(section.getClass())) {
return (S)section;

View File

@ -20,9 +20,13 @@ package org.jclouds.vcloud.director.v1_5.predicates;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.Link;
import com.google.common.base.Predicate;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/**
* Predicates handy when working with Links
@ -31,7 +35,7 @@ import com.google.common.base.Predicate;
*/
public class LinkPredicates {
/**
* matches links of the given relation
*
@ -46,32 +50,57 @@ public class LinkPredicates {
/** @see #relEquals(String) */
public static Predicate<Link> relEquals(final Link.Rel rel) {
checkNotNull(rel, "rel must be defined");
return new Predicate<Link>() {
@Override
public boolean apply(Link link) {
return rel == link.getRel();
}
@Override
public String toString() {
return "relEquals(" + rel.value() + ")";
}
};
return LINK_REL_SELECTORS.apply(checkNotNull(rel, "rel must be defined"));
}
private static final LoadingCache<Link.Rel, Predicate<Link>> LINK_REL_SELECTORS = CacheBuilder.newBuilder()
.maximumSize(Link.Rel.ALL.size())
.build(
new CacheLoader<Link.Rel, Predicate<Link>>() {
public Predicate<Link> load(final Link.Rel rel) {
return new Predicate<Link>() {
@Override
public boolean apply(Link link) {
return rel == link.getRel();
}
@Override
public String toString() {
return "relEquals(" + rel.value() + ")";
}
};
}
});
/**
* @see ReferenceTypePredicates#nameEquals
*/
public static Predicate<Link> nameEquals(String name) {
return ReferencePredicates.nameEquals(name);
return MEDIA_NAME_SELECTORS.apply(name);
}
private static final LoadingCache<String, Predicate<Link>> MEDIA_NAME_SELECTORS = CacheBuilder.newBuilder()
.maximumSize(VCloudDirectorMediaType.ALL.size())
.build(
new CacheLoader<String, Predicate<Link>>() {
public Predicate<Link> load(String key) {
return ReferencePredicates.nameEquals(key);
}
});
/**
* @see ReferenceTypePredicates#typeEquals
*/
public static Predicate<Link> typeEquals(String type) {
return ReferencePredicates.typeEquals(type);
return MEDIA_TYPE_SELECTORS.apply(type);
}
private static final LoadingCache<String, Predicate<Link>> MEDIA_TYPE_SELECTORS = CacheBuilder.newBuilder()
.maximumSize(VCloudDirectorMediaType.ALL.size())
.build(
new CacheLoader<String, Predicate<Link>>() {
public Predicate<Link> load(String key) {
return ReferencePredicates.typeEquals(key);
}
});
}