From 1c6ae91594e131d403f9aad2c1c31b8c1549639e Mon Sep 17 00:00:00 2001 From: kairat_kushaev Date: Thu, 9 Apr 2015 01:29:49 +0300 Subject: Implement update for Manila::Share Implement handle_update for Manila::Share resource. Change-Id: I46e8d74ba72478c3a42458989d6eb8289aaad59f Implements: partial blueprint add-manila-resources --- heat/engine/resources/openstack/manila/share.py | 39 +++++++++++++++ heat/tests/test_manila_share.py | 65 +++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/heat/engine/resources/openstack/manila/share.py b/heat/engine/resources/openstack/manila/share.py index eba1a9b96..dc5034648 100644 --- a/heat/engine/resources/openstack/manila/share.py +++ b/heat/engine/resources/openstack/manila/share.py @@ -300,6 +300,45 @@ class ManilaShare(resource.Resource): 'current': share.status}] self._verify_check_conditions(checks) + def handle_update(self, json_snippet, tmpl_diff, prop_diff): + kwargs = {} + if self.IS_PUBLIC in prop_diff: + kwargs['is_public'] = prop_diff.get(self.IS_PUBLIC) + if self.NAME in prop_diff: + kwargs['display_name'] = prop_diff.get(self.NAME) + if self.DESCRIPTION in prop_diff: + kwargs['display_description'] = prop_diff.get(self.DESCRIPTION) + if kwargs: + self.client().shares.update(self.resource_id, + **kwargs) + + if self.METADATA in prop_diff: + metadata = prop_diff.get(self.METADATA) + self.client().shares.update_all_metadata( + self.resource_id, metadata) + + if self.ACCESS_RULES in prop_diff: + actual_old_rules = [] + for rule in self.client().shares.access_list(self.resource_id): + old_rule = { + self.ACCESS_TO: getattr(rule, self.ACCESS_TO), + self.ACCESS_TYPE: getattr(rule, self.ACCESS_TYPE), + self.ACCESS_LEVEL: getattr(rule, self.ACCESS_LEVEL) + } + if old_rule in prop_diff[self.ACCESS_RULES]: + actual_old_rules.append(old_rule) + else: + self.client().shares.deny(share=self.resource_id, + id=rule.id) + for rule in prop_diff[self.ACCESS_RULES]: + if rule not in actual_old_rules: + self.client().shares.allow( + share=self.resource_id, + access_type=rule.get(self.ACCESS_TYPE), + access=rule.get(self.ACCESS_TO), + access_level=rule.get(self.ACCESS_LEVEL) + ) + def resource_mapping(): return {'OS::Manila::Share': ManilaShare} diff --git a/heat/tests/test_manila_share.py b/heat/tests/test_manila_share.py index 29455f235..acdcea7d7 100644 --- a/heat/tests/test_manila_share.py +++ b/heat/tests/test_manila_share.py @@ -10,6 +10,8 @@ # 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 collections +import copy import mock import six @@ -17,6 +19,7 @@ import six from heat.common import exception from heat.common import template_format from heat.engine.resources.openstack.manila import share as mshare +from heat.engine import rsrc_defn from heat.engine import scheduler from heat.tests import common from heat.tests import utils @@ -154,3 +157,65 @@ class ManilaShareTest(common.HeatTestCase): scheduler.TaskRunner(share.check)) self.assertIn("Error: 'status': expected '['available']'", six.text_type(exc)) + + def test_share_update(self): + share = self._create_share("stack_share_update") + updated_share_props = copy.deepcopy(share.properties.data) + updated_share_props[mshare.ManilaShare.DESCRIPTION] = "desc" + updated_share_props[mshare.ManilaShare.NAME] = "name" + updated_share_props[mshare.ManilaShare.IS_PUBLIC] = True + share.client().shares.update.return_value = None + after = rsrc_defn.ResourceDefinition(share.name, share.type(), + updated_share_props) + scheduler.TaskRunner(share.update, after)() + kwargs = { + "display_name": "name", + "display_description": "desc", + "is_public": True + } + share.client().shares.update.assertCalledOnceWith(share.resource_id, + **kwargs) + + def test_share_update_access_rules(self): + share = self._create_share("stack_share_update_access_rules") + updated_share_props = copy.deepcopy(share.properties.data) + updated_share_props[mshare.ManilaShare.ACCESS_RULES] = [ + {mshare.ManilaShare.ACCESS_TO: "127.0.0.2", + mshare.ManilaShare.ACCESS_TYPE: "ip", + mshare.ManilaShare.ACCESS_LEVEL: "ro"}] + share.client().shares.deny.return_value = None + current_rule = { + mshare.ManilaShare.ACCESS_TO: "127.0.0.1", + mshare.ManilaShare.ACCESS_TYPE: "ip", + mshare.ManilaShare.ACCESS_LEVEL: "ro", + "id": "test_access_rule" + } + rule_tuple = collections.namedtuple("DummyRule", + list(current_rule.keys())) + share.client().shares.access_list.return_value = [ + rule_tuple(**current_rule)] + after = rsrc_defn.ResourceDefinition(share.name, share.type(), + updated_share_props) + scheduler.TaskRunner(share.update, after)() + + share.client().shares.access_list.assert_called_once_with( + share.resource_id) + share.client().shares.allow.assert_called_with( + share=share.resource_id, access_type="ip", + access="127.0.0.2", access_level="ro") + share.client().shares.deny.assert_called_once_with( + share=share.resource_id, id="test_access_rule") + + def test_share_update_metadata(self): + share = self._create_share("stack_share_update_metadata") + updated_share_props = copy.deepcopy(share.properties.data) + updated_share_props[mshare.ManilaShare.METADATA] = { + "fake_key": "fake_value"} + share.client().shares.update_all_metadata.return_value = None + + after = rsrc_defn.ResourceDefinition(share.name, share.type(), + updated_share_props) + scheduler.TaskRunner(share.update, after)() + share.client().shares.update_all_metadata.assert_called_once_with( + share.resource_id, + updated_share_props[mshare.ManilaShare.METADATA]) -- cgit v1.2.1