Package org.jclouds.cloudstack.compute.options

Examples of org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions


      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);

      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.getSSHKeyPairClient().getSSHKeyPair(templateOptions.getKeyPair()) != null) {
            keyPair = client.getSSHKeyPairClient().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 (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());
         }
      }
     
      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.getVirtualMachineClient().deployVirtualMachineInZone(zoneId, serviceOfferingId,
         templateId, options);
      VirtualMachine vm = blockUntilJobCompletesAndReturnResult.<VirtualMachine>apply(job);
      logger.debug("--- virtualmachine: %s", vm);
      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());
      }
     
      if (templateOptions.shouldSetupStaticNat()) {
         Capabilities capabilities = client.getConfigurationClient().listCapabilities();
         // TODO: possibly not all network ids, do we want to do this
         for (String networkId : options.getNetworkIds()) {
            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.getVirtualMachineClient().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 {
View Full Code Here


      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());
         }
      }
     
      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.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());
      }
     
      try {
          if (templateOptions.shouldSetupStaticNat()) {
             Capabilities capabilities = client.getConfigurationApi().listCapabilities();
             // TODO: possibly not all network ids, do we want to do this
             for (String networkId : options.getNetworkIds()) {
                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 {
View Full Code Here

   @Test
   public void testBasicNetworkOptionsConverter() {
      BasicNetworkOptionsConverter converter = new BasicNetworkOptionsConverter();

      CloudStackTemplateOptions optionsIn = CloudStackTemplateOptions.Builder.securityGroupId("42").networks("46");
      DeployVirtualMachineOptions optionsOut = new DeployVirtualMachineOptions();

      DeployVirtualMachineOptions optionsOut2 = converter.apply(optionsIn, EMPTY_NETWORKS_MAP, ZONE_ID, optionsOut);
      assertEquals(optionsOut, optionsOut2);
View Full Code Here

   @Test
   public void testAdvancedSecurityGroupsNotAllowed() {
      boolean exceptionThrown = false;
      AdvancedNetworkOptionsConverter converter = new AdvancedNetworkOptionsConverter();
      CloudStackTemplateOptions optionsIn = CloudStackTemplateOptions.Builder.securityGroupId("42");

      try {
         converter.apply(optionsIn, EMPTY_NETWORKS_MAP, ZONE_ID, DeployVirtualMachineOptions.NONE);
      } catch (IllegalArgumentException e) {
         exceptionThrown = true;
View Full Code Here

         zone = zoneIdToZone.get().get(zoneId);
      } catch (ExecutionException e) {
         throw Throwables.propagate(e);
      }

      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);

      if (templateOptions.getIpOnDefaultNetwork() != null) {
         options.ipOnDefaultNetwork(templateOptions.getIpOnDefaultNetwork());
      }

      if (templateOptions.getIpsToNetworks().size() > 0) {
         options.ipsToNetworks(templateOptions.getIpsToNetworks());
      }

      if (templateOptions.getKeyPair() != null) {
         options.keyPair(templateOptions.getKeyPair());
         if (templateOptions.getRunScript() != null) {
            checkArgument(
               credentialStore.containsKey("keypair#" + templateOptions.getKeyPair()),
               "no private key configured for: %s; please use options.overrideLoginCredentialWith(rsa_private_text)",
               templateOptions.getKeyPair());
         }
      }

      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.getVirtualMachineClient().deployVirtualMachineInZone(zoneId, serviceOfferingId,
         templateId, options);
      VirtualMachine vm = blockUntilJobCompletesAndReturnResult.<VirtualMachine>apply(job);
      logger.debug("--- virtualmachine: %s", vm);
      LoginCredentials credentials = null;
      if (!vm.isPasswordEnabled() || templateOptions.getKeyPair() != null) {
         credentials = LoginCredentials.fromCredentials(credentialStore.get("keypair#" + templateOptions.getKeyPair()));
      } else {
         assert vm.getPassword() != null : vm;
         credentials = LoginCredentials.builder().password(vm.getPassword()).build();
      }
      if (templateOptions.shouldSetupStaticNat()) {
         Capabilities capabilities = client.getConfigurationClient().listCapabilities();
         // TODO: possibly not all network ids, do we want to do this
         for (String networkId : options.getNetworkIds()) {
            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.getVirtualMachineClient().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 {
View Full Code Here

   @Test
   public void testBasicNetworkOptionsConverter() {
      BasicNetworkOptionsConverter converter = new BasicNetworkOptionsConverter();

      CloudStackTemplateOptions optionsIn = CloudStackTemplateOptions.Builder.securityGroupId("42").networkId("46");
      DeployVirtualMachineOptions optionsOut = new DeployVirtualMachineOptions();

      DeployVirtualMachineOptions optionsOut2 = converter.apply(optionsIn, EMPTY_NETWORKS_MAP, ZONE_ID, optionsOut);
      assertEquals(optionsOut, optionsOut2);
View Full Code Here

   @Test
   public void testAdvancedSecurityGroupsNotAllowed() {
      boolean exceptionThrown = false;
      AdvancedNetworkOptionsConverter converter = new AdvancedNetworkOptionsConverter();
      CloudStackTemplateOptions optionsIn = CloudStackTemplateOptions.Builder.securityGroupId("42");

      try {
         converter.apply(optionsIn, EMPTY_NETWORKS_MAP, ZONE_ID, DeployVirtualMachineOptions.NONE);
      } catch(IllegalArgumentException e) {
         exceptionThrown = true;
View Full Code Here

      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());
         }
      }
     
      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.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());
      }
     
      if (templateOptions.shouldSetupStaticNat()) {
         Capabilities capabilities = client.getConfigurationApi().listCapabilities();
         // TODO: possibly not all network ids, do we want to do this
         for (String networkId : options.getNetworkIds()) {
            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 {
View Full Code Here

   @Test
   public void testBasicNetworkOptionsConverter() {
      BasicNetworkOptionsConverter converter = new BasicNetworkOptionsConverter();

      CloudStackTemplateOptions optionsIn = CloudStackTemplateOptions.Builder.securityGroupId("42").networkId("46");
      DeployVirtualMachineOptions optionsOut = new DeployVirtualMachineOptions();

      DeployVirtualMachineOptions optionsOut2 = converter.apply(optionsIn, EMPTY_NETWORKS_MAP, ZONE_ID, optionsOut);
      assertEquals(optionsOut, optionsOut2);
View Full Code Here

   @Test
   public void testAdvancedSecurityGroupsNotAllowed() {
      boolean exceptionThrown = false;
      AdvancedNetworkOptionsConverter converter = new AdvancedNetworkOptionsConverter();
      CloudStackTemplateOptions optionsIn = CloudStackTemplateOptions.Builder.securityGroupId("42");

      try {
         converter.apply(optionsIn, EMPTY_NETWORKS_MAP, ZONE_ID, DeployVirtualMachineOptions.NONE);
      } catch (IllegalArgumentException e) {
         exceptionThrown = true;
View Full Code Here

TOP

Related Classes of org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.