Package org.jclouds.cloudstack.domain

Examples of org.jclouds.cloudstack.domain.VirtualMachine


   public void testListVirtualMachines() throws Exception {
      Set<VirtualMachine> response = client.getVirtualMachineApi().listVirtualMachines();
      assert null != response;
      assertTrue(response.size() >= 0);
      for (VirtualMachine vm : response) {
         VirtualMachine newDetails = getOnlyElement(client.getVirtualMachineApi().listVirtualMachines(
               ListVirtualMachinesOptions.Builder.id(vm.getId())));
         assertEquals(vm.getId(), newDetails.getId());
         checkVm(vm);
      }
   }
View Full Code Here


      assertPredicateResult(State.ERROR, true);
   }

   private void assertPredicateResult(State state, boolean expected) {
      String virtualMachineId = "229";
      VirtualMachine virtualMachine = VirtualMachine.builder().
         id(virtualMachineId).state(state).build();

      expect(virtualMachineClient.getVirtualMachine(virtualMachineId)).andReturn(virtualMachine);
      replay(client, virtualMachineClient);
View Full Code Here

      expect(client.getVirtualMachineApi()).andReturn(virtualMachineClient);
   }

   @Test
   public void testWaitForVirtualMachineToBeExpunged() {
      VirtualMachine virtualMachine = VirtualMachine.builder().id("229").build();
      expect(virtualMachineClient.getVirtualMachine(virtualMachine.getId())).andReturn(null);

      replay(client, virtualMachineClient);
      assertTrue(new VirtualMachineExpunged(client).apply(virtualMachine));
      verify(client, virtualMachineClient);
   }
View Full Code Here

      verify(client, virtualMachineClient);
   }

   @Test
   public void testNoRemovedYet() {
      VirtualMachine virtualMachine = VirtualMachine.builder().id("229").build();
      expect(virtualMachineClient.getVirtualMachine(virtualMachine.getId())).andReturn(virtualMachine);

      replay(client, virtualMachineClient);
      assertFalse(new VirtualMachineExpunged(client).apply(virtualMachine));
      verify(client, virtualMachineClient);
   }
View Full Code Here

      expect(client.getVirtualMachineApi()).andReturn(virtualMachineClient);
   }

   @Test
   public void testIsDestroyed() {
      VirtualMachine virtualMachine = VirtualMachine.builder().
         id("229").state(VirtualMachine.State.DESTROYED).build();

      expect(virtualMachineClient.getVirtualMachine(virtualMachine.getId())).andReturn(virtualMachine);

      replay(client, virtualMachineClient);
      assertTrue(new VirtualMachineDestroyed(client).apply(virtualMachine));
      verify(client, virtualMachineClient);
   }
View Full Code Here

      verify(client, virtualMachineClient);
   }

   @Test
   public void testStillRunning() {
      VirtualMachine virtualMachine = VirtualMachine.builder().
         id("229").state(VirtualMachine.State.RUNNING).build();

      expect(virtualMachineClient.getVirtualMachine(virtualMachine.getId())).andReturn(virtualMachine);

      replay(client, virtualMachineClient);
      assertFalse(new VirtualMachineDestroyed(client).apply(virtualMachine));
      verify(client, virtualMachineClient);
   }
View Full Code Here

      this.jobComplete = checkNotNull(jobComplete, "jobComplete");
   }

   @Override
   public ImageTemplate buildImageTemplateFromNode(String name, String id) {
      VirtualMachine vm = client.getVirtualMachineApi().getVirtualMachine(id);
      if (vm == null)
         throw new NoSuchElementException("Cannot find vm with id: " + id);
      CloneImageTemplate template = new ImageTemplateBuilder.CloneImageTemplateBuilder().nodeId(id).name(name).build();
      return template;
   }
View Full Code Here

   public ListenableFuture<Image> createImage(ImageTemplate template) {
      checkState(template instanceof CloneImageTemplate,
               " cloudstack only currently supports creating images through cloning.");
      CloneImageTemplate cloneTemplate = (CloneImageTemplate) template;

      VirtualMachine vm = client.getVirtualMachineApi().getVirtualMachine(cloneTemplate.getSourceNodeId());
      String stopJob = client.getVirtualMachineApi().stopVirtualMachine(vm.getId());
      jobComplete.apply(stopJob);

      Set<Volume> volumes = client.getVolumeApi().listVolumes(ListVolumesOptions.Builder.virtualMachineId(vm.getId()));
      Volume volume = Iterables.getOnlyElement(volumes);
     
      CreateTemplateOptions options = CreateTemplateOptions.Builder.volumeId(volume.getId());
      AsyncCreateResponse templateJob = client.getTemplateApi().createTemplate(TemplateMetadata.builder()
                                                                                  .name(cloneTemplate.getName())
                                                                                  .osTypeId(vm.getGuestOSId())
                                                                                  .displayText(cloneTemplate.getName())
                                                                                  .build(), options);
      Template newTemplate = blockUntilJobCompletesAndReturnResult.<Template>apply(templateJob);

      logger.info(">> Registered new template %s, waiting for it to become available.", newTemplate.getId());

      final AtomicReference<Image> image = Atomics.newReference(new ImageBuilder()
                                                                .location(find(locations.get(), idEquals(vm.getZoneId())))
                                                                .id(newTemplate.getId())
                                                                .providerId(newTemplate.getId())
                                                                .description(cloneTemplate.getName())
                                                                .operatingSystem(OperatingSystem.builder().description(cloneTemplate.getName()).build())
                                                                .status(Image.Status.PENDING).build());
View Full Code Here

   @Test
   public void testCreateVMWithoutSecurityGroupAssignsDefault() throws Exception {
      skipIfSecurityGroupsNotSupported();
      String defaultTemplate = template != null ? template.getImageId() : null;
      VirtualMachine newVm = VirtualMachineApiLiveTest.createVirtualMachineWithOptionsInZone(DeployVirtualMachineOptions.NONE,
            zone.getId(), defaultTemplateOrPreferredInZone(defaultTemplate, client, zone.getId()), client,
            jobComplete, virtualMachineRunning);
      try {
         VirtualMachine runningVm = client.getVirtualMachineApi().getVirtualMachine(newVm.getId());
         assertEquals(1, runningVm.getSecurityGroups().size());
         assertEquals(Iterables.getOnlyElement(runningVm.getSecurityGroups()).getName(), "default");
      } finally {
         assertTrue(jobComplete.apply(client.getVirtualMachineApi().destroyVirtualMachine(newVm.getId())));
      }
   }
View Full Code Here

      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();
             // 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 {
                   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);
   }
View Full Code Here

TOP

Related Classes of org.jclouds.cloudstack.domain.VirtualMachine

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.