Ejemplo n.º 1
0
def ensure_vlan_bridge(session, vif, cluster=None, create_vlan=True):
    """Create a vlan and bridge unless they already exist."""
    vlan_num = vif["network"].get_meta("vlan")
    bridge = vif["network"]["bridge"]
    vlan_interface = CONF.vmware.vlan_interface

    network_ref = network_util.get_network_with_the_name(session, bridge, cluster)
    if network_ref and network_ref["type"] == "DistributedVirtualPortgroup":
        return network_ref

    if not network_ref:
        # Create a port group on the vSwitch associated with the
        # vlan_interface corresponding physical network adapter on the ESX
        # host.
        vswitch_associated = _get_associated_vswitch_for_interface(session, vlan_interface, cluster)
        network_util.create_port_group(session, bridge, vswitch_associated, vlan_num if create_vlan else 0, cluster)
        network_ref = network_util.get_network_with_the_name(session, bridge, cluster)
    elif create_vlan:
        # Get the vSwitch associated with the Physical Adapter
        vswitch_associated = _get_associated_vswitch_for_interface(session, vlan_interface, cluster)
        # Get the vlan id and vswitch corresponding to the port group
        _get_pg_info = network_util.get_vlanid_and_vswitch_for_portgroup
        pg_vlanid, pg_vswitch = _get_pg_info(session, bridge, cluster)

        # Check if the vswitch associated is proper
        if pg_vswitch != vswitch_associated:
            raise exception.InvalidVLANPortGroup(bridge=bridge, expected=vswitch_associated, actual=pg_vswitch)

        # Check if the vlan id is proper for the port group
        if pg_vlanid != vlan_num:
            raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num, pgroup=pg_vlanid)
    return network_ref
Ejemplo n.º 2
0
    def test_ensure_vlan_bridge_without_vlan(self):
        self.mox.StubOutWithMock(network_util, "get_network_with_the_name")
        self.mox.StubOutWithMock(network_util, "get_vswitch_for_vlan_interface")
        self.mox.StubOutWithMock(network_util, "check_if_vlan_interface_exists")
        self.mox.StubOutWithMock(network_util, "create_port_group")

        network_util.get_network_with_the_name(self.session, "fa0", self.cluster).AndReturn(None)
        network_util.get_vswitch_for_vlan_interface(self.session, "vmnet0", self.cluster).AndReturn("vmnet0")
        network_util.check_if_vlan_interface_exists(self.session, "vmnet0", self.cluster).AndReturn(True)
        network_util.create_port_group(self.session, "fa0", "vmnet0", 0, self.cluster)
        self.mox.ReplayAll()
        vif.ensure_vlan_bridge(self.session, self.vif, create_vlan=False)
    def test_create_port_group_already_exists(self):
        def fake_call_method(module, method, *args, **kwargs):
            if method == 'AddPortGroup':
                raise vexc.AlreadyExistsException()

        with test.nested(
            mock.patch.object(vm_util, 'get_add_vswitch_port_group_spec'),
            mock.patch.object(vm_util, 'get_host_ref'),
            mock.patch.object(self.session, '_call_method',
                              fake_call_method)
        ) as (_add_vswitch, _get_host, _call_method):
            network_util.create_port_group(self.session, 'pg_name',
                                           'vswitch_name', vlan_id=0,
                                           cluster=None)
Ejemplo n.º 4
0
Archivo: vif.py Proyecto: bclau/nova
def ensure_vlan_bridge(session, vif, cluster=None, create_vlan=True):
    """Create a vlan and bridge unless they already exist."""
    vlan_num = vif['network'].get_meta('vlan')
    bridge = vif['network']['bridge']
    vlan_interface = CONF.vmware.vlan_interface

    network_ref = network_util.get_network_with_the_name(session, bridge,
                                                         cluster)
    if network_ref and network_ref['type'] == 'DistributedVirtualPortgroup':
        return network_ref

    # Get the vSwitch associated with the Physical Adapter
    vswitch_associated = network_util.get_vswitch_for_vlan_interface(
                                    session, vlan_interface, cluster)
    if vswitch_associated is None:
        raise exception.SwitchNotFoundForNetworkAdapter(
            adapter=vlan_interface)
    # Check if the vlan_interface physical network adapter exists on the
    # host.
    if not network_util.check_if_vlan_interface_exists(session,
                                        vlan_interface, cluster):
        raise exception.NetworkAdapterNotFound(adapter=vlan_interface)

    if network_ref is None:
        # Create a port group on the vSwitch associated with the
        # vlan_interface corresponding physical network adapter on the ESX
        # host.
        network_util.create_port_group(session, bridge,
                                       vswitch_associated,
                                       vlan_num if create_vlan else 0,
                                       cluster)
        network_ref = network_util.get_network_with_the_name(session,
                                                             bridge,
                                                             cluster)
    elif create_vlan:
        # Get the vlan id and vswitch corresponding to the port group
        _get_pg_info = network_util.get_vlanid_and_vswitch_for_portgroup
        pg_vlanid, pg_vswitch = _get_pg_info(session, bridge, cluster)

        # Check if the vswitch associated is proper
        if pg_vswitch != vswitch_associated:
            raise exception.InvalidVLANPortGroup(
                bridge=bridge, expected=vswitch_associated,
                actual=pg_vswitch)

        # Check if the vlan id is proper for the port group
        if pg_vlanid != vlan_num:
            raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num,
                                       pgroup=pg_vlanid)
    return network_ref
Ejemplo n.º 5
0
    def test_create_port_group_already_exists(self):
        def fake_call_method(module, method, *args, **kwargs):
            if method == 'AddPortGroup':
                raise vexc.AlreadyExistsException()

        with test.nested(
            mock.patch.object(vm_util, 'get_add_vswitch_port_group_spec'),
            mock.patch.object(vm_util, 'get_host_ref'),
            mock.patch.object(self.session, '_call_method',
                              fake_call_method)
        ) as (_add_vswitch, _get_host, _call_method):
            network_util.create_port_group(self.session, 'pg_name',
                                           'vswitch_name', vlan_id=0,
                                           cluster=None)
Ejemplo n.º 6
0
def ensure_vlan_bridge(session, vif, cluster=None, create_vlan=True):
    """Create a vlan and bridge unless they already exist."""
    vlan_num = vif['network'].get_meta('vlan')
    bridge = vif['network']['bridge']
    vlan_interface = CONF.vmware.vlan_interface

    network_ref = network_util.get_network_with_the_name(session, bridge,
                                                         cluster)
    # Get the vSwitch associated with the Physical Adapter
    vswitch_associated = network_util.get_vswitch_for_vlan_interface(
                                    session, vlan_interface, cluster)
    if vswitch_associated is None:
        raise exception.SwitchNotFoundForNetworkAdapter(
            adapter=vlan_interface)
    # Check if the vlan_interface physical network adapter exists on the
    # host.
    if not network_util.check_if_vlan_interface_exists(session,
                                        vlan_interface, cluster):
        raise exception.NetworkAdapterNotFound(adapter=vlan_interface)
    if create_vlan:

        if network_ref is None:
        # Create a port group on the vSwitch associated with the
        # vlan_interface corresponding physical network adapter on the ESX
        # host.
            network_util.create_port_group(session, bridge,
                                       vswitch_associated, vlan_num,
                                       cluster)
        else:
            # Get the vlan id and vswitch corresponding to the port group
            _get_pg_info = network_util.get_vlanid_and_vswitch_for_portgroup
            pg_vlanid, pg_vswitch = _get_pg_info(session, bridge, cluster)

            # Check if the vswitch associated is proper
            if pg_vswitch != vswitch_associated:
                raise exception.InvalidVLANPortGroup(
                    bridge=bridge, expected=vswitch_associated,
                    actual=pg_vswitch)

            # Check if the vlan id is proper for the port group
            if pg_vlanid != vlan_num:
                raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num,
                                           pgroup=pg_vlanid)
    else:
        if network_ref is None:
            network_util.create_port_group(session, bridge,
                                       vswitch_associated, 0,
                                       cluster)
Ejemplo n.º 7
0
def ensure_vlan_bridge(session, vif, cluster=None, create_vlan=True):
    """Create a vlan and bridge unless they already exist."""
    neutron_vlan = vif['network'].get_meta('vmware_neutron_vlan')
    nova_vlan = vif['network'].get_meta('vlan')
    vlan_num = neutron_vlan if neutron_vlan else nova_vlan
    neutron_bridge = vif['network'].get_meta('vmware_neutron_bridge')
    nova_bridge = vif['network']['bridge']
    bridge = neutron_bridge if neutron_bridge else nova_bridge
    vlan_interface = CONF.vmware.vlan_interface

    network_ref = network_util.get_network_with_the_name(
        session, bridge, cluster)
    if network_ref and network_ref['type'] == 'DistributedVirtualPortgroup':
        return network_ref

    if not network_ref:
        # Create a port group on the vSwitch associated with the
        # vlan_interface corresponding physical network adapter on the ESX
        # host.
        vswitch_associated = _get_associated_vswitch_for_interface(
            session, vlan_interface, cluster)
        network_util.create_port_group(session, bridge, vswitch_associated,
                                       vlan_num if create_vlan else 0, cluster)
        network_ref = network_util.get_network_with_the_name(
            session, bridge, cluster)
    elif create_vlan:
        # Get the vSwitch associated with the Physical Adapter
        vswitch_associated = _get_associated_vswitch_for_interface(
            session, vlan_interface, cluster)
        # Get the vlan id and vswitch corresponding to the port group
        _get_pg_info = network_util.get_vlanid_and_vswitch_for_portgroup
        pg_vlanid, pg_vswitch = _get_pg_info(session, bridge, cluster)

        # Check if the vswitch associated is proper
        if pg_vswitch != vswitch_associated:
            raise exception.InvalidVLANPortGroup(bridge=bridge,
                                                 expected=vswitch_associated,
                                                 actual=pg_vswitch)

        # Check if the vlan id is proper for the port group
        if pg_vlanid != vlan_num:
            raise exception.InvalidVLANTag(bridge=bridge,
                                           tag=vlan_num,
                                           pgroup=pg_vlanid)
    return network_ref
Ejemplo n.º 8
0
def ensure_vlan_bridge(self, session, network, cluster=None):
    """Create a vlan and bridge unless they already exist."""
    vlan_num = network['vlan']
    bridge = network['bridge']
    vlan_interface = CONF.vmwareapi_vlan_interface

    # Check if the vlan_interface physical network adapter exists on the
    # host.
    if not network_util.check_if_vlan_interface_exists(session,
                                                       vlan_interface,
                                                       cluster):
        raise exception.NetworkAdapterNotFound(adapter=vlan_interface)

    # Get the vSwitch associated with the Physical Adapter
    vswitch_associated = network_util.get_vswitch_for_vlan_interface(
                                        session, vlan_interface, cluster)
    if vswitch_associated is None:
        raise exception.SwitchNotFoundForNetworkAdapter(
            adapter=vlan_interface)
    # Check whether bridge already exists and retrieve the the ref of the
    # network whose name_label is "bridge"
    network_ref = network_util.get_network_with_the_name(session, bridge,
                                                         cluster)
    if network_ref is None:
        # Create a port group on the vSwitch associated with the
        # vlan_interface corresponding physical network adapter on the ESX
        # host.
        network_util.create_port_group(session, bridge,
                                       vswitch_associated, vlan_num,
                                       cluster)
    else:
        # Get the vlan id and vswitch corresponding to the port group
        _get_pg_info = network_util.get_vlanid_and_vswitch_for_portgroup
        pg_vlanid, pg_vswitch = _get_pg_info(session, bridge, cluster)

        # Check if the vswitch associated is proper
        if pg_vswitch != vswitch_associated:
            raise exception.InvalidVLANPortGroup(
                bridge=bridge, expected=vswitch_associated,
                actual=pg_vswitch)

        # Check if the vlan id is proper for the port group
        if pg_vlanid != vlan_num:
            raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num,
                                           pgroup=pg_vlanid)
Ejemplo n.º 9
0
    def test_ensure_vlan_bridge_without_vlan(self):
        self.mox.StubOutWithMock(network_util, 'get_network_with_the_name')
        self.mox.StubOutWithMock(network_util,
                                 'get_vswitch_for_vlan_interface')
        self.mox.StubOutWithMock(network_util,
                                 'check_if_vlan_interface_exists')
        self.mox.StubOutWithMock(network_util, 'create_port_group')

        network_util.get_network_with_the_name(self.session, 'fa0',
                                               self.cluster).AndReturn(None)
        network_util.get_vswitch_for_vlan_interface(
            self.session, 'vmnet0', self.cluster).AndReturn('vmnet0')
        network_util.check_if_vlan_interface_exists(
            self.session, 'vmnet0', self.cluster).AndReturn(True)
        network_util.create_port_group(self.session, 'fa0', 'vmnet0', 0,
                                       self.cluster)
        self.mox.ReplayAll()
        vif.ensure_vlan_bridge(self.session, self.vif, create_vlan=False)
Ejemplo n.º 10
0
    def test_ensure_vlan_bridge_without_vlan(self):
        self.mox.StubOutWithMock(network_util, 'get_network_with_the_name')
        self.mox.StubOutWithMock(network_util,
            'get_vswitch_for_vlan_interface')
        self.mox.StubOutWithMock(network_util,
            'check_if_vlan_interface_exists')
        self.mox.StubOutWithMock(network_util, 'create_port_group')

        network_util.get_network_with_the_name(self.session, 'fa0',
            self.cluster).AndReturn(None)
        network_util.get_vswitch_for_vlan_interface(self.session, 'vmnet0',
            self.cluster).AndReturn('vmnet0')
        network_util.check_if_vlan_interface_exists(self.session, 'vmnet0',
        self.cluster).AndReturn(True)
        network_util.create_port_group(self.session, 'fa0', 'vmnet0', 0,
            self.cluster)
        self.mox.ReplayAll()
        vif.ensure_vlan_bridge(self.session, self.vif, create_vlan=False)
Ejemplo n.º 11
0
def ensure_vlan_bridge(self, session, network):
    """Create a vlan and bridge unless they already exist."""
    vlan_num = network['vlan']
    bridge = network['bridge']
    vlan_interface = CONF.vmwareapi_vlan_interface

    # Check if the vlan_interface physical network adapter exists on the
    # host.
    if not network_util.check_if_vlan_interface_exists(session,
                                                       vlan_interface):
        raise exception.NetworkAdapterNotFound(adapter=vlan_interface)

    # Get the vSwitch associated with the Physical Adapter
    vswitch_associated = network_util.get_vswitch_for_vlan_interface(
        session, vlan_interface)
    if vswitch_associated is None:
        raise exception.SwitchNotFoundForNetworkAdapter(adapter=vlan_interface)
    # Check whether bridge already exists and retrieve the the ref of the
    # network whose name_label is "bridge"
    network_ref = network_util.get_network_with_the_name(session, bridge)
    if network_ref is None:
        # Create a port group on the vSwitch associated with the
        # vlan_interface corresponding physical network adapter on the ESX
        # host.
        network_util.create_port_group(session, bridge, vswitch_associated,
                                       vlan_num)
    else:
        # Get the vlan id and vswitch corresponding to the port group
        _get_pg_info = network_util.get_vlanid_and_vswitch_for_portgroup
        pg_vlanid, pg_vswitch = _get_pg_info(session, bridge)

        # Check if the vswitch associated is proper
        if pg_vswitch != vswitch_associated:
            raise exception.InvalidVLANPortGroup(bridge=bridge,
                                                 expected=vswitch_associated,
                                                 actual=pg_vswitch)

        # Check if the vlan id is proper for the port group
        if pg_vlanid != vlan_num:
            raise exception.InvalidVLANTag(bridge=bridge,
                                           tag=vlan_num,
                                           pgroup=pg_vlanid)
Ejemplo n.º 12
0
    def spawn(self, context, instance, image_meta, injected_files,
              admin_password, network_info=None, block_device_info=None):
        session = self._session
        if network_info:
            for vif in network_info:
                vlan_id = self.Vlan.alloc_vlan()
                if vlan_id == INVALID_VLAN_ID:
                    raise exception.NovaException("Vlan id space is full")

                vif['network']['bridge'] = vif['network']['label'] + \
                                            '-' + str(instance['hostname']) + \
                                            '-' + str(vif['id'])
                args = {'should_create_vlan':True, 'vlan':vlan_id}
                vif['network']._set_meta(args)
                network_util.create_port_group(session,
                                                vif['network']['bridge'],
                                                CONF.vmware.vmpg_vswitch,
                                                vlan_id)
                self.VifInfo.plug(instance, vif, vlan_id)

        super(ContrailESXDriver, self).spawn(context, instance, image_meta,
                                             injected_files, admin_password,
                                             network_info, block_device_info)