Skip to content

Commit 34e97e4

Browse files
author
Dan Wendlandt
committed
move batch_config.py to client library.
do not yet add it to console_scripts, as it is more of an internal development tool. If we want to expose it to users, we would probably incorporate it into the main quantum client, rather than having a separate utility. Change-Id: I3541dd430b931f883f8e18194776972a9cafcc70
1 parent 6890029 commit 34e97e4

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

quantum/client/batch_config.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2+
3+
# Copyright 2011 Nicira Networks, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
# @author: Dan Wendlandt, Nicira Networks, Inc.
17+
18+
import logging as LOG
19+
from optparse import OptionParser
20+
import os
21+
import sys
22+
23+
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
24+
os.pardir,
25+
os.pardir,
26+
os.pardir))
27+
28+
if os.path.exists(os.path.join(possible_topdir, 'quantum', '__init__.py')):
29+
sys.path.insert(0, possible_topdir)
30+
31+
from quantum.client import Client
32+
33+
FORMAT = "json"
34+
CONTENT_TYPE = "application/" + FORMAT
35+
36+
37+
def delete_all_nets(client):
38+
res = client.list_networks()
39+
for n in res["networks"]:
40+
nid = n["id"]
41+
pres = client.list_ports(nid)
42+
for port in pres["ports"]:
43+
pid = port['id']
44+
client.detach_resource(nid, pid)
45+
client.delete_port(nid, pid)
46+
print "Deleted Virtual Port:%s " \
47+
"on Virtual Network:%s" % (pid, nid)
48+
client.delete_network(nid)
49+
print "Deleted Virtual Network with ID:%s" % nid
50+
51+
52+
def create_net_with_attachments(client, net_name, iface_ids):
53+
data = {'network': {'name': '%s' % net_name}}
54+
res = client.create_network(data)
55+
nid = res["network"]["id"]
56+
print "Created a new Virtual Network %s with ID:%s" % (net_name, nid)
57+
58+
for iface_id in iface_ids:
59+
res = client.create_port(nid, {'port': {'state': 'ACTIVE'}})
60+
new_port_id = res["port"]["id"]
61+
print "Created Virtual Port:%s " \
62+
"on Virtual Network:%s" % (new_port_id, nid)
63+
data = {'attachment': {'id': iface_id}}
64+
client.attach_resource(nid, new_port_id, data)
65+
print "Plugged interface \"%s\" to port:%s on network:%s" % \
66+
(iface_id, new_port_id, nid)
67+
68+
if __name__ == "__main__":
69+
usagestr = "Usage: %prog [OPTIONS] <tenant-id> <config-string> [args]\n" \
70+
"Example config-string: net1=instance-1,instance-2"\
71+
":net2=instance-3,instance-4\n" \
72+
"This string would create two networks: \n" \
73+
"'net1' would have two ports, with iface-ids "\
74+
"instance-1 and instance-2 attached\n" \
75+
"'net2' would have two ports, with iface-ids"\
76+
" instance-3 and instance-4 attached\n"
77+
parser = OptionParser(usage=usagestr)
78+
parser.add_option("-H", "--host", dest="host",
79+
type="string", default="127.0.0.1", help="ip address of api host")
80+
parser.add_option("-p", "--port", dest="port",
81+
type="int", default=9696, help="api poort")
82+
parser.add_option("-s", "--ssl", dest="ssl",
83+
action="store_true", default=False, help="use ssl")
84+
parser.add_option("-v", "--verbose", dest="verbose",
85+
action="store_true", default=False, help="turn on verbose logging")
86+
parser.add_option("-d", "--delete", dest="delete",
87+
action="store_true", default=False, \
88+
help="delete existing tenants networks")
89+
90+
options, args = parser.parse_args()
91+
92+
if options.verbose:
93+
LOG.basicConfig(level=LOG.DEBUG)
94+
else:
95+
LOG.basicConfig(level=LOG.WARN)
96+
97+
if len(args) < 1:
98+
parser.print_help()
99+
sys.exit(1)
100+
101+
nets = {}
102+
tenant_id = args[0]
103+
if len(args) > 1:
104+
config_str = args[1]
105+
for net_str in config_str.split(":"):
106+
arr = net_str.split("=")
107+
net_name = arr[0]
108+
nets[net_name] = arr[1].split(",")
109+
110+
print "nets: %s" % str(nets)
111+
112+
client = Client(options.host, options.port, options.ssl,
113+
format='json', tenant=tenant_id)
114+
115+
if options.delete:
116+
delete_all_nets(client)
117+
118+
for net_name, iface_ids in nets.items():
119+
create_net_with_attachments(client, net_name, iface_ids)
120+
121+
sys.exit(0)

0 commit comments

Comments
 (0)