From dad5dbb764d04cb30a22e5e1bbc62d1f2f5c1e2d Mon Sep 17 00:00:00 2001 From: Dan Wendlandt Date: Sat, 25 Jun 2011 02:04:55 -0700 Subject: [PATCH] refactor batch_config, allow multiple attaches with the empty string --- quantum/db/api.py | 15 +- .../openvswitch/agent/ovs_quantum_agent.py | 2 - tools/batch_config.py | 155 ++++++++++-------- 3 files changed, 96 insertions(+), 76 deletions(-) diff --git a/quantum/db/api.py b/quantum/db/api.py index 989f0d6c38d..59459cadb42 100644 --- a/quantum/db/api.py +++ b/quantum/db/api.py @@ -156,13 +156,14 @@ def port_get(port_id): def port_set_attachment(port_id, new_interface_id): session = get_session() - ports = None - try: - ports = session.query(models.Port).\ - filter_by(interface_id=new_interface_id).\ - all() - except exc.NoResultFound: - pass + ports = [] + if new_interface_id != "": + try: + ports = session.query(models.Port).\ + filter_by(interface_id=new_interface_id).\ + all() + except exc.NoResultFound: + pass if len(ports) == 0: port = port_get(port_id) port.interface_id = new_interface_id diff --git a/quantum/plugins/openvswitch/agent/ovs_quantum_agent.py b/quantum/plugins/openvswitch/agent/ovs_quantum_agent.py index 60c864543b6..5d2a66ca37a 100755 --- a/quantum/plugins/openvswitch/agent/ovs_quantum_agent.py +++ b/quantum/plugins/openvswitch/agent/ovs_quantum_agent.py @@ -222,8 +222,6 @@ class OVSQuantumAgent: new_local_bindings[p.vif_id] = all_bindings[p.vif_id] else: # no binding, put him on the 'dead vlan' - LOG.info("No binding for %s, setting to dead vlan" \ - % p.vif_id) self.int_br.set_db_attribute("Port", p.port_name, "tag", "4095") self.int_br.add_flow(priority=2, diff --git a/tools/batch_config.py b/tools/batch_config.py index 21b35684d0a..9a7efdda50b 100644 --- a/tools/batch_config.py +++ b/tools/batch_config.py @@ -30,6 +30,89 @@ from quantum.cli import MiniClient FORMAT = "json" CONTENT_TYPE = "application/" + FORMAT +def delete_all_nets(client, tenant_id): + res = client.do_request(tenant_id, 'GET', "/networks." + FORMAT) + resdict = json.loads(res.read()) + LOG.debug(resdict) + for n in resdict["networks"]: + nid = n["id"] + + res = client.do_request(tenant_id, 'GET', + "/networks/%s/ports.%s" % (nid, FORMAT)) + output = res.read() + if res.status != 200: + 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 " \ + "on Virtual Network:%s" % (pid, nid) + + res = client.do_request(tenant_id, 'DELETE', + "/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): + data = {'network': {'network-name': '%s' % net_name}} + body = Serializer().serialize(data, CONTENT_TYPE) + res = client.do_request(tenant_id, 'POST', + "/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) + + for iface_id in iface_ids: + res = client.do_request(tenant_id, 'POST', + "/networks/%s/ports.%s" % (nid, FORMAT)) + 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 " \ + "on Virtual Network:%s" % (new_port_id, nid) + data = {'port': {'attachment-id': '%s' % iface_id}} + body = Serializer().serialize(data, CONTENT_TYPE) + 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" % \ + (iface_id, new_port_id, nid) if __name__ == "__main__": usagestr = "Usage: %prog [OPTIONS] [args]\n" \ @@ -49,6 +132,8 @@ if __name__ == "__main__": action="store_true", default=False, help="use ssl") parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="turn on verbose logging") + parser.add_option("-d", "--delete", dest="delete", + action="store_true", default=False, help="delete existing tenants networks") options, args = parser.parse_args() @@ -75,74 +160,10 @@ if __name__ == "__main__": client = MiniClient(options.host, options.port, options.ssl) - res = client.do_request(tenant_id, 'GET', "/networks." + FORMAT) - resdict = json.loads(res.read()) - LOG.debug(resdict) - for n in resdict["networks"]: - nid = n["id"] - - res = client.do_request(tenant_id, 'GET', - "/networks/%s/ports.%s" % (nid, FORMAT)) - output = res.read() - if res.status != 200: - LOG.error("Failed to list ports: %s" % output) - continue - rd = json.loads(output) - LOG.debug(rd) - for port in rd["ports"]: - pid = port["id"] - 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 - LOG.info("Deleted Virtual Port:%s " \ - "on Virtual Network:%s" % (pid, nid)) - - res = client.do_request(tenant_id, 'DELETE', - "/networks/" + nid + "." + FORMAT) - status = res.status - if status != 202: - print "Failed to delete network: %s" % nid - output = res.read() - print output - else: - print "Deleted Virtual Network with ID:%s" % nid + if options.delete: + delete_all_nets(client, tenant_id) for net_name, iface_ids in nets.items(): - data = {'network': {'network-name': '%s' % net_name}} - body = Serializer().serialize(data, CONTENT_TYPE) - res = client.do_request(tenant_id, 'POST', - "/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\n" % (net_name, nid) - - for iface_id in iface_ids: - res = client.do_request(tenant_id, 'POST', - "/networks/%s/ports.%s" % (nid, FORMAT)) - 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 " \ - "on Virtual Network:%s" % (new_port_id, nid) - data = {'port': {'attachment-id': '%s' % iface_id}} - body = Serializer().serialize(data, CONTENT_TYPE) - 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" % \ - (iface_id, new_port_id, nid) + create_net_with_attachments(net_name, iface_ids) sys.exit(0)