}
@Override
public NodeMetadata apply(final VirtualMachine from) {
// convert the result object to a jclouds NodeMetadata
NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getId() + "");
builder.name(from.getName());
// TODO: in cloudstack 2.2.12, when "name" was set fine on the backend,
// but wrong API response was returned to the user
// http://bugs.cloud.com/show_bug.cgi?id=11664
//
// 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(FluentIterable.from(locations.get()).firstMatch(idEquals(from.getZoneId())).orNull());
builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getDisplayName()));
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());
}
builder.hardware(new HardwareBuilder()
.ids(from.getServiceOfferingId() + "")
.name(from.getServiceOfferingName() + "")
// .tags() TODO
.processors(ImmutableList.of(new Processor(from.getCpuCount(), from.getCpuSpeed())))
.ram((int)from.getMemory())//
.hypervisor(from.getHypervisor())//
.build());
builder.status(vmStateToNodeStatus.get(from.getState()));
Set<String> publicAddresses = newHashSet();
Set<String> privateAddresses = newHashSet();
if (from.getIPAddress() != null) {
boolean isPrivate = isPrivateIPAddress(from.getIPAddress());
if (isPrivate) {
privateAddresses.add(from.getIPAddress());
} else {
publicAddresses.add(from.getIPAddress());
}
}
if (from.getPublicIP() != null) {
publicAddresses.add(from.getPublicIP());
}
for (NIC nic : from.getNICs()) {
if (nic.getIPAddress() != null) {
if (isPrivateIPAddress(nic.getIPAddress())) {
privateAddresses.add(nic.getIPAddress());
} else {
publicAddresses.add(nic.getIPAddress());
}
}
}
try {
/* Also add to the list of public IPs any public IP address that has a
forwarding rule that links to this machine */
Iterables.addAll(publicAddresses, transform(
filter(getIPForwardingRulesByVirtualMachine.getUnchecked(from.getId()),
new Predicate<IPForwardingRule>() {
@Override
public boolean apply(IPForwardingRule rule) {
return !"Deleting".equals(rule.getState());
}
}), new Function<IPForwardingRule, String>() {
@Override
public String apply(IPForwardingRule rule) {
return rule.getIPAddress();
}
}));
} catch (UncheckedExecutionException e) {
if (Throwables2.getFirstThrowableOfType(e, ResourceNotFoundException.class) == null) {
Throwables.propagateIfPossible(e.getCause());
throw e;
}
}
return builder.privateAddresses(privateAddresses).publicAddresses(publicAddresses).build();
}