~ajkavanagh/+junk/useful-things

« back to all changes in this revision

Viewing changes to charm-manual-testing/manila/v2_keystone/neutron-tenant-net.py

  • Committer: Alex Kavanagh
  • Date: 2016-12-01 17:01:49 UTC
  • Revision ID: alex.kavanagh@canonical.com-20161201170149-ytewr1hw7z2xre8o
Added serverstack.yaml for juju & manila testing scripts

Note that the manila testing scripts are for serverstack ONLY.
There's a different github repo for manual testing of
manila.

Show diffs side-by-side

added added

removed removed

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