diff --git a/magnum/objects/__init__.py b/magnum/objects/__init__.py index 6c5e67c59d..3f8da9698a 100644 --- a/magnum/objects/__init__.py +++ b/magnum/objects/__init__.py @@ -13,6 +13,7 @@ # under the License. from magnum.objects import bay +from magnum.objects import baylock from magnum.objects import baymodel from magnum.objects import container from magnum.objects import node @@ -23,6 +24,7 @@ from magnum.objects import service Container = container.Container Bay = bay.Bay +BayLock = baylock.BayLock BayModel = baymodel.BayModel Node = node.Node Pod = pod.Pod @@ -30,6 +32,7 @@ ReplicationController = rc.ReplicationController Service = service.Service __all__ = (Bay, + BayLock, BayModel, Container, Node, diff --git a/magnum/objects/baylock.py b/magnum/objects/baylock.py new file mode 100644 index 0000000000..6464faf9ea --- /dev/null +++ b/magnum/objects/baylock.py @@ -0,0 +1,42 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from magnum.db import api as dbapi +from magnum.objects import base +from magnum.objects import utils as obj_utils + + +class BayLock(base.MagnumObject): + # Version 1.0: Initial version + VERSION = '1.0' + + dbapi = dbapi.get_instance() + + fields = { + 'id': int, + 'bay_uuid': obj_utils.str_or_none, + 'conductor_id': obj_utils.str_or_none, + } + + @base.remotable_classmethod + def create(cls, bay_uuid, conductor_id): + return cls.dbapi.create_bay_lock(bay_uuid, conductor_id) + + @base.remotable_classmethod + def steal(cls, bay_uuid, old_conductor_id, new_conductor_id): + return cls.dbapi.steal_bay_lock(bay_uuid, old_conductor_id, + new_conductor_id) + + @base.remotable_classmethod + def release(cls, bay_uuid, conductor_id): + return cls.dbapi.release_bay_lock(bay_uuid, conductor_id) diff --git a/magnum/tests/unit/db/utils.py b/magnum/tests/unit/db/utils.py index 8339b45373..091ef78e81 100644 --- a/magnum/tests/unit/db/utils.py +++ b/magnum/tests/unit/db/utils.py @@ -228,3 +228,12 @@ def create_test_rc(**kw): del service['id'] dbapi = db_api.get_instance() return dbapi.create_rc(service) + + +def get_test_baylock(**kw): + return { + 'id': kw.get('id', 42), + 'bay_uuid': kw.get('bay_uuid', '5d12f6fd-a196-4bf0-ae4c-1f639a523a52'), + 'conductor_id': kw.get('conductor_id', + '72625085-c507-4410-9b28-cd7cf1fbf1ad'), + } diff --git a/magnum/tests/unit/objects/test_baylock.py b/magnum/tests/unit/objects/test_baylock.py new file mode 100644 index 0000000000..220397b415 --- /dev/null +++ b/magnum/tests/unit/objects/test_baylock.py @@ -0,0 +1,51 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock +import uuid + +from magnum import objects +from magnum.tests.unit.db import base +from magnum.tests.unit.db import utils + + +class TestBayLockObject(base.DbTestCase): + + def setUp(self): + super(TestBayLockObject, self).setUp() + baylock_dict = utils.get_test_baylock() + self.bay_uuid = baylock_dict['bay_uuid'] + self.conductor_id = baylock_dict['conductor_id'] + + def test_create(self): + with mock.patch.object(self.dbapi, 'create_bay_lock', + autospec=True) as mock_create_baylock: + objects.BayLock.create(self.bay_uuid, self.conductor_id) + mock_create_baylock.assert_called_once_with(self.bay_uuid, + self.conductor_id) + + def test_steal(self): + with mock.patch.object(self.dbapi, 'steal_bay_lock', + autospec=True) as mock_steal_baylock: + old_conductor_id = self.conductor_id + new_conductor_id = str(uuid.uuid4()) + objects.BayLock.steal(self.bay_uuid, old_conductor_id, + new_conductor_id) + mock_steal_baylock.assert_called_once_with(self.bay_uuid, + old_conductor_id, new_conductor_id) + + def test_release(self): + with mock.patch.object(self.dbapi, 'release_bay_lock', + autospec=True) as mock_release_baylock: + objects.BayLock.release(self.bay_uuid, self.conductor_id) + mock_release_baylock.assert_called_once_with(self.bay_uuid, + self.conductor_id)