buildTemplate();
}
public void buildTemplate() {
IaasProvider iaasInfo = getIaasProvider();
if (iaasInfo.getComputeService() == null) {
throw new CloudControllerException(
"Compute service is null for IaaS provider: "
+ iaasInfo.getName());
}
TemplateBuilder templateBuilder = iaasInfo.getComputeService()
.templateBuilder();
templateBuilder.imageId(iaasInfo.getImage());
if(!(iaasInfo instanceof IaasProvider)) {
templateBuilder.locationId(iaasInfo.getType());
}
// to avoid creation of template objects in each and every time, we
// create all at once!
String instanceType;
// set instance type
if (((instanceType = iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE)) != null)) {
templateBuilder.hardwareId(instanceType);
}
Template template = templateBuilder.build();
// In Openstack the call to IaaS should be blocking, in order to retrieve
// IP addresses.
boolean blockUntilRunning = true;
if(iaasInfo.getProperty(CloudControllerConstants.BLOCK_UNTIL_RUNNING) != null) {
blockUntilRunning = Boolean.parseBoolean(iaasInfo.getProperty(
CloudControllerConstants.BLOCK_UNTIL_RUNNING));
}
template.getOptions().as(TemplateOptions.class)
.blockUntilRunning(blockUntilRunning);
// this is required in order to avoid creation of additional security
// groups by Jclouds.
template.getOptions().as(TemplateOptions.class)
.inboundPorts(new int[] {});
if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUPS) != null) {
template.getOptions()
.as(NovaTemplateOptions.class)
.securityGroupNames(
iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUPS).split(
CloudControllerConstants.ENTRY_SEPARATOR));
}
if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
template.getOptions().as(NovaTemplateOptions.class)
.keyPairName(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
}
if (iaasInfo.getNetworkInterfaces() != null) {
Set<Network> novaNetworksSet = new LinkedHashSet<Network>(iaasInfo.getNetworkInterfaces().length);
for (NetworkInterface ni:iaasInfo.getNetworkInterfaces()) {
novaNetworksSet.add(Network.builder().networkUuid(ni.getNetworkUuid()).fixedIp(ni.getFixedIp())
.portUuid(ni.getPortUuid()).build());
}
template.getOptions().as(NovaTemplateOptions.class).novaNetworks(novaNetworksSet);
}
if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
template.getOptions().as(NovaTemplateOptions.class)
.availabilityZone(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
}
//TODO
// if (iaas.getProperty(CloudControllerConstants.HOST) != null) {
// template.getOptions().as(NovaTemplateOptions.class)
// .(CloudControllerConstants.HOST);
// }
// set Template
iaasInfo.setTemplate(template);
}