~ivoks/charms/precise/nova-cloud-controller/ncc-neutronclient

« back to all changes in this revision

Viewing changes to files/create_tenant_net.py

  • Committer: Adam Gandelman
  • Date: 2012-12-07 00:21:54 UTC
  • mfrom: (46.3.13 nova-cloud-controller)
  • Revision ID: adamg@canonical.com-20121207002154-n5p31om88d767x4i
Merge Quantum networking support from James Page.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
from quantumclient.v2_0 import client
 
4
from keystoneclient.v2_0 import client as ks_client
 
5
import optparse
 
6
import os
 
7
import sys
 
8
import logging
 
9
 
 
10
usage = """Usage: %prog [options] name cidr
 
11
 
 
12
For example:
 
13
 
 
14
  %prog -t admin -r provider-router admin_net 10.5.5.0/24
 
15
 
 
16
will create a new network for the admin tenant called 'admin_net' with a
 
17
default gateway of 10.5.5.1 and a dhcp allocation range of
 
18
10.5.5.2->10.5.5.254
 
19
"""
 
20
 
 
21
if __name__ == '__main__':
 
22
    parser = optparse.OptionParser(usage)
 
23
    parser.add_option('-t', '--tenant',
 
24
                      help='Tenant name to create network for',
 
25
                      dest='tenant', action='store',
 
26
                      default=None)
 
27
    parser.add_option('-s', '--shared',
 
28
                      help='Create a shared rather than private network',
 
29
                      dest='shared', action='store_true', default=False)
 
30
    parser.add_option('-r', '--router',
 
31
                      help='Router to plug new network into',
 
32
                      dest='router', action='store', default=None)
 
33
    parser.add_option("-d", "--debug",
 
34
                      help="Enable debug logging",
 
35
                      dest="debug", action="store_true",  default=False)
 
36
    parser.add_option("-D", "--disable-dhcp",
 
37
                      help="Disable dhcp on network",
 
38
                      dest="dhcp", action="store_false",  default=True)
 
39
    parser.add_option("-N", "--dns-nameservers",
 
40
                      help="Comma separated list of dns servers to use.",
 
41
                      dest="dns_servers", action="store",  default=None)
 
42
    (opts, args) = parser.parse_args()
 
43
 
 
44
    if len(args) != 2:
 
45
        parser.print_help()
 
46
        sys.exit(1)
 
47
 
 
48
    if opts.debug:
 
49
        logging.basicConfig(level=logging.DEBUG)
 
50
    else:
 
51
        logging.basicConfig(level=logging.INFO)
 
52
 
 
53
    net_name = args[0]
 
54
    subnet_name = "{}_subnet".format(net_name)
 
55
    cidr = args[1]
 
56
 
 
57
    keystone = ks_client.Client(username=os.environ['OS_USERNAME'],
 
58
                                password=os.environ['OS_PASSWORD'],
 
59
                                tenant_name=os.environ['OS_TENANT_NAME'],
 
60
                                auth_url=os.environ['OS_AUTH_URL'])
 
61
    quantum = client.Client(username=os.environ['OS_USERNAME'],
 
62
                            password=os.environ['OS_PASSWORD'],
 
63
                            tenant_name=os.environ['OS_TENANT_NAME'],
 
64
                            auth_url=os.environ['OS_AUTH_URL'])
 
65
 
 
66
    # Resolve tenant id
 
67
    tenant_id = None
 
68
    for tenant in [t._info for t in keystone.tenants.list()]:
 
69
        if (tenant['name'] ==
 
70
            (opts.tenant or os.environ['OS_TENANT_NAME'])):
 
71
            tenant_id = tenant['id']
 
72
            break  # Tenant ID found - stop looking
 
73
    if not tenant_id:
 
74
        logging.error("Unable to locate tenant id for %s.", opts.tenant)
 
75
        sys.exit(1)
 
76
 
 
77
    # Create network
 
78
    networks = quantum.list_networks(name=net_name)
 
79
    if len(networks['networks']) == 0:
 
80
        logging.info('Creating network: %s',
 
81
                     net_name)
 
82
        network_msg = {
 
83
            'network': {
 
84
                'name': net_name,
 
85
                'shared': opts.shared,
 
86
                'tenant_id': tenant_id
 
87
            }
 
88
        }
 
89
        network = quantum.create_network(network_msg)['network']
 
90
    else:
 
91
        logging.warning('Network %s already exists.', net_name)
 
92
        network = networks['networks'][0]
 
93
 
 
94
    # Create subnet
 
95
    subnets = quantum.list_subnets(name=subnet_name)
 
96
    if len(subnets['subnets']) == 0:
 
97
        logging.info('Creating subnet for %s',
 
98
                     net_name)
 
99
        subnet_msg = {
 
100
            'subnet': {
 
101
                'name': subnet_name,
 
102
                'network_id': network['id'],
 
103
                'enable_dhcp': opts.dhcp,
 
104
                'cidr': cidr,
 
105
                'ip_version': 4,
 
106
                'tenant_id': tenant_id
 
107
            }
 
108
        }
 
109
        subnet = quantum.create_subnet(subnet_msg)['subnet']
 
110
    else:
 
111
        logging.warning('Subnet %s already exists.', subnet_name)
 
112
        subnet = subnets['subnets'][0]
 
113
 
 
114
    # Update dns_nameservers
 
115
    if opts.dns_servers:
 
116
        msg = {
 
117
            'subnet': {
 
118
                'dns_nameservers': opts.dns_servers.split(',')
 
119
            }
 
120
        }
 
121
        logging.info('Updating dns_nameservers (%s) for subnet %s',
 
122
                     opts.dns_servers,
 
123
                     subnet_name)
 
124
        quantum.update_subnet(subnet['id'], msg)
 
125
 
 
126
    # Plug subnet into router if provided
 
127
    if opts.router:
 
128
        routers = quantum.list_routers(name=opts.router)
 
129
        if len(routers['routers']) == 0:
 
130
            logging.error('Unable to locate provider router %s', opts.router)
 
131
            sys.exit(1)
 
132
        else:
 
133
            # Check to see if subnet already plugged into router
 
134
            ports = quantum.list_ports(device_owner='network:router_interface',
 
135
                                       network_id=network['id'])
 
136
            if len(ports['ports']) == 0:
 
137
                logging.info('Adding interface from %s to %s',
 
138
                             opts.router, subnet_name)
 
139
                router = routers['routers'][0]
 
140
                quantum.add_interface_router(router['id'],
 
141
                                             {'subnet_id': subnet['id']})
 
142
            else:
 
143
                logging.warning('Router already connected to subnet')