adjusted behavior relating to name/hostname

This commit is contained in:
Adrian Cole 2011-12-01 16:33:19 -05:00
parent dc494414ba
commit d0a2f54cb3
5 changed files with 55 additions and 47 deletions

View File

@ -60,16 +60,16 @@ import com.google.common.util.concurrent.UncheckedExecutionException;
public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, NodeMetadata> { public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, NodeMetadata> {
public static final Map<VirtualMachine.State, NodeState> vmStateToNodeState = ImmutableMap public static final Map<VirtualMachine.State, NodeState> vmStateToNodeState = ImmutableMap
.<VirtualMachine.State, NodeState>builder().put(VirtualMachine.State.STARTING, NodeState.PENDING) .<VirtualMachine.State, NodeState> builder().put(VirtualMachine.State.STARTING, NodeState.PENDING)
.put(VirtualMachine.State.RUNNING, NodeState.RUNNING).put(VirtualMachine.State.STOPPING, NodeState.PENDING) .put(VirtualMachine.State.RUNNING, NodeState.RUNNING).put(VirtualMachine.State.STOPPING, NodeState.PENDING)
.put(VirtualMachine.State.STOPPED, NodeState.SUSPENDED) .put(VirtualMachine.State.STOPPED, NodeState.SUSPENDED)
.put(VirtualMachine.State.DESTROYED, NodeState.TERMINATED) .put(VirtualMachine.State.DESTROYED, NodeState.TERMINATED)
.put(VirtualMachine.State.EXPUNGING, NodeState.TERMINATED) .put(VirtualMachine.State.EXPUNGING, NodeState.TERMINATED)
.put(VirtualMachine.State.MIGRATING, NodeState.PENDING).put(VirtualMachine.State.ERROR, NodeState.ERROR) .put(VirtualMachine.State.MIGRATING, NodeState.PENDING).put(VirtualMachine.State.ERROR, NodeState.ERROR)
.put(VirtualMachine.State.UNKNOWN, NodeState.UNRECOGNIZED) .put(VirtualMachine.State.UNKNOWN, NodeState.UNRECOGNIZED)
// TODO: is this really a state? // TODO: is this really a state?
.put(VirtualMachine.State.SHUTDOWNED, NodeState.PENDING) .put(VirtualMachine.State.SHUTDOWNED, NodeState.PENDING)
.put(VirtualMachine.State.UNRECOGNIZED, NodeState.UNRECOGNIZED).build(); .put(VirtualMachine.State.UNRECOGNIZED, NodeState.UNRECOGNIZED).build();
private final FindLocationForVirtualMachine findLocationForVirtualMachine; private final FindLocationForVirtualMachine findLocationForVirtualMachine;
private final FindHardwareForVirtualMachine findHardwareForVirtualMachine; private final FindHardwareForVirtualMachine findHardwareForVirtualMachine;
@ -78,14 +78,14 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
@Inject @Inject
VirtualMachineToNodeMetadata(FindLocationForVirtualMachine findLocationForVirtualMachine, VirtualMachineToNodeMetadata(FindLocationForVirtualMachine findLocationForVirtualMachine,
FindHardwareForVirtualMachine findHardwareForVirtualMachine, FindHardwareForVirtualMachine findHardwareForVirtualMachine,
FindImageForVirtualMachine findImageForVirtualMachine, FindImageForVirtualMachine findImageForVirtualMachine,
Cache<Long, Set<IPForwardingRule>> getIPForwardingRulesByVirtualMachine) { Cache<Long, Set<IPForwardingRule>> getIPForwardingRulesByVirtualMachine) {
this.findLocationForVirtualMachine = checkNotNull(findLocationForVirtualMachine, "findLocationForVirtualMachine"); this.findLocationForVirtualMachine = checkNotNull(findLocationForVirtualMachine, "findLocationForVirtualMachine");
this.findHardwareForVirtualMachine = checkNotNull(findHardwareForVirtualMachine, "findHardwareForVirtualMachine"); this.findHardwareForVirtualMachine = checkNotNull(findHardwareForVirtualMachine, "findHardwareForVirtualMachine");
this.findImageForVirtualMachine = checkNotNull(findImageForVirtualMachine, "findImageForVirtualMachine"); this.findImageForVirtualMachine = checkNotNull(findImageForVirtualMachine, "findImageForVirtualMachine");
this.getIPForwardingRulesByVirtualMachine = checkNotNull(getIPForwardingRulesByVirtualMachine, this.getIPForwardingRulesByVirtualMachine = checkNotNull(getIPForwardingRulesByVirtualMachine,
"getIPForwardingRulesByVirtualMachine"); "getIPForwardingRulesByVirtualMachine");
} }
@Override @Override
@ -94,7 +94,13 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
NodeMetadataBuilder builder = new NodeMetadataBuilder(); NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getId() + ""); builder.ids(from.getId() + "");
builder.name(from.getName()); builder.name(from.getName());
builder.hostname(from.getHostname()); // 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(findLocationForVirtualMachine.apply(from)); builder.location(findLocationForVirtualMachine.apply(from));
builder.group(parseGroupFromName(from.getDisplayName())); builder.group(parseGroupFromName(from.getDisplayName()));
Image image = findImageForVirtualMachine.apply(from); Image image = findImageForVirtualMachine.apply(from);
@ -112,7 +118,7 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
// TODO: check to see public or private // TODO: check to see public or private
if (from.getIPAddress() != null) { if (from.getIPAddress() != null) {
boolean isPrivate = InetAddresses2.isPrivateIPAddress(from.getIPAddress()); boolean isPrivate = InetAddresses2.isPrivateIPAddress(from.getIPAddress());
Set<String> addresses = ImmutableSet.<String>of(from.getIPAddress()); Set<String> addresses = ImmutableSet.<String> of(from.getIPAddress());
if (isPrivate) if (isPrivate)
builder.privateAddresses(addresses); builder.privateAddresses(addresses);
else else
@ -120,19 +126,19 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
} }
try { try {
builder.publicAddresses(transform(filter(getIPForwardingRulesByVirtualMachine.getUnchecked(from.getId()), builder.publicAddresses(transform(
new Predicate<IPForwardingRule>() { filter(getIPForwardingRulesByVirtualMachine.getUnchecked(from.getId()),
@Override new Predicate<IPForwardingRule>() {
public boolean apply(@Nullable IPForwardingRule rule) { @Override
return !"Deleting".equals(rule.getState()); public boolean apply(@Nullable IPForwardingRule rule) {
} return !"Deleting".equals(rule.getState());
}), }
new Function<IPForwardingRule, String>() { }), new Function<IPForwardingRule, String>() {
@Override @Override
public String apply(@Nullable IPForwardingRule rule) { public String apply(@Nullable IPForwardingRule rule) {
return rule.getIPAddress(); return rule.getIPAddress();
} }
})); }));
} catch (UncheckedExecutionException e) { } catch (UncheckedExecutionException e) {
if (Throwables2.getFirstThrowableOfType(e, ResourceNotFoundException.class) == null) { if (Throwables2.getFirstThrowableOfType(e, ResourceNotFoundException.class) == null) {
Throwables.propagateIfPossible(e.getCause()); Throwables.propagateIfPossible(e.getCause());
@ -181,9 +187,9 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
@Override @Override
public boolean matches(VirtualMachine from, Image input) { public boolean matches(VirtualMachine from, Image input) {
return input.getProviderId().equals(from.getTemplateId() + "") return input.getProviderId().equals(from.getTemplateId() + "")
// either location free image (location is null) // either location free image (location is null)
// or in the same zone as the VM // or in the same zone as the VM
&& (input.getLocation() == null || input.getId().equals(from.getZoneId() + "")); && (input.getLocation() == null || input.getId().equals(from.getZoneId() + ""));
} }
} }

View File

@ -121,8 +121,7 @@ public class CloudStackComputeServiceAdapter implements
CloudStackTemplateOptions templateOptions = template.getOptions().as(CloudStackTemplateOptions.class); CloudStackTemplateOptions templateOptions = template.getOptions().as(CloudStackTemplateOptions.class);
// not all hypervisors support setting name DeployVirtualMachineOptions options = displayName(name).name(name);
DeployVirtualMachineOptions options = displayName(name);
if (templateOptions.getSecurityGroupIds().size() > 0) { if (templateOptions.getSecurityGroupIds().size() > 0) {
options.securityGroupIds(templateOptions.getSecurityGroupIds()); options.securityGroupIds(templateOptions.getSecurityGroupIds());
} else if (templateOptions.getNetworkIds().size() > 0) { } else if (templateOptions.getNetworkIds().size() > 0) {

View File

@ -72,7 +72,8 @@ public class CreatePortForwardingRulesForIP {
} }
public Set<IPForwardingRule> apply(PublicIPAddress ip, String protocol, Iterable<Integer> ports) { public Set<IPForwardingRule> apply(PublicIPAddress ip, String protocol, Iterable<Integer> ports) {
checkState(ip.getVirtualMachineId() != -1, "ip should be static NATed to a virtual machine"); checkState(ip.getVirtualMachineId() != -1,
"ip %s should be static NATed to a virtual machine before applying rules", ip);
if (Iterables.size(ports) == 0) if (Iterables.size(ports) == 0)
return ImmutableSet.<IPForwardingRule> of(); return ImmutableSet.<IPForwardingRule> of();
Builder<AsyncCreateResponse> responses = ImmutableSet.<AsyncCreateResponse> builder(); Builder<AsyncCreateResponse> responses = ImmutableSet.<AsyncCreateResponse> builder();

View File

@ -62,6 +62,9 @@ public class DeployVirtualMachineOptions extends AccountInDomainOptions {
} }
/** /**
* sets the displayName - just for display purposes. We don't pass this
* parameter to the backend.
*
* @param displayName * @param displayName
* an optional user generated name for the virtual machine * an optional user generated name for the virtual machine
*/ */
@ -98,6 +101,10 @@ public class DeployVirtualMachineOptions extends AccountInDomainOptions {
} }
/** /**
* sets the hostName, it will be propagated down to the backend and set on
* the user vm. If this parameter is not passed it, it will be defaulted to
* our usual "i-x-y'
*
* @param name * @param name
* host name for the virtual machine * host name for the virtual machine
*/ */
@ -108,7 +115,7 @@ public class DeployVirtualMachineOptions extends AccountInDomainOptions {
/** /**
* @param ipOnDefaultNetwork * @param ipOnDefaultNetwork
* the requested ip address (2.2.12 only option) * the requested ip address (2.2.12 only option)
*/ */
public DeployVirtualMachineOptions ipOnDefaultNetwork(String ipOnDefaultNetwork) { public DeployVirtualMachineOptions ipOnDefaultNetwork(String ipOnDefaultNetwork) {
this.queryParameters.replaceValues("ipaddress", ImmutableSet.of(ipOnDefaultNetwork)); this.queryParameters.replaceValues("ipaddress", ImmutableSet.of(ipOnDefaultNetwork));
@ -117,19 +124,14 @@ public class DeployVirtualMachineOptions extends AccountInDomainOptions {
/** /**
* @param ipsToNetworks * @param ipsToNetworks
* mapping ip addresses to network ids (2.2.12 only option) * mapping ip addresses to network ids (2.2.12 only option)
*/ */
public DeployVirtualMachineOptions ipsToNetworks(Map<String, Long> ipsToNetworks) { public DeployVirtualMachineOptions ipsToNetworks(Map<String, Long> ipsToNetworks) {
int count = 0; int count = 0;
for(String ip : ipsToNetworks.keySet()) { for (String ip : ipsToNetworks.keySet()) {
this.queryParameters.replaceValues( this.queryParameters.replaceValues(String.format("ipnetworklist[%d].ip", count), ImmutableSet.of(ip));
String.format("ipnetworklist[%d].ip", count), this.queryParameters.replaceValues(String.format("ipnetworklist[%d].networkid", count),
ImmutableSet.of(ip) ImmutableSet.of("" + ipsToNetworks.get(ip)));
);
this.queryParameters.replaceValues(
String.format("ipnetworklist[%d].networkid", count),
ImmutableSet.of("" + ipsToNetworks.get(ip))
);
count += 1; count += 1;
} }
return this; return this;

View File

@ -82,7 +82,7 @@ public class VirtualMachineToNodeMetadataTest {
assertEquals( assertEquals(
node.toString(), node.toString(),
new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3") new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3")
.location(ZoneToLocationTest.one).state(NodeState.PENDING) .location(ZoneToLocationTest.one).state(NodeState.PENDING).hostname("i-3-54-VM")
.privateAddresses(ImmutableSet.of("10.1.1.18")).publicAddresses(ImmutableSet.of("1.1.1.1")) .privateAddresses(ImmutableSet.of("10.1.1.18")).publicAddresses(ImmutableSet.of("1.1.1.1"))
.hardware(ServiceOfferingToHardwareTest.one).imageId(TemplateToImageTest.one.getId()) .hardware(ServiceOfferingToHardwareTest.one).imageId(TemplateToImageTest.one.getId())
.operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString()); .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString());
@ -120,7 +120,7 @@ public class VirtualMachineToNodeMetadataTest {
assertEquals( assertEquals(
node.toString(), node.toString(),
new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3") new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3")
.location(ZoneToLocationTest.one).state(NodeState.PENDING) .location(ZoneToLocationTest.one).state(NodeState.PENDING).hostname("i-3-54-VM")
.privateAddresses(ImmutableSet.of("10.1.1.18")).hardware(ServiceOfferingToHardwareTest.one) .privateAddresses(ImmutableSet.of("10.1.1.18")).hardware(ServiceOfferingToHardwareTest.one)
.imageId(TemplateToImageTest.one.getId()) .imageId(TemplateToImageTest.one.getId())
.operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString()); .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString());