public NodeAndInitialCredentials createNodeWithGroupEncodedIntoName()

in oneandone/src/main/java/org/apache/jclouds/oneandone/rest/compute/OneandoneComputeServiceAdapter.java [80:180]


   public NodeAndInitialCredentials<Server> createNodeWithGroupEncodedIntoName(String group, String name, Template template) {

      final String dataCenterId = template.getLocation().getId();
      Hardware hardware = template.getHardware();
      TemplateOptions options = template.getOptions();
      Server updateServer = null;

      final String loginUser = isNullOrEmpty(options.getLoginUser()) ? "root" : options.getLoginUser();
      final String password = options.hasLoginPassword() ? options.getLoginPassword() : passwordGenerator.generate();
      final String privateKey = options.hasLoginPrivateKey() ? options.getPrivateKey() : null;
      final org.jclouds.compute.domain.Image image = template.getImage();
      final int[] inboundPorts = template.getOptions().getInboundPorts();

      //prepare hdds to provision
      List<? extends Volume> volumes = hardware.getVolumes();
      List<Hdd.CreateHdd> hdds = new ArrayList<Hdd.CreateHdd>();

      for (final Volume volume : volumes) {
         try {
            //check if the bootable device has enough size to run the appliance(image).
            float minHddSize = volume.getSize();
            if (volume.isBootDevice()) {
               SingleServerAppliance appliance = api.serverApplianceApi().get(image.getId());
               if (appliance.minHddSize() > volume.getSize()) {
                  minHddSize = appliance.minHddSize();
               }
            }
            Hdd.CreateHdd hdd = Hdd.CreateHdd.create(minHddSize, volume.isBootDevice());
            hdds.add(hdd);
         } catch (Exception ex) {
            throw Throwables.propagate(ex);

         }
      }

      // provision server
      Server server = null;
      Double cores = ComputeServiceUtils.getCores(hardware);
      Double ram = (double) hardware.getRam();
      if (ram < 1024) {
         ram = 0.5;
      } else {
         ram = ram / 1024;
      }

      try {
         org.apache.jclouds.oneandone.rest.domain.Hardware.CreateHardware hardwareRequest
                 = org.apache.jclouds.oneandone.rest.domain.Hardware.CreateHardware.create(cores, 1, ram, hdds);
         final Server.CreateServer serverRequest = Server.CreateServer.builder()
                 .name(name)
                 .description(name)
                 .hardware(hardwareRequest)
                 .rsaKey(options.getPublicKey())
                 .password(privateKey == null ? password : null)
                 .applianceId(image.getId())
                 .dataCenterId(dataCenterId)
                 .powerOn(Boolean.TRUE).build();

         logger.trace("<< provisioning server '%s'", serverRequest);

         server = api.serverApi().create(serverRequest);

         waitServerUntilAvailable.apply(server);

         updateServer = api.serverApi().get(server.id());

         Map<Integer, Integer> portsRange = getPortRangesFromList(inboundPorts);
         List<FirewallPolicy.Rule.CreatePayload> rules = new ArrayList<FirewallPolicy.Rule.CreatePayload>();

         for (Map.Entry<Integer, Integer> range : portsRange.entrySet()) {
            FirewallPolicy.Rule.CreatePayload rule = FirewallPolicy.Rule.CreatePayload.builder()
                    .portFrom(range.getKey())
                    .portTo(range.getValue())
                    .protocol(Types.RuleProtocol.TCP)
                    .build();
            rules.add(rule);
         }
         if (inboundPorts.length > 0) {
            FirewallPolicy rule = api.firewallPolicyApi().create(FirewallPolicy.CreateFirewallPolicy.create(server.name() + " firewall policy", "desc", rules));
            api.serverApi().addFirewallPolicy(updateServer.id(), updateServer.ips().get(0).id(), rule.id());
            waitServerUntilAvailable.apply(server);
         }

         logger.trace(">> provisioning complete for server. returned id='%s'", server.id());

      } catch (Exception ex) {
         logger.error(ex, ">> failed to provision server. rollbacking..");
         if (server != null) {
            destroyNode(server.id());
         }
         throw Throwables.propagate(ex);
      }

      LoginCredentials serverCredentials = LoginCredentials.builder()
              .user(loginUser)
              .password(password)
              .privateKey(privateKey)
              .build();

      return new NodeAndInitialCredentials<Server>(updateServer, updateServer.id(), serverCredentials);
   }