Rework _build_conn_params for policies
We are using an object when calling _build_conn_params(). This is very inconvenient. As an alternative, we just need the 'user' and 'project' data for context initialization. This patch relaxes this constraint, as a preparation for deep policy validation without constructing an object. Change-Id: I40491ae11b6634ba8af72277de629cc15cd86900
This commit is contained in:
parent
6affcb9b67
commit
c46e892b68
|
@ -118,7 +118,7 @@ class AffinityPolicy(base.Policy):
|
|||
"""
|
||||
if self._novaclient is not None:
|
||||
return self._novaclient
|
||||
params = self._build_conn_params(obj)
|
||||
params = self._build_conn_params(obj.user, obj.project)
|
||||
self._novaclient = driver.SenlinDriver().compute(params)
|
||||
return self._novaclient
|
||||
|
||||
|
|
|
@ -279,10 +279,11 @@ class Policy(object):
|
|||
}
|
||||
return pb_dict
|
||||
|
||||
def _build_conn_params(self, cluster):
|
||||
def _build_conn_params(self, user, project):
|
||||
"""Build trust-based connection parameters.
|
||||
|
||||
:param cluster: the cluste for which the trust will be checked.
|
||||
:param user: the user for which the trust will be checked.
|
||||
:param object: the user for which the trust will be checked.
|
||||
"""
|
||||
service_creds = senlin_context.get_service_context()
|
||||
params = {
|
||||
|
@ -292,10 +293,9 @@ class Policy(object):
|
|||
'user_domain_name': service_creds.get('user_domain_name')
|
||||
}
|
||||
|
||||
cred = co.Credential.get(oslo_context.get_current(),
|
||||
cluster.user, cluster.project)
|
||||
cred = co.Credential.get(oslo_context.get_current(), user, project)
|
||||
if cred is None:
|
||||
raise exception.TrustNotFound(trustor=cluster.user)
|
||||
raise exception.TrustNotFound(trustor=user)
|
||||
params['trust_id'] = cred.cred['openstack']['trust']
|
||||
|
||||
return params
|
||||
|
|
|
@ -298,7 +298,7 @@ class LoadBalancingPolicy(base.Policy):
|
|||
nodes = nm.Node.load_all(oslo_context.get_current(),
|
||||
cluster_id=cluster.id)
|
||||
|
||||
params = self._build_conn_params(cluster)
|
||||
params = self._build_conn_params(cluster.user, cluster.project)
|
||||
params['lb_status_timeout'] = self.lb_status_timeout
|
||||
lb_driver = driver_base.SenlinDriver().loadbalancing(params)
|
||||
|
||||
|
@ -342,7 +342,7 @@ class LoadBalancingPolicy(base.Policy):
|
|||
contains a error message.
|
||||
"""
|
||||
reason = _('LB resources deletion succeeded.')
|
||||
params = self._build_conn_params(cluster)
|
||||
params = self._build_conn_params(cluster.user, cluster.project)
|
||||
params['lb_status_timeout'] = self.lb_status_timeout
|
||||
lb_driver = driver_base.SenlinDriver().loadbalancing(params)
|
||||
|
||||
|
@ -435,7 +435,7 @@ class LoadBalancingPolicy(base.Policy):
|
|||
return
|
||||
|
||||
db_cluster = co.Cluster.get(action.context, cluster_id)
|
||||
params = self._build_conn_params(db_cluster)
|
||||
params = self._build_conn_params(db_cluster.user, db_cluster.project)
|
||||
params['lb_status_timeout'] = self.lb_status_timeout
|
||||
lb_driver = driver_base.SenlinDriver().loadbalancing(params)
|
||||
cp = cluster_policy.ClusterPolicy.load(action.context, cluster_id,
|
||||
|
@ -485,7 +485,7 @@ class LoadBalancingPolicy(base.Policy):
|
|||
return
|
||||
|
||||
db_cluster = co.Cluster.get(action.context, cluster_id)
|
||||
params = self._build_conn_params(db_cluster)
|
||||
params = self._build_conn_params(db_cluster.user, db_cluster.project)
|
||||
params['lb_status_timeout'] = self.lb_status_timeout
|
||||
lb_driver = driver_base.SenlinDriver().loadbalancing(params)
|
||||
cp = cluster_policy.ClusterPolicy.load(action.context, cluster_id,
|
||||
|
|
|
@ -107,7 +107,7 @@ class RegionPlacementPolicy(base.Policy):
|
|||
"""
|
||||
if self._keystoneclient is not None:
|
||||
return self._keystoneclient
|
||||
params = self._build_conn_params(obj)
|
||||
params = self._build_conn_params(obj.user, obj.project)
|
||||
self._keystoneclient = driver_base.SenlinDriver().identity(params)
|
||||
return self._keystoneclient
|
||||
|
||||
|
|
|
@ -100,7 +100,7 @@ class ZonePlacementPolicy(base.Policy):
|
|||
if self._novaclient is not None:
|
||||
return self._novaclient
|
||||
|
||||
params = self._build_conn_params(obj)
|
||||
params = self._build_conn_params(obj.user, obj.project)
|
||||
self._novaclient = driver.SenlinDriver().compute(params)
|
||||
return self._novaclient
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ class TestAffinityPolicy(base.SenlinTestCase):
|
|||
|
||||
@mock.patch("senlin.drivers.base.SenlinDriver")
|
||||
def test_nova(self, mock_driver):
|
||||
cluster = mock.Mock()
|
||||
cluster = mock.Mock(user='user1', project='project1')
|
||||
policy = ap.AffinityPolicy('test-policy', self.spec)
|
||||
mock_params = mock.Mock()
|
||||
mock_build = self.patchobject(policy, '_build_conn_params',
|
||||
|
@ -60,7 +60,7 @@ class TestAffinityPolicy(base.SenlinTestCase):
|
|||
x_nova = x_driver.compute.return_value
|
||||
self.assertEqual(x_nova, result)
|
||||
self.assertEqual(x_nova, policy._novaclient)
|
||||
mock_build.assert_called_once_with(cluster)
|
||||
mock_build.assert_called_once_with('user1', 'project1')
|
||||
x_driver.compute.assert_called_once_with(mock_params)
|
||||
|
||||
def test_nova_already_initialized(self):
|
||||
|
|
|
@ -475,7 +475,7 @@ class TestLoadBalancingPolicyOperations(base.SenlinTestCase):
|
|||
m_load, m_conn):
|
||||
ctx = mock.Mock()
|
||||
cid = 'CLUSTER_ID'
|
||||
cluster = mock.Mock()
|
||||
cluster = mock.Mock(user='user1', project='project1')
|
||||
m_cluster_get.return_value = cluster
|
||||
node_obj = mock.Mock(data={})
|
||||
action = mock.Mock(data={}, context=ctx, action=consts.NODE_CREATE)
|
||||
|
@ -507,7 +507,7 @@ class TestLoadBalancingPolicyOperations(base.SenlinTestCase):
|
|||
# assertion
|
||||
self.assertIsNone(res)
|
||||
m_cluster_get.assert_called_once_with(ctx, 'CLUSTER_ID')
|
||||
m_conn.assert_called_once_with(cluster)
|
||||
m_conn.assert_called_once_with('user1', 'project1')
|
||||
m_load.assert_called_once_with(ctx, cid, policy.id)
|
||||
m_extract.assert_called_once_with(cp_data)
|
||||
m_node_load.assert_called_once_with(ctx, node_id='NODE_ID')
|
||||
|
@ -521,16 +521,17 @@ class TestLoadBalancingPolicyOperations(base.SenlinTestCase):
|
|||
def test_post_op_add_nodes(self, m_cluster_get, m_node_load, m_extract,
|
||||
m_load, m_conn):
|
||||
cid = 'CLUSTER_ID'
|
||||
cluster = mock.Mock()
|
||||
cluster = mock.Mock(user='user1', project='project1')
|
||||
m_cluster_get.return_value = cluster
|
||||
node1 = mock.Mock()
|
||||
node2 = mock.Mock()
|
||||
node1.data = {}
|
||||
node2.data = {}
|
||||
action = mock.Mock()
|
||||
action.data = {'creation': {'nodes': ['NODE1_ID', 'NODE2_ID']}}
|
||||
action.context = 'action_context'
|
||||
action.action = consts.CLUSTER_RESIZE
|
||||
node1 = mock.Mock(data={})
|
||||
node2 = mock.Mock(data={})
|
||||
action = mock.Mock(context='action_context',
|
||||
action=consts.CLUSTER_RESIZE,
|
||||
data={
|
||||
'creation': {
|
||||
'nodes': ['NODE1_ID', 'NODE2_ID']
|
||||
}
|
||||
})
|
||||
cp = mock.Mock()
|
||||
policy_data = {
|
||||
'loadbalancer': 'LB_ID',
|
||||
|
@ -549,12 +550,15 @@ class TestLoadBalancingPolicyOperations(base.SenlinTestCase):
|
|||
m_node_load.side_effect = [node1, node2]
|
||||
m_load.return_value = cp
|
||||
m_extract.return_value = policy_data
|
||||
|
||||
policy = lb_policy.LoadBalancingPolicy('test-policy', self.spec)
|
||||
|
||||
# do it
|
||||
res = policy.post_op(cid, action)
|
||||
|
||||
# assertions
|
||||
self.assertIsNone(res)
|
||||
m_cluster_get.assert_called_once_with('action_context', 'CLUSTER_ID')
|
||||
m_conn.assert_called_once_with(cluster)
|
||||
m_conn.assert_called_once_with('user1', 'project1')
|
||||
m_load.assert_called_once_with('action_context', cid, policy.id)
|
||||
m_extract.assert_called_once_with(cp_data)
|
||||
calls_node_load = [
|
||||
|
@ -606,13 +610,10 @@ class TestLoadBalancingPolicyOperations(base.SenlinTestCase):
|
|||
def test_post_op_add_nodes_failed(self, m_cluster_get, m_node_load,
|
||||
m_extract, m_load, m_conn):
|
||||
cluster_id = 'CLUSTER_ID'
|
||||
node1 = mock.Mock()
|
||||
node1.data = {}
|
||||
action = mock.Mock()
|
||||
action.data = {}
|
||||
action.data = {'creation': {'nodes': ['NODE1_ID']}}
|
||||
action.context = 'action_context'
|
||||
action.action = consts.CLUSTER_RESIZE
|
||||
node1 = mock.Mock(data={})
|
||||
action = mock.Mock(data={'creation': {'nodes': ['NODE1_ID']}},
|
||||
context='action_context',
|
||||
action=consts.CLUSTER_RESIZE)
|
||||
self.lb_driver.member_add.return_value = None
|
||||
m_node_load.side_effect = [node1]
|
||||
m_extract.return_value = {
|
||||
|
@ -637,22 +638,18 @@ class TestLoadBalancingPolicyOperations(base.SenlinTestCase):
|
|||
def test_pre_op_del_nodes_ok(self, m_cluster_get, m_node_load, m_extract,
|
||||
m_load, m_conn):
|
||||
cluster_id = 'CLUSTER_ID'
|
||||
cluster = mock.Mock()
|
||||
cluster = mock.Mock(user='user1', project='project1')
|
||||
m_cluster_get.return_value = cluster
|
||||
node1 = mock.Mock()
|
||||
node1.data = {'lb_member': 'MEMBER1_ID'}
|
||||
node2 = mock.Mock()
|
||||
node2.data = {'lb_member': 'MEMBER2_ID'}
|
||||
action = mock.Mock()
|
||||
action.data = {}
|
||||
action.data = {
|
||||
'deletion': {
|
||||
'count': 2,
|
||||
'candidates': ['NODE1_ID', 'NODE2_ID']
|
||||
}
|
||||
}
|
||||
action.context = 'action_context'
|
||||
action.action = consts.CLUSTER_DEL_NODES
|
||||
node1 = mock.Mock(data={'lb_member': 'MEMBER1_ID'})
|
||||
node2 = mock.Mock(data={'lb_member': 'MEMBER2_ID'})
|
||||
action = mock.Mock(
|
||||
context='action_context', action=consts.CLUSTER_DEL_NODES,
|
||||
data={
|
||||
'deletion': {
|
||||
'count': 2,
|
||||
'candidates': ['NODE1_ID', 'NODE2_ID']
|
||||
}
|
||||
})
|
||||
cp = mock.Mock()
|
||||
policy_data = {
|
||||
'loadbalancer': 'LB_ID',
|
||||
|
@ -662,8 +659,7 @@ class TestLoadBalancingPolicyOperations(base.SenlinTestCase):
|
|||
}
|
||||
cp_data = {
|
||||
'LoadBalancingPolicy': {
|
||||
'version': '1.0',
|
||||
'data': policy_data
|
||||
'version': '1.0', 'data': policy_data
|
||||
}
|
||||
}
|
||||
cp.data = cp_data
|
||||
|
@ -678,7 +674,7 @@ class TestLoadBalancingPolicyOperations(base.SenlinTestCase):
|
|||
|
||||
self.assertIsNone(res)
|
||||
m_cluster_get.assert_called_once_with('action_context', 'CLUSTER_ID')
|
||||
m_conn.assert_called_once_with(cluster)
|
||||
m_conn.assert_called_once_with('user1', 'project1')
|
||||
m_load.assert_called_once_with('action_context', cluster_id, policy.id)
|
||||
m_extract.assert_called_once_with(cp_data)
|
||||
calls_node_load = [
|
||||
|
|
|
@ -429,16 +429,14 @@ class TestPolicyBase(base.SenlinTestCase):
|
|||
}
|
||||
}
|
||||
|
||||
cluster = mock.Mock()
|
||||
cluster.user = 'user1'
|
||||
cluster.project = 'project1'
|
||||
cred = mock.Mock()
|
||||
cred.cred = cred_info
|
||||
cred = mock.Mock(cred=cred_info)
|
||||
mock_get_service_ctx.return_value = service_cred
|
||||
mock_get_current.return_value = current_ctx
|
||||
mock_cred_get.return_value = cred
|
||||
|
||||
policy = self._create_policy('test-policy')
|
||||
|
||||
res = policy._build_conn_params('user1', 'project1')
|
||||
|
||||
expected_result = {
|
||||
'auth_url': 'AUTH_URL',
|
||||
'username': 'senlin',
|
||||
|
@ -446,7 +444,6 @@ class TestPolicyBase(base.SenlinTestCase):
|
|||
'password': '123',
|
||||
'trust_id': 'TRUST_ID'
|
||||
}
|
||||
res = policy._build_conn_params(cluster)
|
||||
self.assertEqual(expected_result, res)
|
||||
mock_get_service_ctx.assert_called_once_with()
|
||||
mock_cred_get.assert_called_once_with(current_ctx, 'user1', 'project1')
|
||||
|
@ -466,12 +463,11 @@ class TestPolicyBase(base.SenlinTestCase):
|
|||
|
||||
mock_get_service_ctx.return_value = service_cred
|
||||
mock_cred_get.return_value = None
|
||||
cluster = mock.Mock()
|
||||
cluster.user = 'user1'
|
||||
cluster.project = 'project1'
|
||||
|
||||
policy = self._create_policy('test-policy')
|
||||
|
||||
ex = self.assertRaises(exception.TrustNotFound,
|
||||
policy._build_conn_params, cluster)
|
||||
policy._build_conn_params,
|
||||
'user1', 'project1')
|
||||
|
||||
msg = "The trust for trustor (user1) could not be found."
|
||||
self.assertEqual(msg, six.text_type(ex))
|
||||
|
|
|
@ -77,14 +77,14 @@ class TestRegionPlacementPolicy(base.SenlinTestCase):
|
|||
driver = mock.Mock()
|
||||
driver.identity.return_value = kc
|
||||
mock_sd.return_value = driver
|
||||
cluster = mock.Mock()
|
||||
cluster = mock.Mock(user='user1', project='project1')
|
||||
policy = rp.RegionPlacementPolicy('p1', self.spec)
|
||||
|
||||
res = policy._keystone(cluster)
|
||||
|
||||
self.assertEqual(kc, res)
|
||||
self.assertEqual(kc, policy._keystoneclient)
|
||||
mock_conn.assert_called_once_with(cluster)
|
||||
mock_conn.assert_called_once_with('user1', 'project1')
|
||||
mock_sd.assert_called_once_with()
|
||||
driver.identity.assert_called_once_with(params)
|
||||
|
||||
|
|
|
@ -62,14 +62,14 @@ class TestZonePlacementPolicy(base.SenlinTestCase):
|
|||
driver.compute.return_value = nc
|
||||
mock_driver.return_value = driver
|
||||
|
||||
cluster = mock.Mock()
|
||||
cluster = mock.Mock(user='user1', project='project1')
|
||||
policy = zp.ZonePlacementPolicy('p1', self.spec)
|
||||
|
||||
res = policy._nova(cluster)
|
||||
|
||||
self.assertEqual(nc, res)
|
||||
self.assertEqual(nc, policy._novaclient)
|
||||
mock_conn.assert_called_once_with(cluster)
|
||||
mock_conn.assert_called_once_with('user1', 'project1')
|
||||
mock_driver.assert_called_once_with()
|
||||
driver.compute.assert_called_once_with(params)
|
||||
|
||||
|
|
Loading…
Reference in New Issue