public NodeAndInitialCredentials createNodeWithGroupEncodedIntoName()

in apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/strategy/CloudStackComputeServiceAdapter.java [152:312]


   public NodeAndInitialCredentials<VirtualMachine> createNodeWithGroupEncodedIntoName(String group, String name,
                                                                                       org.jclouds.compute.domain.Template template) {

      checkNotNull(template, "template was null");
      checkNotNull(template.getOptions(), "template options was null");
      checkArgument(template.getOptions().getClass().isAssignableFrom(CloudStackTemplateOptions.class),
         "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().isEmpty()) {
         options.ipsToNetworks(templateOptions.getIpsToNetworks());
      }

      if (templateOptions.getUserData() != null) {
         options.userData(templateOptions.getUserData());
      }

      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().isEmpty()
             && !inboundPorts.isEmpty()
             && 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());
         }
      }

      String templateId = template.getImage().getId();
      String serviceOfferingId = template.getHardware().getId();

      logger.debug("serviceOfferingId %s, templateId %s, zoneId %s, options %s%n", serviceOfferingId, templateId,
         zoneId, options);
      AsyncCreateResponse job = client.getVirtualMachineApi().deployVirtualMachineInZone(zoneId, serviceOfferingId,
         templateId, options);
      VirtualMachine vm = blockUntilJobCompletesAndReturnResult.<VirtualMachine>apply(job);
      logger.debug("--- virtualmachine: %s", vm);
      LoginCredentials credentials = credentialsProvider.get();
      if (credentials == null || credentials.getUser() == null) {
         LoginCredentials.Builder credentialsBuilder = LoginCredentials.builder();
         if (templateOptions.getKeyPair() != null) {
            SshKeyPair keyPair = keyPairCache.getUnchecked(templateOptions.getKeyPair());
            credentialsBuilder.privateKey(keyPair.getPrivateKey());
         } else if (vm.isPasswordEnabled()) {
            assert vm.getPassword() != null : vm;
            credentialsBuilder.password(vm.getPassword());
         }
         credentials = credentialsBuilder.build();
      }

      try {
         ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
         builder.putAll(template.getOptions().getUserMetadata());
         for (String tag : template.getOptions().getTags())
            builder.put(tag, "jclouds-empty-tag-placeholder");
         Map<String, String> common = builder.build();

         if (!common.isEmpty()) {
            logger.debug(">> adding tags %s to virtualmachine(%s)", common, vm.getId());
            CreateTagsOptions tagOptions = CreateTagsOptions.Builder.resourceIds(vm.getId())
                  .resourceType(Tag.ResourceType.USER_VM)
                  .tags(common);
            AsyncCreateResponse tagJob = client.getTagApi().createTags(tagOptions);
            awaitCompletion(tagJob.getJobId());
            logger.debug("<< tags added");
            vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId());
         }
         if (templateOptions.shouldSetupStaticNat()) {
            Capabilities capabilities = client.getConfigurationApi().listCapabilities();

            NIC nic = Iterables.find(vm.getNICs(), new Predicate<NIC>() {
               @Override
               public boolean apply(NIC input) {
                  return (input == null) ? false : input.isDefault();
               }
            });
            String networkId = nic.getNetworkId();

            logger.debug(">> creating static NAT for virtualMachine(%s) in network(%s)", vm.getId(), networkId);
            PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm);
            logger.trace("<< static NATed IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId());
            vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId());
            List<Integer> ports = Ints.asList(templateOptions.getInboundPorts());
            if (capabilities.getCloudStackVersion().startsWith("2")) {
               logger.debug(">> setting up IP forwarding for IPAddress(%s) rules(%s)", ip.getId(), ports);
               Set<IPForwardingRule> rules = setupPortForwardingRulesForIP.apply(ip, ports);
               logger.trace("<< setup %d IP forwarding rules on IPAddress(%s)", rules.size(), ip.getId());
            } else {
               logger.debug(">> setting up firewall rules for IPAddress(%s) rules(%s)", ip.getId(), ports);
               Set<FirewallRule> rules = setupFirewallRulesForIP.apply(ip, ports);
               logger.trace("<< setup %d firewall rules on IPAddress(%s)", rules.size(), ip.getId());
            }
          }
      } catch (RuntimeException re) {
          logger.error("-- exception after node has been created, trying to destroy the created virtualMachine(%s)", vm.getId());
          try {
              destroyNode(vm.getId());
          } catch (RuntimeException re2) {
              logger.debug("-- exception in exceptionHandler while executing destroyNode for virtualMachine(%s), ignoring and rethrowing original exception", vm.getId());
          }
          throw re;
      }
      return new NodeAndInitialCredentials<VirtualMachine>(vm, vm.getId() + "", credentials);
   }