"options class %s should have been assignable from CloudStackTemplateOptions", template.getOptions()
.getClass());
Map<String, Network> networks = networkSupplier.get();
final String zoneId = template.getLocation().getId();
Zone zone = zoneIdToZone.get().getUnchecked(zoneId);
CloudStackTemplateOptions templateOptions = template.getOptions().as(CloudStackTemplateOptions.class);
checkState(optionsConverters.containsKey(zone.getNetworkType()), "no options converter configured for network type %s", zone.getNetworkType());
DeployVirtualMachineOptions options = displayName(name).name(name);
if (templateOptions.getAccount() != null) {
options.accountInDomain(templateOptions.getAccount(), templateOptions.getDomainId());
} else if (templateOptions.getDomainId() != null) {
options.domainId(templateOptions.getDomainId());
}
OptionsConverter optionsConverter = optionsConverters.get(zone.getNetworkType());
options = optionsConverter.apply(templateOptions, networks, zoneId, options);
options.group(group);
if (templateOptions.getIpOnDefaultNetwork() != null) {
options.ipOnDefaultNetwork(templateOptions.getIpOnDefaultNetwork());
}
if (templateOptions.getIpsToNetworks().size() > 0) {
options.ipsToNetworks(templateOptions.getIpsToNetworks());
}
if (templateOptions.getKeyPair() != null) {
SshKeyPair keyPair = null;
if (templateOptions.getLoginPrivateKey() != null) {
String pem = templateOptions.getLoginPrivateKey();
keyPair = SshKeyPair.builder().name(templateOptions.getKeyPair())
.fingerprint(fingerprintPrivateKey(pem)).privateKey(pem).build();
keyPairCache.asMap().put(keyPair.getName(), keyPair);
options.keyPair(keyPair.getName());
} else if (client.getSSHKeyPairApi().getSSHKeyPair(templateOptions.getKeyPair()) != null) {
keyPair = client.getSSHKeyPairApi().getSSHKeyPair(templateOptions.getKeyPair());
}
if (keyPair != null) {
keyPairCache.asMap().put(keyPair.getName(), keyPair);
options.keyPair(keyPair.getName());
}
} else if (templateOptions.shouldGenerateKeyPair()) {
SshKeyPair keyPair = keyPairCache.getUnchecked(namingConvention.create()
.sharedNameForGroup(group));
keyPairCache.asMap().put(keyPair.getName(), keyPair);
templateOptions.keyPair(keyPair.getName());
options.keyPair(keyPair.getName());
}
if (templateOptions.getDiskOfferingId() != null) {
options.diskOfferingId(templateOptions.getDiskOfferingId());
if (templateOptions.getDataDiskSize() > 0) {
options.dataDiskSize(templateOptions.getDataDiskSize());
}
}
if (supportsSecurityGroups().apply(zone)) {
List<Integer> inboundPorts = Ints.asList(templateOptions.getInboundPorts());
if (templateOptions.getSecurityGroupIds().size() == 0
&& inboundPorts.size() > 0
&& templateOptions.shouldGenerateSecurityGroup()) {
String securityGroupName = namingConvention.create().sharedNameForGroup(group);
SecurityGroup sg = securityGroupCache.getUnchecked(ZoneSecurityGroupNamePortsCidrs.builder()
.zone(zone.getId())
.name(securityGroupName)
.ports(ImmutableSet.copyOf(inboundPorts))
.cidrs(ImmutableSet.<String> of()).build());
options.securityGroupId(sg.getId());
}