Ejemplo n.º 1
0
  public static String getNetworkName(
      VMwareClient vmw, ManagedObjectReference vmwInstance, int numNic) throws Exception {
    logger.debug("");

    List<ManagedObjectReference> networkList =
        (List<ManagedObjectReference>)
            vmw.getServiceUtil().getDynamicProperty(vmwInstance, "network");

    String name = null;
    if (networkList != null && networkList.size() >= numNic) {
      name = (String) vmw.getServiceUtil().getDynamicProperty(networkList.get(numNic - 1), "name");
      NetworkSummary summary =
          (NetworkSummary)
              vmw.getServiceUtil().getDynamicProperty(networkList.get(numNic - 1), "summary");

      logger.debug(
          "name: "
              + name
              + " ipPoolId: "
              + summary.getIpPoolName()
              + " ipPoolName: "
              + summary.getName());
    }

    if (name == null) {
      throw new Exception("Failed to retrieve network name from template.");
    }
    return name;
  }
Ejemplo n.º 2
0
  /**
   * Creates the vm config spec object.
   *
   * @param vmName the vm name
   * @param datastoreName the datastore name
   * @param diskSizeMB the disk size in mb
   * @param computeResMor the compute res moref
   * @param hostMor the host mor
   * @return the virtual machine config spec object
   * @throws Exception the exception
   */
  VirtualMachineConfigSpec createVmConfigSpec(
      String vmName,
      String datastoreName,
      int diskSizeMB,
      ManagedObjectReference computeResMor,
      ManagedObjectReference hostMor)
      throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg {

    ConfigTarget configTarget = getConfigTargetForHost(computeResMor, hostMor);
    List<VirtualDevice> defaultDevices = getDefaultDevices(computeResMor, hostMor);
    VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
    String networkName = null;
    if (configTarget.getNetwork() != null) {
      for (int i = 0; i < configTarget.getNetwork().size(); i++) {
        VirtualMachineNetworkInfo netInfo = configTarget.getNetwork().get(i);
        NetworkSummary netSummary = netInfo.getNetwork();
        if (netSummary.isAccessible()) {
          networkName = netSummary.getName();
          break;
        }
      }
    }
    ManagedObjectReference datastoreRef = null;
    if (datastoreName != null) {
      boolean flag = false;
      for (int i = 0; i < configTarget.getDatastore().size(); i++) {
        VirtualMachineDatastoreInfo vdsInfo = configTarget.getDatastore().get(i);
        DatastoreSummary dsSummary = vdsInfo.getDatastore();
        if (dsSummary.getName().equals(datastoreName)) {
          flag = true;
          if (dsSummary.isAccessible()) {
            datastoreRef = dsSummary.getDatastore();
          } else {
            throw new RuntimeException("Specified Datastore is not accessible");
          }
          break;
        }
      }
      if (!flag) {
        throw new RuntimeException("Specified Datastore is not Found");
      }
    } else {
      boolean flag = false;
      for (int i = 0; i < configTarget.getDatastore().size(); i++) {
        VirtualMachineDatastoreInfo vdsInfo = configTarget.getDatastore().get(i);
        DatastoreSummary dsSummary = vdsInfo.getDatastore();
        if (dsSummary.isAccessible()) {
          datastoreName = dsSummary.getName();
          datastoreRef = dsSummary.getDatastore();
          flag = true;
          break;
        }
      }
      if (!flag) {
        throw new RuntimeException("No Datastore found on host");
      }
    }
    String datastoreVolume = getVolumeName(datastoreName);
    VirtualMachineFileInfo vmfi = new VirtualMachineFileInfo();
    vmfi.setVmPathName(datastoreVolume);
    configSpec.setFiles(vmfi);
    // Add a scsi controller
    int diskCtlrKey = 1;
    VirtualDeviceConfigSpec scsiCtrlSpec = new VirtualDeviceConfigSpec();
    scsiCtrlSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController();
    scsiCtrl.setBusNumber(0);
    scsiCtrlSpec.setDevice(scsiCtrl);
    scsiCtrl.setKey(diskCtlrKey);
    scsiCtrl.setSharedBus(VirtualSCSISharing.NO_SHARING);
    String ctlrType = scsiCtrl.getClass().getName();
    ctlrType = ctlrType.substring(ctlrType.lastIndexOf(".") + 1);

    // Find the IDE controller
    VirtualDevice ideCtlr = null;
    for (int di = 0; di < defaultDevices.size(); di++) {
      if (defaultDevices.get(di) instanceof VirtualIDEController) {
        ideCtlr = defaultDevices.get(di);
        break;
      }
    }

    // Add a floppy
    VirtualDeviceConfigSpec floppySpec = new VirtualDeviceConfigSpec();
    floppySpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    VirtualFloppy floppy = new VirtualFloppy();
    VirtualFloppyDeviceBackingInfo flpBacking = new VirtualFloppyDeviceBackingInfo();
    flpBacking.setDeviceName("/dev/fd0");
    floppy.setBacking(flpBacking);
    floppy.setKey(3);
    floppySpec.setDevice(floppy);

    // Add a cdrom based on a physical device
    VirtualDeviceConfigSpec cdSpec = null;

    if (ideCtlr != null) {
      cdSpec = new VirtualDeviceConfigSpec();
      cdSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
      VirtualCdrom cdrom = new VirtualCdrom();
      VirtualCdromIsoBackingInfo cdDeviceBacking = new VirtualCdromIsoBackingInfo();
      cdDeviceBacking.setDatastore(datastoreRef);
      cdDeviceBacking.setFileName(datastoreVolume + "testcd.iso");
      cdrom.setBacking(cdDeviceBacking);
      cdrom.setKey(20);
      cdrom.setControllerKey(new Integer(ideCtlr.getKey()));
      cdrom.setUnitNumber(new Integer(0));
      cdSpec.setDevice(cdrom);
    }

    // Create a new disk - file based - for the vm
    VirtualDeviceConfigSpec diskSpec = null;
    diskSpec = createVirtualDisk(datastoreName, diskCtlrKey, datastoreRef, diskSizeMB);

    // Add a NIC. the network Name must be set as the device name to create the NIC.
    VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec();
    if (networkName != null) {
      nicSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
      VirtualEthernetCard nic = new VirtualPCNet32();
      VirtualEthernetCardNetworkBackingInfo nicBacking =
          new VirtualEthernetCardNetworkBackingInfo();
      nicBacking.setDeviceName(networkName);
      nic.setAddressType("generated");
      nic.setBacking(nicBacking);
      nic.setKey(4);
      nicSpec.setDevice(nic);
    }

    List<VirtualDeviceConfigSpec> deviceConfigSpec = new ArrayList<VirtualDeviceConfigSpec>();
    deviceConfigSpec.add(scsiCtrlSpec);
    deviceConfigSpec.add(floppySpec);
    deviceConfigSpec.add(diskSpec);
    if (ideCtlr != null) {
      deviceConfigSpec.add(cdSpec);
      deviceConfigSpec.add(nicSpec);
    } else {
      deviceConfigSpec = new ArrayList<VirtualDeviceConfigSpec>();
      deviceConfigSpec.add(nicSpec);
    }
    configSpec.getDeviceChange().addAll(deviceConfigSpec);
    return configSpec;
  }