diff --git a/senlinclient/tests/unit/v1/test_shell.py b/senlinclient/tests/unit/v1/test_shell.py index 3d230b8d..4d325efe 100644 --- a/senlinclient/tests/unit/v1/test_shell.py +++ b/senlinclient/tests/unit/v1/test_shell.py @@ -760,10 +760,10 @@ class ShellTest(testtools.TestCase): args = self._make_args(args) node_ids = ['node1', 'node2'] resp = {'action': 'CLUSTER_NODE_ADD'} - client.cluster_add_nodes.return_value = resp + client.conn.cluster.cluster_add_nodes.return_value = resp sh.do_cluster_node_add(client, args) - client.cluster_add_nodes.assert_called_once_with('cluster_id', - node_ids) + client.conn.cluster.cluster_add_nodes.assert_called_once_with( + 'cluster_id', node_ids) def test_do_cluster_node_del(self): client = mock.Mock() @@ -774,10 +774,10 @@ class ShellTest(testtools.TestCase): args = self._make_args(args) node_ids = ['node1', 'node2'] resp = {'action': 'CLUSTER_NODE_DEL'} - client.cluster_del_nodes.return_value = resp + client.conn.cluster.cluster_del_nodes.return_value = resp sh.do_cluster_node_del(client, args) - client.cluster_del_nodes.assert_called_once_with('cluster_id', - node_ids) + client.conn.cluster.cluster_del_nodes.assert_called_once_with( + 'cluster_id', node_ids) def test_do_cluster_resize(self): client = mock.Mock() @@ -792,8 +792,7 @@ class ShellTest(testtools.TestCase): 'strict': True, } args = self._make_args(args) - ex = exc.CommandError - ex = self.assertRaises(ex, + ex = self.assertRaises(exc.CommandError, sh.do_cluster_resize, client, args) msg = _("Only one of 'capacity', 'adjustment' and " @@ -813,13 +812,14 @@ class ShellTest(testtools.TestCase): 'min_step': None, } resp = {'action': 'action_id'} - client.cluster_resize.return_value = resp + client.conn.cluster.cluster_resize.return_value = resp sh.do_cluster_resize(client, args) - client.cluster_resize.assert_called_with('cluster_id', **action_args) + client.conn.cluster.cluster_resize.assert_called_with( + 'cluster_id', **action_args) + # capacity is smaller than 0 args.capacity = -1 - ex = exc.CommandError - ex = self.assertRaises(ex, + ex = self.assertRaises(exc.CommandError, sh.do_cluster_resize, client, args) msg = _('Cluster capacity must be larger than ' @@ -833,11 +833,12 @@ class ShellTest(testtools.TestCase): action_args['adjustment_type'] = 'CHANGE_IN_CAPACITY' action_args['number'] = 1 sh.do_cluster_resize(client, args) - client.cluster_resize.assert_called_with('cluster_id', **action_args) + client.conn.cluster.cluster_resize.assert_called_with( + 'cluster_id', **action_args) + # adjustment is 0 args.adjustment = 0 - ex = exc.CommandError - ex = self.assertRaises(ex, + ex = self.assertRaises(exc.CommandError, sh.do_cluster_resize, client, args) msg = _('Adjustment cannot be zero.') @@ -850,11 +851,12 @@ class ShellTest(testtools.TestCase): action_args['adjustment_type'] = 'CHANGE_IN_PERCENTAGE' action_args['number'] = 50.0 sh.do_cluster_resize(client, args) - client.cluster_resize.assert_called_with('cluster_id', **action_args) + client.conn.cluster.cluster_resize.assert_called_with( + 'cluster_id', **action_args) + # percentage is 0 args.percentage = 0 - ex = exc.CommandError - ex = self.assertRaises(ex, + ex = self.assertRaises(exc.CommandError, sh.do_cluster_resize, client, args) msg = _('Percentage cannot be zero.') @@ -865,8 +867,7 @@ class ShellTest(testtools.TestCase): args.percentage = None args.adjustment = None args.min_step = 1 - ex = exc.CommandError - ex = self.assertRaises(ex, + ex = self.assertRaises(exc.CommandError, sh.do_cluster_resize, client, args) msg = _('Min step is only used with percentage.') @@ -878,12 +879,12 @@ class ShellTest(testtools.TestCase): args.adjustment = None args.min_step = None args.min_size = -1 - ex = exc.CommandError - ex = self.assertRaises(ex, + ex = self.assertRaises(exc.CommandError, sh.do_cluster_resize, client, args) msg = _('Min size cannot be less than zero.') self.assertEqual(msg, six.text_type(ex)) + # max_size < min_size args.capacity = 5 args.percentage = None @@ -891,12 +892,12 @@ class ShellTest(testtools.TestCase): args.min_step = None args.min_size = 5 args.max_size = 4 - ex = exc.CommandError - ex = self.assertRaises(ex, + ex = self.assertRaises(exc.CommandError, sh.do_cluster_resize, client, args) msg = _('Min size cannot be larger than max size.') self.assertEqual(msg, six.text_type(ex)) + # min_size > capacity args.capacity = 5 args.percentage = None @@ -904,12 +905,12 @@ class ShellTest(testtools.TestCase): args.min_step = None args.min_size = 6 args.max_size = 8 - ex = exc.CommandError - ex = self.assertRaises(ex, + ex = self.assertRaises(exc.CommandError, sh.do_cluster_resize, client, args) msg = _('Min size cannot be larger than the specified capacity') self.assertEqual(msg, six.text_type(ex)) + # max_size < capacity args.capacity = 5 args.percentage = None @@ -917,8 +918,7 @@ class ShellTest(testtools.TestCase): args.min_step = None args.min_size = 1 args.max_size = 4 - ex = exc.CommandError - ex = self.assertRaises(ex, + ex = self.assertRaises(exc.CommandError, sh.do_cluster_resize, client, args) msg = _('Max size cannot be less than the specified capacity.') @@ -932,9 +932,10 @@ class ShellTest(testtools.TestCase): } args = self._make_args(args) resp = {'action': 'action_id'} - client.cluster_scale_out.return_value = resp + client.conn.cluster.cluster_scale_out.return_value = resp sh.do_cluster_scale_out(client, args) - client.cluster_scale_out.assert_called_once_with('cluster_id', 3) + client.conn.cluster.cluster_scale_out.assert_called_once_with( + 'cluster_id', 3) def test_do_cluster_scale_in(self): client = mock.Mock() @@ -944,9 +945,10 @@ class ShellTest(testtools.TestCase): } args = self._make_args(args) resp = {'action': 'action_id'} - client.cluster_scale_in.return_value = resp + client.conn.cluster.cluster_scale_in.return_value = resp sh.do_cluster_scale_in(client, args) - client.cluster_scale_in.assert_called_once_with('cluster_id', 3) + client.conn.cluster.cluster_scale_in.assert_called_once_with( + 'cluster_id', 3) @mock.patch.object(utils, 'print_list') def test_do_cluster_policy_list(self, mock_print): @@ -1029,10 +1031,10 @@ class ShellTest(testtools.TestCase): 'enabled': 'True', } resp = {'action': 'action_id'} - client.cluster_attach_policy.return_value = resp + client.conn.cluster.cluster_attach_policy.return_value = resp sh.do_cluster_policy_attach(client, args) - client.cluster_attach_policy.assert_called_once_with('cluster1', - **kwargs) + client.conn.cluster.cluster_attach_policy.assert_called_once_with( + 'cluster1', **kwargs) def test_do_cluster_policy_detach(self): args = { @@ -1042,10 +1044,10 @@ class ShellTest(testtools.TestCase): client = mock.Mock() args = self._make_args(args) resp = {'action': 'action_id'} - client.cluster_detach_policy.return_value = resp + client.conn.cluster.cluster_detach_policy.return_value = resp sh.do_cluster_policy_detach(client, args) - client.cluster_detach_policy.assert_called_once_with('cluster1', - 'policy1') + client.conn.cluster.cluster_detach_policy.assert_called_once_with( + 'cluster1', 'policy1') def test_do_cluster_policy_update(self): client = mock.Mock() @@ -1066,10 +1068,10 @@ class ShellTest(testtools.TestCase): 'enabled': 'True', } resp = {'action': 'action_id'} - client.cluster_update_policy.return_value = resp + client.conn.cluster.cluster_update_policy.return_value = resp sh.do_cluster_policy_update(client, args) - client.cluster_update_policy.assert_called_once_with('cluster1', - **kwargs) + client.conn.cluster.cluster_update_policy.assert_called_once_with( + 'cluster1', **kwargs) def test_do_cluster_policy_enable(self): args = { @@ -1079,10 +1081,10 @@ class ShellTest(testtools.TestCase): args = self._make_args(args) client = mock.Mock() resp = {'action': 'action_id'} - client.cluster_enable_policy.return_value = resp + client.conn.cluster.cluster_enable_policy.return_value = resp sh.do_cluster_policy_enable(client, args) - client.cluster_enable_policy.assert_called_once_with('cluster1', - 'policy1') + client.conn.cluster.cluster_enable_policy.assert_called_once_with( + 'cluster1', 'policy1') def test_do_cluster_policy_disable(self): args = { @@ -1092,10 +1094,10 @@ class ShellTest(testtools.TestCase): args = self._make_args(args) client = mock.Mock() resp = {'action': 'action_id'} - client.cluster_disable_policy.return_value = resp + client.conn.cluster.cluster_disable_policy.return_value = resp sh.do_cluster_policy_disable(client, args) - client.cluster_disable_policy.assert_called_once_with('cluster1', - 'policy1') + client.conn.cluster.cluster_disable_policy.assert_called_once_with( + 'cluster1', 'policy1') @mock.patch.object(utils, 'print_list') def test_do_node_list(self, mock_print): diff --git a/senlinclient/v1/client.py b/senlinclient/v1/client.py index 364e0000..8b246587 100644 --- a/senlinclient/v1/client.py +++ b/senlinclient/v1/client.py @@ -89,53 +89,20 @@ class Client(object): return self.conn.cluster.delete_cluster(value, ignore_missing=ignore_missing) - def cluster_add_nodes(self, value, nodes): - params = { - 'id': value, - 'action': 'add_nodes', - 'action_args': { - 'nodes': nodes, - } - } - return self.action(models.Cluster, params) + def cluster_add_nodes(self, cluster, nodes): + return self.conn.cluster.cluster_add_nodes(cluster, nodes) - def cluster_del_nodes(self, value, nodes): - params = { - 'id': value, - 'action': 'del_nodes', - 'action_args': { - 'nodes': nodes, - } - } - return self.action(models.Cluster, params) + def cluster_del_nodes(self, cluster, nodes): + return self.conn.cluster.cluster_del_nodes(cluster, nodes) - def cluster_resize(self, value, **kwargs): - params = { - 'id': value, - 'action': 'resize', - 'action_args': kwargs, - } - return self.action(models.Cluster, params) + def cluster_resize(self, cluster, **params): + return self.conn.cluster.cluster_resize(cluster, **params) - def cluster_scale_out(self, value, count): - params = { - 'id': value, - 'action': 'scale_out', - 'action_args': { - 'count': count - } - } - return self.action(models.Cluster, params) + def cluster_scale_out(self, cluster, count): + return self.conn.cluster.cluster_scale_out(cluster, count) - def cluster_scale_in(self, value, count): - params = { - 'id': value, - 'action': 'scale_in', - 'action_args': { - 'count': count - } - } - return self.action(models.Cluster, params) + def cluster_scale_in(self, cluster, count): + return self.conn.cluster.cluster_scale_in(cluster, count) def cluster_policies(self, value, **queries): return self.list(models.ClusterPolicy, path_args={'cluster_id': value}, @@ -144,51 +111,22 @@ class Client(object): def get_cluster_policy(self, value): return self.get(models.ClusterPolicy, value) - def cluster_attach_policy(self, value, **kwargs): - params = { - 'id': value, - 'action': 'policy_attach', - 'action_args': kwargs - } - return self.action(models.Cluster, params) + def cluster_attach_policy(self, cluster, policy, **attrs): + return self.conn.cluster.cluster_attach_policy(cluster, policy, + **attrs) - def cluster_detach_policy(self, value, policy): - params = { - 'id': value, - 'action': 'policy_detach', - 'action_args': { - 'policy_id': policy, - } - } - return self.action(models.Cluster, params) + def cluster_detach_policy(self, cluster, policy): + return self.conn.cluster.cluster_detach_policy(cluster, policy) - def cluster_update_policy(self, value, **attrs): - params = { - 'id': value, - 'action': 'policy_update', - 'action_args': attrs - } - return self.action(models.Cluster, params) + def cluster_update_policy(self, cluster, policy, **attrs): + return self.conn.cluster.cluster_update_policy(cluster, policy, + **attrs) - def cluster_enable_policy(self, value, policy): - params = { - 'id': value, - 'action': 'policy_enable', - 'action_args': { - 'policy_id': policy - } - } - return self.action(models.Cluster, params) + def cluster_enable_policy(self, cluster, policy): + return self.conn.cluster.cluster_enable_policy(cluster, policy) - def cluster_disable_policy(self, value, policy): - params = { - 'id': value, - 'action': 'policy_disable', - 'action_args': { - 'policy_id': policy - } - } - return self.action(models.Cluster, params) + def cluster_disable_policy(self, cluster, policy): + return self.conn.cluster.cluster_disable_policy(cluster, policy) def nodes(self, **queries): return self.list(models.Node, **queries) diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py index 387407b0..d7b7f0c9 100644 --- a/senlinclient/v1/shell.py +++ b/senlinclient/v1/shell.py @@ -583,7 +583,7 @@ def do_cluster_node_list(sc, args): def do_cluster_node_add(sc, args): """Add specified nodes to cluster.""" node_ids = args.nodes.split(',') - resp = sc.cluster_add_nodes(args.id, node_ids) + resp = sc.conn.cluster.cluster_add_nodes(args.id, node_ids) print('Request accepted by action: %s' % resp['action']) @@ -595,7 +595,7 @@ def do_cluster_node_add(sc, args): def do_cluster_node_del(sc, args): """Delete specified nodes from cluster.""" node_ids = args.nodes.split(',') - resp = sc.cluster_del_nodes(args.id, node_ids) + resp = sc.conn.cluster.cluster_del_nodes(args.id, node_ids) print('Request accepted by action: %s' % resp['action']) @@ -689,7 +689,7 @@ def do_cluster_resize(sc, args): action_args['min_step'] = min_step action_args['strict'] = args.strict - resp = sc.cluster_resize(args.id, **action_args) + resp = sc.conn.cluster.cluster_resize(args.id, **action_args) print('Request accepted by action: %s' % resp['action']) @@ -699,7 +699,7 @@ def do_cluster_resize(sc, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_scale_out(sc, args): """Scale out a cluster by the specified number of nodes.""" - resp = sc.cluster_scale_out(args.id, args.count) + resp = sc.conn.cluster.cluster_scale_out(args.id, args.count) print('Request accepted by action %s' % resp['action']) @@ -709,7 +709,7 @@ def do_cluster_scale_out(sc, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_scale_in(sc, args): """Scale in a cluster by the specified number of nodes.""" - resp = sc.cluster_scale_in(args.id, args.count) + resp = sc.conn.cluster.cluster_scale_in(args.id, args.count) print('Request accepted by action %s' % resp['action']) @@ -801,7 +801,7 @@ def do_cluster_policy_attach(sc, args): 'enabled': args.enabled, } - resp = sc.cluster_attach_policy(args.id, **kwargs) + resp = sc.conn.cluster.cluster_attach_policy(args.id, **kwargs) print('Request accepted by action: %s' % resp['action']) @@ -811,7 +811,7 @@ def do_cluster_policy_attach(sc, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_policy_detach(sc, args): """Detach policy from cluster.""" - resp = sc.cluster_detach_policy(args.id, args.policy) + resp = sc.conn.cluster.cluster_detach_policy(args.id, args.policy) print('Request accepted by action %s' % resp['action']) @@ -839,7 +839,7 @@ def do_cluster_policy_update(sc, args): 'enabled': args.enabled, } - resp = sc.cluster_update_policy(args.id, **kwargs) + resp = sc.conn.cluster.cluster_update_policy(args.id, **kwargs) print('Request accepted by action: %s' % resp['action']) @@ -849,7 +849,7 @@ def do_cluster_policy_update(sc, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_policy_enable(sc, args): """Enable a policy on a cluster.""" - resp = sc.cluster_enable_policy(args.id, args.policy) + resp = sc.conn.cluster.cluster_enable_policy(args.id, args.policy) print('Request accepted by action: %s' % resp['action']) @@ -859,7 +859,7 @@ def do_cluster_policy_enable(sc, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_policy_disable(sc, args): """Disable a policy on a cluster.""" - resp = sc.cluster_disable_policy(args.id, args.policy) + resp = sc.conn.cluster.cluster_disable_policy(args.id, args.policy) print('Request accepted by action: %s' % resp['action'])