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
This commit is contained in:
jonnary 2017-02-13 19:33:36 +08:00
parent 4906eb50e4
commit fb240766a2
8 changed files with 20 additions and 13 deletions

View File

@ -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

View File

@ -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).
"""

View File

@ -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.
'''

View File

@ -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)

View File

@ -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).
"""

View File

@ -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')

View File

@ -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):

View File

@ -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,