Search in sources :

Example 1 with DistributedVirtualSwitch

use of com.vmware.vim25.mo.DistributedVirtualSwitch in project vsphere-cloud-plugin by jenkinsci.

the class VSphere method getDistributedVirtualSwitchByPortGroup.

/**
 * Find Distributed Virtual Switch from the provided Distributed Virtual Portgroup
 * @param distributedVirtualPortgroup - DistributedVirtualPortgroup object for the provided vDS PortGroup
 * @return returns DistributedVirtualSwitch object that represents the vDS Switch
 * @throws VSphereException If an error occurred.
 */
public DistributedVirtualSwitch getDistributedVirtualSwitchByPortGroup(DistributedVirtualPortgroup distributedVirtualPortgroup) throws VSphereException {
    try {
        ManagedObjectReference managedObjectReference = new ManagedObjectReference();
        managedObjectReference.setType("DistributedVirtualSwitch");
        managedObjectReference.setVal(distributedVirtualPortgroup.getConfig().getDistributedVirtualSwitch().getVal());
        return new DistributedVirtualSwitch(getServiceInstance().getServerConnection(), managedObjectReference);
    } catch (Exception e) {
        throw new VSphereException(e);
    }
}
Also used : DistributedVirtualSwitch(com.vmware.vim25.mo.DistributedVirtualSwitch) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 2 with DistributedVirtualSwitch

use of com.vmware.vim25.mo.DistributedVirtualSwitch in project vsphere-cloud-plugin by jenkinsci.

the class ReconfigureNetworkAdapters method reconfigureNetwork.

public boolean reconfigureNetwork(final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    String expandedDeviceLabel = deviceLabel;
    String expandedMacAddress = macAddress;
    String expandedPortGroup = portGroup;
    String expandedDistributedPortGroup = distributedPortGroup;
    String expandedDistributedPortId = distributedPortId;
    EnvVars env;
    try {
        env = run.getEnvironment(listener);
    } catch (Exception e) {
        throw new VSphereException(e);
    }
    if (run instanceof AbstractBuild) {
        // Add in matrix axes..
        env.overrideAll(((AbstractBuild) run).getBuildVariables());
        expandedDeviceLabel = env.expand(deviceLabel);
        expandedMacAddress = env.expand(macAddress);
        expandedPortGroup = env.expand(portGroup);
        expandedDistributedPortGroup = env.expand(distributedPortGroup);
        expandedDistributedPortId = env.expand(distributedPortId);
    }
    VSphereLogger.vsLogger(jLogger, "Preparing reconfigure: " + deviceAction.getLabel() + " Network Adapter \"" + expandedDeviceLabel + "\"");
    VirtualEthernetCard vEth = null;
    if (deviceAction == DeviceAction.ADD) {
        vEth = new VirtualE1000();
        vEth.setBacking(new VirtualEthernetCardNetworkBackingInfo());
        Description description = vEth.getDeviceInfo();
        if (description == null) {
            description = new Description();
        }
        description.setLabel(expandedDeviceLabel);
        vEth.setDeviceInfo(description);
    } else {
        vEth = findNetworkDeviceByLabel(vm.getConfig().getHardware().getDevice(), expandedDeviceLabel);
    }
    if (vEth == null) {
        throw new VSphereException("Could not find network device named " + expandedDeviceLabel);
    }
    // change mac address
    if (!expandedMacAddress.isEmpty()) {
        VSphereLogger.vsLogger(jLogger, "Reconfiguring MAC Address -> " + expandedMacAddress);
        vEth.setMacAddress(expandedMacAddress);
    }
    // extract backing from ethernet virtual card, always available
    VirtualDeviceBackingInfo virtualDeviceBackingInfo = vEth.getBacking();
    // change our port group
    if (standardSwitch && !expandedPortGroup.isEmpty()) {
        VSphereLogger.vsLogger(jLogger, "Reconfiguring Network Port Group -> " + expandedPortGroup);
        if (virtualDeviceBackingInfo instanceof VirtualEthernetCardNetworkBackingInfo) {
            VirtualEthernetCardNetworkBackingInfo backing = (VirtualEthernetCardNetworkBackingInfo) virtualDeviceBackingInfo;
            Network networkPortGroup = getVsphere().getNetworkPortGroupByName(getVM(), expandedPortGroup);
            if (networkPortGroup != null) {
                backing.deviceName = expandedPortGroup;
            } else {
                VSphereLogger.vsLogger(jLogger, "Failed to find Network for Port Group -> " + expandedPortGroup);
            }
        } else {
            VSphereLogger.vsLogger(jLogger, "Network Device -> " + expandedDeviceLabel + " isn't standard switch");
        }
    } else // change out distributed switch port group
    if (distributedSwitch && !expandedDistributedPortGroup.isEmpty()) {
        VSphereLogger.vsLogger(jLogger, "Reconfiguring Distributed Switch Port Group -> " + expandedDistributedPortGroup + " Port Id -> " + expandedDistributedPortId);
        if (virtualDeviceBackingInfo instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
            VirtualEthernetCardDistributedVirtualPortBackingInfo virtualEthernetCardDistributedVirtualPortBackingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) virtualDeviceBackingInfo;
            DistributedVirtualPortgroup distributedVirtualPortgroup = getVsphere().getDistributedVirtualPortGroupByName(getVM(), expandedDistributedPortGroup);
            if (distributedVirtualPortgroup != null) {
                DistributedVirtualSwitch distributedVirtualSwitch = getVsphere().getDistributedVirtualSwitchByPortGroup(distributedVirtualPortgroup);
                DistributedVirtualSwitchPortConnection distributedVirtualSwitchPortConnection = new DistributedVirtualSwitchPortConnection();
                distributedVirtualSwitchPortConnection.setSwitchUuid(distributedVirtualSwitch.getUuid());
                distributedVirtualSwitchPortConnection.setPortgroupKey(distributedVirtualPortgroup.getKey());
                distributedVirtualSwitchPortConnection.setPortKey(expandedDistributedPortId);
                virtualEthernetCardDistributedVirtualPortBackingInfo.setPort(distributedVirtualSwitchPortConnection);
                VSphereLogger.vsLogger(jLogger, "Distributed Switch Port Group -> " + expandedDistributedPortGroup + "Port Id -> " + expandedDistributedPortId + " successfully configured!");
            } else {
                VSphereLogger.vsLogger(jLogger, "Failed to find Distributed Virtual Portgroup for Port Group -> " + expandedDistributedPortGroup);
            }
        } else {
            VSphereLogger.vsLogger(jLogger, "Network Device -> " + expandedDeviceLabel + " isn't distributed switch");
        }
    }
    VirtualDeviceConfigSpec vdspec = new VirtualDeviceConfigSpec();
    vdspec.setDevice(vEth);
    if (deviceAction == DeviceAction.EDIT) {
        vdspec.setOperation(VirtualDeviceConfigSpecOperation.edit);
    } else if (deviceAction == DeviceAction.REMOVE) {
        vdspec.setOperation(VirtualDeviceConfigSpecOperation.remove);
    }
    // add change into config spec
    VirtualDeviceConfigSpec[] deviceConfigSpecs = spec.getDeviceChange();
    if (deviceConfigSpecs == null) {
        deviceConfigSpecs = new VirtualDeviceConfigSpec[1];
    } else {
        deviceConfigSpecs = Arrays.copyOf(deviceConfigSpecs, deviceConfigSpecs.length + 1);
    }
    deviceConfigSpecs[deviceConfigSpecs.length - 1] = vdspec;
    spec.setDeviceChange(deviceConfigSpecs);
    VSphereLogger.vsLogger(jLogger, "Finished!");
    return true;
}
Also used : PrintStream(java.io.PrintStream) AbstractBuild(hudson.model.AbstractBuild) DistributedVirtualSwitch(com.vmware.vim25.mo.DistributedVirtualSwitch) ServletException(javax.servlet.ServletException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException) DistributedVirtualPortgroup(com.vmware.vim25.mo.DistributedVirtualPortgroup) Network(com.vmware.vim25.mo.Network) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException)

Aggregations

DistributedVirtualSwitch (com.vmware.vim25.mo.DistributedVirtualSwitch)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 DistributedVirtualPortgroup (com.vmware.vim25.mo.DistributedVirtualPortgroup)1 Network (com.vmware.vim25.mo.Network)1 AbstractBuild (hudson.model.AbstractBuild)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 MalformedURLException (java.net.MalformedURLException)1 RemoteException (java.rmi.RemoteException)1 ServletException (javax.servlet.ServletException)1 VSphereException (org.jenkinsci.plugins.vsphere.tools.VSphereException)1