Esempio n. 1
0
 private static boolean customValidation() throws Exception {
   boolean validate = false;
   String datacenterName = datacenter;
   String datastoreName = datastore;
   if (datacenterName.length() != 0
       && datacenterName != null
       && datastoreName.length() != 0
       && datastoreName != null) {
     ManagedObjectReference dcmor = getDatacenterByName(datacenter);
     if (dcmor != null) {
       List<DynamicProperty> datastoresList = getDynamicProarray(dcmor, "Datacenter", "datastore");
       ArrayOfManagedObjectReference ds =
           ((ArrayOfManagedObjectReference) (datastoresList.get(0)).getVal());
       List<ManagedObjectReference> datastores = ds.getManagedObjectReference();
       if (datastores.size() != 0) {
         for (int i = 0; i < datastores.size(); i++) {
           DatastoreSummary dsSummary = getDataStoreSummary(datastores.get(i));
           if (dsSummary.getName().equalsIgnoreCase(datastoreName)) {
             i = datastores.size() + 1;
             validate = true;
           }
         }
         if (!validate) {
           System.out.println(
               "Specified Datastore with name "
                   + datastore
                   + " was not"
                   + " found in specified Datacenter");
         }
         return validate;
       } else {
         System.out.println("No Datastore found in specified Datacenter");
         return validate;
       }
     } else {
       System.out.println("Specified Datacenter with name " + datacenter + " not Found");
       return validate;
     }
   }
   return validate;
 }
Esempio n. 2
0
  private static boolean registerVirtualMachine() throws Exception {
    boolean registered = false;
    ManagedObjectReference resourcePoolRef = new ManagedObjectReference();
    System.out.println("Registering The Virtual Machine ..........");
    ManagedObjectReference host = null;
    // Get The Data Center
    ManagedObjectReference dcmor = getDatacenterByName(datacenter);
    List<DynamicProperty> vmFolderList = getDynamicProarray(dcmor, "Datacenter", "vmFolder");
    ManagedObjectReference vmFolderMor = (ManagedObjectReference) (vmFolderList.get(0)).getVal();

    List<DynamicProperty> hostFolderList = getDynamicProarray(dcmor, "Datacenter", "hostFolder");
    ManagedObjectReference hostFolderMor = (ManagedObjectReference) hostFolderList.get(0).getVal();

    List<DynamicProperty> computeResourceList =
        getDynamicProarray(hostFolderMor, "Folder", "childEntity");
    List<ManagedObjectReference> csMoRefList = new ArrayList<ManagedObjectReference>();
    List<ManagedObjectReference> cSList = new ArrayList<ManagedObjectReference>();
    for (int i = 0; i < computeResourceList.size(); i++) {
      ArrayOfManagedObjectReference moArray =
          (ArrayOfManagedObjectReference) (computeResourceList.get(0)).getVal();
      csMoRefList.addAll(moArray.getManagedObjectReference());
      for (int j = 0; j < csMoRefList.size(); j++) {
        if (csMoRefList.get(j).getType().toString().contains("ComputeResource")) {
          cSList.add(csMoRefList.get(j));
        }
      }
    }
    List<ManagedObjectReference> hosts = new ArrayList<ManagedObjectReference>();
    for (int k = 0; k < cSList.size(); k++) {
      List<DynamicProperty> hostSystemList =
          getDynamicProarray(cSList.get(k), "ComputeResource", "host");
      ArrayOfManagedObjectReference hostsArray =
          (ArrayOfManagedObjectReference) (hostSystemList.get(0)).getVal();
      ManagedObjectReference hostSystem = hostsArray.getManagedObjectReference().get(0);
      hosts.add(hostSystem);
    }

    if (hosts.size() < 1) {
      System.out.println("No host found in datacenter to" + " register the Virtual Machine");
      return registered;
    } else {
      boolean hostFound = false;
      for (int i = 0; i < hosts.size(); i++) {
        ManagedObjectReference hostMor = (ManagedObjectReference) hosts.get(i);
        List<DynamicProperty> datastoresList =
            getDynamicProarray(hostMor, "HostSystem", "datastore");
        ArrayOfManagedObjectReference hostSystemArray =
            ((ArrayOfManagedObjectReference) (datastoresList.get(0)).getVal());
        List<ManagedObjectReference> datastores = hostSystemArray.getManagedObjectReference();

        long dirSize = getDirSize(localPath);
        for (int j = 0; j < datastores.size(); j++) {
          DatastoreSummary dataStoreSummary = getDataStoreSummary(datastores.get(j));

          if (dataStoreSummary.getName().equalsIgnoreCase(datastore)) {
            DatastoreInfo datastoreInfo = getDataStoreInfo(datastores.get(j));
            if (datastoreInfo.getFreeSpace() > dirSize) {
              host = hostMor;
              hostFound = true;
              i = hosts.size() + 1;
              j = datastores.size() + 1;
            }
          }
        }
      }
      if (hostFound) {
        // Get The vmx path
        String vmxPath = "[" + datastore + "] " + vmName + "/" + vmName + ".vmx";
        ManagedObjectReference vmRef = new ManagedObjectReference();
        List<DynamicProperty> vmsPropList = getDynamicProarray(host, "HostSystem", "vm");
        ArrayOfManagedObjectReference vmsPropArray =
            (ArrayOfManagedObjectReference) (vmsPropList.get(0)).getVal();
        List<ManagedObjectReference> vmsList = vmsPropArray.getManagedObjectReference();
        for (int i = 0; i < vmsList.size(); i++) {
          if (vmsList.get(i).getType().equalsIgnoreCase("VirtualMachine")) {
            vmRef = vmsList.get(i);
            break;
          }
        }
        List<DynamicProperty> rpRef = getDynamicProarray(vmRef, "VirtualMachine", "resourcePool");
        resourcePoolRef = (ManagedObjectReference) rpRef.get(0).getVal();
        // Registering The Virtual machine
        ManagedObjectReference taskmor =
            vimPort.registerVMTask(vmFolderMor, vmxPath, vmName, false, resourcePoolRef, host);

        String result = waitForTask(taskmor);
        if (result != null) {
          if (result.equalsIgnoreCase("sucess")) {
            System.out.println("Registering The Virtual Machine ..........Done");
            registered = true;
          } else {
            System.out.println("Some Exception While Registering The VM");
            registered = false;
          }
        } else {
          System.out.println("Some Exception While Registering The VM");
          registered = false;
        }
        return registered;
      } else {
        System.out.println(
            "No host in datacenter has got the" + " specified datastore and free space");
        return registered;
      }
    }
  }