Address review comments: use guava checkArgument(), and use switch instead of if for zone network type

This commit is contained in:
Richard Downer 2012-01-05 09:51:05 +02:00
parent 1b80b28613
commit 8991d8e512
1 changed files with 31 additions and 26 deletions

View File

@ -126,27 +126,9 @@ public class CloudStackComputeServiceAdapter implements
CloudStackTemplateOptions templateOptions = template.getOptions().as(CloudStackTemplateOptions.class); CloudStackTemplateOptions templateOptions = template.getOptions().as(CloudStackTemplateOptions.class);
DeployVirtualMachineOptions options = displayName(name).name(name); DeployVirtualMachineOptions options = displayName(name).name(name);
if (zone.getNetworkType() == NetworkType.ADVANCED) { switch(zone.getNetworkType()) {
// security groups not allowed.
// at least one network must be given to CloudStack, case BASIC:
// but jclouds will try to autodetect an appropriate network if none given.
if (templateOptions.getSecurityGroupIds().size() > 0) {
throw new IllegalArgumentException("security groups cannot be specified for locations (zones) that use advanced networking");
}
if (templateOptions.getNetworkIds().size() > 0) {
options.networkIds(templateOptions.getNetworkIds());
} else {
if (networks.size() == 0) {
throw new IllegalArgumentException("please setup a network for zone: " + zoneId);
}
Network defaultNetworkInZone = Iterables.getFirst(filter(networks.values(), and(defaultNetworkInZone(zoneId), supportsStaticNAT())), null);
if(defaultNetworkInZone == null) {
throw new IllegalArgumentException("please choose a specific network in zone " + zoneId + ": " + networks);
} else {
options.networkId(defaultNetworkInZone.getId());
}
}
} else if(zone.getNetworkType() == NetworkType.BASIC) {
// both security groups and networks are optional, and CloudStack will // both security groups and networks are optional, and CloudStack will
// use the zone/user's default network/security group if none given // use the zone/user's default network/security group if none given
if (templateOptions.getSecurityGroupIds().size() > 0) { if (templateOptions.getSecurityGroupIds().size() > 0) {
@ -155,6 +137,29 @@ public class CloudStackComputeServiceAdapter implements
if (templateOptions.getNetworkIds().size() > 0) { if (templateOptions.getNetworkIds().size() > 0) {
options.networkIds(templateOptions.getNetworkIds()); options.networkIds(templateOptions.getNetworkIds());
} }
break;
case ADVANCED:
// security groups not allowed.
// at least one network must be given to CloudStack,
// but jclouds will try to autodetect an appropriate network if none given.
checkArgument(templateOptions.getSecurityGroupIds().isEmpty(), "security groups cannot be specified for locations (zones) that use advanced networking");
if (templateOptions.getNetworkIds().size() > 0) {
options.networkIds(templateOptions.getNetworkIds());
} else {
checkArgument(!networks.isEmpty(), "please setup a network for zone: " + zoneId);
Network defaultNetworkInZone = Iterables.getFirst(filter(networks.values(), and(defaultNetworkInZone(zoneId), supportsStaticNAT())), null);
if(defaultNetworkInZone == null) {
throw new IllegalArgumentException("please choose a specific network in zone " + zoneId + ": " + networks);
} else {
options.networkId(defaultNetworkInZone.getId());
}
}
break;
default:
throw new IllegalStateException("Zone networking type is unrecognized");
} }
if (templateOptions.getIpOnDefaultNetwork() != null) { if (templateOptions.getIpOnDefaultNetwork() != null) {