From 4ee0d1c54a92f258593ff3dbf2acc50075a5d8e1 Mon Sep 17 00:00:00 2001 From: Mark McClain Date: Mon, 13 Aug 2012 23:18:54 -0400 Subject: [PATCH] Add model support for DHCP lease expiration bug 1022804 This fix is phase 1 of a fix that to address re-allocation of a fixed_ip before the DHCP lease expires. The next phases will add RPC and recycling support. Change-Id: I3c907ade5cde562ff5a222bd97e56ffef367c722 --- etc/quantum.conf | 3 +++ quantum/common/config.py | 1 + quantum/db/db_base_plugin_v2.py | 10 +++++++++- quantum/db/models_v2.py | 1 + quantum/tests/unit/test_db_plugin.py | 13 +++++++++++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/etc/quantum.conf b/etc/quantum.conf index 39f18de53e..d98f9c1851 100644 --- a/etc/quantum.conf +++ b/etc/quantum.conf @@ -39,6 +39,9 @@ api_paste_config = api-paste.ini # Maximum amount of retries to generate a unique MAC address # mac_generation_retries = 16 +# DHCP Lease duration (in seconds) +# dhcp_lease_duration = 120 + # Enable or disable bulk create/update/delete operations # allow_bulk = True # RPC configuration options. Defined in rpc __init__ diff --git a/quantum/common/config.py b/quantum/common/config.py index ba28af7195..501b5a0c79 100644 --- a/quantum/common/config.py +++ b/quantum/common/config.py @@ -48,6 +48,7 @@ core_opts = [ cfg.IntOpt('max_dns_nameservers', default=5), cfg.IntOpt('max_subnet_host_routes', default=20), cfg.StrOpt('state_path', default='.'), + cfg.IntOpt('dhcp_lease_duration', default=120), ] # Register the configuration options diff --git a/quantum/db/db_base_plugin_v2.py b/quantum/db/db_base_plugin_v2.py index 85f5c42ec9..803216622c 100644 --- a/quantum/db/db_base_plugin_v2.py +++ b/quantum/db/db_base_plugin_v2.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import datetime import logging import random @@ -26,6 +27,7 @@ from quantum.common import utils from quantum.db import api as db from quantum.db import models_v2 from quantum.openstack.common import cfg +from quantum.openstack.common import timeutils from quantum import quantum_plugin_base_v2 @@ -266,6 +268,11 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2): QuantumDbPluginV2._delete_ip_allocation(context, network_id, subnet_id, port_id, ip_address) + @staticmethod + def _default_allocation_expiration(): + return (timeutils.utcnow() + + datetime.timedelta(seconds=cfg.CONF.dhcp_lease_duration)) + @staticmethod def _delete_ip_allocation(context, network_id, subnet_id, port_id, ip_address): @@ -1016,7 +1023,8 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2): for ip in ips: allocated = models_v2.IPAllocation( network_id=port['network_id'], port_id=port.id, - ip_address=ip['ip_address'], subnet_id=ip['subnet_id']) + ip_address=ip['ip_address'], subnet_id=ip['subnet_id'], + expiration=self._default_allocation_expiration()) context.session.add(allocated) port.update(p) diff --git a/quantum/db/models_v2.py b/quantum/db/models_v2.py index 6e9dc20454..6be757ac2e 100644 --- a/quantum/db/models_v2.py +++ b/quantum/db/models_v2.py @@ -84,6 +84,7 @@ class IPAllocation(model_base.BASEV2): network_id = sa.Column(sa.String(36), sa.ForeignKey("networks.id", ondelete="CASCADE"), nullable=False, primary_key=True) + expiration = sa.Column(sa.DateTime, nullable=True) class Port(model_base.BASEV2, HasId, HasTenant): diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index b383168f4d..b2d8b66973 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -15,6 +15,7 @@ import contextlib import copy +import datetime import logging import mock import os @@ -34,6 +35,7 @@ from quantum.db import api as db from quantum.db import db_base_plugin_v2 from quantum.manager import QuantumManager from quantum.openstack.common import cfg +from quantum.openstack.common import timeutils from quantum.tests.unit import test_extensions from quantum.tests.unit.testlib_api import create_request from quantum.wsgi import Serializer, JSONDeserializer @@ -2025,3 +2027,14 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase): req = self.new_delete_request('subnets', subnet['subnet']['id']) res = req.get_response(self.api) self.assertEquals(res.status_int, 204) + + def test_default_allocation_expiration(self): + reference = datetime.datetime(2012, 8, 13, 23, 11, 0) + timeutils.utcnow.override_time = reference + + cfg.CONF.set_override('dhcp_lease_duration', 120) + expires = QuantumManager.get_plugin()._default_allocation_expiration() + timeutils.utcnow + cfg.CONF.reset() + timeutils.utcnow.override_time = None + self.assertEqual(expires, reference + datetime.timedelta(seconds=120))