From fb240766a2505d5ad527ba314913c90a9f8da6b1 Mon Sep 17 00:00:00 2001 From: jonnary Date: Mon, 13 Feb 2017 19:33:36 +0800 Subject: [PATCH] Support 'enabled' in attach callback We have supported 'enabled' option in api/db when attaching policy to a cluster. This patch supports 'enabled' in attach callback, or else we will get bug when we run 'senlin cluster-policy-attach' with 'enabled'=False. Closes-Bug: #1664208 Change-Id: I75d3eec974bf728c51cd31b2b8c1c31b7e43d1ba --- senlin/engine/cluster.py | 3 ++- senlin/policies/affinity_policy.py | 5 +++-- senlin/policies/base.py | 5 +++-- senlin/policies/health_policy.py | 6 ++++-- senlin/policies/lb_policy.py | 5 +++-- senlin/tests/unit/engine/test_cluster.py | 6 +++--- senlin/tests/unit/fakes.py | 2 +- senlin/tests/unit/policies/test_health_policy.py | 1 + 8 files changed, 20 insertions(+), 13 deletions(-) diff --git a/senlin/engine/cluster.py b/senlin/engine/cluster.py index edb0e7d32..e15b4ef6d 100644 --- a/senlin/engine/cluster.py +++ b/senlin/engine/cluster.py @@ -321,7 +321,8 @@ class Cluster(object): return False, reason # invoke policy callback - res, data = policy.attach(self) + enabled = bool(values.get('enabled', True)) + res, data = policy.attach(self, enabled=enabled) if not res: return False, data diff --git a/senlin/policies/affinity_policy.py b/senlin/policies/affinity_policy.py index 0ea9d9bc8..560850c4a 100644 --- a/senlin/policies/affinity_policy.py +++ b/senlin/policies/affinity_policy.py @@ -128,10 +128,11 @@ class AffinityPolicy(base.Policy): return True - def attach(self, cluster): + def attach(self, cluster, enabled=True): """Routine to be invoked when policy is to be attached to a cluster. - :para cluster: The target cluster to attach to; + :para cluster: The cluster to which the policy is being attached to. + :param enabled: The attached cluster policy is enabled or disabled. :returns: When the operation was successful, returns a tuple (True, message); otherwise, return a tuple (False, error). """ diff --git a/senlin/policies/base.py b/senlin/policies/base.py index 8551ae5a0..8b089abcb 100644 --- a/senlin/policies/base.py +++ b/senlin/policies/base.py @@ -298,10 +298,11 @@ class Policy(object): self._lbaasclient = driver.SenlinDriver().loadbalancing(params) return self._lbaasclient - def attach(self, cluster): + def attach(self, cluster, enabled=True): '''Method to be invoked before policy is attached to a cluster. - :param cluster: the cluster to which the policy is being attached to. + :param cluster: The cluster to which the policy is being attached to. + :param enabled: The attached cluster policy is enabled or disabled. :returns: (True, message) if the operation is successful, or (False, error) otherwise. ''' diff --git a/senlin/policies/health_policy.py b/senlin/policies/health_policy.py index 8a04cc3f0..7f4d5b946 100644 --- a/senlin/policies/health_policy.py +++ b/senlin/policies/health_policy.py @@ -175,12 +175,13 @@ class HealthPolicy(base.Policy): # TODO(Qiming): Add detection of duplicated action names when # support to list of actions is implemented. - def attach(self, cluster): + def attach(self, cluster, enabled=True): """"Hook for policy attach. Register the cluster for health management. - :param cluster: The target cluster. + :param cluster: The cluster to which the policy is being attached to. + :param enabled: The attached cluster policy is enabled or disabled. :return: A tuple comprising execution result and policy data. """ p_type = cluster.rt['profile'].type_name @@ -195,6 +196,7 @@ class HealthPolicy(base.Policy): 'check_type': self.check_type, 'interval': self.interval, 'params': {}, + 'enabled': enabled } health_manager.register(cluster.id, engine_id=None, **kwargs) diff --git a/senlin/policies/lb_policy.py b/senlin/policies/lb_policy.py index 23c0a94f0..8c0b51354 100644 --- a/senlin/policies/lb_policy.py +++ b/senlin/policies/lb_policy.py @@ -303,10 +303,11 @@ class LoadBalancingPolicy(base.Policy): ) % {'key': self.VIP_SUBNET, 'value': name_or_id} raise exc.InvalidSpec(message=msg) - def attach(self, cluster): + def attach(self, cluster, enabled=True): """Routine to be invoked when policy is to be attached to a cluster. - :param cluster: The target cluster to be attached to; + :param cluster: The cluster to which the policy is being attached to. + :param enabled: The attached cluster policy is enabled or disabled. :returns: When the operation was successful, returns a tuple (True, message); otherwise, return a tuple (False, error). """ diff --git a/senlin/tests/unit/engine/test_cluster.py b/senlin/tests/unit/engine/test_cluster.py index a57c94293..1c4a96bb0 100644 --- a/senlin/tests/unit/engine/test_cluster.py +++ b/senlin/tests/unit/engine/test_cluster.py @@ -505,7 +505,7 @@ class TestCluster(base.SenlinTestCase): values = {'enabled': True} res, reason = cluster.attach_policy(self.context, POLICY_ID, values) - policy.attach.assert_called_once_with(cluster) + policy.attach.assert_called_once_with(cluster, enabled=True) mock_load.assert_called_once_with(self.context, POLICY_ID) mock_cp.assert_called_once_with(CLUSTER_ID, POLICY_ID, priority=10, enabled=True, data=None) @@ -588,7 +588,7 @@ class TestCluster(base.SenlinTestCase): self.assertTrue(res) self.assertEqual('Policy attached.', reason) - policy.attach.assert_called_once_with(cluster) + policy.attach.assert_called_once_with(cluster, enabled=True) mock_load.assert_called_once_with(self.context, new_policy_id) mock_cp.assert_called_once_with(cluster.id, new_policy_id, priority=10, enabled=True, data=None) @@ -609,7 +609,7 @@ class TestCluster(base.SenlinTestCase): self.assertFalse(res) self.assertEqual('Bad things happened.', reason) - policy.attach.assert_called_once_with(cluster) + policy.attach.assert_called_once_with(cluster, enabled=True) mock_load.assert_called_once_with(self.context, new_id) @mock.patch.object(cpo.ClusterPolicy, 'delete') diff --git a/senlin/tests/unit/fakes.py b/senlin/tests/unit/fakes.py index 6d6a8b251..6b8949082 100644 --- a/senlin/tests/unit/fakes.py +++ b/senlin/tests/unit/fakes.py @@ -76,7 +76,7 @@ class TestPolicy(policy_base.Policy): def __init__(self, name, spec, **kwargs): super(TestPolicy, self).__init__(name, spec, **kwargs) - def attach(self, cluster): + def attach(self, cluster, enabled=True): return True, {} def detach(self, cluster): diff --git a/senlin/tests/unit/policies/test_health_policy.py b/senlin/tests/unit/policies/test_health_policy.py index ef7b0aa6e..b2da73087 100644 --- a/senlin/tests/unit/policies/test_health_policy.py +++ b/senlin/tests/unit/policies/test_health_policy.py @@ -103,6 +103,7 @@ class TestHealthPolicy(base.SenlinTestCase): 'check_type': self.hp.check_type, 'interval': self.hp.interval, 'params': {}, + 'enabled': True } mock_hm_reg.assert_called_once_with('CLUSTER_ID', engine_id=None,