コード例 #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;
  }
コード例 #2
0
  private static ManagedObjectReference getNetworkFromHost(
      VMwareClient vmw, ManagedObjectReference vmwInstance, String networkName) throws Exception {
    logger.debug("networkName: " + networkName);

    VirtualMachineRuntimeInfo vmRuntimeInfo =
        (VirtualMachineRuntimeInfo) vmw.getServiceUtil().getDynamicProperty(vmwInstance, "runtime");
    ManagedObjectReference hostRef = vmRuntimeInfo.getHost();
    List<ManagedObjectReference> networkRefList =
        (List<ManagedObjectReference>) vmw.getServiceUtil().getDynamicProperty(hostRef, "network");
    ManagedObjectReference netCard = null;
    StringBuffer networks = new StringBuffer();
    for (ManagedObjectReference networkRef : networkRefList) {
      String netCardName = (String) vmw.getServiceUtil().getDynamicProperty(networkRef, "name");
      networks.append(netCardName + " ");
      if (netCardName.equalsIgnoreCase(networkName)) {
        netCard = networkRef;
        break;
      }
    }

    if (netCard == null) {
      String hostName = (String) vmw.getServiceUtil().getDynamicProperty(hostRef, "name");
      logger.error("Network " + networkName + " not found on host " + hostName);
      logger.debug("available networks are: " + networks.toString());
      throw new Exception("Network card " + networkName + " not found on host " + hostName);
    }

    return netCard;
  }
コード例 #3
0
  public static int getNumberOfNICs(VMwareClient vmw, ManagedObjectReference vmwInstance)
      throws Exception {
    logger.debug("");

    VirtualMachineConfigInfo configInfo =
        (VirtualMachineConfigInfo) vmw.getServiceUtil().getDynamicProperty(vmwInstance, "config");
    List<VirtualEthernetCard> vmNics = getNetworkAdapter(configInfo);
    return vmNics.size();
  }
コード例 #4
0
  /**
   * Replaces the NICs in the given VM.
   *
   * @param vmw connected VMware client entity
   * @param paramHandler entity which holds all properties of the instance
   * @param vmwInstance the virtual machine that gets reconfigured
   */
  public static void configureNetworkAdapter(
      VMwareClient vmw,
      VirtualMachineConfigSpec vmConfigSpec,
      VMPropertyHandler paramHandler,
      ManagedObjectReference vmwInstance)
      throws Exception {
    logger.debug("");

    VirtualMachineConfigInfo configInfo =
        (VirtualMachineConfigInfo) vmw.getServiceUtil().getDynamicProperty(vmwInstance, "config");
    List<VirtualEthernetCard> vmNics = getNetworkAdapter(configInfo);

    int numberOfNICs =
        Integer.parseInt(paramHandler.getServiceSetting(VMPropertyHandler.TS_NUMBER_OF_NICS));

    if (numberOfNICs != vmNics.size()) {
      throw new Exception(
          "the number of NICs in virtual machine does not match the service parameter. VM: "
              + configInfo.getName()
              + " NICs: "
              + vmNics.size()
              + " "
              + VMPropertyHandler.TS_NUMBER_OF_NICS
              + ": "
              + numberOfNICs);
    }

    for (int i = 1; i <= numberOfNICs; i++) {
      String newNetworkName = paramHandler.getNetworkAdapter(i);
      VirtualEthernetCard vmNic = vmNics.get(i - 1);
      String vmNetworkName = getNetworkName(vmw, vmwInstance, i);
      if (newNetworkName != null
          && newNetworkName.length() > 0
          && !newNetworkName.equals(vmNetworkName)) {
        ManagedObjectReference newNetworkRef = getNetworkFromHost(vmw, vmwInstance, newNetworkName);

        replaceNetworkAdapter(vmConfigSpec, vmNic, newNetworkRef, newNetworkName);
      } else {
        connectNIC(vmConfigSpec, vmNic, vmNetworkName);
      }
    }
  }