]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Add host routes and dns nameservers to Midonet DHCP
authorJoe Mills <joe@midokura.com>
Tue, 24 Sep 2013 10:42:08 +0000 (10:42 +0000)
committerJoe Mills <joe@midokura.com>
Fri, 27 Sep 2013 07:33:17 +0000 (07:33 +0000)
In the Midonet plugin, the host routes and dns nameserver information
was not being passed down to the midonet client API. This fix addresses
this by passing down the correct information.

Change-Id: I142ad4ceccdcf8b0e13db55fa54513f82995efc5
Closes-Bug: #1229655

neutron/plugins/midonet/midonet_lib.py
neutron/plugins/midonet/plugin.py
neutron/tests/unit/midonet/test_midonet_lib.py

index ef6dec7a3ae12201d205de8e4bc42d83fba27129..6b96e3812fbec82caf5c38d476d6abf7842ece3a 100644 (file)
@@ -21,7 +21,7 @@
 # @author: Rossella Sblendido, Midokura Japan KK
 
 
-from midonetclient import midoapi_exceptions
+from midonetclient import exc
 from webob import exc as w_exc
 
 from neutron.common import exceptions as n_exc
@@ -37,7 +37,7 @@ def handle_api_error(fn):
         try:
             return fn(*args, **kwargs)
         except (w_exc.HTTPException,
-                midoapi_exceptions.MidoApiConnectionError) as ex:
+                exc.MidoApiConnectionError) as ex:
             raise MidonetApiException(msg=ex)
     return wrapped
 
@@ -107,21 +107,25 @@ class MidoClient:
             raise MidonetResourceNotFound(resource_type='Bridge', id=id)
 
     @handle_api_error
-    def create_dhcp(self, bridge, gateway_ip, cidr):
+    def create_dhcp(self, bridge, gateway_ip, cidr, host_rts=None,
+                    dns_servers=None):
         """Create a new DHCP entry
 
         :param bridge: bridge object to add dhcp to
         :param gateway_ip: IP address of gateway
         :param cidr: subnet represented as x.x.x.x/y
+        :param host_rts: list of routes set in the host
+        :param dns_servers: list of dns servers
         :returns: newly created dhcp
         """
         LOG.debug(_("MidoClient.create_dhcp called: bridge=%(bridge)s, "
-                    "cidr=%(cidr)s, gateway_ip=%(gateway_ip)s"),
-                  {'bridge': bridge, 'cidr': cidr, 'gateway_ip': gateway_ip})
-        net_addr, net_len = net_util.net_addr(cidr)
-        return bridge.add_dhcp_subnet().default_gateway(
-            gateway_ip).subnet_prefix(net_addr).subnet_length(
-                net_len).create()
+                    "cidr=%(cidr)s, gateway_ip=%(gateway_ip)s, "
+                    "host_rts=%(host_rts)s, dns_servers=%(dns_servers)s"),
+                  {'bridge': bridge, 'cidr': cidr, 'gateway_ip': gateway_ip,
+                   'host_rts': host_rts, 'dns_servers': dns_servers})
+        self.mido_api.add_bridge_dhcp(bridge, gateway_ip, cidr,
+                                      host_rts=host_rts,
+                                      dns_nservers=dns_servers)
 
     @handle_api_error
     def add_dhcp_host(self, bridge, cidr, ip, mac):
@@ -319,6 +323,9 @@ class MidoClient:
         prefix, length = dst_ip.split("/")
         routes = [{'destinationPrefix': prefix, 'destinationLength': length,
                    'gatewayAddr': gw_ip}]
+        cur_routes = subnet.get_opt121_routes()
+        if cur_routes:
+            routes = routes + cur_routes
         subnet.opt121_routes(routes).update()
 
     @handle_api_error
index 7fc3a16e515661fb19ae6973e6ae254a7c3fe7d9..cd12ec5ce7faf9c601b25f5a7542dcc8892c77d9 100644 (file)
@@ -24,6 +24,7 @@
 from midonetclient import api
 from oslo.config import cfg
 
+from neutron.api.v2 import attributes
 from neutron.common import constants
 from neutron.common import exceptions as n_exc
 from neutron.common import rpc as n_rpc
@@ -367,7 +368,7 @@ class MidonetPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
         """
         LOG.debug(_("MidonetPluginV2.create_subnet called: subnet=%r"), subnet)
 
-        subnet_data = subnet["subnet"]
+        s = subnet["subnet"]
         net = super(MidonetPluginV2, self).get_network(
             context, subnet['subnet']['network_id'], fields=None)
 
@@ -377,9 +378,19 @@ class MidonetPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
                                                                   subnet)
             bridge = self.client.get_bridge(sn_entry['network_id'])
 
-            gateway_ip = subnet_data['gateway_ip']
-            cidr = subnet_data['cidr']
-            self.client.create_dhcp(bridge, gateway_ip, cidr)
+            gateway_ip = s['gateway_ip']
+            cidr = s['cidr']
+            dns_nameservers = None
+            host_routes = None
+            if s['dns_nameservers'] is not attributes.ATTR_NOT_SPECIFIED:
+                dns_nameservers = s['dns_nameservers']
+
+            if s['host_routes'] is not attributes.ATTR_NOT_SPECIFIED:
+                host_routes = s['host_routes']
+
+            self.client.create_dhcp(bridge, gateway_ip, cidr,
+                                    host_rts=host_routes,
+                                    dns_servers=dns_nameservers)
 
             # For external network, link the bridge to the provider router.
             if net['router:external']:
@@ -503,7 +514,7 @@ class MidonetPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
                     # Create port chains
                     port_chains = {}
                     for d, name in _port_chain_names(
-                        new_port["id"]).iteritems():
+                            new_port["id"]).iteritems():
                         port_chains[d] = self.client.create_chain(tenant_id,
                                                                   name)
 
@@ -526,7 +537,7 @@ class MidonetPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
                 elif _is_dhcp_port(port_data):
                     # For DHCP port, add a metadata route
                     for cidr, ip in self._metadata_subnets(
-                        context, port_data["fixed_ips"]):
+                            context, port_data["fixed_ips"]):
                         self.client.add_dhcp_route_option(bridge, cidr, ip,
                                                           METADATA_DEFAULT_IP)
 
@@ -774,7 +785,7 @@ class MidonetPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
             tenant_id = r["tenant_id"]
             if gw_updated:
                 if (l3_db.EXTERNAL_GW_INFO in r and
-                    r[l3_db.EXTERNAL_GW_INFO] is not None):
+                        r[l3_db.EXTERNAL_GW_INFO] is not None):
                     # Gateway created
                     gw_port = self._get_port(context, r["gw_port_id"])
                     gw_ip = gw_port['fixed_ips'][0]['ip_address']
index 1774657f721955c645aeeba37baba2614968adf7..0fe95f99f185641a9bfa691c1239559964fcd767 100644 (file)
@@ -79,15 +79,21 @@ class MidoClientTestCase(testtools.TestCase):
     def test_create_dhcp(self):
 
         bridge = mock.Mock()
-        gw_call = mock.call.add_dhcp_subnet().default_gateway("192.168.1.1")
-        subnet_prefix_call = gw_call.subnet_prefix("192.168.1.0")
-        subnet_length_call = subnet_prefix_call.subnet_length(24)
 
-        calls = [gw_call, subnet_prefix_call, subnet_length_call,
-                 subnet_length_call.create()]
+        gateway_ip = "192.168.1.1"
+        cidr = "192.168.1.0/24"
+        host_rts = [{'destination': '10.0.0.0/24', 'nexthop': '10.0.0.1'},
+                    {'destination': '10.0.1.0/24', 'nexthop': '10.0.1.1'}]
+        dns_servers = ["8.8.8.8", "8.8.4.4"]
 
-        self.client.create_dhcp(bridge, "192.168.1.1", "192.168.1.0/24")
-        bridge.assert_has_calls(calls, any_order=True)
+        dhcp_call = mock.call.add_bridge_dhcp(bridge, gateway_ip, cidr,
+                                              host_rts=host_rts,
+                                              dns_servers=dns_servers)
+
+        self.client.create_dhcp(bridge, gateway_ip, cidr, host_rts=host_rts,
+                                dns_servers=dns_servers)
+
+        bridge.assert_has_call(dhcp_call)
 
     def test_add_dhcp_host(self):