]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
refactor batch_config, allow multiple attaches with the empty string
authorDan Wendlandt <dan@nicira.com>
Sat, 25 Jun 2011 09:04:55 +0000 (02:04 -0700)
committerDan Wendlandt <dan@nicira.com>
Sat, 25 Jun 2011 09:04:55 +0000 (02:04 -0700)
quantum/db/api.py
quantum/plugins/openvswitch/agent/ovs_quantum_agent.py
tools/batch_config.py

index 989f0d6c38d070b64982dc6e550bf9fb4020c2d8..59459cadb4216af5c6b145732e5ac710937aa454 100644 (file)
@@ -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
index 60c864543b69d07f5b62d9a0b7827fa009b3fc96..5d2a66ca37a12924f05a4899dfb54afadc80a0d1 100755 (executable)
@@ -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,
index 21b35684d0a0f90e7b82f9a23b0dfea2eb4d149c..9a7efdda50b305bbe5f552c621b1b2294eb9e73b 100644 (file)
@@ -30,51 +30,7 @@ from quantum.cli import MiniClient
 FORMAT = "json"
 CONTENT_TYPE = "application/" + FORMAT
 
-
-if __name__ == "__main__":
-    usagestr = "Usage: %prog [OPTIONS] <tenant-id> <config-string> [args]\n" \
-                "Example config-string: net1=instance-1,instance-2"\
-                ":net2=instance-3,instance-4\n" \
-                "This string would create two networks: \n" \
-                "'net1' would have two ports, with iface-ids "\
-                "instance-1 and instance-2 attached\n" \
-                "'net2' would have two ports, with iface-ids"\
-                " instance-3 and instance-4 attached\n"
-    parser = OptionParser(usage=usagestr)
-    parser.add_option("-H", "--host", dest="host",
-      type="string", default="127.0.0.1", help="ip address of api host")
-    parser.add_option("-p", "--port", dest="port",
-      type="int", default=9696, help="api poort")
-    parser.add_option("-s", "--ssl", dest="ssl",
-      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")
-
-    options, args = parser.parse_args()
-
-    if options.verbose:
-        LOG.basicConfig(level=LOG.DEBUG)
-    else:
-        LOG.basicConfig(level=LOG.WARN)
-
-    if len(args) < 1:
-        parser.print_help()
-        help()
-        sys.exit(1)
-
-    nets = {}
-    tenant_id = args[0]
-    if len(args) > 1:
-        config_str = args[1]
-        for net_str in config_str.split(":"):
-            arr = net_str.split("=")
-            net_name = arr[0]
-            nets[net_name] = arr[1].split(",")
-
-    print "nets: %s" % str(nets)
-
-    client = MiniClient(options.host, options.port, options.ssl)
-
+def delete_all_nets(client, tenant_id):
     res = client.do_request(tenant_id, 'GET', "/networks." + FORMAT)
     resdict = json.loads(res.read())
     LOG.debug(resdict)
@@ -86,31 +42,44 @@ if __name__ == "__main__":
         output = res.read()
         if res.status != 200:
             LOG.error("Failed to list ports: %s" % output)
-        continue
+            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
-            LOG.info("Deleted Virtual Port:%s " \
-                "on Virtual Network:%s" % (pid, nid))
+            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:
-            print "Failed to delete network: %s" % nid
+            Log.error("Failed to delete network: %s" % nid)
             output = res.read()
             print output
         else:
             print "Deleted Virtual Network with ID:%s" % nid
 
-    for net_name, iface_ids in nets.items():
+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',
@@ -118,7 +87,7 @@ if __name__ == "__main__":
         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)
+        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',
@@ -126,7 +95,7 @@ if __name__ == "__main__":
             output = res.read()
             if res.status != 200:
                 LOG.error("Failed to create port: %s" % output)
-            continue
+                continue
             rd = json.loads(output)
             new_port_id = rd["ports"]["port"]["id"]
             print "Created Virtual Port:%s " \
@@ -142,7 +111,59 @@ if __name__ == "__main__":
                 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)
 
+if __name__ == "__main__":
+    usagestr = "Usage: %prog [OPTIONS] <tenant-id> <config-string> [args]\n" \
+                "Example config-string: net1=instance-1,instance-2"\
+                ":net2=instance-3,instance-4\n" \
+                "This string would create two networks: \n" \
+                "'net1' would have two ports, with iface-ids "\
+                "instance-1 and instance-2 attached\n" \
+                "'net2' would have two ports, with iface-ids"\
+                " instance-3 and instance-4 attached\n"
+    parser = OptionParser(usage=usagestr)
+    parser.add_option("-H", "--host", dest="host",
+      type="string", default="127.0.0.1", help="ip address of api host")
+    parser.add_option("-p", "--port", dest="port",
+      type="int", default=9696, help="api poort")
+    parser.add_option("-s", "--ssl", dest="ssl",
+      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()
+
+    if options.verbose:
+        LOG.basicConfig(level=LOG.DEBUG)
+    else:
+        LOG.basicConfig(level=LOG.WARN)
+
+    if len(args) < 1:
+        parser.print_help()
+        help()
+        sys.exit(1)
+
+    nets = {}
+    tenant_id = args[0]
+    if len(args) > 1:
+        config_str = args[1]
+        for net_str in config_str.split(":"):
+            arr = net_str.split("=")
+            net_name = arr[0]
+            nets[net_name] = arr[1].split(",")
+
+    print "nets: %s" % str(nets)
+
+    client = MiniClient(options.host, options.port, options.ssl)
+
+    if options.delete:
+        delete_all_nets(client, tenant_id)
+
+    for net_name, iface_ids in nets.items():
+        create_net_with_attachments(net_name, iface_ids)
+
     sys.exit(0)