checkNotNull(template.getOptions(), "template options was null");
checkArgument(template.getOptions().getClass().isAssignableFrom(SoftLayerTemplateOptions.class),
"options class %s should have been assignable from SoftLayerTemplateOptions",
template.getOptions().getClass());
SoftLayerTemplateOptions templateOptions = template.getOptions().as(SoftLayerTemplateOptions.class);
String domainName = templateOptions.getDomainName();
String diskType = templateOptions.getDiskType().or(DEFAULT_DISK_TYPE);
boolean hourlyBillingFlag = templateOptions.isHourlyBillingFlag().or(true);
int maxPortSpeed = templateOptions.getPortSpeed().or(DEFAULT_MAX_PORT_SPEED);
final Datacenter datacenter = Datacenter.builder().name(template.getLocation().getId()).build();
final String imageId = template.getImage().getId();
int cores = (int) template.getHardware().getProcessors().get(0).getCores();
VirtualGuest.Builder virtualGuestBuilder = VirtualGuest.builder()
.domain(domainName)
.hostname(name)
.hourlyBillingFlag(hourlyBillingFlag)
.startCpus(cores)
.maxMemory(template.getHardware().getRam())
.datacenter(datacenter);
// set operating system or blockDeviceTemplateGroup
Optional<OperatingSystem> optionalOperatingSystem = tryExtractOperatingSystemFrom(imageId);
if (optionalOperatingSystem.isPresent()) {
virtualGuestBuilder.operatingSystem(optionalOperatingSystem.get());
// the imageId specified is a the id of a public/private/flex image
} else {
VirtualGuestBlockDeviceTemplateGroup blockDeviceTemplateGroup = VirtualGuestBlockDeviceTemplateGroup
.builder().globalIdentifier(imageId).build();
virtualGuestBuilder.blockDeviceTemplateGroup(blockDeviceTemplateGroup).build();
}
// set multi-disks
if (templateOptions.getBlockDevices().isPresent()) {
List<VirtualGuestBlockDevice> blockDevices = getBlockDevices(templateOptions.getBlockDevices().get(), diskType);
virtualGuestBuilder.blockDevices(blockDevices);
virtualGuestBuilder.localDiskFlag(isLocalDisk(diskType));
}
// set dedicatedAccountHostOnlyFlag
if (templateOptions.isDedicatedAccountHostOnlyFlag().isPresent()) {
virtualGuestBuilder.dedicatedAccountHostOnly(templateOptions.isDedicatedAccountHostOnlyFlag().get());
}
// set privateNetworkOnlyFlag
if (templateOptions.isPrivateNetworkOnlyFlag().isPresent()) {
virtualGuestBuilder.privateNetworkOnlyFlag(templateOptions.isPrivateNetworkOnlyFlag().get());
}
// set primaryNetworkComponent.networkVlan.id
if (templateOptions.getPrimaryNetworkComponentNetworkVlanId().isPresent()) {
int primaryNetworkComponentNetworkVlanId = templateOptions.getPrimaryNetworkComponentNetworkVlanId().get();
virtualGuestBuilder.primaryNetworkComponent(
VirtualGuestNetworkComponent.builder()
.networkVlan(NetworkVlan.builder().id(primaryNetworkComponentNetworkVlanId).build())
.speed(maxPortSpeed).build());
}
// set primaryBackendNetworkComponent.networkVlan.id
if (templateOptions.getPrimaryBackendNetworkComponentNetworkVlanId().isPresent()) {
int primaryBackendNetworkComponentNetworkVlanId = templateOptions.getPrimaryBackendNetworkComponentNetworkVlanId().get();
virtualGuestBuilder.primaryBackendNetworkComponent(
VirtualGuestNetworkComponent.builder()
.networkVlan(NetworkVlan.builder().id(primaryBackendNetworkComponentNetworkVlanId).build())
.speed(maxPortSpeed).build());
}
// set postInstallScriptUri
if (templateOptions.getPostInstallScriptUri().isPresent()) {
// Specifies the uri location of the script to be downloaded and run after installation is complete.
virtualGuestBuilder.postInstallScriptUri(templateOptions.getPostInstallScriptUri().get());
}
// set userData
if (templateOptions.getUserData().isPresent()) {
virtualGuestBuilder.virtualGuestAttribute(VirtualGuestAttribute.builder().value(templateOptions.getUserData().get()).build());
}
// set sshKeys
if (templateOptions.getSshKeys().isPresent()) {
Set<SecuritySshKey> sshKeys = Sets.newHashSet();
for (int sshKeyId : templateOptions.getSshKeys().get()) {
sshKeys.add(SecuritySshKey.builder().id(sshKeyId).build());
}
virtualGuestBuilder.sshKeys(sshKeys);
}
VirtualGuest virtualGuest = virtualGuestBuilder.build();
logger.debug(">> creating new VirtualGuest(%s)", virtualGuest);
VirtualGuest result = api.getVirtualGuestApi().createVirtualGuest(virtualGuest);
logger.trace("<< VirtualGuest(%s)", result.getId());
// tags
if (templateOptions.getTags() != null) {
api.getVirtualGuestApi().setTags(result.getId(), templateOptions.getTags());
}
logger.debug(">> awaiting login details for virtualGuest(%s)", result.getId());
boolean orderInSystem = loginDetailsTester.apply(result);
logger.trace("<< VirtualGuest(%s) complete(%s)", result.getId(), orderInSystem);