Python pyVmomi.vim.DistributedVirtualSwitch() Examples

The following are 11 code examples of pyVmomi.vim.DistributedVirtualSwitch(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyVmomi.vim , or try the search function .
Example #1
Source File: network.py    From vsphere-automation-sdk-python with MIT License 8 votes vote down vote up
def create_vdportgroup(context, vdswitch_name, vdportgroup_name):
    """Create Distributed Switch portgroup"""
    vdportgroup_type = "earlyBinding"

    vdswitch = context.testbed.entities['DISTRIBUTED_SWITCH_IDS'][vdswitch_name]
    vdswitch_mo = vim.DistributedVirtualSwitch(vdswitch, context.soap_stub)

    vdportgroup_spec = vim.dvs.DistributedVirtualPortgroup.ConfigSpec(
        name=vdportgroup_name, type=vdportgroup_type)
    vdportgroup_specs = [vdportgroup_spec]

    task = vdswitch_mo.AddPortgroups(vdportgroup_specs)
    pyVim.task.WaitForTask(task)

    # The AddPortgroup operation doesn't return any information about the
    # created portgroup, so look it up.
    vdportgroup = None
    for vdportgroup_mo in vdswitch_mo.portgroup:
        if vdportgroup_mo.name == vdportgroup_name:
            vdportgroup = vdportgroup_mo._moId
            print(
                "Created Distributed Portgroup '{}' ({}) on Distributed Switch '{}' ({})".
                format(vdportgroup_name, vdportgroup, vdswitch_name, vdswitch))
    return vdportgroup 
Example #2
Source File: network.py    From vsphere-automation-sdk-python with MIT License 6 votes vote down vote up
def remove_host_from_vdswitch(context, vdswitch_mo, host_name):
    """Remove host from Distributed Switch"""
    for host_member in vdswitch_mo.config.host:
        if host_member.config.host.name == host_name:
            dvs_member_config = vim.dvs.HostMember.ConfigSpec(
                operation="remove",
                host=host_member.config.host)

            dvs_config = vim.DistributedVirtualSwitch.ConfigSpec(
                configVersion=vdswitch_mo.config.configVersion,
                host=[dvs_member_config])

            task = vdswitch_mo.Reconfigure(dvs_config)
            pyVim.task.WaitForTask(task)

            print("Removed Host '{}' ({}) from Distributed Switch '{}' ({})".
                  format(host_name, host_member.config.host._moId,
                         vdswitch_mo.summary.name, vdswitch_mo._moId)) 
Example #3
Source File: __init__.py    From kcli with Apache License 2.0 6 votes vote down vote up
def create_network(self, name, cidr=None, dhcp=True, nat=True, domain=None, plan='kvirt', overrides={}):
        return {'result': 'failure', 'reason': "Not implemented yet..."}
        si = self.si
        cluster = self.clu
        networkFolder = self.dc.networkFolder
        rootFolder = self.rootFolder
        net = find(si, rootFolder, vim.Network, name)
        if net is not None:
            return {'result': 'failure', 'reason': "Network %s already there" % name}
        if self.distributed:
            pnic_specs = []
            dvs_host_configs = []
            uplink_port_names = []
            dvs_create_spec = vim.DistributedVirtualSwitch.CreateSpec()
            dvs_config_spec = vim.DistributedVirtualSwitch.ConfigSpec()
            dvs_config_spec.name = name
            dvs_config_spec.uplinkPortPolicy = vim.DistributedVirtualSwitch.NameArrayUplinkPortPolicy()
            for x in range(len(cluster.host)):
                uplink_port_names.append("dvUplink%d" % x)
            for host in cluster.host:
                dvs_config_spec.uplinkPortPolicy.uplinkPortName = uplink_port_names
                dvs_config_spec.maxPorts = 2000
                pnic_spec = vim.dvs.HostMember.PnicSpec()
                pnic_spec.pnicDevice = 'vmnic1'
                pnic_specs.append(pnic_spec)
                dvs_host_config = vim.dvs.HostMember.ConfigSpec()
                dvs_host_config.operation = vim.ConfigSpecOperation.add
                dvs_host_config.host = host
                dvs_host_configs.append(dvs_host_config)
                dvs_host_config.backing = vim.dvs.HostMember.PnicBacking()
                dvs_host_config.backing.pnicSpec = pnic_specs
                dvs_config_spec.host = dvs_host_configs
                dvs_create_spec.configSpec = dvs_config_spec
            dvs_create_spec.productInfo = vim.dvs.ProductSpec(version='5.1.0')
            networkFolder.CreateDistributedVirtualSwitch()
        return {'result': 'success'} 
Example #4
Source File: vmware.py    From skylight with GNU General Public License v3.0 5 votes vote down vote up
def find_dvs_by_name(content, switch_name):
    return find_object_by_name(content, switch_name, [vim.DistributedVirtualSwitch]) 
Example #5
Source File: network.py    From vsphere-automation-sdk-python with MIT License 5 votes vote down vote up
def find_vdswitch(context, datacenter_name, vdswitch_name):
    """ Retrieve an existing Distributed Switch"""
    # TODO Ugly deep nesting.
    for datacenter_mo in context.service_instance.content.rootFolder \
            .childEntity:
        if (isinstance(datacenter_mo, vim.Datacenter) and
                    datacenter_mo.name == datacenter_name):
            for vdswitch_mo in datacenter_mo.networkFolder.childEntity:
                if (isinstance(vdswitch_mo, vim.DistributedVirtualSwitch) and
                            vdswitch_mo.summary.name == vdswitch_name):
                    print("Found VDSwitch '{}' ({}) in Datacenter '{}' ({})".
                          format(vdswitch_name, vdswitch_mo._moId,
                                 datacenter_name, datacenter_mo._moId))
                    return vdswitch_mo
    return None 
Example #6
Source File: network.py    From vsphere-automation-sdk-python with MIT License 5 votes vote down vote up
def create_vdswitch(context, datacenter_name, vdswitch_name):
    """Create Distributed Switch in given datacenter"""
    datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name]
    datacenter_mo = vim.Datacenter(datacenter, context.soap_stub)

    spec = vim.DistributedVirtualSwitch.CreateSpec()
    spec.configSpec = vim.DistributedVirtualSwitch.ConfigSpec(
        name=vdswitch_name)

    task = datacenter_mo.networkFolder.CreateDistributedVirtualSwitch(spec)
    pyVim.task.WaitForTask(task)
    vdswitch_mo = task.info.result
    print("Created Distributed Switch '{}' ({})".
          format(vdswitch_name, vdswitch_mo._moId))
    return vdswitch_mo._moId 
Example #7
Source File: network.py    From vsphere-automation-sdk-python with MIT License 5 votes vote down vote up
def add_host_to_vdswitch(context, vdswitch_name, host_name, pnic_names=None):
    """Add host to Distributed Switch"""
    host = context.testbed.entities['HOST_IDS'][host_name]
    host_mo = vim.HostSystem(host, context.soap_stub)

    vdswitch = context.testbed.entities['DISTRIBUTED_SWITCH_IDS'][vdswitch_name]
    vdswitch_mo = vim.DistributedVirtualSwitch(vdswitch, context.soap_stub)

    pnic_specs = []
    if pnic_names:
        for pnic in pnic_names:
            pnic_specs.append(vim.dvs.HostMember.PnicSpec(pnicDevice=pnic))

    dvs_member_config = vim.dvs.HostMember.ConfigSpec(
        operation="add",
        host=host_mo,
        backing=vim.dvs.HostMember.PnicBacking(pnicSpec=pnic_specs))

    dvs_config = vim.DistributedVirtualSwitch.ConfigSpec(
        configVersion=vdswitch_mo.config.configVersion,
        host=[dvs_member_config])

    task = vdswitch_mo.Reconfigure(dvs_config)
    pyVim.task.WaitForTask(task)

    print("Added Host '{}' ({}) to Distributed Switch '{}' ({})".
          format(host_name, host, vdswitch_name, vdswitch)) 
Example #8
Source File: create_dvs_and_dvport_group.py    From vmware-pyvmomi-examples with Apache License 2.0 5 votes vote down vote up
def create_dvSwitch(si, content, network_folder, cluster):
    pnic_specs = []
    dvs_host_configs = []
    uplink_port_names = []
    dvs_create_spec = vim.DistributedVirtualSwitch.CreateSpec()
    dvs_config_spec = vim.DistributedVirtualSwitch.ConfigSpec()
    dvs_config_spec.name = inputs['dvs_name']
    dvs_config_spec.uplinkPortPolicy = vim.DistributedVirtualSwitch.NameArrayUplinkPortPolicy()
    hosts = cluster.host
    for x in range(len(hosts)):
        uplink_port_names.append("dvUplink%d" % x)

    for host in hosts:
        dvs_config_spec.uplinkPortPolicy.uplinkPortName = uplink_port_names
        dvs_config_spec.maxPorts = 2000
        pnic_spec = vim.dvs.HostMember.PnicSpec()
        pnic_spec.pnicDevice = 'vmnic1'
        pnic_specs.append(pnic_spec)
        dvs_host_config = vim.dvs.HostMember.ConfigSpec()
        dvs_host_config.operation = vim.ConfigSpecOperation.add
        dvs_host_config.host = host
        dvs_host_configs.append(dvs_host_config)
        dvs_host_config.backing = vim.dvs.HostMember.PnicBacking()
        dvs_host_config.backing.pnicSpec = pnic_specs
        dvs_config_spec.host = dvs_host_configs

    dvs_create_spec.configSpec = dvs_config_spec
    dvs_create_spec.productInfo = vim.dvs.ProductSpec(version='5.1.0')

    task = network_folder.CreateDVS_Task(dvs_create_spec)
    wait_for_task(task, si)
    print "Successfully created DVS ", inputs['dvs_name']
    return get_obj(content, [vim.DistributedVirtualSwitch], inputs['dvs_name']) 
Example #9
Source File: manage_dvs_pg.py    From contrail-server-manager with Apache License 2.0 5 votes vote down vote up
def main():
    args = get_args()
    try:
        if is_xenial_or_above():
            ssl = __import__("ssl")
            context = ssl._create_unverified_context()
            si = connect.SmartConnect(host=args.host,
                                      user=args.user,
                                      pwd=args.password,
                                      port=args.port, sslContext=context)
        else:
            si = connect.SmartConnect(host=args.host,
                                      user=args.user,
                                      pwd=args.password,
                                      port=args.port)
        si_content = si.RetrieveContent()
    except:
        print "Unable to connect to %s" % args.host
        exit(1)
    # check if PG exists else return error
    dv_switch = get_obj(si_content, [vim.DistributedVirtualSwitch], args.dvs_name)
    if not dv_switch:
        print "dv switch %s not pressent" %(args.dvs_name)
        exit(1)
    dv_pg = get_dvs_pg_obj(si_content, [vim.dvs.DistributedVirtualPortgroup], args.dv_pg_name, args.dvs_name)
    if not dv_pg:
        print "port-group %s not present in dvs %s" %(args.dv_pg_name, args.dvs_name)
        exit(1)
    update_dv_pg(args, dv_pg)
    add_pvlan_config(dv_switch)
    connect.Disconnect(si) 
Example #10
Source File: get_vcenter_id.py    From ansible-module-chaperone with Apache License 2.0 5 votes vote down vote up
def core(module):

    vim_type = module.params['vcenter_vim_type']
    vcenter_object_name = module.params['vcenter_object_name']

    vim_rec_type = {
        'cluster': vim.ClusterComputeResource,
        'datacenter': vim.Datacenter,
        'datastore': vim.Datastore,
        'vds': vim.DistributedVirtualSwitch,
        'dvs-port': vim.Network,
        'vm': vim.VirtualMachine
    }

    try:
        vimtype = vim_rec_type[vim_type]
    except KeyError:
        module.fail_json(msg="Please specify valid vim type: cluster, datacenter, datastore, vds, vm")

    si = si_connect(module)

    vcenter_id = get_id(module, si,
                        [vimtype],
                        vcenter_object_name,
                        False, True)

    return False, vcenter_id 
Example #11
Source File: configure_dvs_port_group.py    From pyvmomi-community-samples with Apache License 2.0 4 votes vote down vote up
def configure_dvs_pg(service_instance, dvs_name, dv_pg_name):
    """
    Configures the distributed port group
    :param service_instance: Vcenter service instance
    :param dvs_name: Name of the distributed virtual switch
    :param dv_pg_name: Name of distributed virtual port group
    """
    # Retrieve the content
    content = service_instance.RetrieveContent()

    # get distributed Switch and its port group objects
    dvs = get_obj(content, [vim.DistributedVirtualSwitch], dvs_name)
    dv_pg = get_obj(content, [vim.dvs.DistributedVirtualPortgroup], dv_pg_name)
    print("The distributed virtual Switch is {0}" .format(dvs))
    print("The distributed port group is {0}".format(dv_pg))

    # construct selection sets
    selection_sets = []
    dv_pg_ss = vim.dvs.DistributedVirtualPortgroupSelection()
    dv_pg_ss.dvsUuid = dvs.uuid
    dv_pg_ss.portgroupKey.append(dv_pg.key)
    selection_sets.append(dv_pg_ss)
    print("The selected port group configurations  are {0}"
          .format(selection_sets))

    # Backup/Export the configuration
    entity_backup_config = service_instance.content\
        .dvSwitchManager\
        .DVSManagerExportEntity_Task(selection_sets)
    export_result = entity_backup_config.info.result
    print("The result of export configuration are {0}".format(export_result))

    # Destroy the port group configuration
    dv_pg.Destroy_Task()

    # Restore/Import port group configuration
    entity_restore_config = service_instance.content\
        .dvSwitchManager\
        .DVSManagerImportEntity_Task(export_result,
                                     'createEntityWithOriginalIdentifier')
    print("The result of restore configuration is {0}"
          .format(entity_restore_config.info.result))