From ea172068114cccb820f1c633b41380a83caaf778 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Mon, 13 Aug 2012 08:13:20 +0000 Subject: [PATCH] Add device_owner attribute to port in order for firewall logic to be able to poke a hole allowing traffic to/from a DHCP server, we need to expose the IP being used for DHCP in a subnet to external entities. This commit adds device_owner attribute, then dhcp-agent will update the device_owner value of port to dhcp-agent. Implement blueprint expose-dhcp-server-ip Change-Id: I11283485bff8a3a3cf0b1a2716763ad32e43028a --- quantum/agent/dhcp_agent.py | 8 ++++++-- quantum/api/v2/attributes.py | 3 +++ quantum/db/db_base_plugin_v2.py | 6 ++++-- quantum/db/models_v2.py | 1 + quantum/tests/unit/test_api_v2.py | 4 +++- quantum/tests/unit/test_db_plugin.py | 6 ++++-- 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/quantum/agent/dhcp_agent.py b/quantum/agent/dhcp_agent.py index 0643acbf57..5c4409cbc4 100644 --- a/quantum/agent/dhcp_agent.py +++ b/quantum/agent/dhcp_agent.py @@ -182,7 +182,9 @@ class DhcpAgent(object): driver = self.dhcp_driver_cls(self.conf, network, self.conf.root_helper, - DeviceManager(self.conf, self.db)) + DeviceManager(self.conf, + self.db, + 'network:dhcp')) getattr(driver, action)() except Exception, e: @@ -210,9 +212,10 @@ class DeviceManager(object): help="The driver used to manage the virtual interface.") ] - def __init__(self, conf, db): + def __init__(self, conf, db, device_owner=''): self.conf = conf self.db = db + self.device_owner = device_owner if not conf.interface_driver: LOG.error(_('You must specify an interface driver')) self.driver = importutils.import_object(conf.interface_driver, conf) @@ -294,6 +297,7 @@ class DeviceManager(object): body = dict(port=dict( admin_state_up=True, device_id=self.get_device_id(network), + device_owner=self.device_owner, network_id=network.id, tenant_id=network.tenant_id, fixed_ips=[dict(subnet_id=s.id) for s in network.subnets])) diff --git a/quantum/api/v2/attributes.py b/quantum/api/v2/attributes.py index c55caab12b..51d666967d 100644 --- a/quantum/api/v2/attributes.py +++ b/quantum/api/v2/attributes.py @@ -208,6 +208,9 @@ RESOURCE_ATTRIBUTE_MAP = { 'device_id': {'allow_post': True, 'allow_put': True, 'default': '', 'is_visible': True}, + 'device_owner': {'allow_post': True, 'allow_put': True, + 'default': '', + 'is_visible': True}, 'tenant_id': {'allow_post': True, 'allow_put': False, 'required_by_policy': True, 'is_visible': True}, diff --git a/quantum/db/db_base_plugin_v2.py b/quantum/db/db_base_plugin_v2.py index 2f27bf2f59..3523c48ac1 100644 --- a/quantum/db/db_base_plugin_v2.py +++ b/quantum/db/db_base_plugin_v2.py @@ -679,7 +679,8 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2): "fixed_ips": [{'subnet_id': ip["subnet_id"], 'ip_address': ip["ip_address"]} for ip in port["fixed_ips"]], - "device_id": port["device_id"]} + "device_id": port["device_id"], + "device_owner": port["device_owner"]} return self._fields(res, fields) def _create_bulk(self, resource, context, request_items): @@ -849,7 +850,8 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2): mac_address=p['mac_address'], admin_state_up=p['admin_state_up'], status="ACTIVE", - device_id=p['device_id']) + device_id=p['device_id'], + device_owner=p['device_owner']) context.session.add(port) # Update the allocated IP's diff --git a/quantum/db/models_v2.py b/quantum/db/models_v2.py index b727e48813..32605af27e 100644 --- a/quantum/db/models_v2.py +++ b/quantum/db/models_v2.py @@ -96,6 +96,7 @@ class Port(model_base.BASEV2, HasId, HasTenant): admin_state_up = sa.Column(sa.Boolean(), nullable=False) status = sa.Column(sa.String(16), nullable=False) device_id = sa.Column(sa.String(255), nullable=False) + device_owner = sa.Column(sa.String(255), nullable=False) class Subnet(model_base.BASEV2, HasId, HasTenant): diff --git a/quantum/tests/unit/test_api_v2.py b/quantum/tests/unit/test_api_v2.py index 0c7360b8c7..055e7eec2f 100644 --- a/quantum/tests/unit/test_api_v2.py +++ b/quantum/tests/unit/test_api_v2.py @@ -600,13 +600,15 @@ class JSONV2TestCase(APIv2TestBase): full_input = {'port': {'admin_state_up': True, 'mac_address': attributes.ATTR_NOT_SPECIFIED, 'fixed_ips': attributes.ATTR_NOT_SPECIFIED, + 'device_owner': '', 'host_routes': attributes.ATTR_NOT_SPECIFIED}} full_input['port'].update(initial_input['port']) return_value = {'id': _uuid(), 'status': 'ACTIVE', 'admin_state_up': True, 'mac_address': 'ca:fe:de:ad:be:ef', 'host_routes': [], - 'device_id': device_id} + 'device_id': device_id, + 'device_owner': ''} return_value.update(initial_input['port']) instance = self.plugin.return_value diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index 24c1895275..897c3bd7f8 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -215,9 +215,10 @@ class QuantumDbPluginV2TestCase(unittest2.TestCase): content_type = 'application/' + fmt data = {'port': {'network_id': net_id, 'tenant_id': self._tenant_id}} + for arg in ('admin_state_up', 'device_id', - 'mac_address', 'fixed_ips', - 'name', 'tenant_id'): + 'mac_address', 'name', 'fixed_ips', + 'tenant_id', 'device_owner'): # Arg must be present and not empty if arg in kwargs and kwargs[arg]: data['port'][arg] = kwargs[arg] @@ -494,6 +495,7 @@ class TestPortsV2(QuantumDbPluginV2TestCase): 'tenant_id': 'bad_tenant_id', 'admin_state_up': True, 'device_id': 'fake_device', + 'device_owner': 'fake_owner', 'fixed_ips': []}} port_req = self.new_create_request('ports', data)