From de549a931cf77aeecab98eb3a4d8e1ac8a7b5b84 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Sun, 10 Jun 2018 20:53:09 +0000 Subject: [PATCH] zun: use containers.update for renaming The old way of renaming is duplicated [1]. This patch uses the new way to rename a container. In addition, this patch also bump the version of python-zunclient to 2.0.0 [1] https://review.openstack.org/#/c/557595/ Change-Id: I4ef36a3c4a805b3e041fcb9456c297e59865485c --- heat/engine/clients/os/zun.py | 17 +++++++++-- .../resources/openstack/zun/container.py | 6 +--- heat/tests/clients/test_zun_client.py | 30 +++++++++++++++++++ heat/tests/openstack/zun/test_container.py | 10 ++++--- lower-constraints.txt | 2 +- requirements.txt | 2 +- 6 files changed, 54 insertions(+), 13 deletions(-) diff --git a/heat/engine/clients/os/zun.py b/heat/engine/clients/os/zun.py index af829275c0..a50a6428da 100644 --- a/heat/engine/clients/os/zun.py +++ b/heat/engine/clients/os/zun.py @@ -26,14 +26,15 @@ class ZunClientPlugin(client_plugin.ClientPlugin): default_version = '1.12' supported_versions = [ - V1_12 + V1_12, V1_18 ] = [ - '1.12' + '1.12', '1.18' ] def _create(self, version=None): if not version: version = self.default_version + interface = self._get_client_option(CLIENT_NAME, 'endpoint_type') args = { 'interface': interface, @@ -45,6 +46,18 @@ class ZunClientPlugin(client_plugin.ClientPlugin): client = zun_client.Client(version, **args) return client + def update_container(self, container_id, **prop_diff): + try: + if prop_diff: + self.client(version=self.V1_18).containers.update( + container_id, **prop_diff) + except zc_exc.NotAcceptable: + if 'name' in prop_diff: + name = prop_diff.pop('name') + self.client().containers.rename(container_id, name=name) + if prop_diff: + self.client().containers.update(container_id, **prop_diff) + def is_not_found(self, ex): return isinstance(ex, zc_exc.NotFound) diff --git a/heat/engine/resources/openstack/zun/container.py b/heat/engine/resources/openstack/zun/container.py index 56a312c248..3db1eded20 100644 --- a/heat/engine/resources/openstack/zun/container.py +++ b/heat/engine/resources/openstack/zun/container.py @@ -279,11 +279,7 @@ class Container(resource.Resource): .status) def handle_update(self, json_snippet, tmpl_diff, prop_diff): - if self.NAME in prop_diff: - name = prop_diff.pop(self.NAME) - self.client().containers.rename(self.resource_id, name=name) - if prop_diff: - self.client().containers.update(self.resource_id, **prop_diff) + self.client_plugin().update_container(self.resource_id, **prop_diff) def handle_delete(self): if not self.resource_id: diff --git a/heat/tests/clients/test_zun_client.py b/heat/tests/clients/test_zun_client.py index 2bb636554b..2573a095ab 100644 --- a/heat/tests/clients/test_zun_client.py +++ b/heat/tests/clients/test_zun_client.py @@ -10,12 +10,24 @@ # License for the specific language governing permissions and limitations # under the License. +import mock + +from zunclient import exceptions as zc_exc + from heat.tests import common from heat.tests import utils class ZunClientPluginTest(common.HeatTestCase): + def setUp(self): + super(ZunClientPluginTest, self).setUp() + self.client = mock.Mock() + context = utils.dummy_context() + self.plugin = context.clients.client_plugin('zun') + self.plugin.client = lambda **kw: self.client + self.resource_id = '123456' + def test_create(self): context = utils.dummy_context() plugin = context.clients.client_plugin('zun') @@ -24,3 +36,21 @@ class ZunClientPluginTest(common.HeatTestCase): client.containers.api.session.auth.endpoint) self.assertEqual('1.12', client.api_version.get_string()) + + def test_container_update(self): + prop_diff = {'cpu': 10, 'memory': 10, 'name': 'fake-container'} + self.plugin.update_container(self.resource_id, **prop_diff) + self.client.containers.update.assert_called_once_with( + self.resource_id, cpu=10, memory=10, name='fake-container') + + def test_container_update_not_acceptable(self): + self.client.containers.update.side_effect = [ + zc_exc.NotAcceptable(), None] + prop_diff = {'cpu': 10, 'memory': 10, 'name': 'fake-container'} + self.plugin.update_container(self.resource_id, **prop_diff) + self.client.containers.update.assert_has_calls([ + mock.call(self.resource_id, cpu=10, memory=10, + name='fake-container'), + mock.call(self.resource_id, cpu=10, memory=10)]) + self.client.containers.rename.assert_called_once_with( + self.resource_id, name='fake-container') diff --git a/heat/tests/openstack/zun/test_container.py b/heat/tests/openstack/zun/test_container.py index f4be7b3d30..7c06ac1c74 100644 --- a/heat/tests/openstack/zun/test_container.py +++ b/heat/tests/openstack/zun/test_container.py @@ -20,6 +20,7 @@ from zunclient import exceptions as zc_exc from heat.common import exception from heat.common import template_format +from heat.engine.clients.os import zun from heat.engine.resources.openstack.zun import container from heat.engine import scheduler from heat.engine import template @@ -125,6 +126,8 @@ class ZunContainerTest(common.HeatTestCase): self.patchobject(container.Container, 'neutron', return_value=self.neutron_client) self.stub_VolumeConstraint_validate() + self.mock_update = self.patchobject(zun.ZunClientPlugin, + 'update_container') def _mock_get_client(self): value = mock.MagicMock() @@ -263,10 +266,9 @@ class ZunContainerTest(common.HeatTestCase): rsrc_defns = template.Template(new_t).resource_definitions(self.stack) new_c = rsrc_defns[self.fake_name] scheduler.TaskRunner(c.update, new_c)() - self.client.containers.update.assert_called_once_with( - self.resource_id, cpu=10, memory=10) - self.client.containers.rename.assert_called_once_with( - self.resource_id, name='fake-container') + self.mock_update.assert_called_once_with( + self.resource_id, + cpu=10, memory=10, name='fake-container') self.assertEqual((c.UPDATE, c.COMPLETE), c.state) def test_container_delete(self): diff --git a/lower-constraints.txt b/lower-constraints.txt index 54c1476e22..e518dd3fd9 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -122,7 +122,7 @@ python-subunit==1.2.0 python-swiftclient==3.2.0 python-troveclient==2.2.0 python-zaqarclient==1.0.0 -python-zunclient==1.3.0 +python-zunclient==2.0.0 pytz==2013.6 PyYAML==3.12 qpid-python==0.26;python_version=='2.7' # Apache-2.0 diff --git a/requirements.txt b/requirements.txt index 858a9cfd42..ad35b70269 100644 --- a/requirements.txt +++ b/requirements.txt @@ -49,7 +49,7 @@ python-saharaclient>=1.4.0 # Apache-2.0 python-swiftclient>=3.2.0 # Apache-2.0 python-troveclient>=2.2.0 # Apache-2.0 python-zaqarclient>=1.0.0 # Apache-2.0 -python-zunclient>=1.3.0 # Apache-2.0 +python-zunclient>=2.0.0 # Apache-2.0 pytz>=2013.6 # MIT PyYAML>=3.12 # MIT requests>=2.14.2 # Apache-2.0