~0x44/nova/bug838466

« back to all changes in this revision

Viewing changes to nova/network/vmwareapi_net.py

  • Committer: Brian Waldon
  • Date: 2011-07-26 17:13:41 UTC
  • mfrom: (1324 nova)
  • mto: This revision was merged to the branch mainline in revision 1326.
  • Revision ID: brian.waldon@rackspace.com-20110726171341-jrclqn08ew7ngshr
merging trunk; resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright (c) 2011 Citrix Systems, Inc.
4
 
# Copyright 2011 OpenStack LLC.
5
 
#
6
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
7
 
#    not use this file except in compliance with the License. You may obtain
8
 
#    a copy of the License at
9
 
#
10
 
#         http://www.apache.org/licenses/LICENSE-2.0
11
 
#
12
 
#    Unless required by applicable law or agreed to in writing, software
13
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 
#    License for the specific language governing permissions and limitations
16
 
#    under the License.
17
 
 
18
 
"""Implements vlans for vmwareapi."""
19
 
 
20
 
from nova import db
21
 
from nova import exception
22
 
from nova import flags
23
 
from nova import log as logging
24
 
from nova import utils
25
 
from nova.virt.vmwareapi_conn import VMWareAPISession
26
 
from nova.virt.vmwareapi import network_utils
27
 
 
28
 
 
29
 
LOG = logging.getLogger("nova.network.vmwareapi_net")
30
 
 
31
 
 
32
 
FLAGS = flags.FLAGS
33
 
FLAGS['vlan_interface'].SetDefault('vmnic0')
34
 
 
35
 
 
36
 
def ensure_vlan_bridge(vlan_num, bridge, bridge_interface, net_attrs=None):
37
 
    """Create a vlan and bridge unless they already exist."""
38
 
    # Open vmwareapi session
39
 
    host_ip = FLAGS.vmwareapi_host_ip
40
 
    host_username = FLAGS.vmwareapi_host_username
41
 
    host_password = FLAGS.vmwareapi_host_password
42
 
    if not host_ip or host_username is None or host_password is None:
43
 
        raise Exception(_('Must specify vmwareapi_host_ip, '
44
 
                          'vmwareapi_host_username '
45
 
                          'and vmwareapi_host_password to use '
46
 
                          'connection_type=vmwareapi'))
47
 
    session = VMWareAPISession(host_ip, host_username, host_password,
48
 
                               FLAGS.vmwareapi_api_retry_count)
49
 
    vlan_interface = bridge_interface
50
 
    # Check if the vlan_interface physical network adapter exists on the host
51
 
    if not network_utils.check_if_vlan_interface_exists(session,
52
 
                                                        vlan_interface):
53
 
        raise exception.NetworkAdapterNotFound(adapter=vlan_interface)
54
 
 
55
 
    # Get the vSwitch associated with the Physical Adapter
56
 
    vswitch_associated = network_utils.get_vswitch_for_vlan_interface(
57
 
                                        session, vlan_interface)
58
 
    if vswitch_associated is None:
59
 
        raise exception.SwicthNotFoundForNetworkAdapter(adapter=vlan_interface)
60
 
    # Check whether bridge already exists and retrieve the the ref of the
61
 
    # network whose name_label is "bridge"
62
 
    network_ref = network_utils.get_network_with_the_name(session, bridge)
63
 
    if network_ref is None:
64
 
        # Create a port group on the vSwitch associated with the vlan_interface
65
 
        # corresponding physical network adapter on the ESX host
66
 
        network_utils.create_port_group(session, bridge, vswitch_associated,
67
 
                                vlan_num)
68
 
    else:
69
 
        # Get the vlan id and vswitch corresponding to the port group
70
 
        pg_vlanid, pg_vswitch = \
71
 
            network_utils.get_vlanid_and_vswitch_for_portgroup(session, bridge)
72
 
 
73
 
        # Check if the vswitch associated is proper
74
 
        if pg_vswitch != vswitch_associated:
75
 
            raise exception.InvalidVLANPortGroup(bridge=bridge,
76
 
                                                 expected=vswitch_associated,
77
 
                                                 actual=pg_vswitch)
78
 
 
79
 
        # Check if the vlan id is proper for the port group
80
 
        if pg_vlanid != vlan_num:
81
 
            raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num,
82
 
                                           pgroup=pg_vlanid)