Merging fixes and changes batch-config script. Thanks lp:danwent !

This commit is contained in:
Somik Behera 2011-08-19 10:53:00 -07:00
commit 0e1cdf4ca2

View File

@ -15,106 +15,45 @@
# under the License. # under the License.
# @author: Dan Wendlandt, Nicira Networks, Inc. # @author: Dan Wendlandt, Nicira Networks, Inc.
import httplib
import logging as LOG import logging as LOG
import json
import socket
import sys
import urllib
from quantum.manager import QuantumManager
from optparse import OptionParser from optparse import OptionParser
from quantum.common.wsgi import Serializer import sys
from quantum.cli import MiniClient
from quantum.client import Client
from quantum.manager import QuantumManager
FORMAT = "json" FORMAT = "json"
CONTENT_TYPE = "application/" + FORMAT CONTENT_TYPE = "application/" + FORMAT
def delete_all_nets(client, tenant_id): def delete_all_nets(client):
res = client.do_request(tenant_id, 'GET', "/networks." + FORMAT) res = client.list_networks()
resdict = json.loads(res.read()) for n in res["networks"]:
LOG.debug(resdict)
for n in resdict["networks"]:
nid = n["id"] nid = n["id"]
pres = client.list_ports(nid)
res = client.do_request(tenant_id, 'GET', for port in pres["ports"]:
"/networks/%s/ports.%s" % (nid, FORMAT)) pid = port['id']
output = res.read() client.detach_resource(nid, pid)
if res.status != 200: client.delete_port(nid, pid)
LOG.error("Failed to list ports: %s" % output)
continue
rd = json.loads(output)
LOG.debug(rd)
for port in rd["ports"]:
pid = port["id"]
data = {'port': {'attachment-id': ''}}
body = Serializer().serialize(data, CONTENT_TYPE)
res = client.do_request(tenant_id, 'DELETE',
"/networks/%s/ports/%s/attachment.%s" % \
(nid, pid, FORMAT), body=body)
output = res.read()
LOG.debug(output)
if res.status != 202:
LOG.error("Failed to unplug iface from port \"%s\": %s" % (vid,
pid, output))
continue
LOG.info("Unplugged interface from port:%s on network:%s" % (pid,
nid))
res = client.do_request(tenant_id, 'DELETE',
"/networks/%s/ports/%s.%s" % (nid, pid, FORMAT))
output = res.read()
if res.status != 202:
LOG.error("Failed to delete port: %s" % output)
continue
print "Deleted Virtual Port:%s " \ print "Deleted Virtual Port:%s " \
"on Virtual Network:%s" % (pid, nid) "on Virtual Network:%s" % (pid, nid)
client.delete_network(nid)
res = client.do_request(tenant_id, 'DELETE', print "Deleted Virtual Network with ID:%s" % nid
"/networks/" + nid + "." + FORMAT)
status = res.status
if status != 202:
Log.error("Failed to delete network: %s" % nid)
output = res.read()
print output
else:
print "Deleted Virtual Network with ID:%s" % nid
def create_net_with_attachments(net_name, iface_ids): def create_net_with_attachments(client, net_name, iface_ids):
data = {'network': {'net-name': '%s' % net_name}} data = {'network': {'net-name': '%s' % net_name}}
body = Serializer().serialize(data, CONTENT_TYPE) res = client.create_network(data)
res = client.do_request(tenant_id, 'POST', nid = res["networks"]["network"]["id"]
"/networks." + FORMAT, body=body)
rd = json.loads(res.read())
LOG.debug(rd)
nid = rd["networks"]["network"]["id"]
print "Created a new Virtual Network %s with ID:%s" % (net_name, nid) print "Created a new Virtual Network %s with ID:%s" % (net_name, nid)
for iface_id in iface_ids: for iface_id in iface_ids:
res = client.do_request(tenant_id, 'POST', res = client.create_port(nid)
"/networks/%s/ports.%s" % (nid, FORMAT)) new_port_id = res["ports"]["port"]["id"]
output = res.read()
if res.status != 200:
LOG.error("Failed to create port: %s" % output)
continue
rd = json.loads(output)
new_port_id = rd["ports"]["port"]["id"]
print "Created Virtual Port:%s " \ print "Created Virtual Port:%s " \
"on Virtual Network:%s" % (new_port_id, nid) "on Virtual Network:%s" % (new_port_id, nid)
data = {'port': {'attachment-id': '%s' % iface_id}} data = {'port': {'attachment-id': '%s' % iface_id}}
body = Serializer().serialize(data, CONTENT_TYPE) client.attach_resource(nid, new_port_id, data)
res = client.do_request(tenant_id, 'PUT',
"/networks/%s/ports/%s/attachment.%s" %\
(nid, new_port_id, FORMAT), body=body)
output = res.read()
LOG.debug(output)
if res.status != 202:
LOG.error("Failed to plug iface \"%s\" to port \"%s\": %s" % \
(iface_id, new_port_id, output))
continue
print "Plugged interface \"%s\" to port:%s on network:%s" % \ print "Plugged interface \"%s\" to port:%s on network:%s" % \
(iface_id, new_port_id, nid) (iface_id, new_port_id, nid)
@ -149,7 +88,6 @@ if __name__ == "__main__":
if len(args) < 1: if len(args) < 1:
parser.print_help() parser.print_help()
help()
sys.exit(1) sys.exit(1)
nets = {} nets = {}
@ -163,12 +101,13 @@ if __name__ == "__main__":
print "nets: %s" % str(nets) print "nets: %s" % str(nets)
client = MiniClient(options.host, options.port, options.ssl) client = Client(options.host, options.port, options.ssl,
format='json', tenant=tenant_id)
if options.delete: if options.delete:
delete_all_nets(client, tenant_id) delete_all_nets(client)
for net_name, iface_ids in nets.items(): for net_name, iface_ids in nets.items():
create_net_with_attachments(net_name, iface_ids) create_net_with_attachments(client, net_name, iface_ids)
sys.exit(0) sys.exit(0)