Merge "Add host routes and dns nameservers to Midonet DHCP"

This commit is contained in:
Jenkins 2013-09-28 05:38:01 +00:00 committed by Gerrit Code Review
commit 1cd7e6d1f0
3 changed files with 47 additions and 23 deletions

View 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

View 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']

View 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):