~ttx/nova/d4-merge

« back to all changes in this revision

Viewing changes to nova/virt/libvirt/netutils.py

  • Committer: Thierry Carrez
  • Date: 2011-08-23 12:23:07 UTC
  • mfrom: (1130.75.258 nova)
  • Revision ID: thierry@openstack.org-20110823122307-f0vtuyg1ikc14n87
Merge diablo-4 development from trunk (rev1479)

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import netaddr
25
25
 
26
 
from nova import context
27
 
from nova import db
28
 
from nova import exception
29
26
from nova import flags
30
 
from nova import ipv6
31
 
from nova import utils
32
27
 
33
28
 
34
29
FLAGS = flags.FLAGS
47
42
def get_ip_version(cidr):
48
43
    net = netaddr.IPNetwork(cidr)
49
44
    return int(net.version)
50
 
 
51
 
 
52
 
def get_network_info(instance):
53
 
    # TODO(tr3buchet): this function needs to go away! network info
54
 
    #                  MUST be passed down from compute
55
 
    # TODO(adiantum) If we will keep this function
56
 
    # we should cache network_info
57
 
    admin_context = context.get_admin_context()
58
 
 
59
 
    try:
60
 
        fixed_ips = db.fixed_ip_get_by_instance(admin_context, instance['id'])
61
 
    except exception.FixedIpNotFoundForInstance:
62
 
        fixed_ips = []
63
 
 
64
 
    vifs = db.virtual_interface_get_by_instance(admin_context, instance['id'])
65
 
    flavor = db.instance_type_get(admin_context,
66
 
                                        instance['instance_type_id'])
67
 
    network_info = []
68
 
 
69
 
    for vif in vifs:
70
 
        network = vif['network']
71
 
 
72
 
        # determine which of the instance's IPs belong to this network
73
 
        network_ips = [fixed_ip['address'] for fixed_ip in fixed_ips if
74
 
                       fixed_ip['network_id'] == network['id']]
75
 
 
76
 
        def ip_dict(ip):
77
 
            return {
78
 
                'ip': ip,
79
 
                'netmask': network['netmask'],
80
 
                'enabled': '1'}
81
 
 
82
 
        def ip6_dict():
83
 
            prefix = network['cidr_v6']
84
 
            mac = vif['address']
85
 
            project_id = instance['project_id']
86
 
            return  {
87
 
                'ip': ipv6.to_global(prefix, mac, project_id),
88
 
                'netmask': network['netmask_v6'],
89
 
                'enabled': '1'}
90
 
 
91
 
        mapping = {
92
 
            'label': network['label'],
93
 
            'gateway': network['gateway'],
94
 
            'broadcast': network['broadcast'],
95
 
            'dhcp_server': network['gateway'],
96
 
            'mac': vif['address'],
97
 
            'rxtx_cap': flavor['rxtx_cap'],
98
 
            'dns': [],
99
 
            'ips': [ip_dict(ip) for ip in network_ips]}
100
 
 
101
 
        if network['dns1']:
102
 
            mapping['dns'].append(network['dns1'])
103
 
        if network['dns2']:
104
 
            mapping['dns'].append(network['dns2'])
105
 
 
106
 
        if FLAGS.use_ipv6:
107
 
            mapping['ip6s'] = [ip6_dict()]
108
 
            mapping['gateway6'] = network['gateway_v6']
109
 
 
110
 
        network_info.append((network, mapping))
111
 
    return network_info