3
# Stolen from charm-openstack-testing!
5
from neutronclient.v2_0 import client
7
from keystoneclient.v2_0 import client as ks_client
8
from keystoneclient.auth import identity
9
from keystoneclient import session
15
usage = """Usage: %prog [options] name cidr
19
%prog -t admin -r provider-router admin_net 10.5.5.0/24
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
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',
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",
49
dest="network_type", action="store", default='gre')
50
(opts, args) = parser.parse_args()
57
logging.basicConfig(level=logging.DEBUG)
59
logging.basicConfig(level=logging.INFO)
62
subnet_name = "{}_subnet".format(net_name)
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'],
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)
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
97
logging.error("Unable to locate tenant id for %s.", opts.tenant)
101
networks = neutron.list_networks(name=net_name)
102
if len(networks['networks']) == 0:
103
logging.info('Creating tenant network: %s', net_name)
107
'shared': opts.shared,
108
'tenant_id': tenant_id,
109
'provider:network_type': opts.network_type,
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'
119
network_msg['network']['provider:segmentation_id'] = 5
121
network = neutron.create_network(network_msg)['network']
123
logging.warning('Network %s already exists.', net_name)
124
network = networks['networks'][0]
127
subnets = neutron.list_subnets(name=subnet_name)
128
if len(subnets['subnets']) == 0:
129
logging.info('Creating subnet for %s',
134
'network_id': network['id'],
135
'enable_dhcp': opts.dhcp,
138
'tenant_id': tenant_id
141
subnet = neutron.create_subnet(subnet_msg)['subnet']
143
logging.warning('Subnet %s already exists.', subnet_name)
144
subnet = subnets['subnets'][0]
146
# Update dns_nameservers
150
'dns_nameservers': opts.dns_servers.split(',')
153
logging.info('Updating dns_nameservers (%s) for subnet %s',
156
neutron.update_subnet(subnet['id'], msg)
158
# Plug subnet into router if provided
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)
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']})
175
logging.warning('Router already connected to subnet')