From ed8c1320aaebfe15c1503438ac6ab63c4967c239 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Fri, 18 Mar 2016 22:54:53 +0800 Subject: [PATCH 01/25] Spelling mistakes on 'Clustering service command-line client' page The word 'appened' is misspelled in senlin action-list of page http://docs.openstack.org/cli-reference/senlin.html. It should be 'appended'. Change-Id: Ie7b9f11a4eef83ce6514d880c1bdffded584c7f0 Closes-Bug: 1553871 --- senlinclient/v1/shell.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py index 95ea120..fd83428 100644 --- a/senlinclient/v1/shell.py +++ b/senlinclient/v1/shell.py @@ -83,8 +83,8 @@ def do_profile_type_show(service, args): help=_('Only return profiles that appear after the given ID.')) @utils.arg('-o', '--sort', metavar='', help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appened by ' - 'a sort direction (:asc or :desc)')) + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) @utils.arg('-g', '--global-project', default=False, action="store_true", help=_('Indicate that the list should include profiles from' ' all projects. This option is subject to access policy ' @@ -262,8 +262,8 @@ def do_policy_type_show(service, args): help=_('Only return policies that appear after the given ID.')) @utils.arg('-o', '--sort', metavar='', help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appened by ' - 'a sort direction (:asc or :desc)')) + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) @utils.arg('-g', '--global-project', default=False, action="store_true", help=_('Indicate that the list should include policies from' ' all projects. This option is subject to access policy ' @@ -373,8 +373,8 @@ def do_policy_delete(service, args): action='append') @utils.arg('-o', '--sort', metavar='', help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appened by ' - 'a sort direction (:asc or :desc)')) + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) @utils.arg('-l', '--limit', metavar='', help=_('Limit the number of clusters returned.')) @utils.arg('-m', '--marker', metavar='', @@ -693,8 +693,8 @@ def do_cluster_scale_in(service, args): action='append') @utils.arg('-o', '--sort', metavar='', help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appened by ' - 'a sort direction (:asc or :desc)')) + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) @utils.arg('-F', '--full-id', default=False, action="store_true", help=_('Print full IDs in list.')) @utils.arg('id', metavar='', @@ -808,8 +808,8 @@ def do_cluster_recover(service, args): action='append') @utils.arg('-o', '--sort', metavar='', help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appened by ' - 'a sort direction (:asc or :desc)')) + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) @utils.arg('-l', '--limit', metavar='', help=_('Limit the number of nodes returned.')) @utils.arg('-m', '--marker', metavar='', @@ -1010,8 +1010,8 @@ def do_node_recover(service, args): help=_('Only return receivers that appear after the given ID.')) @utils.arg('-o', '--sort', metavar='', help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appened by ' - 'a sort direction (:asc or :desc)')) + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) @utils.arg('-g', '--global-project', default=False, action="store_true", help=_('Indicate that the list should include receivers from' ' all projects. This option is subject to access policy ' @@ -1125,8 +1125,8 @@ def do_receiver_delete(service, args): help=_('Only return events that appear after the given event ID.')) @utils.arg('-o', '--sort', metavar='', help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appened by ' - 'a sort direction (:asc or :desc)')) + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) @utils.arg('-g', '--global-project', default=False, action="store_true", help=_('Whether events from all projects should be listed. ' ' Default to False. Setting this to True may demand ' @@ -1179,8 +1179,8 @@ def do_event_show(service, args): action='append') @utils.arg('-o', '--sort', metavar='', help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appened by ' - 'a sort direction (:asc or :desc)')) + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) @utils.arg('-l', '--limit', metavar='', help=_('Limit the number of actions returned.')) @utils.arg('-m', '--marker', metavar='', From decbc7cef712f6d4db6b666a9dee297f23c41ab0 Mon Sep 17 00:00:00 2001 From: xiaolihope Date: Mon, 21 Mar 2016 15:21:05 +0800 Subject: [PATCH 02/25] Add OSC command for senlin node-check/recover This change added two commands as OSC plugin: - openstack node check - openstack node recover Based on the existing senlin command: - senlin node-check - senlin node-recover Change-Id: I63261b8fa120c4f5a4c7b54178faec42ed967975 --- senlinclient/osc/v1/node.py | 54 +++++++++++++++++++++ senlinclient/tests/unit/osc/v1/test_node.py | 54 +++++++++++++++++++++ setup.cfg | 2 + 3 files changed, 110 insertions(+) diff --git a/senlinclient/osc/v1/node.py b/senlinclient/osc/v1/node.py index 387af28..9e2ac63 100644 --- a/senlinclient/osc/v1/node.py +++ b/senlinclient/osc/v1/node.py @@ -332,3 +332,57 @@ class DeleteNode(command.Command): {'count': failure_count, 'total': len(parsed_args.node)}) print('Request accepted') + + +class CheckNode(command.Command): + """Check the node(s).""" + log = logging.getLogger(__name__ + ".CheckNode") + + def get_parser(self, prog_name): + parser = super(CheckNode, self).get_parser(prog_name) + parser.add_argument( + 'node', + metavar='', + nargs='+', + help=_('ID or name of node(s) to check.') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + senlin_client = self.app.client_manager.clustering + for nid in parsed_args.node: + try: + resp = senlin_client.check_node(nid) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Node not found: %s') % nid) + print('Node check request on node %(nid)s is accepted by ' + 'action %(action)s.' + % {'nid': nid, 'action': resp['action']}) + + +class RecoverNode(command.Command): + """Recover the node(s).""" + log = logging.getLogger(__name__ + ".RecoverNode") + + def get_parser(self, prog_name): + parser = super(RecoverNode, self).get_parser(prog_name) + parser.add_argument( + 'node', + metavar='', + nargs='+', + help=_('ID or name of node(s) to recover.') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + senlin_client = self.app.client_manager.clustering + for nid in parsed_args.node: + try: + resp = senlin_client.recover_node(nid) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Node not found: %s') % nid) + print('Node recover request on node %(nid)s is accepted by ' + 'action %(action)s.' + % {'nid': nid, 'action': resp['action']}) diff --git a/senlinclient/tests/unit/osc/v1/test_node.py b/senlinclient/tests/unit/osc/v1/test_node.py index b7c84c0..afdd238 100644 --- a/senlinclient/tests/unit/osc/v1/test_node.py +++ b/senlinclient/tests/unit/osc/v1/test_node.py @@ -402,3 +402,57 @@ class TestNodeDelete(TestNode): mock_stdin.readline.assert_called_with() self.mock_client.delete_node.assert_not_called() + + +class TestNodeCheck(TestNode): + response = {"action": "8bb476c3-0f4c-44ee-9f64-c7b0260814de"} + + def setUp(self): + super(TestNodeCheck, self).setUp() + self.cmd = osc_node.CheckNode(self.app, None) + self.mock_client.check_node = mock.Mock( + return_value=self.response) + + def test_node_check(self): + arglist = ['node1', 'node2', 'node3'] + parsed_args = self.check_parser(self.cmd, arglist, []) + self.cmd.take_action(parsed_args) + self.mock_client.check_node.assert_has_calls( + [mock.call('node1'), mock.call('node2'), + mock.call('node3')] + ) + + def test_node_check_not_found(self): + arglist = ['node1'] + self.mock_client.check_node.side_effect = sdk_exc.ResourceNotFound + parsed_args = self.check_parser(self.cmd, arglist, []) + error = self.assertRaises(exc.CommandError, self.cmd.take_action, + parsed_args) + self.assertIn('Node not found: node1', str(error)) + + +class TestNodeRecover(TestNode): + response = {"action": "8bb476c3-0f4c-44ee-9f64-c7b0260814de"} + + def setUp(self): + super(TestNodeRecover, self).setUp() + self.cmd = osc_node.RecoverNode(self.app, None) + self.mock_client.recover_node = mock.Mock( + return_value=self.response) + + def test_node_recover(self): + arglist = ['node1', 'node2', 'node3'] + parsed_args = self.check_parser(self.cmd, arglist, []) + self.cmd.take_action(parsed_args) + self.mock_client.recover_node.assert_has_calls( + [mock.call('node1'), mock.call('node2'), + mock.call('node3')] + ) + + def test_node_recover_not_found(self): + arglist = ['node1'] + self.mock_client.recover_node.side_effect = sdk_exc.ResourceNotFound + parsed_args = self.check_parser(self.cmd, arglist, []) + error = self.assertRaises(exc.CommandError, self.cmd.take_action, + parsed_args) + self.assertIn('Node not found: node1', str(error)) diff --git a/setup.cfg b/setup.cfg index 6836168..0789a39 100644 --- a/setup.cfg +++ b/setup.cfg @@ -42,9 +42,11 @@ openstack.clustering.v1 = cluster_members_list = senlinclient.osc.v1.cluster:ClusterNodeList cluster_members_add = senlinclient.osc.v1.cluster:ClusterNodeAdd cluster_members_del = senlinclient.osc.v1.cluster:ClusterNodeDel + cluster_node_check = senlinclient.osc.v1.node:CheckNode cluster_node_create = senlinclient.osc.v1.node:CreateNode cluster_node_delete = senlinclient.osc.v1.node:DeleteNode cluster_node_list = senlinclient.osc.v1.node:ListNode + cluster_node_recover = senlinclient.osc.v1.node:RecoverNode cluster_node_show = senlinclient.osc.v1.node:ShowNode cluster_node_update = senlinclient.osc.v1.node:UpdateNode cluster_policy_attach = senlinclient.osc.v1.cluster:ClusterPolicyAttach From 040dcc8eba92f386a5b8a590834497a5ed445d38 Mon Sep 17 00:00:00 2001 From: xiaolihope Date: Mon, 21 Mar 2016 14:53:12 +0800 Subject: [PATCH 03/25] Add deprecation warnings for senlin commands Some new senlin openstack commands are a little different with the senlin commands, and users need to use openstack commands instead of senlin commands. So, This change added the deprecation warnings. Change-Id: I866680e358f99047164c9d318a16755ac6d17278 --- senlinclient/tests/unit/v1/test_shell.py | 8 +++ senlinclient/v1/shell.py | 80 ++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/senlinclient/tests/unit/v1/test_shell.py b/senlinclient/tests/unit/v1/test_shell.py index 01b19bd..e2d1ef2 100644 --- a/senlinclient/tests/unit/v1/test_shell.py +++ b/senlinclient/tests/unit/v1/test_shell.py @@ -16,6 +16,7 @@ import six import testtools from openstack import exceptions as oexc +from oslotest import mockpatch from senlinclient.common import exc from senlinclient.common.i18n import _ from senlinclient.common import utils @@ -39,6 +40,13 @@ class ShellTest(testtools.TestCase): 'template': {"Template": "data"} } } + self.patch('senlinclient.v1.shell.show_deprecated') + + # NOTE(pshchelo): this overrides the testtools.TestCase.patch method + # that does simple monkey-patching in favor of mock's patching + def patch(self, target, **kwargs): + mockfixture = self.useFixture(mockpatch.Patch(target, **kwargs)) + return mockfixture.mock def _make_args(self, args): """Convert a dict to an object.""" diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py index fd83428..6ef78e3 100644 --- a/senlinclient/v1/shell.py +++ b/senlinclient/v1/shell.py @@ -15,17 +15,27 @@ import logging from openstack import exceptions as sdk_exc from senlinclient.common import exc from senlinclient.common.i18n import _ +from senlinclient.common.i18n import _LW from senlinclient.common import utils logger = logging.getLogger(__name__) +def show_deprecated(deprecated, recommended): + logger.warning(_LW('"%(old)s" is deprecated, ' + 'please use "%(new)s" instead.'), + {'old': deprecated, + 'new': recommended} + ) + + def do_build_info(service, args=None): """Retrieve build information. :param sc: Instance of senlinclient. :param args: Additional command line arguments, if any. """ + show_deprecated('senlin build-info', 'openstack cluster build info') result = service.get_build_info() formatters = { @@ -44,6 +54,8 @@ def do_profile_type_list(service, args=None): :param sc: Instance of senlinclient. :param args: Additional command line arguments, if any. """ + show_deprecated('senlin profile-type-list', + 'openstack cluster profile type list') types = service.profile_types() utils.print_list(types, ['name'], sortby_index=0) @@ -56,6 +68,8 @@ def do_profile_type_list(service, args=None): % ', '.join(utils.supported_formats.keys())) def do_profile_type_show(service, args): """Get the details about a profile type.""" + show_deprecated('senlin profile-type-show', + 'openstack cluster profile type show') try: res = service.get_profile_type(args.type_name) except sdk_exc.ResourceNotFound: @@ -93,6 +107,7 @@ def do_profile_type_show(service, args): help=_('Print full IDs in list.')) def do_profile_list(service, args=None): """List profiles that meet the criteria.""" + show_deprecated('senlin profile-list', 'openstack cluster profile list') fields = ['id', 'name', 'type', 'created_at'] queries = { 'limit': args.limit, @@ -143,7 +158,8 @@ def _show_profile(service, profile_id): help=_('Name of the profile to create.')) def do_profile_create(service, args): """Create a profile.""" - + show_deprecated('senlin profile-create', + 'openstack cluster profile create') spec = utils.get_spec_content(args.spec_file) type_name = spec.get('type', None) type_version = spec.get('version', None) @@ -173,6 +189,7 @@ def do_profile_create(service, args): help=_('Name or ID of profile to show.')) def do_profile_show(service, args): """Show the profile details.""" + show_deprecated('senlin profile-show', 'openstack cluster profile show') _show_profile(service, args.id) @@ -187,6 +204,8 @@ def do_profile_show(service, args): help=_('Name or ID of the profile to update.')) def do_profile_update(service, args): """Update a profile.""" + show_deprecated('senlin profile-update', + 'openstack cluster profile update') params = { 'name': args.name, } @@ -206,6 +225,8 @@ def do_profile_update(service, args): help=_('Name or ID of profile(s) to delete.')) def do_profile_delete(service, args): """Delete profile(s).""" + show_deprecated('senlin profile-delete', + 'openstack cluster profile delete') failure_count = 0 for pid in args.id: @@ -225,6 +246,8 @@ def do_profile_delete(service, args): def do_policy_type_list(service, args): """List the available policy types.""" + show_deprecated('senlin policy-type-list', + 'openstack cluster policy type list') types = service.policy_types() utils.print_list(types, ['name'], sortby_index=0) @@ -237,6 +260,8 @@ def do_policy_type_list(service, args): % ', '.join(utils.supported_formats.keys())) def do_policy_type_show(service, args): """Get the details about a policy type.""" + show_deprecated('senlin policy-type-show', + 'openstack cluster policy type show') try: res = service.get_policy_type(args.type_name) except sdk_exc.ResourceNotFound: @@ -272,6 +297,7 @@ def do_policy_type_show(service, args): help=_('Print full IDs in list.')) def do_policy_list(service, args=None): """List policies that meet the criteria.""" + show_deprecated('senlin policy-list', 'openstack cluster policy list') fields = ['id', 'name', 'type', 'created_at'] queries = { 'limit': args.limit, @@ -312,6 +338,7 @@ def _show_policy(service, policy_id): help=_('Name of the policy to create.')) def do_policy_create(service, args): """Create a policy.""" + show_deprecated('senlin policy-create', 'openstack cluster policy create') spec = utils.get_spec_content(args.spec_file) attrs = { 'name': args.name, @@ -326,6 +353,7 @@ def do_policy_create(service, args): help=_('Name of the policy to be updated.')) def do_policy_show(service, args): """Show the policy details.""" + show_deprecated('senlin policy-show', 'openstack cluster policy show') _show_policy(service, policy_id=args.id) @@ -335,6 +363,7 @@ def do_policy_show(service, args): help=_('Name of the policy to be updated.')) def do_policy_update(service, args): """Update a policy.""" + show_deprecated('senlin policy-update', 'openstack cluster policy update') params = { 'name': args.name, } @@ -349,6 +378,7 @@ def do_policy_update(service, args): help=_('Name or ID of policy(s) to delete.')) def do_policy_delete(service, args): """Delete policy(s).""" + show_deprecated('senlin policy-delete', 'openstack cluster policy delete') failure_count = 0 for pid in args.id: @@ -388,6 +418,7 @@ def do_policy_delete(service, args): help=_('Print full IDs in list.')) def do_cluster_list(service, args=None): """List the user's clusters.""" + show_deprecated('senlin cluster-list', 'openstack cluster list') fields = ['id', 'name', 'status', 'created_at', 'updated_at'] queries = { 'limit': args.limit, @@ -443,6 +474,7 @@ def _show_cluster(service, cluster_id): help=_('Name of the cluster to create.')) def do_cluster_create(service, args): """Create the cluster.""" + show_deprecated('senlin cluster-create', 'openstack cluster create') if args.min_size and not args.desired_capacity: args.desired_capacity = args.min_size attrs = { @@ -463,6 +495,7 @@ def do_cluster_create(service, args): help=_('Name or ID of cluster(s) to delete.')) def do_cluster_delete(service, args): """Delete the cluster(s).""" + show_deprecated('senlin cluster-delete', 'openstack cluster delete') failure_count = 0 for cid in args.id: @@ -492,6 +525,7 @@ def do_cluster_delete(service, args): help=_('Name or ID of cluster to be updated.')) def do_cluster_update(service, args): """Update the cluster.""" + show_deprecated('senlin cluster-update', 'openstack cluster update') cluster = service.get_cluster(args.id) attrs = { 'name': args.name, @@ -508,6 +542,7 @@ def do_cluster_update(service, args): help=_('Name or ID of cluster to show.')) def do_cluster_show(service, args): """Show details of the cluster.""" + show_deprecated('senlin cluster-show', 'openstack cluster show') _show_cluster(service, args.id) @@ -526,7 +561,8 @@ def do_cluster_show(service, args): help=_('Name or ID of cluster to nodes from.')) def do_cluster_node_list(service, args): """List nodes from cluster.""" - + show_deprecated('senlin cluster-node-list', + 'openstack cluster node members list') queries = { 'cluster_id': args.id, 'limit': args.limit, @@ -555,6 +591,8 @@ def do_cluster_node_list(service, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_node_add(service, args): """Add specified nodes to cluster.""" + show_deprecated('senlin cluster-node-add', + 'openstack cluster node members add') node_ids = args.nodes.split(',') resp = service.cluster_add_nodes(args.id, node_ids) print('Request accepted by action: %s' % resp['action']) @@ -567,6 +605,8 @@ def do_cluster_node_add(service, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_node_del(service, args): """Delete specified nodes from cluster.""" + show_deprecated('senlin cluster-node-del', + 'openstack cluster node members del') node_ids = args.nodes.split(',') resp = service.cluster_del_nodes(args.id, node_ids) print('Request accepted by action: %s' % resp['action']) @@ -599,7 +639,7 @@ def do_cluster_resize(service, args): """Resize a cluster.""" # validate parameters # NOTE: this will be much simpler if cliutils supports exclusive groups - + show_deprecated('senlin cluster-resize', 'openstack cluster resize') action_args = {} capacity = args.capacity @@ -672,6 +712,7 @@ def do_cluster_resize(service, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_scale_out(service, args): """Scale out a cluster by the specified number of nodes.""" + show_deprecated('senlin cluster-scale-out', 'openstack cluster scale out') resp = service.cluster_scale_out(args.id, args.count) print('Request accepted by action %s' % resp['action']) @@ -682,6 +723,7 @@ def do_cluster_scale_out(service, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_scale_in(service, args): """Scale in a cluster by the specified number of nodes.""" + show_deprecated('senlin cluster-scale-in', 'openstack cluster scale in') resp = service.cluster_scale_in(args.id, args.count) print('Request accepted by action %s' % resp['action']) @@ -701,6 +743,8 @@ def do_cluster_scale_in(service, args): help=_('Name or ID of cluster to query on.')) def do_cluster_policy_list(service, args): """List policies from cluster.""" + show_deprecated('senlin cluster-policy-list', + 'openstack cluster policy binding list') fields = ['policy_id', 'policy_name', 'policy_type', 'enabled'] cluster = service.get_cluster(args.id) @@ -729,6 +773,8 @@ def do_cluster_policy_list(service, args): help=_('ID or name of the cluster to query on.')) def do_cluster_policy_show(service, args): """Show a specific policy that is bound to the specified cluster.""" + show_deprecated('senlin cluster-policy-show', + 'openstack cluster policy binding show') binding = service.get_cluster_policy(args.policy, args.id) utils.print_dict(binding.to_dict()) @@ -742,6 +788,8 @@ def do_cluster_policy_show(service, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_policy_attach(service, args): """Attach policy to cluster.""" + show_deprecated('senlin cluster-policy-attach', + 'openstack cluster policy attach') kwargs = { 'enabled': args.enabled, } @@ -756,6 +804,8 @@ def do_cluster_policy_attach(service, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_policy_detach(service, args): """Detach policy from cluster.""" + show_deprecated('senlin cluster-policy-detach', + 'openstack cluster policy detach') resp = service.cluster_detach_policy(args.id, args.policy) print('Request accepted by action %s' % resp['action']) @@ -768,6 +818,8 @@ def do_cluster_policy_detach(service, args): help=_('Name or ID of cluster to operate on.')) def do_cluster_policy_update(service, args): """Update a policy's properties on a cluster.""" + show_deprecated('senlin cluster-policy-update', + 'openstack cluster policy binding update') kwargs = { 'enabled': args.enabled, } @@ -780,6 +832,7 @@ def do_cluster_policy_update(service, args): help=_('ID or name of cluster(s) to operate on.')) def do_cluster_check(service, args): """Check the cluster(s).""" + show_deprecated('senlin cluster-check', 'openstack cluster check') for cid in args.id: resp = service.check_cluster(cid) print('Cluster check request on cluster %(cid)s is accepted by ' @@ -790,6 +843,7 @@ def do_cluster_check(service, args): help=_('ID or name of cluster(s) to operate on.')) def do_cluster_recover(service, args): """Recover the cluster(s).""" + show_deprecated('senlin cluster-recover', 'openstack cluster recover') for cid in args.id: resp = service.recover_cluster(cid) print('Cluster recover request on cluster %(cid)s is accepted by ' @@ -822,6 +876,7 @@ def do_cluster_recover(service, args): help=_('Print full IDs in list.')) def do_node_list(service, args): """Show list of nodes.""" + show_deprecated('senlin node-list', 'openstack cluster node list') fields = ['id', 'name', 'index', 'status', 'cluster_id', 'physical_id', 'profile_name', 'created_at', 'updated_at'] @@ -855,7 +910,6 @@ def do_node_list(service, args): def _show_node(service, node_id, show_details=False): """Show detailed info about the specified node.""" - args = {'show_details': True} if show_details else None try: node = service.get_node(node_id, args=args) @@ -889,6 +943,7 @@ def _show_node(service, node_id, show_details=False): help=_('Name of the node to create.')) def do_node_create(service, args): """Create the node.""" + show_deprecated('senlin node-create', 'openstack cluster node create') attrs = { 'name': args.name, 'cluster_id': args.cluster, @@ -907,6 +962,7 @@ def do_node_create(service, args): help=_('Name or ID of the node to show the details for.')) def do_node_show(service, args): """Show detailed info about the specified node.""" + show_deprecated('senlin node-show', 'openstack cluster node show') _show_node(service, args.id, args.details) @@ -914,6 +970,7 @@ def do_node_show(service, args): help=_('Name or ID of node(s) to delete.')) def do_node_delete(service, args): """Delete the node(s).""" + show_deprecated('senlin node-delete', 'openstack cluster node delete') failure_count = 0 for nid in args.id: @@ -943,6 +1000,7 @@ def do_node_delete(service, args): help=_('Name or ID of node to update.')) def do_node_update(service, args): """Update the node.""" + show_deprecated('senlin node-update', 'openstack cluster node update') # Find the node first, we need its UUID try: node = service.get_node(args.id) @@ -964,6 +1022,7 @@ def do_node_update(service, args): help=_('ID of node(s) to check.')) def do_node_check(service, args): """Check the node(s).""" + show_deprecated('senlin node-check', 'openstack cluster node check') failure_count = 0 for nid in args.id: @@ -982,6 +1041,7 @@ def do_node_check(service, args): help=_('ID of node(s) to recover.')) def do_node_recover(service, args): """Recover the node(s).""" + show_deprecated('senlin node-recover', 'openstack cluster node recover') failure_count = 0 for nid in args.id: @@ -1020,6 +1080,7 @@ def do_node_recover(service, args): help=_('Print full IDs in list.')) def do_receiver_list(service, args): """List receivers that meet the criteria.""" + show_deprecated('senlin receiver-list', 'openstack cluster receiver list') fields = ['id', 'name', 'type', 'cluster_id', 'action', 'created_at'] queries = { 'limit': args.limit, @@ -1063,6 +1124,7 @@ def _show_receiver(service, receiver_id): help=_('Name or ID of the receiver to show.')) def do_receiver_show(service, args): """Show the receiver details.""" + show_deprecated('senlin receiver-show', 'openstack cluster receiver show') _show_receiver(service, receiver_id=args.id) @@ -1080,6 +1142,8 @@ def do_receiver_show(service, args): help=_('Name of the receiver to create.')) def do_receiver_create(service, args): """Create a receiver.""" + show_deprecated('senlin receiver-create', + 'openstack cluster receiver create') params = { 'name': args.name, @@ -1097,6 +1161,8 @@ def do_receiver_create(service, args): help=_('Name or ID of receiver(s) to delete.')) def do_receiver_delete(service, args): """Delete receiver(s).""" + show_deprecated('senlin receiver-delete', + 'openstack cluster receiver delete') failure_count = 0 for wid in args.id: @@ -1135,7 +1201,7 @@ def do_receiver_delete(service, args): help=_('Print full IDs in list.')) def do_event_list(service, args): """List events.""" - + show_deprecated('senlin event-list', 'openstack cluster event list') fields = ['id', 'timestamp', 'obj_type', 'obj_id', 'obj_name', 'action', 'status', 'status_reason', 'level'] queries = { @@ -1161,6 +1227,7 @@ def do_event_list(service, args): help=_('ID of event to display details for.')) def do_event_show(service, args): """Describe the event.""" + show_deprecated('senlin event-show', 'openstack cluster event show') try: event = service.get_event(args.id) except sdk_exc.ResourceNotFound: @@ -1189,7 +1256,7 @@ def do_event_show(service, args): help=_('Print full IDs in list.')) def do_action_list(service, args): """List actions.""" - + show_deprecated('senlin action-list', 'openstack cluster action list') fields = ['id', 'name', 'action', 'status', 'target', 'depends_on', 'depended_by', 'created_at'] @@ -1229,6 +1296,7 @@ def do_action_list(service, args): help=_('Name or ID of the action to show the details for.')) def do_action_show(service, args): """Show detailed info about the specified action.""" + show_deprecated('senlin action-show', 'openstack cluster action show') try: action = service.get_action(args.id) except sdk_exc.ResourceNotFound: From 735b018198ab620857273e501d8126278056326e Mon Sep 17 00:00:00 2001 From: tengqm Date: Thu, 24 Mar 2016 22:30:22 -0400 Subject: [PATCH 04/25] Refactor osc support This patch refactors the OSC (OpenStackClient) support so that they are moved out of a sub directory. We will use it as the 'default' CLI while deprecating the previous shell. Change-Id: I2553f0f38e5fa30735be63ab00dec4b3a4080ba5 --- senlinclient/osc/__init__.py | 0 senlinclient/osc/v1/__init__.py | 0 senlinclient/{osc => }/plugin.py | 0 senlinclient/tests/unit/osc/__init__.py | 0 senlinclient/tests/unit/osc/v1/__init__.py | 0 senlinclient/tests/unit/osc/v1/fakes.py | 33 ------- senlinclient/tests/unit/{osc => v1}/fakes.py | 19 ++++ .../tests/unit/{osc => }/v1/test_action.py | 4 +- .../unit/{osc => }/v1/test_build_info.py | 4 +- .../tests/unit/{osc => }/v1/test_cluster.py | 4 +- .../unit/{osc => }/v1/test_cluster_policy.py | 4 +- .../tests/unit/{osc => }/v1/test_event.py | 4 +- .../tests/unit/{osc => }/v1/test_node.py | 4 +- .../tests/unit/{osc => }/v1/test_policy.py | 4 +- .../unit/{osc => }/v1/test_policy_type.py | 4 +- .../tests/unit/{osc => }/v1/test_profile.py | 4 +- .../unit/{osc => }/v1/test_profile_type.py | 4 +- .../tests/unit/{osc => }/v1/test_receiver.py | 4 +- senlinclient/{osc => }/v1/action.py | 0 senlinclient/{osc => }/v1/build_info.py | 0 senlinclient/{osc => }/v1/cluster.py | 0 senlinclient/{osc => }/v1/cluster_policy.py | 0 senlinclient/{osc => }/v1/event.py | 0 senlinclient/{osc => }/v1/node.py | 0 senlinclient/{osc => }/v1/policy.py | 0 senlinclient/{osc => }/v1/policy_type.py | 0 senlinclient/{osc => }/v1/profile.py | 0 senlinclient/{osc => }/v1/profile_type.py | 0 senlinclient/{osc => }/v1/receiver.py | 0 setup.cfg | 98 +++++++++---------- 30 files changed, 90 insertions(+), 104 deletions(-) delete mode 100644 senlinclient/osc/__init__.py delete mode 100644 senlinclient/osc/v1/__init__.py rename senlinclient/{osc => }/plugin.py (100%) delete mode 100644 senlinclient/tests/unit/osc/__init__.py delete mode 100644 senlinclient/tests/unit/osc/v1/__init__.py delete mode 100644 senlinclient/tests/unit/osc/v1/fakes.py rename senlinclient/tests/unit/{osc => v1}/fakes.py (89%) rename senlinclient/tests/unit/{osc => }/v1/test_action.py (98%) rename senlinclient/tests/unit/{osc => }/v1/test_build_info.py (92%) rename senlinclient/tests/unit/{osc => }/v1/test_cluster.py (99%) rename senlinclient/tests/unit/{osc => }/v1/test_cluster_policy.py (97%) rename senlinclient/tests/unit/{osc => }/v1/test_event.py (98%) rename senlinclient/tests/unit/{osc => }/v1/test_node.py (99%) rename senlinclient/tests/unit/{osc => }/v1/test_policy.py (99%) rename senlinclient/tests/unit/{osc => }/v1/test_policy_type.py (96%) rename senlinclient/tests/unit/{osc => }/v1/test_profile.py (99%) rename senlinclient/tests/unit/{osc => }/v1/test_profile_type.py (96%) rename senlinclient/tests/unit/{osc => }/v1/test_receiver.py (99%) rename senlinclient/{osc => }/v1/action.py (100%) rename senlinclient/{osc => }/v1/build_info.py (100%) rename senlinclient/{osc => }/v1/cluster.py (100%) rename senlinclient/{osc => }/v1/cluster_policy.py (100%) rename senlinclient/{osc => }/v1/event.py (100%) rename senlinclient/{osc => }/v1/node.py (100%) rename senlinclient/{osc => }/v1/policy.py (100%) rename senlinclient/{osc => }/v1/policy_type.py (100%) rename senlinclient/{osc => }/v1/profile.py (100%) rename senlinclient/{osc => }/v1/profile_type.py (100%) rename senlinclient/{osc => }/v1/receiver.py (100%) diff --git a/senlinclient/osc/__init__.py b/senlinclient/osc/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/senlinclient/osc/v1/__init__.py b/senlinclient/osc/v1/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/senlinclient/osc/plugin.py b/senlinclient/plugin.py similarity index 100% rename from senlinclient/osc/plugin.py rename to senlinclient/plugin.py diff --git a/senlinclient/tests/unit/osc/__init__.py b/senlinclient/tests/unit/osc/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/senlinclient/tests/unit/osc/v1/__init__.py b/senlinclient/tests/unit/osc/v1/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/senlinclient/tests/unit/osc/v1/fakes.py b/senlinclient/tests/unit/osc/v1/fakes.py deleted file mode 100644 index 19a8363..0000000 --- a/senlinclient/tests/unit/osc/v1/fakes.py +++ /dev/null @@ -1,33 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# 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 mock - -from openstackclient.tests import utils -from senlinclient.tests.unit.osc import fakes - - -class FakeClusteringv1Client(object): - def __init__(self, **kwargs): - self.http_client = mock.Mock() - self.http_client.auth_token = kwargs['token'] - self.profiles = fakes.FakeResource(None, {}) - - -class TestClusteringv1(utils.TestCommand): - def setUp(self): - super(TestClusteringv1, self).setUp() - - self.app.client_manager.clustering = FakeClusteringv1Client( - token=fakes.AUTH_TOKEN, - auth_url=fakes.AUTH_URL - ) diff --git a/senlinclient/tests/unit/osc/fakes.py b/senlinclient/tests/unit/v1/fakes.py similarity index 89% rename from senlinclient/tests/unit/osc/fakes.py rename to senlinclient/tests/unit/v1/fakes.py index f6d4718..d591454 100644 --- a/senlinclient/tests/unit/osc/fakes.py +++ b/senlinclient/tests/unit/v1/fakes.py @@ -11,10 +11,13 @@ # under the License. import json +import mock import requests import six import sys +from openstackclient.tests import utils + AUTH_TOKEN = "foobar" AUTH_URL = "http://0.0.0.0" @@ -158,3 +161,19 @@ class FakeResponse(requests.Response): self._content = json.dumps(data) if not isinstance(self._content, six.binary_type): self._content = self._content.encode() + + +class FakeClusteringv1Client(object): + def __init__(self, **kwargs): + self.http_client = mock.Mock() + self.http_client.auth_token = kwargs['token'] + self.profiles = FakeResource(None, {}) + + +class TestClusteringv1(utils.TestCommand): + def setUp(self): + super(TestClusteringv1, self).setUp() + + self.app.client_manager.clustering = FakeClusteringv1Client( + token=AUTH_TOKEN, auth_url=AUTH_URL + ) diff --git a/senlinclient/tests/unit/osc/v1/test_action.py b/senlinclient/tests/unit/v1/test_action.py similarity index 98% rename from senlinclient/tests/unit/osc/v1/test_action.py rename to senlinclient/tests/unit/v1/test_action.py index f2df79a..21927dc 100644 --- a/senlinclient/tests/unit/osc/v1/test_action.py +++ b/senlinclient/tests/unit/v1/test_action.py @@ -17,8 +17,8 @@ from openstack.cluster.v1 import action as sdk_action from openstack import exceptions as sdk_exc from openstackclient.common import exceptions as exc -from senlinclient.osc.v1 import action as osc_action -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import action as osc_action class TestAction(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_build_info.py b/senlinclient/tests/unit/v1/test_build_info.py similarity index 92% rename from senlinclient/tests/unit/osc/v1/test_build_info.py rename to senlinclient/tests/unit/v1/test_build_info.py index 1bf8e58..ba78774 100644 --- a/senlinclient/tests/unit/osc/v1/test_build_info.py +++ b/senlinclient/tests/unit/v1/test_build_info.py @@ -14,8 +14,8 @@ import mock from openstack.cluster.v1 import build_info as sdk_build_info -from senlinclient.osc.v1 import build_info as osc_build_info -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import build_info as osc_build_info class TestBuildInfo(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_cluster.py b/senlinclient/tests/unit/v1/test_cluster.py similarity index 99% rename from senlinclient/tests/unit/osc/v1/test_cluster.py rename to senlinclient/tests/unit/v1/test_cluster.py index 093417d..6495ea0 100644 --- a/senlinclient/tests/unit/osc/v1/test_cluster.py +++ b/senlinclient/tests/unit/v1/test_cluster.py @@ -18,8 +18,8 @@ from openstack.cluster.v1 import cluster as sdk_cluster from openstack import exceptions as sdk_exc from openstackclient.common import exceptions as exc -from senlinclient.osc.v1 import cluster as osc_cluster -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import cluster as osc_cluster class TestCluster(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_cluster_policy.py b/senlinclient/tests/unit/v1/test_cluster_policy.py similarity index 97% rename from senlinclient/tests/unit/osc/v1/test_cluster_policy.py rename to senlinclient/tests/unit/v1/test_cluster_policy.py index 2d3d3cf..c034552 100644 --- a/senlinclient/tests/unit/osc/v1/test_cluster_policy.py +++ b/senlinclient/tests/unit/v1/test_cluster_policy.py @@ -14,8 +14,8 @@ import mock from openstack.cluster.v1 import cluster_policy as sdk_cluster_policy -from senlinclient.osc.v1 import cluster_policy as osc_cluster_policy -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import cluster_policy as osc_cluster_policy class TestClusterPolicy(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_event.py b/senlinclient/tests/unit/v1/test_event.py similarity index 98% rename from senlinclient/tests/unit/osc/v1/test_event.py rename to senlinclient/tests/unit/v1/test_event.py index 06eb434..7cd2f6a 100644 --- a/senlinclient/tests/unit/osc/v1/test_event.py +++ b/senlinclient/tests/unit/v1/test_event.py @@ -17,8 +17,8 @@ from openstack.cluster.v1 import event as sdk_event from openstack import exceptions as sdk_exc from openstackclient.common import exceptions as exc -from senlinclient.osc.v1 import event as osc_event -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import event as osc_event class TestEvent(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_node.py b/senlinclient/tests/unit/v1/test_node.py similarity index 99% rename from senlinclient/tests/unit/osc/v1/test_node.py rename to senlinclient/tests/unit/v1/test_node.py index afdd238..6bfdada 100644 --- a/senlinclient/tests/unit/osc/v1/test_node.py +++ b/senlinclient/tests/unit/v1/test_node.py @@ -18,8 +18,8 @@ from openstack.cluster.v1 import node as sdk_node from openstack import exceptions as sdk_exc from openstackclient.common import exceptions as exc -from senlinclient.osc.v1 import node as osc_node -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import node as osc_node class TestNode(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_policy.py b/senlinclient/tests/unit/v1/test_policy.py similarity index 99% rename from senlinclient/tests/unit/osc/v1/test_policy.py rename to senlinclient/tests/unit/v1/test_policy.py index c6920de..350a7d9 100644 --- a/senlinclient/tests/unit/osc/v1/test_policy.py +++ b/senlinclient/tests/unit/v1/test_policy.py @@ -18,8 +18,8 @@ from openstack.cluster.v1 import policy as sdk_policy from openstack import exceptions as sdk_exc from openstackclient.common import exceptions as exc -from senlinclient.osc.v1 import policy as osc_policy -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import policy as osc_policy class TestPolicy(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_policy_type.py b/senlinclient/tests/unit/v1/test_policy_type.py similarity index 96% rename from senlinclient/tests/unit/osc/v1/test_policy_type.py rename to senlinclient/tests/unit/v1/test_policy_type.py index 84506f7..956dc16 100644 --- a/senlinclient/tests/unit/osc/v1/test_policy_type.py +++ b/senlinclient/tests/unit/v1/test_policy_type.py @@ -16,8 +16,8 @@ from openstack.cluster.v1 import policy_type as sdk_policy_type from openstack import exceptions as sdk_exc from openstackclient.common import exceptions as exc -from senlinclient.osc.v1 import policy_type as osc_policy_type -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import policy_type as osc_policy_type class TestPolicyType(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_profile.py b/senlinclient/tests/unit/v1/test_profile.py similarity index 99% rename from senlinclient/tests/unit/osc/v1/test_profile.py rename to senlinclient/tests/unit/v1/test_profile.py index b81d1ed..d84e0d0 100644 --- a/senlinclient/tests/unit/osc/v1/test_profile.py +++ b/senlinclient/tests/unit/v1/test_profile.py @@ -19,8 +19,8 @@ from openstack import exceptions as sdk_exc from openstackclient.common import exceptions as exc from openstackclient.common import utils -from senlinclient.osc.v1 import profile as osc_profile -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import profile as osc_profile class TestProfile(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_profile_type.py b/senlinclient/tests/unit/v1/test_profile_type.py similarity index 96% rename from senlinclient/tests/unit/osc/v1/test_profile_type.py rename to senlinclient/tests/unit/v1/test_profile_type.py index 9826d16..5a1fa0f 100644 --- a/senlinclient/tests/unit/osc/v1/test_profile_type.py +++ b/senlinclient/tests/unit/v1/test_profile_type.py @@ -16,8 +16,8 @@ from openstack.cluster.v1 import profile_type as sdk_profile_type from openstack import exceptions as sdk_exc from openstackclient.common import exceptions as exc -from senlinclient.osc.v1 import profile_type as osc_profile_type -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import profile_type as osc_profile_type class TestProfileType(fakes.TestClusteringv1): diff --git a/senlinclient/tests/unit/osc/v1/test_receiver.py b/senlinclient/tests/unit/v1/test_receiver.py similarity index 99% rename from senlinclient/tests/unit/osc/v1/test_receiver.py rename to senlinclient/tests/unit/v1/test_receiver.py index 98fed37..053bdc7 100644 --- a/senlinclient/tests/unit/osc/v1/test_receiver.py +++ b/senlinclient/tests/unit/v1/test_receiver.py @@ -18,8 +18,8 @@ from openstack.cluster.v1 import receiver as sdk_receiver from openstack import exceptions as sdk_exc from openstackclient.common import exceptions as exc -from senlinclient.osc.v1 import receiver as osc_receiver -from senlinclient.tests.unit.osc.v1 import fakes +from senlinclient.tests.unit.v1 import fakes +from senlinclient.v1 import receiver as osc_receiver class TestReceiver(fakes.TestClusteringv1): diff --git a/senlinclient/osc/v1/action.py b/senlinclient/v1/action.py similarity index 100% rename from senlinclient/osc/v1/action.py rename to senlinclient/v1/action.py diff --git a/senlinclient/osc/v1/build_info.py b/senlinclient/v1/build_info.py similarity index 100% rename from senlinclient/osc/v1/build_info.py rename to senlinclient/v1/build_info.py diff --git a/senlinclient/osc/v1/cluster.py b/senlinclient/v1/cluster.py similarity index 100% rename from senlinclient/osc/v1/cluster.py rename to senlinclient/v1/cluster.py diff --git a/senlinclient/osc/v1/cluster_policy.py b/senlinclient/v1/cluster_policy.py similarity index 100% rename from senlinclient/osc/v1/cluster_policy.py rename to senlinclient/v1/cluster_policy.py diff --git a/senlinclient/osc/v1/event.py b/senlinclient/v1/event.py similarity index 100% rename from senlinclient/osc/v1/event.py rename to senlinclient/v1/event.py diff --git a/senlinclient/osc/v1/node.py b/senlinclient/v1/node.py similarity index 100% rename from senlinclient/osc/v1/node.py rename to senlinclient/v1/node.py diff --git a/senlinclient/osc/v1/policy.py b/senlinclient/v1/policy.py similarity index 100% rename from senlinclient/osc/v1/policy.py rename to senlinclient/v1/policy.py diff --git a/senlinclient/osc/v1/policy_type.py b/senlinclient/v1/policy_type.py similarity index 100% rename from senlinclient/osc/v1/policy_type.py rename to senlinclient/v1/policy_type.py diff --git a/senlinclient/osc/v1/profile.py b/senlinclient/v1/profile.py similarity index 100% rename from senlinclient/osc/v1/profile.py rename to senlinclient/v1/profile.py diff --git a/senlinclient/osc/v1/profile_type.py b/senlinclient/v1/profile_type.py similarity index 100% rename from senlinclient/osc/v1/profile_type.py rename to senlinclient/v1/profile_type.py diff --git a/senlinclient/osc/v1/receiver.py b/senlinclient/v1/receiver.py similarity index 100% rename from senlinclient/osc/v1/receiver.py rename to senlinclient/v1/receiver.py diff --git a/setup.cfg b/setup.cfg index 0789a39..65a456d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,57 +27,57 @@ console_scripts = senlin = senlinclient.shell:main openstack.cli.extension = - clustering = senlinclient.osc.plugin + clustering = senlinclient.plugin openstack.clustering.v1 = - cluster_action_list = senlinclient.osc.v1.action:ListAction - cluster_action_show = senlinclient.osc.v1.action:ShowAction - cluster_build_info = senlinclient.osc.v1.build_info:BuildInfo - cluster_check = senlinclient.osc.v1.cluster:CheckCluster - cluster_create = senlinclient.osc.v1.cluster:CreateCluster - cluster_delete = senlinclient.osc.v1.cluster:DeleteCluster - cluster_event_list = senlinclient.osc.v1.event:ListEvent - cluster_event_show = senlinclient.osc.v1.event:ShowEvent - cluster_list = senlinclient.osc.v1.cluster:ListCluster - cluster_members_list = senlinclient.osc.v1.cluster:ClusterNodeList - cluster_members_add = senlinclient.osc.v1.cluster:ClusterNodeAdd - cluster_members_del = senlinclient.osc.v1.cluster:ClusterNodeDel - cluster_node_check = senlinclient.osc.v1.node:CheckNode - cluster_node_create = senlinclient.osc.v1.node:CreateNode - cluster_node_delete = senlinclient.osc.v1.node:DeleteNode - cluster_node_list = senlinclient.osc.v1.node:ListNode - cluster_node_recover = senlinclient.osc.v1.node:RecoverNode - cluster_node_show = senlinclient.osc.v1.node:ShowNode - cluster_node_update = senlinclient.osc.v1.node:UpdateNode - cluster_policy_attach = senlinclient.osc.v1.cluster:ClusterPolicyAttach - cluster_policy_binding_list = senlinclient.osc.v1.cluster_policy:ClusterPolicyList - cluster_policy_binding_show = senlinclient.osc.v1.cluster_policy:ClusterPolicyShow - cluster_policy_binding_update = senlinclient.osc.v1.cluster_policy:ClusterPolicyUpdate - cluster_policy_create = senlinclient.osc.v1.policy:CreatePolicy - cluster_policy_delete = senlinclient.osc.v1.policy:DeletePolicy - cluster_policy_detach = senlinclient.osc.v1.cluster:ClusterPolicyDetach - cluster_policy_list = senlinclient.osc.v1.policy:ListPolicy - cluster_policy_show = senlinclient.osc.v1.policy:ShowPolicy - cluster_policy_type_list = senlinclient.osc.v1.policy_type:PolicyTypeList - cluster_policy_type_show = senlinclient.osc.v1.policy_type:PolicyTypeShow - cluster_policy_update = senlinclient.osc.v1.policy:UpdatePolicy - cluster_profile_create = senlinclient.osc.v1.profile:CreateProfile - cluster_profile_delete = senlinclient.osc.v1.profile:DeleteProfile - cluster_profile_list = senlinclient.osc.v1.profile:ListProfile - cluster_profile_show = senlinclient.osc.v1.profile:ShowProfile - cluster_profile_type_list = senlinclient.osc.v1.profile_type:ProfileTypeList - cluster_profile_type_show = senlinclient.osc.v1.profile_type:ProfileTypeShow - cluster_profile_update = senlinclient.osc.v1.profile:UpdateProfile - cluster_receiver_create = senlinclient.osc.v1.receiver:CreateReceiver - cluster_receiver_delete = senlinclient.osc.v1.receiver:DeleteReceiver - cluster_receiver_list = senlinclient.osc.v1.receiver:ListReceiver - cluster_receiver_show = senlinclient.osc.v1.receiver:ShowReceiver - cluster_recover = senlinclient.osc.v1.cluster:RecoverCluster - cluster_resize = senlinclient.osc.v1.cluster:ResizeCluster - cluster_scale_in = senlinclient.osc.v1.cluster:ScaleInCluster - cluster_scale_out = senlinclient.osc.v1.cluster:ScaleOutCluster - cluster_show = senlinclient.osc.v1.cluster:ShowCluster - cluster_update = senlinclient.osc.v1.cluster:UpdateCluster + cluster_action_list = senlinclient.v1.action:ListAction + cluster_action_show = senlinclient.v1.action:ShowAction + cluster_build_info = senlinclient.v1.build_info:BuildInfo + cluster_check = senlinclient.v1.cluster:CheckCluster + cluster_create = senlinclient.v1.cluster:CreateCluster + cluster_delete = senlinclient.v1.cluster:DeleteCluster + cluster_event_list = senlinclient.v1.event:ListEvent + cluster_event_show = senlinclient.v1.event:ShowEvent + cluster_list = senlinclient.v1.cluster:ListCluster + cluster_members_list = senlinclient.v1.cluster:ClusterNodeList + cluster_members_add = senlinclient.v1.cluster:ClusterNodeAdd + cluster_members_del = senlinclient.v1.cluster:ClusterNodeDel + cluster_node_check = senlinclient.v1.node:CheckNode + cluster_node_create = senlinclient.v1.node:CreateNode + cluster_node_delete = senlinclient.v1.node:DeleteNode + cluster_node_list = senlinclient.v1.node:ListNode + cluster_node_recover = senlinclient.v1.node:RecoverNode + cluster_node_show = senlinclient.v1.node:ShowNode + cluster_node_update = senlinclient.v1.node:UpdateNode + cluster_policy_attach = senlinclient.v1.cluster:ClusterPolicyAttach + cluster_policy_binding_list = senlinclient.v1.cluster_policy:ClusterPolicyList + cluster_policy_binding_show = senlinclient.v1.cluster_policy:ClusterPolicyShow + cluster_policy_binding_update = senlinclient.v1.cluster_policy:ClusterPolicyUpdate + cluster_policy_create = senlinclient.v1.policy:CreatePolicy + cluster_policy_delete = senlinclient.v1.policy:DeletePolicy + cluster_policy_detach = senlinclient.v1.cluster:ClusterPolicyDetach + cluster_policy_list = senlinclient.v1.policy:ListPolicy + cluster_policy_show = senlinclient.v1.policy:ShowPolicy + cluster_policy_type_list = senlinclient.v1.policy_type:PolicyTypeList + cluster_policy_type_show = senlinclient.v1.policy_type:PolicyTypeShow + cluster_policy_update = senlinclient.v1.policy:UpdatePolicy + cluster_profile_create = senlinclient.v1.profile:CreateProfile + cluster_profile_delete = senlinclient.v1.profile:DeleteProfile + cluster_profile_list = senlinclient.v1.profile:ListProfile + cluster_profile_show = senlinclient.v1.profile:ShowProfile + cluster_profile_type_list = senlinclient.v1.profile_type:ProfileTypeList + cluster_profile_type_show = senlinclient.v1.profile_type:ProfileTypeShow + cluster_profile_update = senlinclient.v1.profile:UpdateProfile + cluster_receiver_create = senlinclient.v1.receiver:CreateReceiver + cluster_receiver_delete = senlinclient.v1.receiver:DeleteReceiver + cluster_receiver_list = senlinclient.v1.receiver:ListReceiver + cluster_receiver_show = senlinclient.v1.receiver:ShowReceiver + cluster_recover = senlinclient.v1.cluster:RecoverCluster + cluster_resize = senlinclient.v1.cluster:ResizeCluster + cluster_scale_in = senlinclient.v1.cluster:ScaleInCluster + cluster_scale_out = senlinclient.v1.cluster:ScaleOutCluster + cluster_show = senlinclient.v1.cluster:ShowCluster + cluster_update = senlinclient.v1.cluster:UpdateCluster [global] setup-hooks = From 3bda3b997bf3701746c7a2e4760f9528a18a2cbd Mon Sep 17 00:00:00 2001 From: tengqm Date: Fri, 25 Mar 2016 01:07:01 -0400 Subject: [PATCH 05/25] Remove senlin CLI commands We are deprecating the senlin CLI in favor of the OSC-plugin way of command line support. This patch removes shell support from the code base. Change-Id: I1ff2f4643ab9f8e719abad9b7cca3c2a60fbffc2 --- senlinclient/cliargs.py | 210 ---- senlinclient/shell.py | 321 ------ senlinclient/tests/unit/test_cliargs.py | 78 -- senlinclient/tests/unit/test_shell.py | 373 ------ senlinclient/tests/unit/v1/test_shell.py | 1348 ---------------------- senlinclient/v1/shell.py | 1314 --------------------- setup.cfg | 3 - 7 files changed, 3647 deletions(-) delete mode 100644 senlinclient/cliargs.py delete mode 100644 senlinclient/shell.py delete mode 100644 senlinclient/tests/unit/test_cliargs.py delete mode 100644 senlinclient/tests/unit/test_shell.py delete mode 100644 senlinclient/tests/unit/v1/test_shell.py delete mode 100644 senlinclient/v1/shell.py diff --git a/senlinclient/cliargs.py b/senlinclient/cliargs.py deleted file mode 100644 index 694d017..0000000 --- a/senlinclient/cliargs.py +++ /dev/null @@ -1,210 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# 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 argparse - -from senlinclient.common.i18n import _ -from senlinclient.common import sdk -from senlinclient.common import utils - - -def add_global_identity_args(parser): - parser.add_argument( - '--os-auth-plugin', dest='auth_plugin', metavar='AUTH_PLUGIN', - default=utils.env('OS_AUTH_PLUGIN', default=None), - help=_('Authentication plugin, default to env[OS_AUTH_PLUGIN]')) - - parser.add_argument( - '--os-auth-url', dest='auth_url', metavar='AUTH_URL', - default=utils.env('OS_AUTH_URL'), - help=_('Defaults to env[OS_AUTH_URL]')) - - parser.add_argument( - '--os-project-id', dest='project_id', metavar='PROJECT_ID', - default=utils.env('OS_PROJECT_ID'), - help=_('Defaults to env[OS_PROJECT_ID].')) - - parser.add_argument( - '--os-project-name', dest='project_name', metavar='PROJECT_NAME', - default=utils.env('OS_PROJECT_NAME'), - help=_('Defaults to env[OS_PROJECT_NAME].')) - - parser.add_argument( - '--os-tenant-id', dest='tenant_id', metavar='TENANT_ID', - default=utils.env('OS_TENANT_ID'), - help=_('Defaults to env[OS_TENANT_ID].')) - - parser.add_argument( - '--os-tenant-name', dest='tenant_name', metavar='TENANT_NAME', - default=utils.env('OS_TENANT_NAME'), - help=_('Defaults to env[OS_TENANT_NAME].')) - - parser.add_argument( - '--os-domain-id', dest='domain_id', metavar='DOMAIN_ID', - default=utils.env('OS_DOMAIN_ID'), - help=_('Domain ID for scope of authorization, defaults to ' - 'env[OS_DOMAIN_ID].')) - - parser.add_argument( - '--os-domain-name', dest='domain_name', metavar='DOMAIN_NAME', - default=utils.env('OS_DOMAIN_NAME'), - help=_('Domain name for scope of authorization, defaults to ' - 'env[OS_DOMAIN_NAME].')) - - parser.add_argument( - '--os-project-domain-id', dest='project_domain_id', - metavar='PROJECT_DOMAIN_ID', - default=utils.env('OS_PROJECT_DOMAIN_ID'), - help=_('Project domain ID for scope of authorization, defaults to ' - 'env[OS_PROJECT_DOMAIN_ID].')) - - parser.add_argument( - '--os-project-domain-name', dest='project_domain_name', - metavar='PROJECT_DOMAIN_NAME', - default=utils.env('OS_PROJECT_DOMAIN_NAME'), - help=_('Project domain name for scope of authorization, defaults to ' - 'env[OS_PROJECT_DOMAIN_NAME].')) - - parser.add_argument( - '--os-user-domain-id', dest='user_domain_id', - metavar='USER_DOMAIN_ID', - default=utils.env('OS_USER_DOMAIN_ID'), - help=_('User domain ID for scope of authorization, defaults to ' - 'env[OS_USER_DOMAIN_ID].')) - - parser.add_argument( - '--os-user-domain-name', dest='user_domain_name', - metavar='USER_DOMAIN_NAME', - default=utils.env('OS_USER_DOMAIN_NAME'), - help=_('User domain name for scope of authorization, defaults to ' - 'env[OS_USER_DOMAIN_NAME].')) - - parser.add_argument( - '--os-username', dest='username', metavar='USERNAME', - default=utils.env('OS_USERNAME'), - help=_('Defaults to env[OS_USERNAME].')) - - parser.add_argument( - '--os-user-id', dest='user_id', metavar='USER_ID', - default=utils.env('OS_USER_ID'), - help=_('Defaults to env[OS_USER_ID].')) - - parser.add_argument( - '--os-password', dest='password', metavar='PASSWORD', - default=utils.env('OS_PASSWORD'), - help=_('Defaults to env[OS_PASSWORD]')) - - parser.add_argument( - '--os-trust-id', dest='trust_id', metavar='TRUST_ID', - default=utils.env('OS_TRUST_ID'), - help=_('Defaults to env[OS_TRUST_ID]')) - - verify_group = parser.add_mutually_exclusive_group() - - verify_group.add_argument( - '--os-cacert', dest='verify', metavar='CA_BUNDLE_FILE', - default=utils.env('OS_CACERT', default=True), - help=_('Path of CA TLS certificate(s) used to verify the remote ' - 'server\'s certificate. Without this option senlin looks ' - 'for the default system CA certificates.')) - - verify_group.add_argument( - '--verify', - action='store_true', - help=_('Verify server certificate (default)')) - - verify_group.add_argument( - '--insecure', dest='verify', action='store_false', - help=_('Explicitly allow senlinclient to perform "insecure SSL" ' - '(HTTPS) requests. The server\'s certificate will not be ' - 'verified against any certificate authorities. This ' - 'option should be used with caution.')) - - parser.add_argument( - '--os-token', dest='token', metavar='TOKEN', - default=utils.env('OS_TOKEN', default=None), - help=_('A string token to bootstrap the Keystone database, defaults ' - 'to env[OS_TOKEN]')) - - parser.add_argument( - '--os-access-info', dest='access_info', metavar='ACCESS_INFO', - default=utils.env('OS_ACCESS_INFO'), - help=_('Access info, defaults to env[OS_ACCESS_INFO]')) - - parser.add_argument( - '--os-api-name', dest='user_preferences', - metavar='=', - action=sdk.ProfileAction, - default=sdk.ProfileAction.env('OS_API_NAME'), - help=_('Desired API names, defaults to env[OS_API_NAME]')) - - parser.add_argument( - '--os-api-region', dest='user_preferences', - metavar='=', - action=sdk.ProfileAction, - default=sdk.ProfileAction.env('OS_API_REGION', 'OS_REGION_NAME'), - help=_('Desired API region, defaults to env[OS_API_REGION]')) - - parser.add_argument( - '--os-api-version', dest='user_preferences', - metavar='=', - action=sdk.ProfileAction, - default=sdk.ProfileAction.env('OS_API_VERSION'), - help=_('Desired API versions, defaults to env[OS_API_VERSION]')) - - parser.add_argument( - '--os-api-interface', dest='user_preferences', - metavar='=', - action=sdk.ProfileAction, - default=sdk.ProfileAction.env('OS_INTERFACE'), - help=_('Desired API interface, defaults to env[OS_INTERFACE]')) - - -# parser.add_argument( -# '--os-cert', -# help=_('Path of certificate file to use in SSL connection. This ' -# 'file can optionally be prepended with the private key.')) -# -# parser.add_argument( -# '--os-key', -# help=_('Path of client key to use in SSL connection. This option is ' -# 'not necessary if your key is prepended to your cert file.')) - - -def add_global_args(parser, version): - # GLOBAL ARGUMENTS - parser.add_argument( - '-h', '--help', action='store_true', - help=argparse.SUPPRESS) - - parser.add_argument( - '--version', action='version', version=version, - help=_("Shows the client version and exits.")) - - parser.add_argument( - '-d', '--debug', action='store_true', - default=bool(utils.env('SENLINCLIENT_DEBUG')), - help=_('Defaults to env[SENLINCLIENT_DEBUG].')) - - parser.add_argument( - '-v', '--verbose', action="store_true", default=False, - help=_("Print more verbose output.")) - - parser.add_argument( - '--api-timeout', - help=_('Number of seconds to wait for an API response, ' - 'defaults to system socket timeout')) - - parser.add_argument( - '--senlin-api-version', - default=utils.env('SENLIN_API_VERSION', default='1'), - help=_('Version number for Senlin API to use, Default to "1".')) diff --git a/senlinclient/shell.py b/senlinclient/shell.py deleted file mode 100644 index 5954982..0000000 --- a/senlinclient/shell.py +++ /dev/null @@ -1,321 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -""" -Command-line interface to the Senlin clustering API. -""" - -from __future__ import print_function - -import argparse -import logging -import sys - -from oslo_utils import encodeutils -from oslo_utils import importutils -import six - -import senlinclient -from senlinclient import cliargs -from senlinclient import client as senlin_client -from senlinclient.common import exc -from senlinclient.common.i18n import _ -from senlinclient.common import utils - -osprofiler_profiler = importutils.try_import("osprofiler.profiler") -USER_AGENT = 'python-senlinclient' -LOG = logging.getLogger(__name__) - - -class HelpFormatter(argparse.HelpFormatter): - def start_section(self, heading): - # Title-case the headings - heading = '%s%s' % (heading[0].upper(), heading[1:]) - super(HelpFormatter, self).start_section(heading) - - -class SenlinShell(object): - def _setup_logging(self, debug): - log_lvl = logging.DEBUG if debug else logging.WARNING - logging.basicConfig(format="%(levelname)s (%(module)s) %(message)s", - level=log_lvl) - logging.getLogger('iso8601').setLevel(logging.WARNING) - logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING) - - def _setup_verbose(self, verbose): - if verbose: - exc.verbose = 1 - - def _find_actions(self, subparsers, actions_module): - for attr in (a for a in dir(actions_module) if a.startswith('do_')): - command = attr[3:].replace('_', '-') - callback = getattr(actions_module, attr) - - # get callback documentation string - desc = callback.__doc__ or '' - help = desc.strip().split('\n')[0] - arguments = getattr(callback, 'arguments', []) - - subparser = subparsers.add_parser(command, - help=help, - description=desc, - add_help=False, - formatter_class=HelpFormatter) - - subparser.add_argument('-h', '--help', - action='help', - help=argparse.SUPPRESS) - - for (args, kwargs) in arguments: - subparser.add_argument(*args, **kwargs) - subparser.set_defaults(func=callback) - - self.subcommands[command] = subparser - - def do_bash_completion(self, args): - """Prints all of the commands and options to stdout. - - The senlin.bash_completion script doesn't have to hard code them. - """ - commands = set() - options = set() - for sc_str, sc in self.subcommands.items(): - if sc_str == 'bash_completion' or sc_str == 'bash-completion': - continue - - commands.add(sc_str) - for option in list(sc._optionals._option_string_actions): - options.add(option) - - print(' '.join(commands | options)) - - def add_profiler_args(self, parser): - if osprofiler_profiler: - parser.add_argument( - '--profile', metavar='HMAC_KEY', - help=_('HMAC key to use for encrypting context data for ' - 'performance profiling of operation. This key should ' - 'be the value of HMAC key configured in osprofiler ' - 'middleware in senlin, it is specified in the paste ' - 'deploy configuration (/etc/senlin/api-paste.ini). ' - 'Without the key, profiling will not be triggered ' - 'even if osprofiler is enabled on server side.')) - - def _add_bash_completion_subparser(self, subparsers): - subparser = subparsers.add_parser('bash_completion', - add_help=False, - formatter_class=HelpFormatter) - - subparser.set_defaults(func=self.do_bash_completion) - self.subcommands['bash_completion'] = subparser - - def get_subcommand_parser(self, base_parser, version): - parser = base_parser - - self.subcommands = {} - subparsers = parser.add_subparsers(metavar='') - submodule = utils.import_versioned_module(version, 'shell') - self._find_actions(subparsers, submodule) - self._find_actions(subparsers, self) - self._add_bash_completion_subparser(subparsers) - - return parser - - @utils.arg('command', metavar='', nargs='?', - help=_('Display help for .')) - def do_help(self, args): - """Display help about this program or one of its subcommands.""" - if getattr(args, 'command', None): - if args.command in self.subcommands: - self.subcommands[args.command].print_help() - else: - raise exc.CommandError("'%s' is not a valid subcommand" % - args.command) - else: - self.parser.print_help() - - def _check_identity_arguments(self, args): - # TODO(Qiming): validate the token authentication path and the trust - # authentication path - - if not args.auth_url: - msg = _('You must provide an auth url via --os-auth-url (or ' - ' env[OS_AUTH_URL])') - raise exc.CommandError(msg) - - # username or user_id or token must be specified - if not (args.username or args.user_id or args.token): - msg = _('You must provide a user name, a user_id or a ' - 'token for authentication') - raise exc.CommandError(msg) - - # if both username and user_id are specified, user_id takes precedence - if (args.username and args.user_id): - msg = _('Both user name and user ID are specified, Senlin will ' - 'use user ID for authentication') - print(_('WARNING: %s') % msg) - - if 'v3' in args.auth_url: - if (args.username and not args.user_id): - if not (args.user_domain_id or args.user_domain_name): - msg = _('Either user domain ID (--user-domain-id / ' - 'env[OS_USER_DOMAIN_ID]) or user domain name ' - '(--user-domain-name / env[OS_USER_DOMAIN_NAME]) ' - 'must be specified, because user name may not be ' - 'unique.') - raise exc.CommandError(msg) - - # password is needed if username or user_id is present - if (args.username or args.user_id) and not (args.password): - msg = _('You must provide a password for user %s') % ( - args.username or args.user_id) - raise exc.CommandError(msg) - - # project name or ID is needed, or else sdk may find the wrong project - if (not (args.project_id or args.project_name or args.tenant_id - or args.tenant_name)): - if not (args.user_id): - msg = _('Either project/tenant ID or project/tenant name ' - 'must be specified, or else Senlin cannot know ' - 'which project to use.') - raise exc.CommandError(msg) - else: - msg = _('Neither project ID nor project name is specified. ' - 'Senlin will use user\'s default project which may ' - 'result in authentication error.') - print(_('WARNING: %s') % msg) - - # both project name and ID are specified, ID takes precedence - if ((args.project_id or args.tenant_id) and - (args.project_name or args.tenant_name)): - msg = _('Both project/tenant name and project/tenant ID are ' - 'specified, Senlin will use project ID for ' - 'authentication') - print(_('WARNING: %s') % msg) - - # project name may not be unique - if 'v3' in args.auth_url: - if (not (args.project_id or args.tenant_id) and - (args.project_name or args.tenant_name) and - not (args.project_domain_id or args.project_domain_name)): - msg = _('Either project domain ID (--project-domain-id / ' - 'env[OS_PROJECT_DOMAIN_ID]) orr project domain name ' - '(--project-domain-name / env[OS_PROJECT_DOMAIN_NAME ' - 'must be specified, because project/tenant name may ' - 'not be unique.') - raise exc.CommandError(msg) - - def _setup_senlin_client(self, api_ver, args): - """Create senlin client using given args.""" - kwargs = { - 'auth_plugin': args.auth_plugin or 'password', - 'auth_url': args.auth_url, - 'project_name': args.project_name or args.tenant_name, - 'project_id': args.project_id or args.tenant_id, - 'domain_name': args.domain_name, - 'domain_id': args.domain_id, - 'project_domain_name': args.project_domain_name, - 'project_domain_id': args.project_domain_id, - 'user_domain_name': args.user_domain_name, - 'user_domain_id': args.user_domain_id, - 'username': args.username, - 'user_id': args.user_id, - 'password': args.password, - 'verify': args.verify, - 'token': args.token, - 'trust_id': args.trust_id, - } - - return senlin_client.Client('1', args.user_preferences, USER_AGENT, - **kwargs) - - def main(self, argv): - # Parse args once to find version - parser = argparse.ArgumentParser( - prog='senlin', - description=__doc__.strip(), - epilog=_('Type "senlin help " for help on a specific ' - 'command.'), - add_help=False, - formatter_class=HelpFormatter, - ) - - cliargs.add_global_args(parser, version=senlinclient.__version__) - cliargs.add_global_identity_args(parser) - self.add_profiler_args(parser) - base_parser = parser - - (options, args) = base_parser.parse_known_args(argv) - - self._setup_logging(options.debug) - self._setup_verbose(options.verbose) - - # build available subcommands based on version - api_ver = options.senlin_api_version - LOG.info(api_ver) - subcommand_parser = self.get_subcommand_parser(base_parser, api_ver) - self.parser = subcommand_parser - - # Handle top-level --help/-h before attempting to parse - # a command off the command line - if not args and options.help or not argv: - self.do_help(options) - return 0 - - # Parse args again and call whatever callback was selected - args = subcommand_parser.parse_args(argv) - - # Short-circuit and deal with help command right away. - if args.func == self.do_help: - self.do_help(args) - return 0 - elif args.func == self.do_bash_completion: - self.do_bash_completion(args) - return 0 - - # Check if identity information are sufficient - self._check_identity_arguments(args) - - # Setup Senlin client connection - sc = self._setup_senlin_client(api_ver, args) - - profile = osprofiler_profiler and options.profile - if profile: - osprofiler_profiler.init(options.profile) - - args.func(sc.service, args) - - if profile: - trace_id = osprofiler_profiler.get().get_base_id() - print(_("Trace ID: %s") % trace_id) - print(_("To display trace use next command:\n" - "osprofiler trace show --html %s ") % trace_id) - - -def main(args=None): - try: - if args is None: - args = sys.argv[1:] - - SenlinShell().main(args) - except KeyboardInterrupt: - print(_("... terminating senlin client"), file=sys.stderr) - sys.exit(130) - except Exception as e: - if '--debug' in args or '-d' in args: - raise - else: - print(encodeutils.safe_encode(six.text_type(e)), file=sys.stderr) - sys.exit(1) - -if __name__ == "__main__": - main() diff --git a/senlinclient/tests/unit/test_cliargs.py b/senlinclient/tests/unit/test_cliargs.py deleted file mode 100644 index 8bc6df8..0000000 --- a/senlinclient/tests/unit/test_cliargs.py +++ /dev/null @@ -1,78 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# 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 mock -import testtools - -from senlinclient import cliargs - - -class TestCliArgs(testtools.TestCase): - - def test_add_global_identity_args(self): - parser = mock.Mock() - - cliargs.add_global_identity_args(parser) - expected = [ - '--os-auth-plugin', - '--os-auth-url', - '--os-project-id', - '--os-project-name', - '--os-tenant-id', - '--os-tenant-name', - '--os-domain-id', - '--os-domain-name', - '--os-project-domain-id', - '--os-project-domain-name', - '--os-user-domain-id', - '--os-user-domain-name', - '--os-username', - '--os-user-id', - '--os-password', - '--os-trust-id', - '--os-token', - '--os-access-info', - '--os-api-name', - '--os-api-region', - '--os-api-version', - '--os-api-interface' - ] - - options = [arg[0][0] for arg in parser.add_argument.call_args_list] - self.assertEqual(expected, options) - - parser.add_mutually_exclusive_group.assert_called_once_with() - group = parser.add_mutually_exclusive_group.return_value - - verify_opts = [arg[0][0] for arg in group.add_argument.call_args_list] - verify_args = [ - '--os-cacert', - '--verify', - '--insecure' - ] - self.assertEqual(verify_args, verify_opts) - - def test_add_global_args(self): - parser = mock.Mock() - - cliargs.add_global_args(parser, '1') - expected = [ - '-h', - '--version', - '-d', - '-v', - '--api-timeout', - '--senlin-api-version' - ] - - options = [arg[0][0] for arg in parser.add_argument.call_args_list] - self.assertEqual(expected, options) diff --git a/senlinclient/tests/unit/test_shell.py b/senlinclient/tests/unit/test_shell.py deleted file mode 100644 index dbeaaf7..0000000 --- a/senlinclient/tests/unit/test_shell.py +++ /dev/null @@ -1,373 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# 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 argparse -import logging -import sys - -import mock -import six -from six.moves import builtins -import testtools - -from senlinclient import client as senlin_client -from senlinclient.common import exc -from senlinclient.common.i18n import _ -from senlinclient.common import sdk -from senlinclient.common import utils -from senlinclient import shell -from senlinclient.tests.unit import fakes - - -class HelpFormatterTest(testtools.TestCase): - - def test_start_section(self): - fmtr = shell.HelpFormatter('senlin') - res = fmtr.start_section(('heading', 'text1', 30)) - self.assertIsNone(res) - h = fmtr._current_section.heading - self.assertEqual("HEADING('text1', 30)", h) - - -class TestArgs(testtools.TestCase): - - def __init__(self): - self.auth_url = 'http://fakeurl/v3' - self.auth_plugin = 'test_plugin' - self.username = 'test_user_name' - self.user_id = 'test_user_id' - self.token = 'test_token' - self.project_id = 'test_project_id' - self.project_name = 'test_project_name' - self.tenant_id = 'test_tenant_id' - self.tenant_name = 'test_tenant_name' - self.password = 'test_password' - self.user_domain_id = 'test_user_domain_id' - self.user_domain_name = 'test_user_domain_name' - self.project_domain_id = 'test_project_domain_id' - self.project_domain_name = 'test_project_domain_name' - self.domain_name = 'test_domain_name' - self.domain_id = 'test_domain_id' - self.verify = 'test_verify' - self.user_preferences = 'test_preferences' - self.trust_id = 'test_trust' - - -class ShellTest(testtools.TestCase): - - def setUp(self): - super(ShellTest, self).setUp() - - def SHELL(self, func, *args, **kwargs): - orig_out = sys.stdout - sys.stdout = six.StringIO() - func(*args, **kwargs) - output = sys.stdout.getvalue() - sys.stdout.close() - sys.stdout = orig_out - - return output - - @mock.patch.object(logging, 'basicConfig') - @mock.patch.object(logging, 'getLogger') - def test_setup_logging_debug(self, x_get, x_config): - sh = shell.SenlinShell() - sh._setup_logging(True) - - x_config.assert_called_once_with( - format="%(levelname)s (%(module)s) %(message)s", - level=logging.DEBUG) - mock_calls = [ - mock.call('iso8601'), - mock.call().setLevel(logging.WARNING), - mock.call('urllib3.connectionpool'), - mock.call().setLevel(logging.WARNING), - ] - x_get.assert_has_calls(mock_calls) - - @mock.patch.object(logging, 'basicConfig') - @mock.patch.object(logging, 'getLogger') - def test_setup_logging_no_debug(self, x_get, x_config): - sh = shell.SenlinShell() - sh._setup_logging(False) - - x_config.assert_called_once_with( - format="%(levelname)s (%(module)s) %(message)s", - level=logging.WARNING) - mock_calls = [ - mock.call('iso8601'), - mock.call().setLevel(logging.WARNING), - mock.call('urllib3.connectionpool'), - mock.call().setLevel(logging.WARNING), - ] - x_get.assert_has_calls(mock_calls) - - def test_setup_verbose(self): - sh = shell.SenlinShell() - sh._setup_verbose(True) - self.assertEqual(1, exc.verbose) - - sh._setup_verbose(False) - self.assertEqual(1, exc.verbose) - - def test_find_actions(self): - sh = shell.SenlinShell() - sh.subcommands = {} - subparsers = mock.Mock() - x_subparser1 = mock.Mock() - x_subparser2 = mock.Mock() - x_add_parser = mock.MagicMock(side_effect=[x_subparser1, x_subparser2]) - subparsers.add_parser = x_add_parser - - # subparsers.add_parser = mock.Mock(return_value=x_subparser) - sh._find_actions(subparsers, fakes) - - self.assertEqual({'command-bar': x_subparser1, - 'command-foo': x_subparser2}, - sh.subcommands) - add_calls = [ - mock.call('command-bar', help='This is the command doc.', - description='This is the command doc.', - add_help=False, - formatter_class=shell.HelpFormatter), - mock.call('command-foo', help='Pydoc for command foo.', - description='Pydoc for command foo.', - add_help=False, - formatter_class=shell.HelpFormatter), - ] - x_add_parser.assert_has_calls(add_calls) - - calls_1 = [ - mock.call('-h', '--help', action='help', - help=argparse.SUPPRESS), - mock.call('-F', '--flag', metavar='', - help='Flag desc.'), - mock.call('arg1', metavar='', - help='Arg1 desc') - ] - x_subparser1.add_argument.assert_has_calls(calls_1) - x_subparser1.set_defaults.assert_called_once_with( - func=fakes.do_command_bar) - - calls_2 = [ - mock.call('-h', '--help', action='help', - help=argparse.SUPPRESS), - ] - x_subparser2.add_argument.assert_has_calls(calls_2) - x_subparser2.set_defaults.assert_called_once_with( - func=fakes.do_command_foo) - - def test_do_bash_completion(self): - sh = shell.SenlinShell() - sc1 = mock.Mock() - sc2 = mock.Mock() - sc1._optionals._option_string_actions = ('A1', 'A2', 'C') - sc2._optionals._option_string_actions = ('B1', 'B2', 'C') - sh.subcommands = { - 'command-foo': sc1, - 'command-bar': sc2, - 'bash-completion': None, - 'bash_completion': None, - } - - output = self.SHELL(sh.do_bash_completion, None) - - output = output.split('\n')[0] - output_list = output.split(' ') - for option in ('A1', 'A2', 'C', 'B1', 'B2', - 'command-foo', 'command-bar'): - self.assertIn(option, output_list) - - def test_do_add_profiler_args(self): - sh = shell.SenlinShell() - parser = mock.Mock() - - sh.add_profiler_args(parser) - - self.assertEqual(0, parser.add_argument.call_count) - - def test_add_bash_completion_subparser(self): - sh = shell.SenlinShell() - sh.subcommands = {} - x_subparser = mock.Mock() - x_subparsers = mock.Mock() - x_subparsers.add_parser.return_value = x_subparser - - sh._add_bash_completion_subparser(x_subparsers) - - x_subparsers.add_parser.assert_called_once_with( - 'bash_completion', add_help=False, - formatter_class=shell.HelpFormatter) - self.assertEqual({'bash_completion': x_subparser}, sh.subcommands) - x_subparser.set_defaults.assert_called_once_with( - func=sh.do_bash_completion) - - @mock.patch.object(utils, 'import_versioned_module') - @mock.patch.object(shell.SenlinShell, '_find_actions') - @mock.patch.object(shell.SenlinShell, '_add_bash_completion_subparser') - def test_get_subcommand_parser(self, x_add, x_find, x_import): - x_base = mock.Mock() - x_module = mock.Mock() - x_import.return_value = x_module - sh = shell.SenlinShell() - - res = sh.get_subcommand_parser(x_base, 'v100') - - self.assertEqual(x_base, res) - x_base.add_subparsers.assert_called_once_with( - metavar='') - x_subparsers = x_base.add_subparsers.return_value - x_import.assert_called_once_with('v100', 'shell') - find_calls = [ - mock.call(x_subparsers, x_module), - mock.call(x_subparsers, sh) - ] - - x_find.assert_has_calls(find_calls) - x_add.assert_called_once_with(x_subparsers) - - @mock.patch.object(argparse.ArgumentParser, 'print_help') - def test_do_help(self, mock_print): - sh = shell.SenlinShell() - args = mock.Mock() - args.command = mock.Mock() - sh.subcommands = {args.command: argparse.ArgumentParser} - sh.do_help(args) - self.assertTrue(mock_print.called) - - sh.subcommands = {} - ex = self.assertRaises(exc.CommandError, - sh.do_help, args) - msg = _("'%s' is not a valid subcommand") % args.command - self.assertEqual(msg, six.text_type(ex)) - - @mock.patch.object(builtins, 'print') - def test_check_identity_arguments(self, mock_print): - sh = shell.SenlinShell() - # auth_url is not specified. - args = TestArgs() - args.auth_url = None - ex = self.assertRaises(exc.CommandError, - sh._check_identity_arguments, args) - msg = _('You must provide an auth url via --os-auth-url (or ' - ' env[OS_AUTH_URL])') - self.assertEqual(msg, six.text_type(ex)) - # username, user_id and token are not specified. - args = TestArgs() - args.username = None - args.user_id = None - args.token = None - msg = _('You must provide a user name, a user_id or a ' - 'token for authentication') - ex = self.assertRaises(exc.CommandError, - sh._check_identity_arguments, args) - self.assertEqual(msg, six.text_type(ex)) - # Both username and user_id are specified. - args = TestArgs() - args.project_id = None - args.tenant_id = None - sh._check_identity_arguments(args) - msg = _('WARNING: Both user name and user ID are specified, ' - 'Senlin will use user ID for authentication') - mock_print.assert_called_with(msg) - - # 'v3' in auth_url but neither user_domain_id nor user_domain_name - # is specified. - args = TestArgs() - args.user_id = None - args.user_domain_id = None - args.user_domain_name = None - msg = _('Either user domain ID (--user-domain-id / ' - 'env[OS_USER_DOMAIN_ID]) or user domain name ' - '(--user-domain-name / env[OS_USER_DOMAIN_NAME]) ' - 'must be specified, because user name may not be ' - 'unique.') - ex = self.assertRaises(exc.CommandError, - sh._check_identity_arguments, args) - self.assertEqual(msg, six.text_type(ex)) - # user_id, project_id, project_name, tenant_id and tenant_name are all - # not specified. - args = TestArgs() - args.project_id = None - args.project_name = None - args.tenant_id = None - args.tenant_name = None - args.user_id = None - msg = _('Either project/tenant ID or project/tenant name ' - 'must be specified, or else Senlin cannot know ' - 'which project to use.') - ex = self.assertRaises(exc.CommandError, - sh._check_identity_arguments, args) - self.assertEqual(msg, six.text_type(ex)) - args.user_id = 'test_user_id' - sh._check_identity_arguments(args) - msg = _('Neither project ID nor project name is specified. ' - 'Senlin will use user\'s default project which may ' - 'result in authentication error.') - mock_print.assert_called_with(_('WARNING: %s') % msg) - - # Both project_name and project_id are specified - args = TestArgs() - args.user_id = None - sh._check_identity_arguments(args) - msg = _('Both project/tenant name and project/tenant ID are ' - 'specified, Senlin will use project ID for ' - 'authentication') - mock_print.assert_called_with(_('WARNING: %s') % msg) - # Project name may not be unique - args = TestArgs() - args.user_id = None - args.project_id = None - args.tenant_id = None - args.project_domain_id = None - args.project_domain_name = None - msg = _('Either project domain ID (--project-domain-id / ' - 'env[OS_PROJECT_DOMAIN_ID]) orr project domain name ' - '(--project-domain-name / env[OS_PROJECT_DOMAIN_NAME ' - 'must be specified, because project/tenant name may ' - 'not be unique.') - ex = self.assertRaises(exc.CommandError, - sh._check_identity_arguments, args) - self.assertEqual(msg, six.text_type(ex)) - - @mock.patch.object(sdk, 'create_connection') - def test_setup_senlinclient(self, mock_conn): - USER_AGENT = 'python-senlinclient' - args = TestArgs() - kwargs = { - 'auth_plugin': args.auth_plugin, - 'auth_url': args.auth_url, - 'project_name': args.project_name or args.tenant_name, - 'project_id': args.project_id or args.tenant_id, - 'domain_name': args.domain_name, - 'domain_id': args.domain_id, - 'project_domain_name': args.project_domain_name, - 'project_domain_id': args.project_domain_id, - 'user_domain_name': args.user_domain_name, - 'user_domain_id': args.user_domain_id, - 'username': args.username, - 'user_id': args.user_id, - 'password': args.password, - 'verify': args.verify, - 'token': args.token, - 'trust_id': args.trust_id, - } - sh = shell.SenlinShell() - conn = mock.Mock() - mock_conn.return_value = conn - conn.session = mock.Mock() - sh._setup_senlin_client('1', args) - mock_conn.assert_called_once_with(args.user_preferences, USER_AGENT, - **kwargs) - client = mock.Mock() - senlin_client.Client = mock.MagicMock(return_value=client) - self.assertEqual(client, sh._setup_senlin_client('1', args)) diff --git a/senlinclient/tests/unit/v1/test_shell.py b/senlinclient/tests/unit/v1/test_shell.py deleted file mode 100644 index e2d1ef2..0000000 --- a/senlinclient/tests/unit/v1/test_shell.py +++ /dev/null @@ -1,1348 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# 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 copy -import mock -import six -import testtools - -from openstack import exceptions as oexc -from oslotest import mockpatch -from senlinclient.common import exc -from senlinclient.common.i18n import _ -from senlinclient.common import utils -from senlinclient.v1 import shell as sh - - -class ShellTest(testtools.TestCase): - - def setUp(self): - super(ShellTest, self).setUp() - self.profile_args = { - 'spec_file': mock.Mock(), - 'name': 'stack_spec', - 'metadata': {'user': 'demo'} - } - self.profile_spec = { - 'type': 'os.heat.stack', - 'version': 1.0, - 'properties': { - 'name': 'stack1', - 'template': {"Template": "data"} - } - } - self.patch('senlinclient.v1.shell.show_deprecated') - - # NOTE(pshchelo): this overrides the testtools.TestCase.patch method - # that does simple monkey-patching in favor of mock's patching - def patch(self, target, **kwargs): - mockfixture = self.useFixture(mockpatch.Patch(target, **kwargs)) - return mockfixture.mock - - def _make_args(self, args): - """Convert a dict to an object.""" - class Args(object): - def __init__(self, entries): - self.__dict__.update(entries) - - return Args(args) - - @mock.patch.object(utils, 'print_dict') - def test_do_build_info(self, mock_print): - service = mock.Mock() - result = mock.Mock() - service.get_build_info.return_value = result - sh.do_build_info(service) - formatters = { - 'api': utils.json_formatter, - 'engine': utils.json_formatter, - } - mock_print.assert_called_once_with(result, formatters=formatters) - self.assertTrue(service.get_build_info.called) - - @mock.patch.object(utils, 'print_list') - def test_do_profile_type_list(self, mock_print): - service = mock.Mock() - types = mock.Mock() - service.profile_types.return_value = types - sh.do_profile_type_list(service) - mock_print.assert_called_once_with(types, ['name'], sortby_index=0) - self.assertTrue(service.profile_types.called) - - @mock.patch.object(utils, 'format_output') - def test_do_profile_type_show(self, mock_format): - service = mock.Mock() - fake_pt = mock.Mock() - fake_pt.to_dict.return_value = {'foo': 'bar'} - service.get_profile_type = mock.Mock(return_value=fake_pt) - args_dict = { - 'format': 'json', - 'type_name': 'os.nova.server' - } - args = self._make_args(args_dict) - sh.do_profile_type_show(service, args) - mock_format.assert_called_with({'foo': 'bar'}, format=args.format) - service.get_profile_type.assert_called_with('os.nova.server') - args.format = None - sh.do_profile_type_show(service, args) - mock_format.assert_called_with({'foo': 'bar'}) - - def test_do_profile_type_show_type_not_found(self): - service = mock.Mock() - args = { - 'type_name': 'wrong_type', - 'format': 'json' - } - args = self._make_args(args) - ex = oexc.ResourceNotFound - service.get_profile_type = mock.Mock(side_effect=ex) - ex = self.assertRaises(exc.CommandError, - sh.do_profile_type_show, - service, args) - self.assertEqual(_('Profile Type not found: wrong_type'), - six.text_type(ex)) - - @mock.patch.object(utils, 'print_list') - def test_do_profile_list(self, mock_print): - service = mock.Mock() - profiles = mock.Mock() - service.profiles.return_value = profiles - fields = ['id', 'name', 'type', 'created_at'] - args = { - 'limit': 20, - 'marker': 'mark_id', - 'sort': 'key:dir', - 'global_project': True, - 'filters': ['name=stack_spec'] - } - queries = copy.deepcopy(args) - del queries['filters'] - queries['name'] = 'stack_spec' - formatters = {} - args = self._make_args(args) - args.full_id = True - sh.do_profile_list(service, args) - service.profiles.assert_called_once_with(**queries) - mock_print.assert_called_with(profiles, fields, formatters=formatters, - sortby_index=None) - - args.sort = None - sh.do_profile_list(service, args) - mock_print.assert_called_with(profiles, fields, formatters=formatters, - sortby_index=1) - - @mock.patch.object(utils, 'nested_dict_formatter') - @mock.patch.object(utils, 'print_dict') - def test_show_profile(self, mock_print, mock_dict): - service = mock.Mock() - profile = mock.Mock() - profile_id = mock.Mock() - service.get_profile.return_value = profile - pro_to_dict = mock.Mock() - profile.to_dict.return_value = pro_to_dict - json_formatter = mock.Mock() - utils.json_formatter = json_formatter - dict_formatter = mock.Mock() - mock_dict.return_value = dict_formatter - formatters = { - 'metadata': json_formatter, - 'spec': dict_formatter - } - sh._show_profile(service, profile_id) - service.get_profile.assert_called_once_with(profile_id) - mock_dict.assert_called_once_with(['type', 'version', 'properties'], - ['property', 'value']) - mock_print.assert_called_once_with(pro_to_dict, formatters=formatters) - - def test_show_profile_not_found(self): - service = mock.Mock() - ex = oexc.ResourceNotFound - service.get_profile.side_effect = ex - profile_id = 'wrong_id' - ex = self.assertRaises(exc.CommandError, - sh._show_profile, - service, profile_id) - self.assertEqual(_('Profile not found: wrong_id'), six.text_type(ex)) - service.get_profile.assert_called_once_with(profile_id) - - @mock.patch.object(sh, '_show_profile') - @mock.patch.object(utils, 'format_parameters') - @mock.patch.object(utils, 'process_stack_spec') - @mock.patch.object(utils, 'get_spec_content') - def test_do_profile_create(self, mock_get, mock_proc, mock_format, - mock_show): - args = copy.deepcopy(self.profile_args) - args = self._make_args(args) - spec = copy.deepcopy(self.profile_spec) - mock_get.return_value = spec - stack_properties = mock.Mock() - mock_proc.return_value = stack_properties - mock_format.return_value = {'user': 'demo'} - params = { - 'name': 'stack_spec', - 'spec': spec, - 'metadata': {'user': 'demo'}, - } - service = mock.Mock() - profile = mock.Mock() - profile_id = mock.Mock() - profile.id = profile_id - service.create_profile.return_value = profile - - sh.do_profile_create(service, args) - - mock_get.assert_called_once_with(args.spec_file) - mock_proc.assert_called_once_with(self.profile_spec['properties']) - mock_format.assert_called_once_with(args.metadata) - service.create_profile.assert_called_once_with(**params) - mock_show.assert_called_once_with(service, profile_id) - - # Miss 'type' key in spec file - del spec['type'] - ex = self.assertRaises(exc.CommandError, - sh.do_profile_create, - service, args) - self.assertEqual(_("Missing 'type' key in spec file."), - six.text_type(ex)) - # Miss 'version' key in spec file - spec['type'] = 'os.heat.stack' - del spec['version'] - ex = self.assertRaises(exc.CommandError, - sh.do_profile_create, - service, args) - self.assertEqual(_("Missing 'version' key in spec file."), - six.text_type(ex)) - # Miss 'properties' key in spec file - spec['version'] = 1.0 - del spec['properties'] - ex = self.assertRaises(exc.CommandError, - sh.do_profile_create, - service, args) - self.assertEqual(_("Missing 'properties' key in spec file."), - six.text_type(ex)) - - @mock.patch.object(sh, '_show_profile') - def test_do_profile_show(self, mock_show): - service = mock.Mock() - args = {'id': 'profile_id'} - args = self._make_args(args) - sh.do_profile_show(service, args) - mock_show.assert_called_once_with(service, args.id) - - @mock.patch.object(sh, '_show_profile') - @mock.patch.object(utils, 'format_parameters') - def test_do_profile_update(self, mock_format, mock_show): - args = copy.deepcopy(self.profile_args) - args = self._make_args(args) - mock_format.return_value = {'user': 'demo'} - service = mock.Mock() - profile = mock.Mock() - profile_id = mock.Mock() - profile.id = profile_id - args.id = 'FAKE_ID' - service.get_profile.return_value = profile - - sh.do_profile_update(service, args) - - mock_format.assert_called_once_with(args.metadata) - service.get_profile.assert_called_once_with('FAKE_ID') - params = { - 'name': 'stack_spec', - 'metadata': {'user': 'demo'}, - } - service.update_profile.assert_called_once_with(profile_id, **params) - mock_show.assert_called_once_with(service, profile_id) - - @mock.patch.object(utils, 'format_parameters') - def test_do_profile_update_not_found(self, mock_format): - service = mock.Mock() - args = copy.deepcopy(self.profile_args) - args = self._make_args(args) - args.id = 'FAKE_ID' - ex = oexc.ResourceNotFound - service.get_profile.side_effect = ex - ex = self.assertRaises(exc.CommandError, - sh.do_profile_update, - service, args) - self.assertEqual(_('Profile not found: FAKE_ID'), - six.text_type(ex)) - mock_format.assert_called_once_with(args.metadata) - - def test_do_profile_delete(self): - service = mock.Mock() - args = {'id': ['profile_id']} - args = self._make_args(args) - sh.do_profile_delete(service, args) - service.delete_profile.assert_called_with('profile_id', False) - - def test_do_profile_delete_not_found(self): - service = mock.Mock() - args = {'id': ['profile1', 'profile2']} - args = self._make_args(args) - sh.do_profile_delete(service, args) - service.delete_profile.side_effect = oexc.ResourceNotFound - ex = self.assertRaises(exc.CommandError, - sh.do_profile_delete, - service, args) - msg = _("Failed to delete some of the specified profile(s).") - self.assertEqual(msg, six.text_type(ex)) - service.delete_profile.assert_called_with('profile2', False) - - @mock.patch.object(utils, 'print_list') - def test_do_policy_type_list(self, mock_print): - service = mock.Mock() - args = mock.Mock() - types = mock.Mock() - service.policy_types.return_value = types - sh.do_policy_type_list(service, args) - mock_print.assert_called_once_with(types, ['name'], sortby_index=0) - - @mock.patch.object(utils, 'format_output') - def test_do_policy_type_show(self, mock_format): - service = mock.Mock() - args = { - 'type_name': 'senlin.policy.deletion', - 'format': 'yaml' - } - args = self._make_args(args) - res = mock.Mock() - pt = mock.Mock() - res.to_dict.return_value = pt - service.get_policy_type.return_value = res - sh.do_policy_type_show(service, args) - mock_format.assert_called_with(pt, format=args.format) - - # no format attribute - args = { - 'type_name': 'senlin.policy.deletion', - 'format': None - } - args = self._make_args(args) - service.get_policy_type.return_value = res - sh.do_policy_type_show(service, args) - mock_format.assert_called_with(pt) - - def test_do_policy_type_show_not_found(self): - service = mock.Mock() - args = {'type_name': 'BAD'} - args = self._make_args(args) - - service.get_policy_type.side_effect = oexc.ResourceNotFound - ex = self.assertRaises(exc.CommandError, - sh.do_policy_type_show, service, args) - msg = _('Policy type not found: BAD') - self.assertEqual(msg, six.text_type(ex)) - - @mock.patch.object(utils, 'print_list') - def test_do_receiver_list(self, mock_print): - service = mock.Mock() - params = { - 'limit': 10, - 'marker': 'fake_id', - 'sort': 'key:dir', - 'filters': ['filter_key=filter_value'], - 'global_project': False, - 'full_id': False, - } - fields = ['id', 'name', 'type', 'cluster_id', 'action', 'created_at'] - args = self._make_args(params) - queries = copy.deepcopy(params) - del queries['filters'] - queries['filter_key'] = 'filter_value' - r1 = mock.Mock() - r1.id = '01234567-abcd-efgh' - r1.cluster_id = 'abcdefgh-abcd-efgh' - receivers = [r1] - service.receivers.return_value = receivers - formatters = { - 'id': mock.ANY, - 'cluster_id': mock.ANY - } - sh.do_receiver_list(service, args) - mock_print.assert_called_with(receivers, fields, - formatters=formatters, - sortby_index=None) - # full_id is requested - args.full_id = True - sh.do_receiver_list(service, args) - mock_print.assert_called_with(receivers, fields, - formatters={}, - sortby_index=None) - - # default sorting - args.sort = None - sh.do_receiver_list(service, args) - mock_print.assert_called_with(receivers, fields, - formatters={}, - sortby_index=0) - - @mock.patch.object(utils, 'print_dict') - def test_show_receiver(self, mock_print): - service = mock.Mock() - receiver = mock.Mock() - receiver_id = '01234567-abcd-abcd-abcdef' - receiver.id = receiver_id - service.get_receiver.return_value = receiver - receiver_dict = mock.Mock() - receiver.to_dict.return_value = receiver_dict - sh._show_receiver(service, receiver_id) - formatters = { - 'actor': utils.json_formatter, - 'params': utils.json_formatter, - 'channel': utils.json_formatter, - } - service.get_receiver.assert_called_once_with(receiver_id) - mock_print.assert_called_once_with(receiver_dict, - formatters=formatters) - - def test_show_receiver_not_found(self): - service = mock.Mock() - receiver = mock.Mock() - receiver_id = 'wrong_id' - receiver.id = receiver_id - - service.get_receiver.side_effect = oexc.ResourceNotFound - ex = self.assertRaises(exc.CommandError, - sh._show_receiver, service, receiver_id) - self.assertEqual(_('Receiver not found: wrong_id'), six.text_type(ex)) - - @mock.patch.object(sh, '_show_receiver') - def test_do_receiver_show(self, mock_show): - service = mock.Mock() - args = {'id': 'receiver_id'} - args = self._make_args(args) - sh.do_receiver_show(service, args) - mock_show.assert_called_once_with(service, - receiver_id='receiver_id') - - @mock.patch.object(sh, '_show_receiver') - def test_do_receiver_create(self, mock_show): - service = mock.Mock() - args = { - 'name': 'receiver1', - 'type': 'webhook', - 'cluster': 'cluster1', - 'action': 'CLUSTER_SCALE_IN', - 'params': {} - } - args = self._make_args(args) - params = { - 'name': 'receiver1', - 'type': 'webhook', - 'cluster_id': 'cluster1', - 'action': 'CLUSTER_SCALE_IN', - 'params': {} - } - receiver = mock.Mock() - receiver.id = 'FAKE_ID' - service.create_receiver.return_value = receiver - sh.do_receiver_create(service, args) - service.create_receiver.assert_called_once_with(**params) - mock_show.assert_called_once_with(service, 'FAKE_ID') - - def test_do_receiver_delete(self): - service = mock.Mock() - args = {'id': ['FAKE']} - args = self._make_args(args) - service.delete_receiver = mock.Mock() - sh.do_receiver_delete(service, args) - service.delete_receiver.assert_called_once_with('FAKE', False) - - def test_do_receiver_delete_not_found(self): - service = mock.Mock() - args = {'id': ['receiver_id']} - args = self._make_args(args) - - service.delete_receiver.side_effect = oexc.ResourceNotFound - ex = self.assertRaises(exc.CommandError, - sh.do_receiver_delete, service, args) - msg = _("Failed to delete some of the specified receiver(s).") - self.assertEqual(msg, six.text_type(ex)) - - @mock.patch.object(utils, 'print_list') - def test_do_policy_list(self, mock_print): - service = mock.Mock() - fields = ['id', 'name', 'type', 'created_at'] - args = { - 'limit': 20, - 'marker': 'fake_id', - 'sort': 'name', - 'global_project': False, - 'full_id': True, - 'filters': ['name=stack_spec'] - } - args = self._make_args(args) - queries = { - 'limit': 20, - 'marker': 'fake_id', - 'sort': 'name', - 'global_project': False, - 'name': 'stack_spec', - } - policies = mock.Mock() - service.policies.return_value = policies - formatters = {} - sh.do_policy_list(service, args) - service.policies.assert_called_once_with(**queries) - mock_print.assert_called_once_with( - policies, fields, formatters=formatters, sortby_index=None) - mock_print.reset_mock() - - args.sort = None - sh.do_policy_list(service, args) - mock_print.assert_called_once_with( - policies, fields, formatters=formatters, sortby_index=1) - - @mock.patch.object(utils, 'print_dict') - def test_show_policy(self, mock_print): - service = mock.Mock() - formatters = { - 'metadata': utils.json_formatter, - 'spec': utils.json_formatter, - } - policy_id = 'fake_policy_id' - policy = mock.Mock() - policy.id = policy_id - service.get_policy.return_value = policy - policy_dict = mock.Mock() - policy.to_dict.return_value = policy_dict - sh._show_policy(service, policy_id) - mock_print.assert_called_once_with(policy_dict, - formatters=formatters) - - # policy not found - ex = oexc.ResourceNotFound - service.get_policy.side_effect = ex - ex = self.assertRaises(exc.CommandError, - sh._show_policy, - service, policy_id) - msg = _('Policy not found: fake_policy_id') - self.assertEqual(msg, six.text_type(ex)) - - @mock.patch.object(sh, '_show_policy') - @mock.patch.object(utils, 'get_spec_content') - def test_do_policy_create(self, mock_get, mock_show): - service = mock.Mock() - spec = mock.Mock() - mock_get.return_value = spec - args = { - 'name': 'new_policy', - 'spec_file': 'policy_file', - } - args = self._make_args(args) - attrs = { - 'name': 'new_policy', - 'spec': spec, - } - policy = mock.Mock() - policy.id = 'policy_id' - service.create_policy.return_value = policy - sh.do_policy_create(service, args) - mock_get.assert_called_once_with(args.spec_file) - service.create_policy.assert_called_once_with(**attrs) - mock_show.assert_called_once_with(service, policy.id) - - @mock.patch.object(sh, '_show_policy') - def test_do_policy_show(self, mock_show): - service = mock.Mock() - args = {'id': 'policy_id'} - args = self._make_args(args) - sh.do_policy_show(service, args) - mock_show.assert_called_once_with(service, policy_id='policy_id') - - @mock.patch.object(sh, '_show_policy') - def test_do_policy_update(self, mock_show): - service = mock.Mock() - args = { - 'name': 'deletion_policy', - 'id': 'policy_id', - } - args = self._make_args(args) - params = { - 'name': 'deletion_policy', - } - policy = mock.Mock() - service.get_policy.return_value = policy - policy.id = 'policy_id' - sh.do_policy_update(service, args) - service.get_policy.assert_called_once_with('policy_id') - service.update_policy.assert_called_once_with( - 'policy_id', **params) - mock_show(service, policy_id=policy.id) - - def test_do_policy_delete(self): - service = mock.Mock() - args = {'id': ['policy_id']} - args = self._make_args(args) - service.delete_policy = mock.Mock() - sh.do_policy_delete(service, args) - service.delete_policy.assert_called_once_with('policy_id', False) - - def test_do_policy_delete_not_found(self): - service = mock.Mock() - args = {'id': ['policy_id']} - args = self._make_args(args) - - service.delete_policy.side_effect = oexc.ResourceNotFound - ex = self.assertRaises(exc.CommandError, - sh.do_policy_delete, service, args) - msg = _("Failed to delete some of the specified policy(s).") - self.assertEqual(msg, six.text_type(ex)) - - @mock.patch.object(utils, 'print_list') - def test_do_cluster_list(self, mock_print): - service = mock.Mock() - fields = ['id', 'name', 'status', 'created_at', 'updated_at'] - args = { - 'limit': 20, - 'marker': 'fake_id', - 'sort': 'key:dir', - 'global_project': False, - 'filters': ['status=ACTIVE'], - } - queries = copy.deepcopy(args) - del queries['filters'] - queries['status'] = 'ACTIVE' - args = self._make_args(args) - clusters = mock.Mock() - service.clusters.return_value = clusters - args.full_id = True - formatters = {} - sh.do_cluster_list(service, args) - service.clusters.assert_called_once_with(**queries) - mock_print.assert_called_once_with(clusters, fields, - formatters=formatters, - sortby_index=None) - args.sort = None - sh.do_cluster_list(service, args) - mock_print.assert_called_with(clusters, fields, - formatters={}, sortby_index=3) - - @mock.patch.object(utils, 'print_dict') - def test_show_cluster(self, mock_print): - service = mock.Mock() - cluster_id = 'cluster_id' - cluster = mock.Mock() - cluster.id = cluster_id - service.get_cluster.return_value = cluster - formatters = { - 'metadata': utils.json_formatter, - 'nodes': utils.list_formatter, - } - cluster_dict = mock.Mock() - cluster.to_dict.return_value = cluster_dict - sh._show_cluster(service, cluster_id) - mock_print.assert_called_once_with(cluster_dict, formatters=formatters) - - @mock.patch.object(sh, '_show_cluster') - def test_do_cluster_create(self, mock_show): - service = mock.Mock() - args = { - 'name': 'CLUSTER1', - 'profile': 'profile1', - 'min_size': 1, - 'max_size': 10, - 'desired_capacity': 5, - 'metadata': ['user=demo'], - 'timeout': 200, - } - attrs = copy.deepcopy(args) - attrs['profile_id'] = args['profile'] - args = self._make_args(args) - del attrs['profile'] - attrs['metadata'] = {'user': 'demo'} - cluster = mock.Mock() - service.create_cluster.return_value = cluster - cluster.id = 'cluster_id' - sh.do_cluster_create(service, args) - service.create_cluster.assert_called_once_with(**attrs) - mock_show.assert_called_once_with(service, 'cluster_id') - - def test_do_cluster_delete(self): - service = mock.Mock() - args = {'id': ['CID']} - args = self._make_args(args) - service.delete_cluster = mock.Mock() - sh.do_cluster_delete(service, args) - service.delete_cluster.assert_called_once_with('CID', False) - - def test_do_cluster_delete_not_found(self): - service = mock.Mock() - args = {'id': ['cluster_id']} - args = self._make_args(args) - - service.delete_cluster.side_effect = oexc.ResourceNotFound - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_delete, service, args) - msg = _('Failed to delete some of the specified clusters.') - self.assertEqual(msg, six.text_type(ex)) - - @mock.patch.object(sh, '_show_cluster') - def test_do_cluster_update(self, mock_show): - service = mock.Mock() - args = { - 'profile': 'test_profile', - 'name': 'CLUSTER1', - 'metadata': ['user=demo'], - 'timeout': 100, - } - attrs = copy.deepcopy(args) - attrs['metadata'] = {'user': 'demo'} - attrs['profile_id'] = 'test_profile' - del attrs['profile'] - args = self._make_args(args) - args.id = 'CID' - cluster = mock.Mock() - cluster.id = 'CID' - service.get_cluster.return_value = cluster - service.update_cluster = mock.Mock() - - sh.do_cluster_update(service, args) - - service.get_cluster.assert_called_once_with('CID') - service.update_cluster.assert_called_once_with('CID', **attrs) - mock_show.assert_called_once_with(service, 'CID') - - @mock.patch.object(sh, '_show_cluster') - def test_do_cluster_show(self, mock_show): - service = mock.Mock() - args = {'id': 'cluster_id'} - args = self._make_args(args) - sh.do_cluster_show(service, args) - mock_show.assert_called_once_with(service, 'cluster_id') - - @mock.patch.object(utils, 'print_list') - def test_do_cluster_node_list(self, mock_print): - service = mock.Mock() - args = { - 'id': 'cluster_id', - 'limit': 20, - 'marker': 'marker_id', - 'filters': ['status=ACTIVE'], - } - queries = copy.deepcopy(args) - queries['cluster_id'] = args['id'] - del queries['id'] - del queries['filters'] - queries['status'] = 'ACTIVE' - args = self._make_args(args) - args.full_id = True - nodes = mock.Mock() - service.nodes.return_value = nodes - formatters = {} - fields = ['id', 'name', 'index', 'status', 'physical_id', 'created_at'] - sh.do_cluster_node_list(service, args) - service.nodes.assert_called_once_with(**queries) - mock_print.assert_called_once_with(nodes, fields, - formatters=formatters, - sortby_index=5) - - def test_do_cluster_node_add(self): - service = mock.Mock() - args = { - 'id': 'cluster_id', - 'nodes': 'node1,node2' - } - args = self._make_args(args) - node_ids = ['node1', 'node2'] - resp = {'action': 'CLUSTER_NODE_ADD'} - service.cluster_add_nodes.return_value = resp - sh.do_cluster_node_add(service, args) - service.cluster_add_nodes.assert_called_once_with( - 'cluster_id', node_ids) - - def test_do_cluster_node_del(self): - service = mock.Mock() - args = { - 'id': 'cluster_id', - 'nodes': 'node1,node2' - } - args = self._make_args(args) - node_ids = ['node1', 'node2'] - resp = {'action': 'CLUSTER_NODE_DEL'} - service.cluster_del_nodes.return_value = resp - - sh.do_cluster_node_del(service, args) - - service.cluster_del_nodes.assert_called_once_with('cluster_id', - node_ids) - - def test_do_cluster_resize(self): - service = mock.Mock() - args = { - 'id': 'cluster_id', - 'capacity': 2, - 'adjustment': 1, - 'percentage': 50.0, - 'min_size': 1, - 'max_size': 10, - 'min_step': 1, - 'strict': True, - } - args = self._make_args(args) - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_resize, - service, args) - msg = _("Only one of 'capacity', 'adjustment' and " - "'percentage' can be specified.") - self.assertEqual(msg, six.text_type(ex)) - - # capacity - args.adjustment = None - args.percentage = None - args.min_step = None - action_args = { - 'adjustment_type': 'EXACT_CAPACITY', - 'number': 2, - 'min_size': 1, - 'max_size': 10, - 'strict': True, - 'min_step': None, - } - resp = {'action': 'action_id'} - service.cluster_resize.return_value = resp - sh.do_cluster_resize(service, args) - service.cluster_resize.assert_called_with('cluster_id', **action_args) - - # capacity is smaller than 0 - args.capacity = -1 - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_resize, - service, args) - msg = _('Cluster capacity must be larger than ' - ' or equal to zero.') - self.assertEqual(msg, six.text_type(ex)) - - # adjustment - args.capacity = None - args.percentage = None - args.adjustment = 1 - action_args['adjustment_type'] = 'CHANGE_IN_CAPACITY' - action_args['number'] = 1 - sh.do_cluster_resize(service, args) - service.cluster_resize.assert_called_with('cluster_id', **action_args) - - # adjustment is 0 - args.adjustment = 0 - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_resize, - service, args) - msg = _('Adjustment cannot be zero.') - self.assertEqual(msg, six.text_type(ex)) - - # percentage - args.capacity = None - args.percentage = 50.0 - args.adjustment = None - action_args['adjustment_type'] = 'CHANGE_IN_PERCENTAGE' - action_args['number'] = 50.0 - sh.do_cluster_resize(service, args) - service.cluster_resize.assert_called_with('cluster_id', **action_args) - - # percentage is 0 - args.percentage = 0 - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_resize, - service, args) - msg = _('Percentage cannot be zero.') - self.assertEqual(msg, six.text_type(ex)) - - # min_step is not None while percentage is None - args.capacity = 2 - args.percentage = None - args.adjustment = None - args.min_step = 1 - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_resize, - service, args) - msg = _('Min step is only used with percentage.') - self.assertEqual(msg, six.text_type(ex)) - - # min_size < 0 - args.capacity = 2 - args.percentage = None - args.adjustment = None - args.min_step = None - args.min_size = -1 - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_resize, - service, 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 - args.adjustment = None - args.min_step = None - args.min_size = 5 - args.max_size = 4 - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_resize, - service, 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 - args.adjustment = None - args.min_step = None - args.min_size = 6 - args.max_size = 8 - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_resize, - service, 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 - args.adjustment = None - args.min_step = None - args.min_size = 1 - args.max_size = 4 - ex = self.assertRaises(exc.CommandError, - sh.do_cluster_resize, - service, args) - msg = _('Max size cannot be less than the specified capacity.') - self.assertEqual(msg, six.text_type(ex)) - - def test_do_cluster_scale_out(self): - service = mock.Mock() - args = { - 'id': 'cluster_id', - 'count': 3, - } - args = self._make_args(args) - resp = {'action': 'action_id'} - service.cluster_scale_out.return_value = resp - sh.do_cluster_scale_out(service, args) - service.cluster_scale_out.assert_called_once_with( - 'cluster_id', 3) - - def test_do_cluster_scale_in(self): - service = mock.Mock() - args = { - 'id': 'cluster_id', - 'count': 3, - } - args = self._make_args(args) - resp = {'action': 'action_id'} - service.cluster_scale_in.return_value = resp - - sh.do_cluster_scale_in(service, args) - - service.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): - fields = ['policy_id', 'policy_name', 'policy_type', 'enabled'] - service = mock.Mock() - args = { - 'id': 'C1', - 'filters': ['enabled=True'], - 'sort': 'enabled:asc', - 'full_id': True, - } - args = self._make_args(args) - queries = { - 'sort': 'enabled:asc', - 'enabled': 'True', - } - cluster = mock.Mock() - cluster.id = 'C1' - service.get_cluster.return_value = cluster - policies = mock.Mock() - service.cluster_policies.return_value = policies - sh.do_cluster_policy_list(service, args) - service.get_cluster.assert_called_once_with('C1') - service.cluster_policies.assert_called_once_with('C1', **queries) - formatters = {} - mock_print.assert_called_once_with(policies, fields, - formatters=formatters, - sortby_index=None) - - @mock.patch.object(utils, 'print_dict') - def test_do_cluster_policy_show(self, mock_print): - class Binding(object): - def to_dict(self): - pass - - service = mock.Mock() - args = { - 'id': 'CC', - 'policy': 'PP', - } - args = self._make_args(args) - binding = Binding() - service.get_cluster_policy.return_value = binding - sh.do_cluster_policy_show(service, args) - service.get_cluster_policy.assert_called_once_with('PP', 'CC') - mock_print.assert_called_once_with(binding.to_dict()) - - def test_do_cluster_policy_attach(self): - service = mock.Mock() - args = { - 'id': 'C1', - 'policy': 'P1', - 'enabled': 'True', - } - args = self._make_args(args) - kwargs = { - 'enabled': 'True', - } - service.cluster_attach_policy.return_value = {'action': 'action_id'} - sh.do_cluster_policy_attach(service, args) - service.cluster_attach_policy.assert_called_once_with('C1', 'P1', - **kwargs) - - def test_do_cluster_policy_detach(self): - args = { - 'id': 'CC', - 'policy': 'PP' - } - service = mock.Mock() - args = self._make_args(args) - resp = {'action': 'action_id'} - service.cluster_detach_policy.return_value = resp - sh.do_cluster_policy_detach(service, args) - service.cluster_detach_policy.assert_called_once_with('CC', 'PP') - - def test_do_cluster_policy_update(self): - service = mock.Mock() - args = { - 'id': 'C1', - 'policy': 'policy1', - 'enabled': 'True', - } - args = self._make_args(args) - kwargs = { - 'enabled': 'True', - } - service.cluster_update_policy.return_value = {'action': 'action_id'} - - sh.do_cluster_policy_update(service, args) - - service.cluster_update_policy.assert_called_once_with('C1', 'policy1', - **kwargs) - - def test_do_cluster_check(self): - service = mock.Mock() - args = self._make_args({'id': ['cluster1']}) - service.check_cluster = mock.Mock() - service.check_cluster.return_value = {'action': 'action_id'} - sh.do_cluster_check(service, args) - - service.check_cluster.assert_called_once_with('cluster1') - - def test_do_cluster_recover(self): - service = mock.Mock() - args = self._make_args({'id': ['cluster1']}) - service.recover_cluster = mock.Mock() - service.recover_cluster.return_value = {'action': 'action_id'} - - sh.do_cluster_recover(service, args) - - service.recover_cluster.assert_called_once_with('cluster1') - - @mock.patch.object(utils, 'print_list') - def test_do_node_list(self, mock_print): - service = mock.Mock() - fields = ['id', 'name', 'index', 'status', 'cluster_id', 'physical_id', - 'profile_name', 'created_at', 'updated_at'] - args = { - 'cluster': 'cluster1', - 'sort': 'name:asc', - 'limit': 20, - 'marker': 'marker_id', - 'global_project': True, - 'filters': ['status=active'], - 'full_id': True, - } - queries = { - 'cluster_id': 'cluster1', - 'sort': 'name:asc', - 'limit': 20, - 'marker': 'marker_id', - 'global_project': True, - 'status': 'active', - } - args = self._make_args(args) - nodes = mock.Mock() - service.nodes.return_value = nodes - formatters = {} - sh.do_node_list(service, args) - mock_print.assert_called_once_with(nodes, fields, - formatters=formatters, - sortby_index=None) - service.nodes.assert_called_once_with(**queries) - - @mock.patch.object(utils, 'print_dict') - @mock.patch.object(utils, 'nested_dict_formatter') - def test_show_node(self, mock_nested, mock_print): - service = mock.Mock() - node_id = 'node1' - node = mock.Mock() - service.get_node.return_value = node - formatters = { - 'metadata': utils.json_formatter, - 'data': utils.json_formatter, - } - data = mock.Mock() - node.to_dict.return_value = data - - sh._show_node(service, node_id, show_details=False) - - service.get_node.assert_called_once_with(node_id, args=None) - mock_print.assert_called_once_with(data, formatters=formatters) - - @mock.patch.object(sh, '_show_node') - def test_do_node_create(self, mock_show): - args = { - 'name': 'node1', - 'cluster': 'cluster1', - 'profile': 'profile1', - 'role': 'master', - 'metadata': ['user=demo'], - } - args = self._make_args(args) - attrs = { - 'name': 'node1', - 'cluster_id': 'cluster1', - 'profile_id': 'profile1', - 'role': 'master', - 'metadata': {'user': 'demo'}, - } - service = mock.Mock() - node = mock.Mock() - node.id = 'node_id' - service.create_node.return_value = node - sh.do_node_create(service, args) - service.create_node.assert_called_once_with(**attrs) - mock_show.assert_called_once_with(service, 'node_id') - - @mock.patch.object(sh, '_show_node') - def test_do_node_show(self, mock_show): - service = mock.Mock() - args = { - 'id': 'node1', - 'details': False - } - args = self._make_args(args) - sh.do_node_show(service, args) - mock_show.assert_called_once_with(service, 'node1', False) - - def test_do_node_delete(self): - service = mock.Mock() - args = self._make_args({'id': ['node1']}) - service.delete_node = mock.Mock() - - sh.do_node_delete(service, args) - - service.delete_node.assert_called_once_with('node1', False) - - def test_do_node_delete_not_found(self): - service = mock.Mock() - ex = oexc.ResourceNotFound - service.delete_node.side_effect = ex - - args = self._make_args({'id': ['node1']}) - ex = self.assertRaises(exc.CommandError, - sh.do_node_delete, service, args) - msg = _('Failed to delete some of the specified nodes.') - self.assertEqual(msg, six.text_type(ex)) - - def test_do_node_check(self): - service = mock.Mock() - args = self._make_args({'id': ['node1']}) - service.check_node = mock.Mock() - - sh.do_node_check(service, args) - - service.check_node.assert_called_once_with('node1') - - def test_do_node_check_not_found(self): - service = mock.Mock() - ex = exc.HTTPNotFound - service.check_node.side_effect = ex - - args = self._make_args({'id': ['node1']}) - ex = self.assertRaises(exc.CommandError, - sh.do_node_check, service, args) - msg = _('Failed to check some of the specified nodes.') - self.assertEqual(msg, six.text_type(ex)) - - def test_do_node_recover(self): - service = mock.Mock() - args = self._make_args({'id': ['node1']}) - service.check_node = mock.Mock() - - sh.do_node_recover(service, args) - - service.recover_node.assert_called_once_with('node1') - - def test_do_node_recover_not_found(self): - service = mock.Mock() - ex = exc.HTTPNotFound - service.recover_node.side_effect = ex - - args = self._make_args({'id': ['node1']}) - ex = self.assertRaises(exc.CommandError, - sh.do_node_recover, service, args) - msg = _('Failed to recover some of the specified nodes.') - self.assertEqual(msg, six.text_type(ex)) - - @mock.patch.object(sh, '_show_node') - def test_do_node_update(self, mock_show): - service = mock.Mock() - args = { - 'id': 'node_id', - 'name': 'node1', - 'role': 'master', - 'profile': 'profile1', - 'metadata': ['user=demo'], - } - args = self._make_args(args) - attrs = { - 'name': 'node1', - 'role': 'master', - 'profile_id': 'profile1', - 'metadata': {'user': 'demo'}, - } - node = mock.Mock() - node.id = 'node_id' - service.get_node.return_value = node - sh.do_node_update(service, args) - service.get_node.assert_called_once_with('node_id') - service.update_node.assert_called_once_with('node_id', **attrs) - mock_show.assert_called_once_with(service, 'node_id') - - @mock.patch.object(utils, 'print_list') - def test_do_event_list(self, mock_print): - service = mock.Mock() - fields = ['id', 'timestamp', 'obj_type', 'obj_id', 'obj_name', - 'action', 'status', 'status_reason', 'level'] - args = { - 'sort': 'timestamp:asc', - 'limit': 20, - 'marker': 'marker_id', - 'global_project': True, - 'filters': ['action=NODE_DELETE'], - 'full_id': True, - } - queries = copy.deepcopy(args) - del queries['full_id'] - del queries['filters'] - queries['action'] = 'NODE_DELETE' - args = self._make_args(args) - formatters = {} - events = mock.Mock() - service.events.return_value = events - - sh.do_event_list(service, args) - - service.events.assert_called_once_with(**queries) - mock_print.assert_called_once_with(events, fields, - formatters=formatters) - - @mock.patch.object(utils, 'print_dict') - def test_do_event_show(self, mock_print): - class FakeEvent(object): - def to_dict(self): - pass - - service = mock.Mock() - args = { - 'id': 'event_id' - } - args = self._make_args(args) - - event = FakeEvent() - service.get_event.return_value = event - sh.do_event_show(service, args) - service.get_event.assert_called_once_with('event_id') - mock_print.assert_called_once_with(event.to_dict()) - - def test_do_event_show_not_found(self): - service = mock.Mock() - args = self._make_args({'id': 'FAKE'}) - # event not found - ex = exc.CommandError - service.get_event.side_effect = oexc.ResourceNotFound - ex = self.assertRaises(ex, - sh.do_event_show, - service, args) - - @mock.patch.object(utils, 'print_list') - def test_do_action_list(self, mock_print): - service = mock.Mock() - fields = ['id', 'name', 'action', 'status', 'target', 'depends_on', - 'depended_by', 'created_at'] - args = { - 'sort': 'status', - 'limit': 20, - 'marker': 'marker_id', - } - queries = copy.deepcopy(args) - args = self._make_args(args) - args.filters = ['status=ACTIVE'] - queries['status'] = 'ACTIVE' - actions = mock.Mock() - service.actions.return_value = actions - formatters = { - 'depends_on': mock.ANY, - 'depended_by': mock.ANY - } - args.full_id = True - sortby_index = None - sh.do_action_list(service, args) - service.actions.assert_called_once_with(**queries) - mock_print.assert_called_once_with(actions, fields, - formatters=formatters, - sortby_index=sortby_index) - - @mock.patch.object(utils, 'print_dict') - def test_do_action_show(self, mock_print): - class FakeAction(object): - def to_dict(self): - pass - - service = mock.Mock() - args = self._make_args({'id': 'action_id'}) - - action = FakeAction() - service.get_action.return_value = action - formatters = { - 'inputs': utils.json_formatter, - 'outputs': utils.json_formatter, - 'metadata': utils.json_formatter, - 'data': utils.json_formatter, - 'depends_on': utils.list_formatter, - 'depended_by': utils.list_formatter, - } - sh.do_action_show(service, args) - service.get_action.assert_called_once_with('action_id') - mock_print.assert_called_once_with(action.to_dict(), - formatters=formatters) - - def test_do_action_show_not_found(self): - service = mock.Mock() - args = self._make_args({'id': 'fake_id'}) - - service.get_action.side_effect = oexc.ResourceNotFound - ex = self.assertRaises(exc.CommandError, - sh.do_action_show, - service, args) - msg = _('Action not found: fake_id') - self.assertEqual(msg, six.text_type(ex)) diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py deleted file mode 100644 index 6ef78e3..0000000 --- a/senlinclient/v1/shell.py +++ /dev/null @@ -1,1314 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# 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 logging - -from openstack import exceptions as sdk_exc -from senlinclient.common import exc -from senlinclient.common.i18n import _ -from senlinclient.common.i18n import _LW -from senlinclient.common import utils - -logger = logging.getLogger(__name__) - - -def show_deprecated(deprecated, recommended): - logger.warning(_LW('"%(old)s" is deprecated, ' - 'please use "%(new)s" instead.'), - {'old': deprecated, - 'new': recommended} - ) - - -def do_build_info(service, args=None): - """Retrieve build information. - - :param sc: Instance of senlinclient. - :param args: Additional command line arguments, if any. - """ - show_deprecated('senlin build-info', 'openstack cluster build info') - result = service.get_build_info() - - formatters = { - 'api': utils.json_formatter, - 'engine': utils.json_formatter, - } - utils.print_dict(result, formatters=formatters) - - -# PROFILE TYPES - - -def do_profile_type_list(service, args=None): - """List the available profile types. - - :param sc: Instance of senlinclient. - :param args: Additional command line arguments, if any. - """ - show_deprecated('senlin profile-type-list', - 'openstack cluster profile type list') - types = service.profile_types() - utils.print_list(types, ['name'], sortby_index=0) - - -@utils.arg('type_name', metavar='', - help=_('Profile type to retrieve.')) -@utils.arg('-F', '--format', metavar='', - choices=utils.supported_formats.keys(), - help=_("The template output format, one of: %s.") - % ', '.join(utils.supported_formats.keys())) -def do_profile_type_show(service, args): - """Get the details about a profile type.""" - show_deprecated('senlin profile-type-show', - 'openstack cluster profile type show') - try: - res = service.get_profile_type(args.type_name) - except sdk_exc.ResourceNotFound: - raise exc.CommandError( - _('Profile Type not found: %s') % args.type_name) - - pt = res.to_dict() - - if args.format: - print(utils.format_output(pt, format=args.format)) - else: - print(utils.format_output(pt)) - - -# PROFILES - -@utils.arg('-f', '--filters', metavar='', - help=_('Filter parameters to apply on returned profiles. ' - 'This can be specified multiple times, or once with ' - 'parameters separated by a semicolon.'), - action='append') -@utils.arg('-l', '--limit', metavar='', - help=_('Limit the number of profiles returned.')) -@utils.arg('-m', '--marker', metavar='', - help=_('Only return profiles that appear after the given ID.')) -@utils.arg('-o', '--sort', metavar='', - help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appended ' - 'by a sort direction (:asc or :desc)')) -@utils.arg('-g', '--global-project', default=False, action="store_true", - help=_('Indicate that the list should include profiles from' - ' all projects. This option is subject to access policy ' - 'checking. Default is False.')) -@utils.arg('-F', '--full-id', default=False, action="store_true", - help=_('Print full IDs in list.')) -def do_profile_list(service, args=None): - """List profiles that meet the criteria.""" - show_deprecated('senlin profile-list', 'openstack cluster profile list') - fields = ['id', 'name', 'type', 'created_at'] - queries = { - 'limit': args.limit, - 'marker': args.marker, - 'sort': args.sort, - 'global_project': args.global_project, - } - if args.filters: - queries.update(utils.format_parameters(args.filters)) - - sortby_index = None if args.sort else 1 - - profiles = service.profiles(**queries) - formatters = {} - if not args.full_id: - formatters = { - 'id': lambda x: x.id[:8], - } - utils.print_list(profiles, fields, formatters=formatters, - sortby_index=sortby_index) - - -def _show_profile(service, profile_id): - try: - profile = service.get_profile(profile_id) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_('Profile not found: %s') % profile_id) - - formatters = { - 'metadata': utils.json_formatter, - } - - formatters['spec'] = utils.nested_dict_formatter( - ['type', 'version', 'properties'], - ['property', 'value']) - - utils.print_dict(profile.to_dict(), formatters=formatters) - - -@utils.arg('-s', '--spec-file', metavar='', required=True, - help=_('The spec file used to create the profile.')) -@utils.arg('-M', '--metadata', metavar='', - help=_('Metadata values to be attached to the profile. ' - 'This can be specified multiple times, or once with ' - 'key-value pairs separated by a semicolon.'), - action='append') -@utils.arg('name', metavar='', - help=_('Name of the profile to create.')) -def do_profile_create(service, args): - """Create a profile.""" - show_deprecated('senlin profile-create', - 'openstack cluster profile create') - spec = utils.get_spec_content(args.spec_file) - type_name = spec.get('type', None) - type_version = spec.get('version', None) - properties = spec.get('properties', None) - if type_name is None: - raise exc.CommandError(_("Missing 'type' key in spec file.")) - if type_version is None: - raise exc.CommandError(_("Missing 'version' key in spec file.")) - if properties is None: - raise exc.CommandError(_("Missing 'properties' key in spec file.")) - - if type_name == 'os.heat.stack': - stack_properties = utils.process_stack_spec(properties) - spec['properties'] = stack_properties - - params = { - 'name': args.name, - 'spec': spec, - 'metadata': utils.format_parameters(args.metadata), - } - - profile = service.create_profile(**params) - _show_profile(service, profile.id) - - -@utils.arg('id', metavar='', - help=_('Name or ID of profile to show.')) -def do_profile_show(service, args): - """Show the profile details.""" - show_deprecated('senlin profile-show', 'openstack cluster profile show') - _show_profile(service, args.id) - - -@utils.arg('-n', '--name', metavar='', - help=_('The new name for the profile.')) -@utils.arg('-M', '--metadata', metavar='', - help=_('Metadata values to be attached to the profile. ' - 'This can be specified multiple times, or once with ' - 'key-value pairs separated by a semicolon.'), - action='append') -@utils.arg('id', metavar='', - help=_('Name or ID of the profile to update.')) -def do_profile_update(service, args): - """Update a profile.""" - show_deprecated('senlin profile-update', - 'openstack cluster profile update') - params = { - 'name': args.name, - } - if args.metadata: - params['metadata'] = utils.format_parameters(args.metadata) - - # Find the profile first, we need its id - try: - profile = service.get_profile(args.id) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_('Profile not found: %s') % args.id) - service.update_profile(profile.id, **params) - _show_profile(service, profile.id) - - -@utils.arg('id', metavar='', nargs='+', - help=_('Name or ID of profile(s) to delete.')) -def do_profile_delete(service, args): - """Delete profile(s).""" - show_deprecated('senlin profile-delete', - 'openstack cluster profile delete') - failure_count = 0 - - for pid in args.id: - try: - service.delete_profile(pid, False) - except Exception as ex: - failure_count += 1 - print(ex) - if failure_count > 0: - msg = _('Failed to delete some of the specified profile(s).') - raise exc.CommandError(msg) - print('Profile deleted: %s' % args.id) - - -# POLICY TYPES - - -def do_policy_type_list(service, args): - """List the available policy types.""" - show_deprecated('senlin policy-type-list', - 'openstack cluster policy type list') - types = service.policy_types() - utils.print_list(types, ['name'], sortby_index=0) - - -@utils.arg('type_name', metavar='', - help=_('Policy type to retrieve.')) -@utils.arg('-F', '--format', metavar='', - choices=utils.supported_formats.keys(), - help=_("The template output format, one of: %s.") - % ', '.join(utils.supported_formats.keys())) -def do_policy_type_show(service, args): - """Get the details about a policy type.""" - show_deprecated('senlin policy-type-show', - 'openstack cluster policy type show') - try: - res = service.get_policy_type(args.type_name) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_('Policy type not found: %s') % args.type_name) - - pt = res.to_dict() - if args.format: - print(utils.format_output(pt, format=args.format)) - else: - print(utils.format_output(pt)) - - -# POLICIES - -@utils.arg('-f', '--filters', metavar='', - help=_('Filter parameters to apply on returned policies. ' - 'This can be specified multiple times, or once with ' - 'parameters separated by a semicolon.'), - action='append') -@utils.arg('-l', '--limit', metavar='', - help=_('Limit the number of policies returned.')) -@utils.arg('-m', '--marker', metavar='', - help=_('Only return policies that appear after the given ID.')) -@utils.arg('-o', '--sort', metavar='', - help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appended ' - 'by a sort direction (:asc or :desc)')) -@utils.arg('-g', '--global-project', default=False, action="store_true", - help=_('Indicate that the list should include policies from' - ' all projects. This option is subject to access policy ' - 'checking. Default is False.')) -@utils.arg('-F', '--full-id', default=False, action="store_true", - help=_('Print full IDs in list.')) -def do_policy_list(service, args=None): - """List policies that meet the criteria.""" - show_deprecated('senlin policy-list', 'openstack cluster policy list') - fields = ['id', 'name', 'type', 'created_at'] - queries = { - 'limit': args.limit, - 'marker': args.marker, - 'sort': args.sort, - 'global_project': args.global_project, - } - if args.filters: - queries.update(utils.format_parameters(args.filters)) - - sortby_index = None if args.sort else 1 - policies = service.policies(**queries) - formatters = {} - if not args.full_id: - formatters = { - 'id': lambda x: x.id[:8] - } - utils.print_list(policies, fields, formatters=formatters, - sortby_index=sortby_index) - - -def _show_policy(service, policy_id): - try: - policy = service.get_policy(policy_id) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_('Policy not found: %s') % policy_id) - - formatters = { - 'metadata': utils.json_formatter, - 'spec': utils.json_formatter, - } - utils.print_dict(policy.to_dict(), formatters=formatters) - - -@utils.arg('-s', '--spec-file', metavar='', required=True, - help=_('The spec file used to create the policy.')) -@utils.arg('name', metavar='', - help=_('Name of the policy to create.')) -def do_policy_create(service, args): - """Create a policy.""" - show_deprecated('senlin policy-create', 'openstack cluster policy create') - spec = utils.get_spec_content(args.spec_file) - attrs = { - 'name': args.name, - 'spec': spec, - } - - policy = service.create_policy(**attrs) - _show_policy(service, policy.id) - - -@utils.arg('id', metavar='', - help=_('Name of the policy to be updated.')) -def do_policy_show(service, args): - """Show the policy details.""" - show_deprecated('senlin policy-show', 'openstack cluster policy show') - _show_policy(service, policy_id=args.id) - - -@utils.arg('-n', '--name', metavar='', - help=_('New name of the policy to be updated.')) -@utils.arg('id', metavar='', - help=_('Name of the policy to be updated.')) -def do_policy_update(service, args): - """Update a policy.""" - show_deprecated('senlin policy-update', 'openstack cluster policy update') - params = { - 'name': args.name, - } - - policy = service.get_policy(args.id) - if policy is not None: - service.update_policy(policy.id, **params) - _show_policy(service, policy_id=policy.id) - - -@utils.arg('id', metavar='', nargs='+', - help=_('Name or ID of policy(s) to delete.')) -def do_policy_delete(service, args): - """Delete policy(s).""" - show_deprecated('senlin policy-delete', 'openstack cluster policy delete') - failure_count = 0 - - for pid in args.id: - try: - service.delete_policy(pid, False) - except Exception as ex: - failure_count += 1 - print(ex) - if failure_count > 0: - msg = _('Failed to delete some of the specified policy(s).') - raise exc.CommandError(msg) - print('Policy deleted: %s' % args.id) - - -# CLUSTERS - - -@utils.arg('-f', '--filters', metavar='', - help=_('Filter parameters to apply on returned clusters. ' - 'This can be specified multiple times, or once with ' - 'parameters separated by a semicolon.'), - action='append') -@utils.arg('-o', '--sort', metavar='', - help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appended ' - 'by a sort direction (:asc or :desc)')) -@utils.arg('-l', '--limit', metavar='', - help=_('Limit the number of clusters returned.')) -@utils.arg('-m', '--marker', metavar='', - help=_('Only return clusters that appear after the given cluster ' - 'ID.')) -@utils.arg('-g', '--global-project', default=False, action="store_true", - help=_('Indicate that the cluster list should include clusters from' - ' all projects. This option is subject to access policy ' - 'checking. Default is False.')) -@utils.arg('-F', '--full-id', default=False, action="store_true", - help=_('Print full IDs in list.')) -def do_cluster_list(service, args=None): - """List the user's clusters.""" - show_deprecated('senlin cluster-list', 'openstack cluster list') - fields = ['id', 'name', 'status', 'created_at', 'updated_at'] - queries = { - 'limit': args.limit, - 'marker': args.marker, - 'sort': args.sort, - 'global_project': args.global_project, - } - if args.filters: - queries.update(utils.format_parameters(args.filters)) - - sortby_index = None if args.sort else 3 - - clusters = service.clusters(**queries) - formatters = {} - if not args.full_id: - formatters = { - 'id': lambda x: x.id[:8] - } - utils.print_list(clusters, fields, formatters=formatters, - sortby_index=sortby_index) - - -def _show_cluster(service, cluster_id): - try: - cluster = service.get_cluster(cluster_id) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_('Cluster not found: %s') % cluster_id) - - formatters = { - 'metadata': utils.json_formatter, - 'nodes': utils.list_formatter, - } - utils.print_dict(cluster.to_dict(), formatters=formatters) - - -@utils.arg('-p', '--profile', metavar='', required=True, - help=_('Profile Id used for this cluster.')) -@utils.arg('-n', '--min-size', metavar='', default=0, - help=_('Min size of the cluster. Default to 0.')) -@utils.arg('-m', '--max-size', metavar='', default=-1, - help=_('Max size of the cluster. Default to -1, means unlimited.')) -@utils.arg('-c', '--desired-capacity', metavar='', default=0, - help=_('Desired capacity of the cluster. Default to min_size if ' - 'min_size is specified else 0.')) -@utils.arg('-t', '--timeout', metavar='', type=int, - help=_('Cluster creation timeout in seconds.')) -@utils.arg('-M', '--metadata', metavar='', - help=_('Metadata values to be attached to the cluster. ' - 'This can be specified multiple times, or once with ' - 'key-value pairs separated by a semicolon.'), - action='append') -@utils.arg('name', metavar='', - help=_('Name of the cluster to create.')) -def do_cluster_create(service, args): - """Create the cluster.""" - show_deprecated('senlin cluster-create', 'openstack cluster create') - if args.min_size and not args.desired_capacity: - args.desired_capacity = args.min_size - attrs = { - 'name': args.name, - 'profile_id': args.profile, - 'min_size': args.min_size, - 'max_size': args.max_size, - 'desired_capacity': args.desired_capacity, - 'metadata': utils.format_parameters(args.metadata), - 'timeout': args.timeout - } - - cluster = service.create_cluster(**attrs) - _show_cluster(service, cluster.id) - - -@utils.arg('id', metavar='', nargs='+', - help=_('Name or ID of cluster(s) to delete.')) -def do_cluster_delete(service, args): - """Delete the cluster(s).""" - show_deprecated('senlin cluster-delete', 'openstack cluster delete') - failure_count = 0 - - for cid in args.id: - try: - service.delete_cluster(cid, False) - except Exception as ex: - failure_count += 1 - print(ex) - if failure_count > 0: - msg = _('Failed to delete some of the specified clusters.') - raise exc.CommandError(msg) - print('Request accepted') - - -@utils.arg('-p', '--profile', metavar='', - help=_('ID of new profile to use.')) -@utils.arg('-t', '--timeout', metavar='', - help=_('New timeout (in seconds) value for the cluster.')) -@utils.arg('-M', '--metadata', metavar='', - help=_('Metadata values to be attached to the cluster. ' - 'This can be specified multiple times, or once with ' - 'key-value pairs separated by a semicolon.'), - action='append') -@utils.arg('-n', '--name', metavar='', - help=_('New name for the cluster to update.')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to be updated.')) -def do_cluster_update(service, args): - """Update the cluster.""" - show_deprecated('senlin cluster-update', 'openstack cluster update') - cluster = service.get_cluster(args.id) - attrs = { - 'name': args.name, - 'profile_id': args.profile, - 'metadata': utils.format_parameters(args.metadata), - 'timeout': args.timeout, - } - - service.update_cluster(cluster.id, **attrs) - _show_cluster(service, cluster.id) - - -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to show.')) -def do_cluster_show(service, args): - """Show details of the cluster.""" - show_deprecated('senlin cluster-show', 'openstack cluster show') - _show_cluster(service, args.id) - - -@utils.arg('-f', '--filters', metavar='', - help=_('Filter parameters to apply on returned nodes. ' - 'This can be specified multiple times, or once with ' - 'parameters separated by a semicolon.'), - action='append') -@utils.arg('-l', '--limit', metavar='', - help=_('Limit the number of nodes returned.')) -@utils.arg('-m', '--marker', metavar='', - help=_('Only return nodes that appear after the given node ID.')) -@utils.arg('-F', '--full-id', default=False, action="store_true", - help=_('Print full IDs in list.')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to nodes from.')) -def do_cluster_node_list(service, args): - """List nodes from cluster.""" - show_deprecated('senlin cluster-node-list', - 'openstack cluster node members list') - queries = { - 'cluster_id': args.id, - 'limit': args.limit, - 'marker': args.marker, - } - if args.filters: - queries.update(utils.format_parameters(args.filters)) - - nodes = service.nodes(**queries) - if not args.full_id: - formatters = { - 'id': lambda x: x.id[:8], - 'physical_id': lambda x: x.physical_id[:8] if x.physical_id else '' - } - else: - formatters = {} - - fields = ['id', 'name', 'index', 'status', 'physical_id', 'created_at'] - utils.print_list(nodes, fields, formatters=formatters, sortby_index=5) - - -@utils.arg('-n', '--nodes', metavar='', required=True, - help=_('ID of nodes to be added; multiple nodes can be separated ' - 'with ","')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to operate on.')) -def do_cluster_node_add(service, args): - """Add specified nodes to cluster.""" - show_deprecated('senlin cluster-node-add', - 'openstack cluster node members add') - node_ids = args.nodes.split(',') - resp = service.cluster_add_nodes(args.id, node_ids) - print('Request accepted by action: %s' % resp['action']) - - -@utils.arg('-n', '--nodes', metavar='', required=True, - help=_('ID of nodes to be deleted; multiple nodes can be separated ' - 'with ",".')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to operate on.')) -def do_cluster_node_del(service, args): - """Delete specified nodes from cluster.""" - show_deprecated('senlin cluster-node-del', - 'openstack cluster node members del') - node_ids = args.nodes.split(',') - resp = service.cluster_del_nodes(args.id, node_ids) - print('Request accepted by action: %s' % resp['action']) - - -@utils.arg('-c', '--capacity', metavar='', type=int, - help=_('The desired number of nodes of the cluster.')) -@utils.arg('-a', '--adjustment', metavar='', type=int, - help=_('A positive integer meaning the number of nodes to add, ' - 'or a negative integer indicating the number of nodes to ' - 'remove.')) -@utils.arg('-p', '--percentage', metavar='', type=float, - help=_('A value that is interpreted as the percentage of size ' - 'adjustment. This value can be positive or negative.')) -@utils.arg('-t', '--min-step', metavar='', type=int, - help=_('An integer specifying the number of nodes for adjustment ' - 'when is specified.')) -@utils.arg('-s', '--strict', action='store_true', default=False, - help=_('A boolean specifying whether the resize should be ' - 'performed on a best-effort basis when the new capacity ' - 'may go beyond size constraints.')) -@utils.arg('-n', '--min-size', metavar='MIN', type=int, - help=_('New lower bound of cluster size.')) -@utils.arg('-m', '--max-size', metavar='MAX', type=int, - help=_('New upper bound of cluster size. A value of -1 indicates ' - 'no upper limit on cluster size.')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to operate on.')) -def do_cluster_resize(service, args): - """Resize a cluster.""" - # validate parameters - # NOTE: this will be much simpler if cliutils supports exclusive groups - show_deprecated('senlin cluster-resize', 'openstack cluster resize') - action_args = {} - - capacity = args.capacity - adjustment = args.adjustment - percentage = args.percentage - min_size = args.min_size - max_size = args.max_size - min_step = args.min_step - - if sum(v is not None for v in (capacity, adjustment, percentage)) > 1: - raise exc.CommandError(_("Only one of 'capacity', 'adjustment' and " - "'percentage' can be specified.")) - - action_args['adjustment_type'] = None - action_args['number'] = None - - if capacity is not None: - if capacity < 0: - raise exc.CommandError(_('Cluster capacity must be larger than ' - ' or equal to zero.')) - action_args['adjustment_type'] = 'EXACT_CAPACITY' - action_args['number'] = capacity - - if adjustment is not None: - if adjustment == 0: - raise exc.CommandError(_('Adjustment cannot be zero.')) - action_args['adjustment_type'] = 'CHANGE_IN_CAPACITY' - action_args['number'] = adjustment - - if percentage is not None: - if (percentage == 0 or percentage == 0.0): - raise exc.CommandError(_('Percentage cannot be zero.')) - action_args['adjustment_type'] = 'CHANGE_IN_PERCENTAGE' - action_args['number'] = percentage - - if min_step is not None: - if percentage is None: - raise exc.CommandError(_('Min step is only used with percentage.')) - - if min_size is not None: - if min_size < 0: - raise exc.CommandError(_('Min size cannot be less than zero.')) - if max_size is not None and max_size >= 0 and min_size > max_size: - raise exc.CommandError(_('Min size cannot be larger than ' - 'max size.')) - if capacity is not None and min_size > capacity: - raise exc.CommandError(_('Min size cannot be larger than the ' - 'specified capacity')) - - if max_size is not None: - if capacity is not None and max_size > 0 and max_size < capacity: - raise exc.CommandError(_('Max size cannot be less than the ' - 'specified capacity.')) - # do a normalization - if max_size < 0: - max_size = -1 - - action_args['min_size'] = min_size - action_args['max_size'] = max_size - action_args['min_step'] = min_step - action_args['strict'] = args.strict - - resp = service.cluster_resize(args.id, **action_args) - print('Request accepted by action: %s' % resp['action']) - - -@utils.arg('-c', '--count', metavar='', - help=_('Number of nodes to be added to the specified cluster.')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to operate on.')) -def do_cluster_scale_out(service, args): - """Scale out a cluster by the specified number of nodes.""" - show_deprecated('senlin cluster-scale-out', 'openstack cluster scale out') - resp = service.cluster_scale_out(args.id, args.count) - print('Request accepted by action %s' % resp['action']) - - -@utils.arg('-c', '--count', metavar='', - help=_('Number of nodes to be deleted from the specified cluster.')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to operate on.')) -def do_cluster_scale_in(service, args): - """Scale in a cluster by the specified number of nodes.""" - show_deprecated('senlin cluster-scale-in', 'openstack cluster scale in') - resp = service.cluster_scale_in(args.id, args.count) - print('Request accepted by action %s' % resp['action']) - - -@utils.arg('-f', '--filters', metavar='', - help=_('Filter parameters to apply on returned results. ' - 'This can be specified multiple times, or once with ' - 'parameters separated by a semicolon.'), - action='append') -@utils.arg('-o', '--sort', metavar='', - help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appended ' - 'by a sort direction (:asc or :desc)')) -@utils.arg('-F', '--full-id', default=False, action="store_true", - help=_('Print full IDs in list.')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to query on.')) -def do_cluster_policy_list(service, args): - """List policies from cluster.""" - show_deprecated('senlin cluster-policy-list', - 'openstack cluster policy binding list') - fields = ['policy_id', 'policy_name', 'policy_type', 'enabled'] - - cluster = service.get_cluster(args.id) - queries = { - 'sort': args.sort, - } - - if args.filters: - queries.update(utils.format_parameters(args.filters)) - - sortby_index = None if args.sort else 3 - policies = service.cluster_policies(cluster.id, **queries) - formatters = {} - if not args.full_id: - formatters = { - 'policy_id': lambda x: x.id[:8] - } - - utils.print_list(policies, fields, formatters=formatters, - sortby_index=sortby_index) - - -@utils.arg('-p', '--policy', metavar='', required=True, - help=_('ID or name of the policy to query on.')) -@utils.arg('id', metavar='', - help=_('ID or name of the cluster to query on.')) -def do_cluster_policy_show(service, args): - """Show a specific policy that is bound to the specified cluster.""" - show_deprecated('senlin cluster-policy-show', - 'openstack cluster policy binding show') - binding = service.get_cluster_policy(args.policy, args.id) - utils.print_dict(binding.to_dict()) - - -@utils.arg('-p', '--policy', metavar='', required=True, - help=_('ID or name of policy to be attached.')) -@utils.arg('-e', '--enabled', default=True, action="store_true", - help=_('Whether the policy should be enabled once attached. ' - 'Default to enabled.')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to operate on.')) -def do_cluster_policy_attach(service, args): - """Attach policy to cluster.""" - show_deprecated('senlin cluster-policy-attach', - 'openstack cluster policy attach') - kwargs = { - 'enabled': args.enabled, - } - - resp = service.cluster_attach_policy(args.id, args.policy, **kwargs) - print('Request accepted by action: %s' % resp['action']) - - -@utils.arg('-p', '--policy', metavar='', required=True, - help=_('ID or name of policy to be detached.')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to operate on.')) -def do_cluster_policy_detach(service, args): - """Detach policy from cluster.""" - show_deprecated('senlin cluster-policy-detach', - 'openstack cluster policy detach') - resp = service.cluster_detach_policy(args.id, args.policy) - print('Request accepted by action %s' % resp['action']) - - -@utils.arg('-p', '--policy', metavar='', required=True, - help=_('ID or name of policy to be updated.')) -@utils.arg('-e', '--enabled', metavar='', - help=_('Whether the policy should be enabled.')) -@utils.arg('id', metavar='', - help=_('Name or ID of cluster to operate on.')) -def do_cluster_policy_update(service, args): - """Update a policy's properties on a cluster.""" - show_deprecated('senlin cluster-policy-update', - 'openstack cluster policy binding update') - kwargs = { - 'enabled': args.enabled, - } - - resp = service.cluster_update_policy(args.id, args.policy, **kwargs) - print('Request accepted by action: %s' % resp['action']) - - -@utils.arg('id', metavar='', nargs='+', - help=_('ID or name of cluster(s) to operate on.')) -def do_cluster_check(service, args): - """Check the cluster(s).""" - show_deprecated('senlin cluster-check', 'openstack cluster check') - for cid in args.id: - resp = service.check_cluster(cid) - print('Cluster check request on cluster %(cid)s is accepted by ' - 'action %(action)s.' % {'cid': cid, 'action': resp['action']}) - - -@utils.arg('id', metavar='', nargs='+', - help=_('ID or name of cluster(s) to operate on.')) -def do_cluster_recover(service, args): - """Recover the cluster(s).""" - show_deprecated('senlin cluster-recover', 'openstack cluster recover') - for cid in args.id: - resp = service.recover_cluster(cid) - print('Cluster recover request on cluster %(cid)s is accepted by ' - 'action %(action)s.' % {'cid': cid, 'action': resp['action']}) - - -# NODES - - -@utils.arg('-c', '--cluster', metavar='', - help=_('ID or name of cluster from which nodes are to be listed.')) -@utils.arg('-f', '--filters', metavar='', - help=_('Filter parameters to apply on returned nodes. ' - 'This can be specified multiple times, or once with ' - 'parameters separated by a semicolon.'), - action='append') -@utils.arg('-o', '--sort', metavar='', - help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appended ' - 'by a sort direction (:asc or :desc)')) -@utils.arg('-l', '--limit', metavar='', - help=_('Limit the number of nodes returned.')) -@utils.arg('-m', '--marker', metavar='', - help=_('Only return nodes that appear after the given node ID.')) -@utils.arg('-g', '--global-project', default=False, action="store_true", - help=_('Indicate that this node list should include nodes from ' - 'all projects. This option is subject to access policy ' - 'checking. Default is False.')) -@utils.arg('-F', '--full-id', default=False, action="store_true", - help=_('Print full IDs in list.')) -def do_node_list(service, args): - """Show list of nodes.""" - show_deprecated('senlin node-list', 'openstack cluster node list') - - fields = ['id', 'name', 'index', 'status', 'cluster_id', 'physical_id', - 'profile_name', 'created_at', 'updated_at'] - queries = { - 'cluster_id': args.cluster, - 'sort': args.sort, - 'limit': args.limit, - 'marker': args.marker, - 'global_project': args.global_project, - } - - if args.filters: - queries.update(utils.format_parameters(args.filters)) - - sortby_index = None if args.sort else 6 - - nodes = service.nodes(**queries) - - if not args.full_id: - formatters = { - 'id': lambda x: x.id[:8], - 'cluster_id': lambda x: x.cluster_id[:8] if x.cluster_id else '', - 'physical_id': lambda x: x.physical_id[:8] if x.physical_id else '' - } - else: - formatters = {} - - utils.print_list(nodes, fields, formatters=formatters, - sortby_index=sortby_index) - - -def _show_node(service, node_id, show_details=False): - """Show detailed info about the specified node.""" - args = {'show_details': True} if show_details else None - try: - node = service.get_node(node_id, args=args) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_('Node not found: %s') % node_id) - - formatters = { - 'metadata': utils.json_formatter, - 'data': utils.json_formatter, - } - data = node.to_dict() - if show_details: - formatters['details'] = utils.nested_dict_formatter( - list(node['details'].keys()), ['property', 'value']) - - utils.print_dict(data, formatters=formatters) - - -@utils.arg('-p', '--profile', metavar='', required=True, - help=_('Profile Id used for this node.')) -@utils.arg('-c', '--cluster', metavar='', - help=_('Cluster Id for this node.')) -@utils.arg('-r', '--role', metavar='', - help=_('Role for this node in the specific cluster.')) -@utils.arg('-M', '--metadata', metavar='', - help=_('Metadata values to be attached to the node. ' - 'This can be specified multiple times, or once with ' - 'key-value pairs separated by a semicolon.'), - action='append') -@utils.arg('name', metavar='', - help=_('Name of the node to create.')) -def do_node_create(service, args): - """Create the node.""" - show_deprecated('senlin node-create', 'openstack cluster node create') - attrs = { - 'name': args.name, - 'cluster_id': args.cluster, - 'profile_id': args.profile, - 'role': args.role, - 'metadata': utils.format_parameters(args.metadata), - } - - node = service.create_node(**attrs) - _show_node(service, node.id) - - -@utils.arg('-D', '--details', default=False, action="store_true", - help=_('Include physical object details.')) -@utils.arg('id', metavar='', - help=_('Name or ID of the node to show the details for.')) -def do_node_show(service, args): - """Show detailed info about the specified node.""" - show_deprecated('senlin node-show', 'openstack cluster node show') - _show_node(service, args.id, args.details) - - -@utils.arg('id', metavar='', nargs='+', - help=_('Name or ID of node(s) to delete.')) -def do_node_delete(service, args): - """Delete the node(s).""" - show_deprecated('senlin node-delete', 'openstack cluster node delete') - failure_count = 0 - - for nid in args.id: - try: - service.delete_node(nid, False) - except Exception as ex: - failure_count += 1 - print(ex) - if failure_count > 0: - msg = _('Failed to delete some of the specified nodes.') - raise exc.CommandError(msg) - print('Request accepted') - - -@utils.arg('-n', '--name', metavar='', - help=_('New name for the node.')) -@utils.arg('-p', '--profile', metavar='', - help=_('ID of new profile to use.')) -@utils.arg('-r', '--role', metavar='', - help=_('Role for this node in the specific cluster.')) -@utils.arg('-M', '--metadata', metavar='', - help=_('Metadata values to be attached to the node. ' - 'Metadata can be specified multiple times, or once with ' - 'key-value pairs separated by a semicolon.'), - action='append') -@utils.arg('id', metavar='', - help=_('Name or ID of node to update.')) -def do_node_update(service, args): - """Update the node.""" - show_deprecated('senlin node-update', 'openstack cluster node update') - # Find the node first, we need its UUID - try: - node = service.get_node(args.id) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_('Node not found: %s') % args.id) - - attrs = { - 'name': args.name, - 'role': args.role, - 'profile_id': args.profile, - 'metadata': utils.format_parameters(args.metadata), - } - - service.update_node(args.id, **attrs) - _show_node(service, node.id) - - -@utils.arg('id', metavar='', nargs='+', - help=_('ID of node(s) to check.')) -def do_node_check(service, args): - """Check the node(s).""" - show_deprecated('senlin node-check', 'openstack cluster node check') - failure_count = 0 - - for nid in args.id: - try: - service.check_node(nid) - except exc.HTTPNotFound: - failure_count += 1 - print('Node id "%s" not found' % nid) - if failure_count > 0: - msg = _('Failed to check some of the specified nodes.') - raise exc.CommandError(msg) - print('Request accepted') - - -@utils.arg('id', metavar='', nargs='+', - help=_('ID of node(s) to recover.')) -def do_node_recover(service, args): - """Recover the node(s).""" - show_deprecated('senlin node-recover', 'openstack cluster node recover') - failure_count = 0 - - for nid in args.id: - try: - service.recover_node(nid) - except exc.HTTPNotFound: - failure_count += 1 - print('Node id "%s" not found' % nid) - if failure_count > 0: - msg = _('Failed to recover some of the specified nodes.') - raise exc.CommandError(msg) - print('Request accepted') - - -# RECEIVERS - - -@utils.arg('-f', '--filters', metavar='', - help=_('Filter parameters to apply on returned receivers. ' - 'This can be specified multiple times, or once with ' - 'parameters separated by a semicolon.'), - action='append') -@utils.arg('-l', '--limit', metavar='', - help=_('Limit the number of receivers returned.')) -@utils.arg('-m', '--marker', metavar='', - help=_('Only return receivers that appear after the given ID.')) -@utils.arg('-o', '--sort', metavar='', - help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appended ' - 'by a sort direction (:asc or :desc)')) -@utils.arg('-g', '--global-project', default=False, action="store_true", - help=_('Indicate that the list should include receivers from' - ' all projects. This option is subject to access policy ' - 'checking. Default is False.')) -@utils.arg('-F', '--full-id', default=False, action="store_true", - help=_('Print full IDs in list.')) -def do_receiver_list(service, args): - """List receivers that meet the criteria.""" - show_deprecated('senlin receiver-list', 'openstack cluster receiver list') - fields = ['id', 'name', 'type', 'cluster_id', 'action', 'created_at'] - queries = { - 'limit': args.limit, - 'marker': args.marker, - 'sort': args.sort, - 'global_project': args.global_project, - } - - if args.filters: - queries.update(utils.format_parameters(args.filters)) - - sortby_index = None if args.sort else 0 - - receivers = service.receivers(**queries) - formatters = {} - if not args.full_id: - formatters = { - 'id': lambda x: x.id[:8], - 'cluster_id': lambda x: x.cluster_id[:8], - } - utils.print_list(receivers, fields, formatters=formatters, - sortby_index=sortby_index) - - -def _show_receiver(service, receiver_id): - try: - receiver = service.get_receiver(receiver_id) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_('Receiver not found: %s') % receiver_id) - - formatters = { - 'actor': utils.json_formatter, - 'params': utils.json_formatter, - 'channel': utils.json_formatter, - } - - utils.print_dict(receiver.to_dict(), formatters=formatters) - - -@utils.arg('id', metavar='', - help=_('Name or ID of the receiver to show.')) -def do_receiver_show(service, args): - """Show the receiver details.""" - show_deprecated('senlin receiver-show', 'openstack cluster receiver show') - _show_receiver(service, receiver_id=args.id) - - -@utils.arg('-t', '--type', metavar='', default='webhook', - help=_('Type of the receiver to create.')) -@utils.arg('-c', '--cluster', metavar='', required=True, - help=_('Targeted cluster for this receiver.')) -@utils.arg('-a', '--action', metavar='', required=True, - help=_('Name or ID of the targeted action to be triggered.')) -@utils.arg('-P', '--params', metavar='', - help=_('A dictionary of parameters that will be passed to target ' - 'action when the receiver is triggered.'), - action='append') -@utils.arg('name', metavar='', - help=_('Name of the receiver to create.')) -def do_receiver_create(service, args): - """Create a receiver.""" - show_deprecated('senlin receiver-create', - 'openstack cluster receiver create') - - params = { - 'name': args.name, - 'type': args.type, - 'cluster_id': args.cluster, - 'action': args.action, - 'params': utils.format_parameters(args.params) - } - - receiver = service.create_receiver(**params) - _show_receiver(service, receiver.id) - - -@utils.arg('id', metavar='', nargs='+', - help=_('Name or ID of receiver(s) to delete.')) -def do_receiver_delete(service, args): - """Delete receiver(s).""" - show_deprecated('senlin receiver-delete', - 'openstack cluster receiver delete') - failure_count = 0 - - for wid in args.id: - try: - service.delete_receiver(wid, False) - except Exception as ex: - failure_count += 1 - print(ex) - if failure_count > 0: - msg = _('Failed to delete some of the specified receiver(s).') - raise exc.CommandError(msg) - print('Receivers deleted: %s' % args.id) - - -# EVENTS - - -@utils.arg('-f', '--filters', metavar='', - help=_('Filter parameters to apply on returned events. ' - 'This can be specified multiple times, or once with ' - 'parameters separated by a semicolon.'), - action='append') -@utils.arg('-l', '--limit', metavar='', - help=_('Limit the number of events returned.')) -@utils.arg('-m', '--marker', metavar='', - help=_('Only return events that appear after the given event ID.')) -@utils.arg('-o', '--sort', metavar='', - help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appended ' - 'by a sort direction (:asc or :desc)')) -@utils.arg('-g', '--global-project', default=False, action="store_true", - help=_('Whether events from all projects should be listed. ' - ' Default to False. Setting this to True may demand ' - 'for an admin privilege.')) -@utils.arg('-F', '--full-id', default=False, action="store_true", - help=_('Print full IDs in list.')) -def do_event_list(service, args): - """List events.""" - show_deprecated('senlin event-list', 'openstack cluster event list') - fields = ['id', 'timestamp', 'obj_type', 'obj_id', 'obj_name', 'action', - 'status', 'status_reason', 'level'] - queries = { - 'sort': args.sort, - 'limit': args.limit, - 'marker': args.marker, - 'global_project': args.global_project, - } - - if args.filters: - queries.update(utils.format_parameters(args.filters)) - - formatters = {} - if not args.full_id: - formatters['id'] = lambda x: x.id[:8] - formatters['obj_id'] = lambda x: x.obj_id[:8] if x.obj_id else '' - - events = service.events(**queries) - utils.print_list(events, fields, formatters=formatters) - - -@utils.arg('id', metavar='', - help=_('ID of event to display details for.')) -def do_event_show(service, args): - """Describe the event.""" - show_deprecated('senlin event-show', 'openstack cluster event show') - try: - event = service.get_event(args.id) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_("Event not found: %s") % args.id) - - utils.print_dict(event.to_dict()) - - -# ACTIONS - - -@utils.arg('-f', '--filters', metavar='', - help=_('Filter parameters to apply on returned actions. ' - 'This can be specified multiple times, or once with ' - 'parameters separated by a semicolon.'), - action='append') -@utils.arg('-o', '--sort', metavar='', - help=_('Sorting option which is a string containing a list of keys ' - 'separated by commas. Each key can be optionally appended ' - 'by a sort direction (:asc or :desc)')) -@utils.arg('-l', '--limit', metavar='', - help=_('Limit the number of actions returned.')) -@utils.arg('-m', '--marker', metavar='', - help=_('Only return actions that appear after the given node ID.')) -@utils.arg('-F', '--full-id', default=False, action="store_true", - help=_('Print full IDs in list.')) -def do_action_list(service, args): - """List actions.""" - show_deprecated('senlin action-list', 'openstack cluster action list') - fields = ['id', 'name', 'action', 'status', 'target', 'depends_on', - 'depended_by', 'created_at'] - - queries = { - 'sort': args.sort, - 'limit': args.limit, - 'marker': args.marker, - } - - if args.filters: - queries.update(utils.format_parameters(args.filters)) - - sortby_index = None if args.sort else 0 - - actions = service.actions(**queries) - - formatters = {} - if args.full_id: - f_depon = lambda x: '\n'.join(a for a in x.depends_on) - f_depby = lambda x: '\n'.join(a for a in x.depended_by) - - formatters['depends_on'] = f_depon - formatters['depended_by'] = f_depby - else: - formatters['id'] = lambda x: x.id[:8] - formatters['target'] = lambda x: x.target[:8] - f_depon = lambda x: '\n'.join(a[:8] for a in x.depends_on) - f_depby = lambda x: '\n'.join(a[:8] for a in x.depended_by) - formatters['depends_on'] = f_depon - formatters['depended_by'] = f_depby - - utils.print_list(actions, fields, formatters=formatters, - sortby_index=sortby_index) - - -@utils.arg('id', metavar='', - help=_('Name or ID of the action to show the details for.')) -def do_action_show(service, args): - """Show detailed info about the specified action.""" - show_deprecated('senlin action-show', 'openstack cluster action show') - try: - action = service.get_action(args.id) - except sdk_exc.ResourceNotFound: - raise exc.CommandError(_('Action not found: %s') % args.id) - - formatters = { - 'inputs': utils.json_formatter, - 'outputs': utils.json_formatter, - 'metadata': utils.json_formatter, - 'data': utils.json_formatter, - 'depends_on': utils.list_formatter, - 'depended_by': utils.list_formatter, - } - - utils.print_dict(action.to_dict(), formatters=formatters) diff --git a/setup.cfg b/setup.cfg index 65a456d..c55ba74 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,9 +23,6 @@ packages = senlinclient [entry_points] -console_scripts = - senlin = senlinclient.shell:main - openstack.cli.extension = clustering = senlinclient.plugin From 9c261fd848064c1c9d14e2de0af5547297be3e4e Mon Sep 17 00:00:00 2001 From: tengqm Date: Fri, 25 Mar 2016 04:32:27 -0400 Subject: [PATCH 06/25] Rename cluster scaling command This patch renames the 'cluster scale in' and 'cluster scale out' commands to 'cluster shrink' and 'cluster expand' respectively. Change-Id: I70a2172bb08dcdb920922c25d3340ef7afd13a66 --- setup.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index c55ba74..b4027d4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -71,8 +71,8 @@ openstack.clustering.v1 = cluster_receiver_show = senlinclient.v1.receiver:ShowReceiver cluster_recover = senlinclient.v1.cluster:RecoverCluster cluster_resize = senlinclient.v1.cluster:ResizeCluster - cluster_scale_in = senlinclient.v1.cluster:ScaleInCluster - cluster_scale_out = senlinclient.v1.cluster:ScaleOutCluster + cluster_shrink = senlinclient.v1.cluster:ScaleInCluster + cluster_expand = senlinclient.v1.cluster:ScaleOutCluster cluster_show = senlinclient.v1.cluster:ShowCluster cluster_update = senlinclient.v1.cluster:UpdateCluster From 02f5a76d0a1069eb32ad9a1a7d0bd752d2b10a07 Mon Sep 17 00:00:00 2001 From: yanyanhu Date: Mon, 28 Mar 2016 05:49:33 -0400 Subject: [PATCH 07/25] Support more parameters for senlinclient creation This patch revises senlinclient initialization method to allow user to define more parameters like region_name, interface when creating senlinclient. Change-Id: I490c454ddee6a7d880dab131b1854a47a92b930b Closes-Bug: #1562730 --- senlinclient/common/sdk.py | 9 ++++++ senlinclient/tests/unit/test_sdk.py | 47 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/senlinclient/common/sdk.py b/senlinclient/common/sdk.py index e853926..4850744 100644 --- a/senlinclient/common/sdk.py +++ b/senlinclient/common/sdk.py @@ -124,6 +124,15 @@ class Resource(base.Resource): def create_connection(prof=None, user_agent=None, **kwargs): + if not prof: + prof = profile.Profile() + interface = kwargs.pop('interface', None) + region_name = kwargs.pop('region_name', None) + if interface: + prof.set_interface('clustering', interface) + if region_name: + prof.set_region('clustering', region_name) + try: conn = connection.Connection(profile=prof, user_agent=user_agent, **kwargs) diff --git a/senlinclient/tests/unit/test_sdk.py b/senlinclient/tests/unit/test_sdk.py index 3654a19..dbbb0ce 100644 --- a/senlinclient/tests/unit/test_sdk.py +++ b/senlinclient/tests/unit/test_sdk.py @@ -14,6 +14,9 @@ import mock import os import testtools +from openstack import connection as sdk_connection +from openstack import profile as sdk_profile + from senlinclient.common import sdk @@ -54,3 +57,47 @@ class TestSdk(testtools.TestCase): mock_prof.ALL = 'mock_prof.ALL' sdk.ProfileAction.set_option('interface', 'test=val1') mock_prof.set_interface.assert_called_once_with('test', 'val1') + + @mock.patch.object(sdk_connection, 'Connection') + def test_create_connection_with_profile(self, mock_connection): + mock_prof = mock.Mock() + mock_conn = mock.Mock() + mock_connection.return_value = mock_conn + kwargs = { + 'user_id': '123', + 'password': 'abc', + 'auth_url': 'test_url' + } + res = sdk.create_connection(mock_prof, **kwargs) + mock_connection.assert_called_once_with(profile=mock_prof, + user_agent=None, + user_id='123', + password='abc', + auth_url='test_url') + self.assertEqual(mock_conn, res) + + @mock.patch.object(sdk_connection, 'Connection') + @mock.patch.object(sdk_profile, 'Profile') + def test_create_connection_without_profile(self, mock_profile, + mock_connection): + mock_prof = mock.Mock() + mock_conn = mock.Mock() + mock_profile.return_value = mock_prof + mock_connection.return_value = mock_conn + kwargs = { + 'interface': 'public', + 'region_name': 'RegionOne', + 'user_id': '123', + 'password': 'abc', + 'auth_url': 'test_url' + } + res = sdk.create_connection(**kwargs) + + mock_prof.set_interface.assert_called_once_with('clustering', 'public') + mock_prof.set_region.assert_called_once_with('clustering', 'RegionOne') + mock_connection.assert_called_once_with(profile=mock_prof, + user_agent=None, + user_id='123', + password='abc', + auth_url='test_url') + self.assertEqual(mock_conn, res) From ece213914bcce0b806612439084a822ce3f68654 Mon Sep 17 00:00:00 2001 From: tengqm Date: Thu, 31 Mar 2016 21:56:01 -0400 Subject: [PATCH 08/25] Revert "Remove senlin CLI commands" This reverts commit 3bda3b997bf3701746c7a2e4760f9528a18a2cbd. We cannot rely on openstackclient as it is today. There are OSC parameters preventing senlin client from normal operation and we see no easy resolution when both OSC and senlinclient has been released. Change-Id: I6b09699afcd45d36caea3e9f6ee0d132cc57354f --- senlinclient/cliargs.py | 210 ++++ senlinclient/shell.py | 321 ++++++ senlinclient/tests/unit/test_cliargs.py | 78 ++ senlinclient/tests/unit/test_shell.py | 373 ++++++ senlinclient/tests/unit/v1/test_shell.py | 1348 ++++++++++++++++++++++ senlinclient/v1/shell.py | 1314 +++++++++++++++++++++ setup.cfg | 3 + 7 files changed, 3647 insertions(+) create mode 100644 senlinclient/cliargs.py create mode 100644 senlinclient/shell.py create mode 100644 senlinclient/tests/unit/test_cliargs.py create mode 100644 senlinclient/tests/unit/test_shell.py create mode 100644 senlinclient/tests/unit/v1/test_shell.py create mode 100644 senlinclient/v1/shell.py diff --git a/senlinclient/cliargs.py b/senlinclient/cliargs.py new file mode 100644 index 0000000..694d017 --- /dev/null +++ b/senlinclient/cliargs.py @@ -0,0 +1,210 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# 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 argparse + +from senlinclient.common.i18n import _ +from senlinclient.common import sdk +from senlinclient.common import utils + + +def add_global_identity_args(parser): + parser.add_argument( + '--os-auth-plugin', dest='auth_plugin', metavar='AUTH_PLUGIN', + default=utils.env('OS_AUTH_PLUGIN', default=None), + help=_('Authentication plugin, default to env[OS_AUTH_PLUGIN]')) + + parser.add_argument( + '--os-auth-url', dest='auth_url', metavar='AUTH_URL', + default=utils.env('OS_AUTH_URL'), + help=_('Defaults to env[OS_AUTH_URL]')) + + parser.add_argument( + '--os-project-id', dest='project_id', metavar='PROJECT_ID', + default=utils.env('OS_PROJECT_ID'), + help=_('Defaults to env[OS_PROJECT_ID].')) + + parser.add_argument( + '--os-project-name', dest='project_name', metavar='PROJECT_NAME', + default=utils.env('OS_PROJECT_NAME'), + help=_('Defaults to env[OS_PROJECT_NAME].')) + + parser.add_argument( + '--os-tenant-id', dest='tenant_id', metavar='TENANT_ID', + default=utils.env('OS_TENANT_ID'), + help=_('Defaults to env[OS_TENANT_ID].')) + + parser.add_argument( + '--os-tenant-name', dest='tenant_name', metavar='TENANT_NAME', + default=utils.env('OS_TENANT_NAME'), + help=_('Defaults to env[OS_TENANT_NAME].')) + + parser.add_argument( + '--os-domain-id', dest='domain_id', metavar='DOMAIN_ID', + default=utils.env('OS_DOMAIN_ID'), + help=_('Domain ID for scope of authorization, defaults to ' + 'env[OS_DOMAIN_ID].')) + + parser.add_argument( + '--os-domain-name', dest='domain_name', metavar='DOMAIN_NAME', + default=utils.env('OS_DOMAIN_NAME'), + help=_('Domain name for scope of authorization, defaults to ' + 'env[OS_DOMAIN_NAME].')) + + parser.add_argument( + '--os-project-domain-id', dest='project_domain_id', + metavar='PROJECT_DOMAIN_ID', + default=utils.env('OS_PROJECT_DOMAIN_ID'), + help=_('Project domain ID for scope of authorization, defaults to ' + 'env[OS_PROJECT_DOMAIN_ID].')) + + parser.add_argument( + '--os-project-domain-name', dest='project_domain_name', + metavar='PROJECT_DOMAIN_NAME', + default=utils.env('OS_PROJECT_DOMAIN_NAME'), + help=_('Project domain name for scope of authorization, defaults to ' + 'env[OS_PROJECT_DOMAIN_NAME].')) + + parser.add_argument( + '--os-user-domain-id', dest='user_domain_id', + metavar='USER_DOMAIN_ID', + default=utils.env('OS_USER_DOMAIN_ID'), + help=_('User domain ID for scope of authorization, defaults to ' + 'env[OS_USER_DOMAIN_ID].')) + + parser.add_argument( + '--os-user-domain-name', dest='user_domain_name', + metavar='USER_DOMAIN_NAME', + default=utils.env('OS_USER_DOMAIN_NAME'), + help=_('User domain name for scope of authorization, defaults to ' + 'env[OS_USER_DOMAIN_NAME].')) + + parser.add_argument( + '--os-username', dest='username', metavar='USERNAME', + default=utils.env('OS_USERNAME'), + help=_('Defaults to env[OS_USERNAME].')) + + parser.add_argument( + '--os-user-id', dest='user_id', metavar='USER_ID', + default=utils.env('OS_USER_ID'), + help=_('Defaults to env[OS_USER_ID].')) + + parser.add_argument( + '--os-password', dest='password', metavar='PASSWORD', + default=utils.env('OS_PASSWORD'), + help=_('Defaults to env[OS_PASSWORD]')) + + parser.add_argument( + '--os-trust-id', dest='trust_id', metavar='TRUST_ID', + default=utils.env('OS_TRUST_ID'), + help=_('Defaults to env[OS_TRUST_ID]')) + + verify_group = parser.add_mutually_exclusive_group() + + verify_group.add_argument( + '--os-cacert', dest='verify', metavar='CA_BUNDLE_FILE', + default=utils.env('OS_CACERT', default=True), + help=_('Path of CA TLS certificate(s) used to verify the remote ' + 'server\'s certificate. Without this option senlin looks ' + 'for the default system CA certificates.')) + + verify_group.add_argument( + '--verify', + action='store_true', + help=_('Verify server certificate (default)')) + + verify_group.add_argument( + '--insecure', dest='verify', action='store_false', + help=_('Explicitly allow senlinclient to perform "insecure SSL" ' + '(HTTPS) requests. The server\'s certificate will not be ' + 'verified against any certificate authorities. This ' + 'option should be used with caution.')) + + parser.add_argument( + '--os-token', dest='token', metavar='TOKEN', + default=utils.env('OS_TOKEN', default=None), + help=_('A string token to bootstrap the Keystone database, defaults ' + 'to env[OS_TOKEN]')) + + parser.add_argument( + '--os-access-info', dest='access_info', metavar='ACCESS_INFO', + default=utils.env('OS_ACCESS_INFO'), + help=_('Access info, defaults to env[OS_ACCESS_INFO]')) + + parser.add_argument( + '--os-api-name', dest='user_preferences', + metavar='=', + action=sdk.ProfileAction, + default=sdk.ProfileAction.env('OS_API_NAME'), + help=_('Desired API names, defaults to env[OS_API_NAME]')) + + parser.add_argument( + '--os-api-region', dest='user_preferences', + metavar='=', + action=sdk.ProfileAction, + default=sdk.ProfileAction.env('OS_API_REGION', 'OS_REGION_NAME'), + help=_('Desired API region, defaults to env[OS_API_REGION]')) + + parser.add_argument( + '--os-api-version', dest='user_preferences', + metavar='=', + action=sdk.ProfileAction, + default=sdk.ProfileAction.env('OS_API_VERSION'), + help=_('Desired API versions, defaults to env[OS_API_VERSION]')) + + parser.add_argument( + '--os-api-interface', dest='user_preferences', + metavar='=', + action=sdk.ProfileAction, + default=sdk.ProfileAction.env('OS_INTERFACE'), + help=_('Desired API interface, defaults to env[OS_INTERFACE]')) + + +# parser.add_argument( +# '--os-cert', +# help=_('Path of certificate file to use in SSL connection. This ' +# 'file can optionally be prepended with the private key.')) +# +# parser.add_argument( +# '--os-key', +# help=_('Path of client key to use in SSL connection. This option is ' +# 'not necessary if your key is prepended to your cert file.')) + + +def add_global_args(parser, version): + # GLOBAL ARGUMENTS + parser.add_argument( + '-h', '--help', action='store_true', + help=argparse.SUPPRESS) + + parser.add_argument( + '--version', action='version', version=version, + help=_("Shows the client version and exits.")) + + parser.add_argument( + '-d', '--debug', action='store_true', + default=bool(utils.env('SENLINCLIENT_DEBUG')), + help=_('Defaults to env[SENLINCLIENT_DEBUG].')) + + parser.add_argument( + '-v', '--verbose', action="store_true", default=False, + help=_("Print more verbose output.")) + + parser.add_argument( + '--api-timeout', + help=_('Number of seconds to wait for an API response, ' + 'defaults to system socket timeout')) + + parser.add_argument( + '--senlin-api-version', + default=utils.env('SENLIN_API_VERSION', default='1'), + help=_('Version number for Senlin API to use, Default to "1".')) diff --git a/senlinclient/shell.py b/senlinclient/shell.py new file mode 100644 index 0000000..5954982 --- /dev/null +++ b/senlinclient/shell.py @@ -0,0 +1,321 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Command-line interface to the Senlin clustering API. +""" + +from __future__ import print_function + +import argparse +import logging +import sys + +from oslo_utils import encodeutils +from oslo_utils import importutils +import six + +import senlinclient +from senlinclient import cliargs +from senlinclient import client as senlin_client +from senlinclient.common import exc +from senlinclient.common.i18n import _ +from senlinclient.common import utils + +osprofiler_profiler = importutils.try_import("osprofiler.profiler") +USER_AGENT = 'python-senlinclient' +LOG = logging.getLogger(__name__) + + +class HelpFormatter(argparse.HelpFormatter): + def start_section(self, heading): + # Title-case the headings + heading = '%s%s' % (heading[0].upper(), heading[1:]) + super(HelpFormatter, self).start_section(heading) + + +class SenlinShell(object): + def _setup_logging(self, debug): + log_lvl = logging.DEBUG if debug else logging.WARNING + logging.basicConfig(format="%(levelname)s (%(module)s) %(message)s", + level=log_lvl) + logging.getLogger('iso8601').setLevel(logging.WARNING) + logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING) + + def _setup_verbose(self, verbose): + if verbose: + exc.verbose = 1 + + def _find_actions(self, subparsers, actions_module): + for attr in (a for a in dir(actions_module) if a.startswith('do_')): + command = attr[3:].replace('_', '-') + callback = getattr(actions_module, attr) + + # get callback documentation string + desc = callback.__doc__ or '' + help = desc.strip().split('\n')[0] + arguments = getattr(callback, 'arguments', []) + + subparser = subparsers.add_parser(command, + help=help, + description=desc, + add_help=False, + formatter_class=HelpFormatter) + + subparser.add_argument('-h', '--help', + action='help', + help=argparse.SUPPRESS) + + for (args, kwargs) in arguments: + subparser.add_argument(*args, **kwargs) + subparser.set_defaults(func=callback) + + self.subcommands[command] = subparser + + def do_bash_completion(self, args): + """Prints all of the commands and options to stdout. + + The senlin.bash_completion script doesn't have to hard code them. + """ + commands = set() + options = set() + for sc_str, sc in self.subcommands.items(): + if sc_str == 'bash_completion' or sc_str == 'bash-completion': + continue + + commands.add(sc_str) + for option in list(sc._optionals._option_string_actions): + options.add(option) + + print(' '.join(commands | options)) + + def add_profiler_args(self, parser): + if osprofiler_profiler: + parser.add_argument( + '--profile', metavar='HMAC_KEY', + help=_('HMAC key to use for encrypting context data for ' + 'performance profiling of operation. This key should ' + 'be the value of HMAC key configured in osprofiler ' + 'middleware in senlin, it is specified in the paste ' + 'deploy configuration (/etc/senlin/api-paste.ini). ' + 'Without the key, profiling will not be triggered ' + 'even if osprofiler is enabled on server side.')) + + def _add_bash_completion_subparser(self, subparsers): + subparser = subparsers.add_parser('bash_completion', + add_help=False, + formatter_class=HelpFormatter) + + subparser.set_defaults(func=self.do_bash_completion) + self.subcommands['bash_completion'] = subparser + + def get_subcommand_parser(self, base_parser, version): + parser = base_parser + + self.subcommands = {} + subparsers = parser.add_subparsers(metavar='') + submodule = utils.import_versioned_module(version, 'shell') + self._find_actions(subparsers, submodule) + self._find_actions(subparsers, self) + self._add_bash_completion_subparser(subparsers) + + return parser + + @utils.arg('command', metavar='', nargs='?', + help=_('Display help for .')) + def do_help(self, args): + """Display help about this program or one of its subcommands.""" + if getattr(args, 'command', None): + if args.command in self.subcommands: + self.subcommands[args.command].print_help() + else: + raise exc.CommandError("'%s' is not a valid subcommand" % + args.command) + else: + self.parser.print_help() + + def _check_identity_arguments(self, args): + # TODO(Qiming): validate the token authentication path and the trust + # authentication path + + if not args.auth_url: + msg = _('You must provide an auth url via --os-auth-url (or ' + ' env[OS_AUTH_URL])') + raise exc.CommandError(msg) + + # username or user_id or token must be specified + if not (args.username or args.user_id or args.token): + msg = _('You must provide a user name, a user_id or a ' + 'token for authentication') + raise exc.CommandError(msg) + + # if both username and user_id are specified, user_id takes precedence + if (args.username and args.user_id): + msg = _('Both user name and user ID are specified, Senlin will ' + 'use user ID for authentication') + print(_('WARNING: %s') % msg) + + if 'v3' in args.auth_url: + if (args.username and not args.user_id): + if not (args.user_domain_id or args.user_domain_name): + msg = _('Either user domain ID (--user-domain-id / ' + 'env[OS_USER_DOMAIN_ID]) or user domain name ' + '(--user-domain-name / env[OS_USER_DOMAIN_NAME]) ' + 'must be specified, because user name may not be ' + 'unique.') + raise exc.CommandError(msg) + + # password is needed if username or user_id is present + if (args.username or args.user_id) and not (args.password): + msg = _('You must provide a password for user %s') % ( + args.username or args.user_id) + raise exc.CommandError(msg) + + # project name or ID is needed, or else sdk may find the wrong project + if (not (args.project_id or args.project_name or args.tenant_id + or args.tenant_name)): + if not (args.user_id): + msg = _('Either project/tenant ID or project/tenant name ' + 'must be specified, or else Senlin cannot know ' + 'which project to use.') + raise exc.CommandError(msg) + else: + msg = _('Neither project ID nor project name is specified. ' + 'Senlin will use user\'s default project which may ' + 'result in authentication error.') + print(_('WARNING: %s') % msg) + + # both project name and ID are specified, ID takes precedence + if ((args.project_id or args.tenant_id) and + (args.project_name or args.tenant_name)): + msg = _('Both project/tenant name and project/tenant ID are ' + 'specified, Senlin will use project ID for ' + 'authentication') + print(_('WARNING: %s') % msg) + + # project name may not be unique + if 'v3' in args.auth_url: + if (not (args.project_id or args.tenant_id) and + (args.project_name or args.tenant_name) and + not (args.project_domain_id or args.project_domain_name)): + msg = _('Either project domain ID (--project-domain-id / ' + 'env[OS_PROJECT_DOMAIN_ID]) orr project domain name ' + '(--project-domain-name / env[OS_PROJECT_DOMAIN_NAME ' + 'must be specified, because project/tenant name may ' + 'not be unique.') + raise exc.CommandError(msg) + + def _setup_senlin_client(self, api_ver, args): + """Create senlin client using given args.""" + kwargs = { + 'auth_plugin': args.auth_plugin or 'password', + 'auth_url': args.auth_url, + 'project_name': args.project_name or args.tenant_name, + 'project_id': args.project_id or args.tenant_id, + 'domain_name': args.domain_name, + 'domain_id': args.domain_id, + 'project_domain_name': args.project_domain_name, + 'project_domain_id': args.project_domain_id, + 'user_domain_name': args.user_domain_name, + 'user_domain_id': args.user_domain_id, + 'username': args.username, + 'user_id': args.user_id, + 'password': args.password, + 'verify': args.verify, + 'token': args.token, + 'trust_id': args.trust_id, + } + + return senlin_client.Client('1', args.user_preferences, USER_AGENT, + **kwargs) + + def main(self, argv): + # Parse args once to find version + parser = argparse.ArgumentParser( + prog='senlin', + description=__doc__.strip(), + epilog=_('Type "senlin help " for help on a specific ' + 'command.'), + add_help=False, + formatter_class=HelpFormatter, + ) + + cliargs.add_global_args(parser, version=senlinclient.__version__) + cliargs.add_global_identity_args(parser) + self.add_profiler_args(parser) + base_parser = parser + + (options, args) = base_parser.parse_known_args(argv) + + self._setup_logging(options.debug) + self._setup_verbose(options.verbose) + + # build available subcommands based on version + api_ver = options.senlin_api_version + LOG.info(api_ver) + subcommand_parser = self.get_subcommand_parser(base_parser, api_ver) + self.parser = subcommand_parser + + # Handle top-level --help/-h before attempting to parse + # a command off the command line + if not args and options.help or not argv: + self.do_help(options) + return 0 + + # Parse args again and call whatever callback was selected + args = subcommand_parser.parse_args(argv) + + # Short-circuit and deal with help command right away. + if args.func == self.do_help: + self.do_help(args) + return 0 + elif args.func == self.do_bash_completion: + self.do_bash_completion(args) + return 0 + + # Check if identity information are sufficient + self._check_identity_arguments(args) + + # Setup Senlin client connection + sc = self._setup_senlin_client(api_ver, args) + + profile = osprofiler_profiler and options.profile + if profile: + osprofiler_profiler.init(options.profile) + + args.func(sc.service, args) + + if profile: + trace_id = osprofiler_profiler.get().get_base_id() + print(_("Trace ID: %s") % trace_id) + print(_("To display trace use next command:\n" + "osprofiler trace show --html %s ") % trace_id) + + +def main(args=None): + try: + if args is None: + args = sys.argv[1:] + + SenlinShell().main(args) + except KeyboardInterrupt: + print(_("... terminating senlin client"), file=sys.stderr) + sys.exit(130) + except Exception as e: + if '--debug' in args or '-d' in args: + raise + else: + print(encodeutils.safe_encode(six.text_type(e)), file=sys.stderr) + sys.exit(1) + +if __name__ == "__main__": + main() diff --git a/senlinclient/tests/unit/test_cliargs.py b/senlinclient/tests/unit/test_cliargs.py new file mode 100644 index 0000000..8bc6df8 --- /dev/null +++ b/senlinclient/tests/unit/test_cliargs.py @@ -0,0 +1,78 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# 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 mock +import testtools + +from senlinclient import cliargs + + +class TestCliArgs(testtools.TestCase): + + def test_add_global_identity_args(self): + parser = mock.Mock() + + cliargs.add_global_identity_args(parser) + expected = [ + '--os-auth-plugin', + '--os-auth-url', + '--os-project-id', + '--os-project-name', + '--os-tenant-id', + '--os-tenant-name', + '--os-domain-id', + '--os-domain-name', + '--os-project-domain-id', + '--os-project-domain-name', + '--os-user-domain-id', + '--os-user-domain-name', + '--os-username', + '--os-user-id', + '--os-password', + '--os-trust-id', + '--os-token', + '--os-access-info', + '--os-api-name', + '--os-api-region', + '--os-api-version', + '--os-api-interface' + ] + + options = [arg[0][0] for arg in parser.add_argument.call_args_list] + self.assertEqual(expected, options) + + parser.add_mutually_exclusive_group.assert_called_once_with() + group = parser.add_mutually_exclusive_group.return_value + + verify_opts = [arg[0][0] for arg in group.add_argument.call_args_list] + verify_args = [ + '--os-cacert', + '--verify', + '--insecure' + ] + self.assertEqual(verify_args, verify_opts) + + def test_add_global_args(self): + parser = mock.Mock() + + cliargs.add_global_args(parser, '1') + expected = [ + '-h', + '--version', + '-d', + '-v', + '--api-timeout', + '--senlin-api-version' + ] + + options = [arg[0][0] for arg in parser.add_argument.call_args_list] + self.assertEqual(expected, options) diff --git a/senlinclient/tests/unit/test_shell.py b/senlinclient/tests/unit/test_shell.py new file mode 100644 index 0000000..dbeaaf7 --- /dev/null +++ b/senlinclient/tests/unit/test_shell.py @@ -0,0 +1,373 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# 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 argparse +import logging +import sys + +import mock +import six +from six.moves import builtins +import testtools + +from senlinclient import client as senlin_client +from senlinclient.common import exc +from senlinclient.common.i18n import _ +from senlinclient.common import sdk +from senlinclient.common import utils +from senlinclient import shell +from senlinclient.tests.unit import fakes + + +class HelpFormatterTest(testtools.TestCase): + + def test_start_section(self): + fmtr = shell.HelpFormatter('senlin') + res = fmtr.start_section(('heading', 'text1', 30)) + self.assertIsNone(res) + h = fmtr._current_section.heading + self.assertEqual("HEADING('text1', 30)", h) + + +class TestArgs(testtools.TestCase): + + def __init__(self): + self.auth_url = 'http://fakeurl/v3' + self.auth_plugin = 'test_plugin' + self.username = 'test_user_name' + self.user_id = 'test_user_id' + self.token = 'test_token' + self.project_id = 'test_project_id' + self.project_name = 'test_project_name' + self.tenant_id = 'test_tenant_id' + self.tenant_name = 'test_tenant_name' + self.password = 'test_password' + self.user_domain_id = 'test_user_domain_id' + self.user_domain_name = 'test_user_domain_name' + self.project_domain_id = 'test_project_domain_id' + self.project_domain_name = 'test_project_domain_name' + self.domain_name = 'test_domain_name' + self.domain_id = 'test_domain_id' + self.verify = 'test_verify' + self.user_preferences = 'test_preferences' + self.trust_id = 'test_trust' + + +class ShellTest(testtools.TestCase): + + def setUp(self): + super(ShellTest, self).setUp() + + def SHELL(self, func, *args, **kwargs): + orig_out = sys.stdout + sys.stdout = six.StringIO() + func(*args, **kwargs) + output = sys.stdout.getvalue() + sys.stdout.close() + sys.stdout = orig_out + + return output + + @mock.patch.object(logging, 'basicConfig') + @mock.patch.object(logging, 'getLogger') + def test_setup_logging_debug(self, x_get, x_config): + sh = shell.SenlinShell() + sh._setup_logging(True) + + x_config.assert_called_once_with( + format="%(levelname)s (%(module)s) %(message)s", + level=logging.DEBUG) + mock_calls = [ + mock.call('iso8601'), + mock.call().setLevel(logging.WARNING), + mock.call('urllib3.connectionpool'), + mock.call().setLevel(logging.WARNING), + ] + x_get.assert_has_calls(mock_calls) + + @mock.patch.object(logging, 'basicConfig') + @mock.patch.object(logging, 'getLogger') + def test_setup_logging_no_debug(self, x_get, x_config): + sh = shell.SenlinShell() + sh._setup_logging(False) + + x_config.assert_called_once_with( + format="%(levelname)s (%(module)s) %(message)s", + level=logging.WARNING) + mock_calls = [ + mock.call('iso8601'), + mock.call().setLevel(logging.WARNING), + mock.call('urllib3.connectionpool'), + mock.call().setLevel(logging.WARNING), + ] + x_get.assert_has_calls(mock_calls) + + def test_setup_verbose(self): + sh = shell.SenlinShell() + sh._setup_verbose(True) + self.assertEqual(1, exc.verbose) + + sh._setup_verbose(False) + self.assertEqual(1, exc.verbose) + + def test_find_actions(self): + sh = shell.SenlinShell() + sh.subcommands = {} + subparsers = mock.Mock() + x_subparser1 = mock.Mock() + x_subparser2 = mock.Mock() + x_add_parser = mock.MagicMock(side_effect=[x_subparser1, x_subparser2]) + subparsers.add_parser = x_add_parser + + # subparsers.add_parser = mock.Mock(return_value=x_subparser) + sh._find_actions(subparsers, fakes) + + self.assertEqual({'command-bar': x_subparser1, + 'command-foo': x_subparser2}, + sh.subcommands) + add_calls = [ + mock.call('command-bar', help='This is the command doc.', + description='This is the command doc.', + add_help=False, + formatter_class=shell.HelpFormatter), + mock.call('command-foo', help='Pydoc for command foo.', + description='Pydoc for command foo.', + add_help=False, + formatter_class=shell.HelpFormatter), + ] + x_add_parser.assert_has_calls(add_calls) + + calls_1 = [ + mock.call('-h', '--help', action='help', + help=argparse.SUPPRESS), + mock.call('-F', '--flag', metavar='', + help='Flag desc.'), + mock.call('arg1', metavar='', + help='Arg1 desc') + ] + x_subparser1.add_argument.assert_has_calls(calls_1) + x_subparser1.set_defaults.assert_called_once_with( + func=fakes.do_command_bar) + + calls_2 = [ + mock.call('-h', '--help', action='help', + help=argparse.SUPPRESS), + ] + x_subparser2.add_argument.assert_has_calls(calls_2) + x_subparser2.set_defaults.assert_called_once_with( + func=fakes.do_command_foo) + + def test_do_bash_completion(self): + sh = shell.SenlinShell() + sc1 = mock.Mock() + sc2 = mock.Mock() + sc1._optionals._option_string_actions = ('A1', 'A2', 'C') + sc2._optionals._option_string_actions = ('B1', 'B2', 'C') + sh.subcommands = { + 'command-foo': sc1, + 'command-bar': sc2, + 'bash-completion': None, + 'bash_completion': None, + } + + output = self.SHELL(sh.do_bash_completion, None) + + output = output.split('\n')[0] + output_list = output.split(' ') + for option in ('A1', 'A2', 'C', 'B1', 'B2', + 'command-foo', 'command-bar'): + self.assertIn(option, output_list) + + def test_do_add_profiler_args(self): + sh = shell.SenlinShell() + parser = mock.Mock() + + sh.add_profiler_args(parser) + + self.assertEqual(0, parser.add_argument.call_count) + + def test_add_bash_completion_subparser(self): + sh = shell.SenlinShell() + sh.subcommands = {} + x_subparser = mock.Mock() + x_subparsers = mock.Mock() + x_subparsers.add_parser.return_value = x_subparser + + sh._add_bash_completion_subparser(x_subparsers) + + x_subparsers.add_parser.assert_called_once_with( + 'bash_completion', add_help=False, + formatter_class=shell.HelpFormatter) + self.assertEqual({'bash_completion': x_subparser}, sh.subcommands) + x_subparser.set_defaults.assert_called_once_with( + func=sh.do_bash_completion) + + @mock.patch.object(utils, 'import_versioned_module') + @mock.patch.object(shell.SenlinShell, '_find_actions') + @mock.patch.object(shell.SenlinShell, '_add_bash_completion_subparser') + def test_get_subcommand_parser(self, x_add, x_find, x_import): + x_base = mock.Mock() + x_module = mock.Mock() + x_import.return_value = x_module + sh = shell.SenlinShell() + + res = sh.get_subcommand_parser(x_base, 'v100') + + self.assertEqual(x_base, res) + x_base.add_subparsers.assert_called_once_with( + metavar='') + x_subparsers = x_base.add_subparsers.return_value + x_import.assert_called_once_with('v100', 'shell') + find_calls = [ + mock.call(x_subparsers, x_module), + mock.call(x_subparsers, sh) + ] + + x_find.assert_has_calls(find_calls) + x_add.assert_called_once_with(x_subparsers) + + @mock.patch.object(argparse.ArgumentParser, 'print_help') + def test_do_help(self, mock_print): + sh = shell.SenlinShell() + args = mock.Mock() + args.command = mock.Mock() + sh.subcommands = {args.command: argparse.ArgumentParser} + sh.do_help(args) + self.assertTrue(mock_print.called) + + sh.subcommands = {} + ex = self.assertRaises(exc.CommandError, + sh.do_help, args) + msg = _("'%s' is not a valid subcommand") % args.command + self.assertEqual(msg, six.text_type(ex)) + + @mock.patch.object(builtins, 'print') + def test_check_identity_arguments(self, mock_print): + sh = shell.SenlinShell() + # auth_url is not specified. + args = TestArgs() + args.auth_url = None + ex = self.assertRaises(exc.CommandError, + sh._check_identity_arguments, args) + msg = _('You must provide an auth url via --os-auth-url (or ' + ' env[OS_AUTH_URL])') + self.assertEqual(msg, six.text_type(ex)) + # username, user_id and token are not specified. + args = TestArgs() + args.username = None + args.user_id = None + args.token = None + msg = _('You must provide a user name, a user_id or a ' + 'token for authentication') + ex = self.assertRaises(exc.CommandError, + sh._check_identity_arguments, args) + self.assertEqual(msg, six.text_type(ex)) + # Both username and user_id are specified. + args = TestArgs() + args.project_id = None + args.tenant_id = None + sh._check_identity_arguments(args) + msg = _('WARNING: Both user name and user ID are specified, ' + 'Senlin will use user ID for authentication') + mock_print.assert_called_with(msg) + + # 'v3' in auth_url but neither user_domain_id nor user_domain_name + # is specified. + args = TestArgs() + args.user_id = None + args.user_domain_id = None + args.user_domain_name = None + msg = _('Either user domain ID (--user-domain-id / ' + 'env[OS_USER_DOMAIN_ID]) or user domain name ' + '(--user-domain-name / env[OS_USER_DOMAIN_NAME]) ' + 'must be specified, because user name may not be ' + 'unique.') + ex = self.assertRaises(exc.CommandError, + sh._check_identity_arguments, args) + self.assertEqual(msg, six.text_type(ex)) + # user_id, project_id, project_name, tenant_id and tenant_name are all + # not specified. + args = TestArgs() + args.project_id = None + args.project_name = None + args.tenant_id = None + args.tenant_name = None + args.user_id = None + msg = _('Either project/tenant ID or project/tenant name ' + 'must be specified, or else Senlin cannot know ' + 'which project to use.') + ex = self.assertRaises(exc.CommandError, + sh._check_identity_arguments, args) + self.assertEqual(msg, six.text_type(ex)) + args.user_id = 'test_user_id' + sh._check_identity_arguments(args) + msg = _('Neither project ID nor project name is specified. ' + 'Senlin will use user\'s default project which may ' + 'result in authentication error.') + mock_print.assert_called_with(_('WARNING: %s') % msg) + + # Both project_name and project_id are specified + args = TestArgs() + args.user_id = None + sh._check_identity_arguments(args) + msg = _('Both project/tenant name and project/tenant ID are ' + 'specified, Senlin will use project ID for ' + 'authentication') + mock_print.assert_called_with(_('WARNING: %s') % msg) + # Project name may not be unique + args = TestArgs() + args.user_id = None + args.project_id = None + args.tenant_id = None + args.project_domain_id = None + args.project_domain_name = None + msg = _('Either project domain ID (--project-domain-id / ' + 'env[OS_PROJECT_DOMAIN_ID]) orr project domain name ' + '(--project-domain-name / env[OS_PROJECT_DOMAIN_NAME ' + 'must be specified, because project/tenant name may ' + 'not be unique.') + ex = self.assertRaises(exc.CommandError, + sh._check_identity_arguments, args) + self.assertEqual(msg, six.text_type(ex)) + + @mock.patch.object(sdk, 'create_connection') + def test_setup_senlinclient(self, mock_conn): + USER_AGENT = 'python-senlinclient' + args = TestArgs() + kwargs = { + 'auth_plugin': args.auth_plugin, + 'auth_url': args.auth_url, + 'project_name': args.project_name or args.tenant_name, + 'project_id': args.project_id or args.tenant_id, + 'domain_name': args.domain_name, + 'domain_id': args.domain_id, + 'project_domain_name': args.project_domain_name, + 'project_domain_id': args.project_domain_id, + 'user_domain_name': args.user_domain_name, + 'user_domain_id': args.user_domain_id, + 'username': args.username, + 'user_id': args.user_id, + 'password': args.password, + 'verify': args.verify, + 'token': args.token, + 'trust_id': args.trust_id, + } + sh = shell.SenlinShell() + conn = mock.Mock() + mock_conn.return_value = conn + conn.session = mock.Mock() + sh._setup_senlin_client('1', args) + mock_conn.assert_called_once_with(args.user_preferences, USER_AGENT, + **kwargs) + client = mock.Mock() + senlin_client.Client = mock.MagicMock(return_value=client) + self.assertEqual(client, sh._setup_senlin_client('1', args)) diff --git a/senlinclient/tests/unit/v1/test_shell.py b/senlinclient/tests/unit/v1/test_shell.py new file mode 100644 index 0000000..e2d1ef2 --- /dev/null +++ b/senlinclient/tests/unit/v1/test_shell.py @@ -0,0 +1,1348 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# 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 copy +import mock +import six +import testtools + +from openstack import exceptions as oexc +from oslotest import mockpatch +from senlinclient.common import exc +from senlinclient.common.i18n import _ +from senlinclient.common import utils +from senlinclient.v1 import shell as sh + + +class ShellTest(testtools.TestCase): + + def setUp(self): + super(ShellTest, self).setUp() + self.profile_args = { + 'spec_file': mock.Mock(), + 'name': 'stack_spec', + 'metadata': {'user': 'demo'} + } + self.profile_spec = { + 'type': 'os.heat.stack', + 'version': 1.0, + 'properties': { + 'name': 'stack1', + 'template': {"Template": "data"} + } + } + self.patch('senlinclient.v1.shell.show_deprecated') + + # NOTE(pshchelo): this overrides the testtools.TestCase.patch method + # that does simple monkey-patching in favor of mock's patching + def patch(self, target, **kwargs): + mockfixture = self.useFixture(mockpatch.Patch(target, **kwargs)) + return mockfixture.mock + + def _make_args(self, args): + """Convert a dict to an object.""" + class Args(object): + def __init__(self, entries): + self.__dict__.update(entries) + + return Args(args) + + @mock.patch.object(utils, 'print_dict') + def test_do_build_info(self, mock_print): + service = mock.Mock() + result = mock.Mock() + service.get_build_info.return_value = result + sh.do_build_info(service) + formatters = { + 'api': utils.json_formatter, + 'engine': utils.json_formatter, + } + mock_print.assert_called_once_with(result, formatters=formatters) + self.assertTrue(service.get_build_info.called) + + @mock.patch.object(utils, 'print_list') + def test_do_profile_type_list(self, mock_print): + service = mock.Mock() + types = mock.Mock() + service.profile_types.return_value = types + sh.do_profile_type_list(service) + mock_print.assert_called_once_with(types, ['name'], sortby_index=0) + self.assertTrue(service.profile_types.called) + + @mock.patch.object(utils, 'format_output') + def test_do_profile_type_show(self, mock_format): + service = mock.Mock() + fake_pt = mock.Mock() + fake_pt.to_dict.return_value = {'foo': 'bar'} + service.get_profile_type = mock.Mock(return_value=fake_pt) + args_dict = { + 'format': 'json', + 'type_name': 'os.nova.server' + } + args = self._make_args(args_dict) + sh.do_profile_type_show(service, args) + mock_format.assert_called_with({'foo': 'bar'}, format=args.format) + service.get_profile_type.assert_called_with('os.nova.server') + args.format = None + sh.do_profile_type_show(service, args) + mock_format.assert_called_with({'foo': 'bar'}) + + def test_do_profile_type_show_type_not_found(self): + service = mock.Mock() + args = { + 'type_name': 'wrong_type', + 'format': 'json' + } + args = self._make_args(args) + ex = oexc.ResourceNotFound + service.get_profile_type = mock.Mock(side_effect=ex) + ex = self.assertRaises(exc.CommandError, + sh.do_profile_type_show, + service, args) + self.assertEqual(_('Profile Type not found: wrong_type'), + six.text_type(ex)) + + @mock.patch.object(utils, 'print_list') + def test_do_profile_list(self, mock_print): + service = mock.Mock() + profiles = mock.Mock() + service.profiles.return_value = profiles + fields = ['id', 'name', 'type', 'created_at'] + args = { + 'limit': 20, + 'marker': 'mark_id', + 'sort': 'key:dir', + 'global_project': True, + 'filters': ['name=stack_spec'] + } + queries = copy.deepcopy(args) + del queries['filters'] + queries['name'] = 'stack_spec' + formatters = {} + args = self._make_args(args) + args.full_id = True + sh.do_profile_list(service, args) + service.profiles.assert_called_once_with(**queries) + mock_print.assert_called_with(profiles, fields, formatters=formatters, + sortby_index=None) + + args.sort = None + sh.do_profile_list(service, args) + mock_print.assert_called_with(profiles, fields, formatters=formatters, + sortby_index=1) + + @mock.patch.object(utils, 'nested_dict_formatter') + @mock.patch.object(utils, 'print_dict') + def test_show_profile(self, mock_print, mock_dict): + service = mock.Mock() + profile = mock.Mock() + profile_id = mock.Mock() + service.get_profile.return_value = profile + pro_to_dict = mock.Mock() + profile.to_dict.return_value = pro_to_dict + json_formatter = mock.Mock() + utils.json_formatter = json_formatter + dict_formatter = mock.Mock() + mock_dict.return_value = dict_formatter + formatters = { + 'metadata': json_formatter, + 'spec': dict_formatter + } + sh._show_profile(service, profile_id) + service.get_profile.assert_called_once_with(profile_id) + mock_dict.assert_called_once_with(['type', 'version', 'properties'], + ['property', 'value']) + mock_print.assert_called_once_with(pro_to_dict, formatters=formatters) + + def test_show_profile_not_found(self): + service = mock.Mock() + ex = oexc.ResourceNotFound + service.get_profile.side_effect = ex + profile_id = 'wrong_id' + ex = self.assertRaises(exc.CommandError, + sh._show_profile, + service, profile_id) + self.assertEqual(_('Profile not found: wrong_id'), six.text_type(ex)) + service.get_profile.assert_called_once_with(profile_id) + + @mock.patch.object(sh, '_show_profile') + @mock.patch.object(utils, 'format_parameters') + @mock.patch.object(utils, 'process_stack_spec') + @mock.patch.object(utils, 'get_spec_content') + def test_do_profile_create(self, mock_get, mock_proc, mock_format, + mock_show): + args = copy.deepcopy(self.profile_args) + args = self._make_args(args) + spec = copy.deepcopy(self.profile_spec) + mock_get.return_value = spec + stack_properties = mock.Mock() + mock_proc.return_value = stack_properties + mock_format.return_value = {'user': 'demo'} + params = { + 'name': 'stack_spec', + 'spec': spec, + 'metadata': {'user': 'demo'}, + } + service = mock.Mock() + profile = mock.Mock() + profile_id = mock.Mock() + profile.id = profile_id + service.create_profile.return_value = profile + + sh.do_profile_create(service, args) + + mock_get.assert_called_once_with(args.spec_file) + mock_proc.assert_called_once_with(self.profile_spec['properties']) + mock_format.assert_called_once_with(args.metadata) + service.create_profile.assert_called_once_with(**params) + mock_show.assert_called_once_with(service, profile_id) + + # Miss 'type' key in spec file + del spec['type'] + ex = self.assertRaises(exc.CommandError, + sh.do_profile_create, + service, args) + self.assertEqual(_("Missing 'type' key in spec file."), + six.text_type(ex)) + # Miss 'version' key in spec file + spec['type'] = 'os.heat.stack' + del spec['version'] + ex = self.assertRaises(exc.CommandError, + sh.do_profile_create, + service, args) + self.assertEqual(_("Missing 'version' key in spec file."), + six.text_type(ex)) + # Miss 'properties' key in spec file + spec['version'] = 1.0 + del spec['properties'] + ex = self.assertRaises(exc.CommandError, + sh.do_profile_create, + service, args) + self.assertEqual(_("Missing 'properties' key in spec file."), + six.text_type(ex)) + + @mock.patch.object(sh, '_show_profile') + def test_do_profile_show(self, mock_show): + service = mock.Mock() + args = {'id': 'profile_id'} + args = self._make_args(args) + sh.do_profile_show(service, args) + mock_show.assert_called_once_with(service, args.id) + + @mock.patch.object(sh, '_show_profile') + @mock.patch.object(utils, 'format_parameters') + def test_do_profile_update(self, mock_format, mock_show): + args = copy.deepcopy(self.profile_args) + args = self._make_args(args) + mock_format.return_value = {'user': 'demo'} + service = mock.Mock() + profile = mock.Mock() + profile_id = mock.Mock() + profile.id = profile_id + args.id = 'FAKE_ID' + service.get_profile.return_value = profile + + sh.do_profile_update(service, args) + + mock_format.assert_called_once_with(args.metadata) + service.get_profile.assert_called_once_with('FAKE_ID') + params = { + 'name': 'stack_spec', + 'metadata': {'user': 'demo'}, + } + service.update_profile.assert_called_once_with(profile_id, **params) + mock_show.assert_called_once_with(service, profile_id) + + @mock.patch.object(utils, 'format_parameters') + def test_do_profile_update_not_found(self, mock_format): + service = mock.Mock() + args = copy.deepcopy(self.profile_args) + args = self._make_args(args) + args.id = 'FAKE_ID' + ex = oexc.ResourceNotFound + service.get_profile.side_effect = ex + ex = self.assertRaises(exc.CommandError, + sh.do_profile_update, + service, args) + self.assertEqual(_('Profile not found: FAKE_ID'), + six.text_type(ex)) + mock_format.assert_called_once_with(args.metadata) + + def test_do_profile_delete(self): + service = mock.Mock() + args = {'id': ['profile_id']} + args = self._make_args(args) + sh.do_profile_delete(service, args) + service.delete_profile.assert_called_with('profile_id', False) + + def test_do_profile_delete_not_found(self): + service = mock.Mock() + args = {'id': ['profile1', 'profile2']} + args = self._make_args(args) + sh.do_profile_delete(service, args) + service.delete_profile.side_effect = oexc.ResourceNotFound + ex = self.assertRaises(exc.CommandError, + sh.do_profile_delete, + service, args) + msg = _("Failed to delete some of the specified profile(s).") + self.assertEqual(msg, six.text_type(ex)) + service.delete_profile.assert_called_with('profile2', False) + + @mock.patch.object(utils, 'print_list') + def test_do_policy_type_list(self, mock_print): + service = mock.Mock() + args = mock.Mock() + types = mock.Mock() + service.policy_types.return_value = types + sh.do_policy_type_list(service, args) + mock_print.assert_called_once_with(types, ['name'], sortby_index=0) + + @mock.patch.object(utils, 'format_output') + def test_do_policy_type_show(self, mock_format): + service = mock.Mock() + args = { + 'type_name': 'senlin.policy.deletion', + 'format': 'yaml' + } + args = self._make_args(args) + res = mock.Mock() + pt = mock.Mock() + res.to_dict.return_value = pt + service.get_policy_type.return_value = res + sh.do_policy_type_show(service, args) + mock_format.assert_called_with(pt, format=args.format) + + # no format attribute + args = { + 'type_name': 'senlin.policy.deletion', + 'format': None + } + args = self._make_args(args) + service.get_policy_type.return_value = res + sh.do_policy_type_show(service, args) + mock_format.assert_called_with(pt) + + def test_do_policy_type_show_not_found(self): + service = mock.Mock() + args = {'type_name': 'BAD'} + args = self._make_args(args) + + service.get_policy_type.side_effect = oexc.ResourceNotFound + ex = self.assertRaises(exc.CommandError, + sh.do_policy_type_show, service, args) + msg = _('Policy type not found: BAD') + self.assertEqual(msg, six.text_type(ex)) + + @mock.patch.object(utils, 'print_list') + def test_do_receiver_list(self, mock_print): + service = mock.Mock() + params = { + 'limit': 10, + 'marker': 'fake_id', + 'sort': 'key:dir', + 'filters': ['filter_key=filter_value'], + 'global_project': False, + 'full_id': False, + } + fields = ['id', 'name', 'type', 'cluster_id', 'action', 'created_at'] + args = self._make_args(params) + queries = copy.deepcopy(params) + del queries['filters'] + queries['filter_key'] = 'filter_value' + r1 = mock.Mock() + r1.id = '01234567-abcd-efgh' + r1.cluster_id = 'abcdefgh-abcd-efgh' + receivers = [r1] + service.receivers.return_value = receivers + formatters = { + 'id': mock.ANY, + 'cluster_id': mock.ANY + } + sh.do_receiver_list(service, args) + mock_print.assert_called_with(receivers, fields, + formatters=formatters, + sortby_index=None) + # full_id is requested + args.full_id = True + sh.do_receiver_list(service, args) + mock_print.assert_called_with(receivers, fields, + formatters={}, + sortby_index=None) + + # default sorting + args.sort = None + sh.do_receiver_list(service, args) + mock_print.assert_called_with(receivers, fields, + formatters={}, + sortby_index=0) + + @mock.patch.object(utils, 'print_dict') + def test_show_receiver(self, mock_print): + service = mock.Mock() + receiver = mock.Mock() + receiver_id = '01234567-abcd-abcd-abcdef' + receiver.id = receiver_id + service.get_receiver.return_value = receiver + receiver_dict = mock.Mock() + receiver.to_dict.return_value = receiver_dict + sh._show_receiver(service, receiver_id) + formatters = { + 'actor': utils.json_formatter, + 'params': utils.json_formatter, + 'channel': utils.json_formatter, + } + service.get_receiver.assert_called_once_with(receiver_id) + mock_print.assert_called_once_with(receiver_dict, + formatters=formatters) + + def test_show_receiver_not_found(self): + service = mock.Mock() + receiver = mock.Mock() + receiver_id = 'wrong_id' + receiver.id = receiver_id + + service.get_receiver.side_effect = oexc.ResourceNotFound + ex = self.assertRaises(exc.CommandError, + sh._show_receiver, service, receiver_id) + self.assertEqual(_('Receiver not found: wrong_id'), six.text_type(ex)) + + @mock.patch.object(sh, '_show_receiver') + def test_do_receiver_show(self, mock_show): + service = mock.Mock() + args = {'id': 'receiver_id'} + args = self._make_args(args) + sh.do_receiver_show(service, args) + mock_show.assert_called_once_with(service, + receiver_id='receiver_id') + + @mock.patch.object(sh, '_show_receiver') + def test_do_receiver_create(self, mock_show): + service = mock.Mock() + args = { + 'name': 'receiver1', + 'type': 'webhook', + 'cluster': 'cluster1', + 'action': 'CLUSTER_SCALE_IN', + 'params': {} + } + args = self._make_args(args) + params = { + 'name': 'receiver1', + 'type': 'webhook', + 'cluster_id': 'cluster1', + 'action': 'CLUSTER_SCALE_IN', + 'params': {} + } + receiver = mock.Mock() + receiver.id = 'FAKE_ID' + service.create_receiver.return_value = receiver + sh.do_receiver_create(service, args) + service.create_receiver.assert_called_once_with(**params) + mock_show.assert_called_once_with(service, 'FAKE_ID') + + def test_do_receiver_delete(self): + service = mock.Mock() + args = {'id': ['FAKE']} + args = self._make_args(args) + service.delete_receiver = mock.Mock() + sh.do_receiver_delete(service, args) + service.delete_receiver.assert_called_once_with('FAKE', False) + + def test_do_receiver_delete_not_found(self): + service = mock.Mock() + args = {'id': ['receiver_id']} + args = self._make_args(args) + + service.delete_receiver.side_effect = oexc.ResourceNotFound + ex = self.assertRaises(exc.CommandError, + sh.do_receiver_delete, service, args) + msg = _("Failed to delete some of the specified receiver(s).") + self.assertEqual(msg, six.text_type(ex)) + + @mock.patch.object(utils, 'print_list') + def test_do_policy_list(self, mock_print): + service = mock.Mock() + fields = ['id', 'name', 'type', 'created_at'] + args = { + 'limit': 20, + 'marker': 'fake_id', + 'sort': 'name', + 'global_project': False, + 'full_id': True, + 'filters': ['name=stack_spec'] + } + args = self._make_args(args) + queries = { + 'limit': 20, + 'marker': 'fake_id', + 'sort': 'name', + 'global_project': False, + 'name': 'stack_spec', + } + policies = mock.Mock() + service.policies.return_value = policies + formatters = {} + sh.do_policy_list(service, args) + service.policies.assert_called_once_with(**queries) + mock_print.assert_called_once_with( + policies, fields, formatters=formatters, sortby_index=None) + mock_print.reset_mock() + + args.sort = None + sh.do_policy_list(service, args) + mock_print.assert_called_once_with( + policies, fields, formatters=formatters, sortby_index=1) + + @mock.patch.object(utils, 'print_dict') + def test_show_policy(self, mock_print): + service = mock.Mock() + formatters = { + 'metadata': utils.json_formatter, + 'spec': utils.json_formatter, + } + policy_id = 'fake_policy_id' + policy = mock.Mock() + policy.id = policy_id + service.get_policy.return_value = policy + policy_dict = mock.Mock() + policy.to_dict.return_value = policy_dict + sh._show_policy(service, policy_id) + mock_print.assert_called_once_with(policy_dict, + formatters=formatters) + + # policy not found + ex = oexc.ResourceNotFound + service.get_policy.side_effect = ex + ex = self.assertRaises(exc.CommandError, + sh._show_policy, + service, policy_id) + msg = _('Policy not found: fake_policy_id') + self.assertEqual(msg, six.text_type(ex)) + + @mock.patch.object(sh, '_show_policy') + @mock.patch.object(utils, 'get_spec_content') + def test_do_policy_create(self, mock_get, mock_show): + service = mock.Mock() + spec = mock.Mock() + mock_get.return_value = spec + args = { + 'name': 'new_policy', + 'spec_file': 'policy_file', + } + args = self._make_args(args) + attrs = { + 'name': 'new_policy', + 'spec': spec, + } + policy = mock.Mock() + policy.id = 'policy_id' + service.create_policy.return_value = policy + sh.do_policy_create(service, args) + mock_get.assert_called_once_with(args.spec_file) + service.create_policy.assert_called_once_with(**attrs) + mock_show.assert_called_once_with(service, policy.id) + + @mock.patch.object(sh, '_show_policy') + def test_do_policy_show(self, mock_show): + service = mock.Mock() + args = {'id': 'policy_id'} + args = self._make_args(args) + sh.do_policy_show(service, args) + mock_show.assert_called_once_with(service, policy_id='policy_id') + + @mock.patch.object(sh, '_show_policy') + def test_do_policy_update(self, mock_show): + service = mock.Mock() + args = { + 'name': 'deletion_policy', + 'id': 'policy_id', + } + args = self._make_args(args) + params = { + 'name': 'deletion_policy', + } + policy = mock.Mock() + service.get_policy.return_value = policy + policy.id = 'policy_id' + sh.do_policy_update(service, args) + service.get_policy.assert_called_once_with('policy_id') + service.update_policy.assert_called_once_with( + 'policy_id', **params) + mock_show(service, policy_id=policy.id) + + def test_do_policy_delete(self): + service = mock.Mock() + args = {'id': ['policy_id']} + args = self._make_args(args) + service.delete_policy = mock.Mock() + sh.do_policy_delete(service, args) + service.delete_policy.assert_called_once_with('policy_id', False) + + def test_do_policy_delete_not_found(self): + service = mock.Mock() + args = {'id': ['policy_id']} + args = self._make_args(args) + + service.delete_policy.side_effect = oexc.ResourceNotFound + ex = self.assertRaises(exc.CommandError, + sh.do_policy_delete, service, args) + msg = _("Failed to delete some of the specified policy(s).") + self.assertEqual(msg, six.text_type(ex)) + + @mock.patch.object(utils, 'print_list') + def test_do_cluster_list(self, mock_print): + service = mock.Mock() + fields = ['id', 'name', 'status', 'created_at', 'updated_at'] + args = { + 'limit': 20, + 'marker': 'fake_id', + 'sort': 'key:dir', + 'global_project': False, + 'filters': ['status=ACTIVE'], + } + queries = copy.deepcopy(args) + del queries['filters'] + queries['status'] = 'ACTIVE' + args = self._make_args(args) + clusters = mock.Mock() + service.clusters.return_value = clusters + args.full_id = True + formatters = {} + sh.do_cluster_list(service, args) + service.clusters.assert_called_once_with(**queries) + mock_print.assert_called_once_with(clusters, fields, + formatters=formatters, + sortby_index=None) + args.sort = None + sh.do_cluster_list(service, args) + mock_print.assert_called_with(clusters, fields, + formatters={}, sortby_index=3) + + @mock.patch.object(utils, 'print_dict') + def test_show_cluster(self, mock_print): + service = mock.Mock() + cluster_id = 'cluster_id' + cluster = mock.Mock() + cluster.id = cluster_id + service.get_cluster.return_value = cluster + formatters = { + 'metadata': utils.json_formatter, + 'nodes': utils.list_formatter, + } + cluster_dict = mock.Mock() + cluster.to_dict.return_value = cluster_dict + sh._show_cluster(service, cluster_id) + mock_print.assert_called_once_with(cluster_dict, formatters=formatters) + + @mock.patch.object(sh, '_show_cluster') + def test_do_cluster_create(self, mock_show): + service = mock.Mock() + args = { + 'name': 'CLUSTER1', + 'profile': 'profile1', + 'min_size': 1, + 'max_size': 10, + 'desired_capacity': 5, + 'metadata': ['user=demo'], + 'timeout': 200, + } + attrs = copy.deepcopy(args) + attrs['profile_id'] = args['profile'] + args = self._make_args(args) + del attrs['profile'] + attrs['metadata'] = {'user': 'demo'} + cluster = mock.Mock() + service.create_cluster.return_value = cluster + cluster.id = 'cluster_id' + sh.do_cluster_create(service, args) + service.create_cluster.assert_called_once_with(**attrs) + mock_show.assert_called_once_with(service, 'cluster_id') + + def test_do_cluster_delete(self): + service = mock.Mock() + args = {'id': ['CID']} + args = self._make_args(args) + service.delete_cluster = mock.Mock() + sh.do_cluster_delete(service, args) + service.delete_cluster.assert_called_once_with('CID', False) + + def test_do_cluster_delete_not_found(self): + service = mock.Mock() + args = {'id': ['cluster_id']} + args = self._make_args(args) + + service.delete_cluster.side_effect = oexc.ResourceNotFound + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_delete, service, args) + msg = _('Failed to delete some of the specified clusters.') + self.assertEqual(msg, six.text_type(ex)) + + @mock.patch.object(sh, '_show_cluster') + def test_do_cluster_update(self, mock_show): + service = mock.Mock() + args = { + 'profile': 'test_profile', + 'name': 'CLUSTER1', + 'metadata': ['user=demo'], + 'timeout': 100, + } + attrs = copy.deepcopy(args) + attrs['metadata'] = {'user': 'demo'} + attrs['profile_id'] = 'test_profile' + del attrs['profile'] + args = self._make_args(args) + args.id = 'CID' + cluster = mock.Mock() + cluster.id = 'CID' + service.get_cluster.return_value = cluster + service.update_cluster = mock.Mock() + + sh.do_cluster_update(service, args) + + service.get_cluster.assert_called_once_with('CID') + service.update_cluster.assert_called_once_with('CID', **attrs) + mock_show.assert_called_once_with(service, 'CID') + + @mock.patch.object(sh, '_show_cluster') + def test_do_cluster_show(self, mock_show): + service = mock.Mock() + args = {'id': 'cluster_id'} + args = self._make_args(args) + sh.do_cluster_show(service, args) + mock_show.assert_called_once_with(service, 'cluster_id') + + @mock.patch.object(utils, 'print_list') + def test_do_cluster_node_list(self, mock_print): + service = mock.Mock() + args = { + 'id': 'cluster_id', + 'limit': 20, + 'marker': 'marker_id', + 'filters': ['status=ACTIVE'], + } + queries = copy.deepcopy(args) + queries['cluster_id'] = args['id'] + del queries['id'] + del queries['filters'] + queries['status'] = 'ACTIVE' + args = self._make_args(args) + args.full_id = True + nodes = mock.Mock() + service.nodes.return_value = nodes + formatters = {} + fields = ['id', 'name', 'index', 'status', 'physical_id', 'created_at'] + sh.do_cluster_node_list(service, args) + service.nodes.assert_called_once_with(**queries) + mock_print.assert_called_once_with(nodes, fields, + formatters=formatters, + sortby_index=5) + + def test_do_cluster_node_add(self): + service = mock.Mock() + args = { + 'id': 'cluster_id', + 'nodes': 'node1,node2' + } + args = self._make_args(args) + node_ids = ['node1', 'node2'] + resp = {'action': 'CLUSTER_NODE_ADD'} + service.cluster_add_nodes.return_value = resp + sh.do_cluster_node_add(service, args) + service.cluster_add_nodes.assert_called_once_with( + 'cluster_id', node_ids) + + def test_do_cluster_node_del(self): + service = mock.Mock() + args = { + 'id': 'cluster_id', + 'nodes': 'node1,node2' + } + args = self._make_args(args) + node_ids = ['node1', 'node2'] + resp = {'action': 'CLUSTER_NODE_DEL'} + service.cluster_del_nodes.return_value = resp + + sh.do_cluster_node_del(service, args) + + service.cluster_del_nodes.assert_called_once_with('cluster_id', + node_ids) + + def test_do_cluster_resize(self): + service = mock.Mock() + args = { + 'id': 'cluster_id', + 'capacity': 2, + 'adjustment': 1, + 'percentage': 50.0, + 'min_size': 1, + 'max_size': 10, + 'min_step': 1, + 'strict': True, + } + args = self._make_args(args) + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_resize, + service, args) + msg = _("Only one of 'capacity', 'adjustment' and " + "'percentage' can be specified.") + self.assertEqual(msg, six.text_type(ex)) + + # capacity + args.adjustment = None + args.percentage = None + args.min_step = None + action_args = { + 'adjustment_type': 'EXACT_CAPACITY', + 'number': 2, + 'min_size': 1, + 'max_size': 10, + 'strict': True, + 'min_step': None, + } + resp = {'action': 'action_id'} + service.cluster_resize.return_value = resp + sh.do_cluster_resize(service, args) + service.cluster_resize.assert_called_with('cluster_id', **action_args) + + # capacity is smaller than 0 + args.capacity = -1 + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_resize, + service, args) + msg = _('Cluster capacity must be larger than ' + ' or equal to zero.') + self.assertEqual(msg, six.text_type(ex)) + + # adjustment + args.capacity = None + args.percentage = None + args.adjustment = 1 + action_args['adjustment_type'] = 'CHANGE_IN_CAPACITY' + action_args['number'] = 1 + sh.do_cluster_resize(service, args) + service.cluster_resize.assert_called_with('cluster_id', **action_args) + + # adjustment is 0 + args.adjustment = 0 + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_resize, + service, args) + msg = _('Adjustment cannot be zero.') + self.assertEqual(msg, six.text_type(ex)) + + # percentage + args.capacity = None + args.percentage = 50.0 + args.adjustment = None + action_args['adjustment_type'] = 'CHANGE_IN_PERCENTAGE' + action_args['number'] = 50.0 + sh.do_cluster_resize(service, args) + service.cluster_resize.assert_called_with('cluster_id', **action_args) + + # percentage is 0 + args.percentage = 0 + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_resize, + service, args) + msg = _('Percentage cannot be zero.') + self.assertEqual(msg, six.text_type(ex)) + + # min_step is not None while percentage is None + args.capacity = 2 + args.percentage = None + args.adjustment = None + args.min_step = 1 + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_resize, + service, args) + msg = _('Min step is only used with percentage.') + self.assertEqual(msg, six.text_type(ex)) + + # min_size < 0 + args.capacity = 2 + args.percentage = None + args.adjustment = None + args.min_step = None + args.min_size = -1 + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_resize, + service, 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 + args.adjustment = None + args.min_step = None + args.min_size = 5 + args.max_size = 4 + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_resize, + service, 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 + args.adjustment = None + args.min_step = None + args.min_size = 6 + args.max_size = 8 + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_resize, + service, 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 + args.adjustment = None + args.min_step = None + args.min_size = 1 + args.max_size = 4 + ex = self.assertRaises(exc.CommandError, + sh.do_cluster_resize, + service, args) + msg = _('Max size cannot be less than the specified capacity.') + self.assertEqual(msg, six.text_type(ex)) + + def test_do_cluster_scale_out(self): + service = mock.Mock() + args = { + 'id': 'cluster_id', + 'count': 3, + } + args = self._make_args(args) + resp = {'action': 'action_id'} + service.cluster_scale_out.return_value = resp + sh.do_cluster_scale_out(service, args) + service.cluster_scale_out.assert_called_once_with( + 'cluster_id', 3) + + def test_do_cluster_scale_in(self): + service = mock.Mock() + args = { + 'id': 'cluster_id', + 'count': 3, + } + args = self._make_args(args) + resp = {'action': 'action_id'} + service.cluster_scale_in.return_value = resp + + sh.do_cluster_scale_in(service, args) + + service.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): + fields = ['policy_id', 'policy_name', 'policy_type', 'enabled'] + service = mock.Mock() + args = { + 'id': 'C1', + 'filters': ['enabled=True'], + 'sort': 'enabled:asc', + 'full_id': True, + } + args = self._make_args(args) + queries = { + 'sort': 'enabled:asc', + 'enabled': 'True', + } + cluster = mock.Mock() + cluster.id = 'C1' + service.get_cluster.return_value = cluster + policies = mock.Mock() + service.cluster_policies.return_value = policies + sh.do_cluster_policy_list(service, args) + service.get_cluster.assert_called_once_with('C1') + service.cluster_policies.assert_called_once_with('C1', **queries) + formatters = {} + mock_print.assert_called_once_with(policies, fields, + formatters=formatters, + sortby_index=None) + + @mock.patch.object(utils, 'print_dict') + def test_do_cluster_policy_show(self, mock_print): + class Binding(object): + def to_dict(self): + pass + + service = mock.Mock() + args = { + 'id': 'CC', + 'policy': 'PP', + } + args = self._make_args(args) + binding = Binding() + service.get_cluster_policy.return_value = binding + sh.do_cluster_policy_show(service, args) + service.get_cluster_policy.assert_called_once_with('PP', 'CC') + mock_print.assert_called_once_with(binding.to_dict()) + + def test_do_cluster_policy_attach(self): + service = mock.Mock() + args = { + 'id': 'C1', + 'policy': 'P1', + 'enabled': 'True', + } + args = self._make_args(args) + kwargs = { + 'enabled': 'True', + } + service.cluster_attach_policy.return_value = {'action': 'action_id'} + sh.do_cluster_policy_attach(service, args) + service.cluster_attach_policy.assert_called_once_with('C1', 'P1', + **kwargs) + + def test_do_cluster_policy_detach(self): + args = { + 'id': 'CC', + 'policy': 'PP' + } + service = mock.Mock() + args = self._make_args(args) + resp = {'action': 'action_id'} + service.cluster_detach_policy.return_value = resp + sh.do_cluster_policy_detach(service, args) + service.cluster_detach_policy.assert_called_once_with('CC', 'PP') + + def test_do_cluster_policy_update(self): + service = mock.Mock() + args = { + 'id': 'C1', + 'policy': 'policy1', + 'enabled': 'True', + } + args = self._make_args(args) + kwargs = { + 'enabled': 'True', + } + service.cluster_update_policy.return_value = {'action': 'action_id'} + + sh.do_cluster_policy_update(service, args) + + service.cluster_update_policy.assert_called_once_with('C1', 'policy1', + **kwargs) + + def test_do_cluster_check(self): + service = mock.Mock() + args = self._make_args({'id': ['cluster1']}) + service.check_cluster = mock.Mock() + service.check_cluster.return_value = {'action': 'action_id'} + sh.do_cluster_check(service, args) + + service.check_cluster.assert_called_once_with('cluster1') + + def test_do_cluster_recover(self): + service = mock.Mock() + args = self._make_args({'id': ['cluster1']}) + service.recover_cluster = mock.Mock() + service.recover_cluster.return_value = {'action': 'action_id'} + + sh.do_cluster_recover(service, args) + + service.recover_cluster.assert_called_once_with('cluster1') + + @mock.patch.object(utils, 'print_list') + def test_do_node_list(self, mock_print): + service = mock.Mock() + fields = ['id', 'name', 'index', 'status', 'cluster_id', 'physical_id', + 'profile_name', 'created_at', 'updated_at'] + args = { + 'cluster': 'cluster1', + 'sort': 'name:asc', + 'limit': 20, + 'marker': 'marker_id', + 'global_project': True, + 'filters': ['status=active'], + 'full_id': True, + } + queries = { + 'cluster_id': 'cluster1', + 'sort': 'name:asc', + 'limit': 20, + 'marker': 'marker_id', + 'global_project': True, + 'status': 'active', + } + args = self._make_args(args) + nodes = mock.Mock() + service.nodes.return_value = nodes + formatters = {} + sh.do_node_list(service, args) + mock_print.assert_called_once_with(nodes, fields, + formatters=formatters, + sortby_index=None) + service.nodes.assert_called_once_with(**queries) + + @mock.patch.object(utils, 'print_dict') + @mock.patch.object(utils, 'nested_dict_formatter') + def test_show_node(self, mock_nested, mock_print): + service = mock.Mock() + node_id = 'node1' + node = mock.Mock() + service.get_node.return_value = node + formatters = { + 'metadata': utils.json_formatter, + 'data': utils.json_formatter, + } + data = mock.Mock() + node.to_dict.return_value = data + + sh._show_node(service, node_id, show_details=False) + + service.get_node.assert_called_once_with(node_id, args=None) + mock_print.assert_called_once_with(data, formatters=formatters) + + @mock.patch.object(sh, '_show_node') + def test_do_node_create(self, mock_show): + args = { + 'name': 'node1', + 'cluster': 'cluster1', + 'profile': 'profile1', + 'role': 'master', + 'metadata': ['user=demo'], + } + args = self._make_args(args) + attrs = { + 'name': 'node1', + 'cluster_id': 'cluster1', + 'profile_id': 'profile1', + 'role': 'master', + 'metadata': {'user': 'demo'}, + } + service = mock.Mock() + node = mock.Mock() + node.id = 'node_id' + service.create_node.return_value = node + sh.do_node_create(service, args) + service.create_node.assert_called_once_with(**attrs) + mock_show.assert_called_once_with(service, 'node_id') + + @mock.patch.object(sh, '_show_node') + def test_do_node_show(self, mock_show): + service = mock.Mock() + args = { + 'id': 'node1', + 'details': False + } + args = self._make_args(args) + sh.do_node_show(service, args) + mock_show.assert_called_once_with(service, 'node1', False) + + def test_do_node_delete(self): + service = mock.Mock() + args = self._make_args({'id': ['node1']}) + service.delete_node = mock.Mock() + + sh.do_node_delete(service, args) + + service.delete_node.assert_called_once_with('node1', False) + + def test_do_node_delete_not_found(self): + service = mock.Mock() + ex = oexc.ResourceNotFound + service.delete_node.side_effect = ex + + args = self._make_args({'id': ['node1']}) + ex = self.assertRaises(exc.CommandError, + sh.do_node_delete, service, args) + msg = _('Failed to delete some of the specified nodes.') + self.assertEqual(msg, six.text_type(ex)) + + def test_do_node_check(self): + service = mock.Mock() + args = self._make_args({'id': ['node1']}) + service.check_node = mock.Mock() + + sh.do_node_check(service, args) + + service.check_node.assert_called_once_with('node1') + + def test_do_node_check_not_found(self): + service = mock.Mock() + ex = exc.HTTPNotFound + service.check_node.side_effect = ex + + args = self._make_args({'id': ['node1']}) + ex = self.assertRaises(exc.CommandError, + sh.do_node_check, service, args) + msg = _('Failed to check some of the specified nodes.') + self.assertEqual(msg, six.text_type(ex)) + + def test_do_node_recover(self): + service = mock.Mock() + args = self._make_args({'id': ['node1']}) + service.check_node = mock.Mock() + + sh.do_node_recover(service, args) + + service.recover_node.assert_called_once_with('node1') + + def test_do_node_recover_not_found(self): + service = mock.Mock() + ex = exc.HTTPNotFound + service.recover_node.side_effect = ex + + args = self._make_args({'id': ['node1']}) + ex = self.assertRaises(exc.CommandError, + sh.do_node_recover, service, args) + msg = _('Failed to recover some of the specified nodes.') + self.assertEqual(msg, six.text_type(ex)) + + @mock.patch.object(sh, '_show_node') + def test_do_node_update(self, mock_show): + service = mock.Mock() + args = { + 'id': 'node_id', + 'name': 'node1', + 'role': 'master', + 'profile': 'profile1', + 'metadata': ['user=demo'], + } + args = self._make_args(args) + attrs = { + 'name': 'node1', + 'role': 'master', + 'profile_id': 'profile1', + 'metadata': {'user': 'demo'}, + } + node = mock.Mock() + node.id = 'node_id' + service.get_node.return_value = node + sh.do_node_update(service, args) + service.get_node.assert_called_once_with('node_id') + service.update_node.assert_called_once_with('node_id', **attrs) + mock_show.assert_called_once_with(service, 'node_id') + + @mock.patch.object(utils, 'print_list') + def test_do_event_list(self, mock_print): + service = mock.Mock() + fields = ['id', 'timestamp', 'obj_type', 'obj_id', 'obj_name', + 'action', 'status', 'status_reason', 'level'] + args = { + 'sort': 'timestamp:asc', + 'limit': 20, + 'marker': 'marker_id', + 'global_project': True, + 'filters': ['action=NODE_DELETE'], + 'full_id': True, + } + queries = copy.deepcopy(args) + del queries['full_id'] + del queries['filters'] + queries['action'] = 'NODE_DELETE' + args = self._make_args(args) + formatters = {} + events = mock.Mock() + service.events.return_value = events + + sh.do_event_list(service, args) + + service.events.assert_called_once_with(**queries) + mock_print.assert_called_once_with(events, fields, + formatters=formatters) + + @mock.patch.object(utils, 'print_dict') + def test_do_event_show(self, mock_print): + class FakeEvent(object): + def to_dict(self): + pass + + service = mock.Mock() + args = { + 'id': 'event_id' + } + args = self._make_args(args) + + event = FakeEvent() + service.get_event.return_value = event + sh.do_event_show(service, args) + service.get_event.assert_called_once_with('event_id') + mock_print.assert_called_once_with(event.to_dict()) + + def test_do_event_show_not_found(self): + service = mock.Mock() + args = self._make_args({'id': 'FAKE'}) + # event not found + ex = exc.CommandError + service.get_event.side_effect = oexc.ResourceNotFound + ex = self.assertRaises(ex, + sh.do_event_show, + service, args) + + @mock.patch.object(utils, 'print_list') + def test_do_action_list(self, mock_print): + service = mock.Mock() + fields = ['id', 'name', 'action', 'status', 'target', 'depends_on', + 'depended_by', 'created_at'] + args = { + 'sort': 'status', + 'limit': 20, + 'marker': 'marker_id', + } + queries = copy.deepcopy(args) + args = self._make_args(args) + args.filters = ['status=ACTIVE'] + queries['status'] = 'ACTIVE' + actions = mock.Mock() + service.actions.return_value = actions + formatters = { + 'depends_on': mock.ANY, + 'depended_by': mock.ANY + } + args.full_id = True + sortby_index = None + sh.do_action_list(service, args) + service.actions.assert_called_once_with(**queries) + mock_print.assert_called_once_with(actions, fields, + formatters=formatters, + sortby_index=sortby_index) + + @mock.patch.object(utils, 'print_dict') + def test_do_action_show(self, mock_print): + class FakeAction(object): + def to_dict(self): + pass + + service = mock.Mock() + args = self._make_args({'id': 'action_id'}) + + action = FakeAction() + service.get_action.return_value = action + formatters = { + 'inputs': utils.json_formatter, + 'outputs': utils.json_formatter, + 'metadata': utils.json_formatter, + 'data': utils.json_formatter, + 'depends_on': utils.list_formatter, + 'depended_by': utils.list_formatter, + } + sh.do_action_show(service, args) + service.get_action.assert_called_once_with('action_id') + mock_print.assert_called_once_with(action.to_dict(), + formatters=formatters) + + def test_do_action_show_not_found(self): + service = mock.Mock() + args = self._make_args({'id': 'fake_id'}) + + service.get_action.side_effect = oexc.ResourceNotFound + ex = self.assertRaises(exc.CommandError, + sh.do_action_show, + service, args) + msg = _('Action not found: fake_id') + self.assertEqual(msg, six.text_type(ex)) diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py new file mode 100644 index 0000000..6ef78e3 --- /dev/null +++ b/senlinclient/v1/shell.py @@ -0,0 +1,1314 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# 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 logging + +from openstack import exceptions as sdk_exc +from senlinclient.common import exc +from senlinclient.common.i18n import _ +from senlinclient.common.i18n import _LW +from senlinclient.common import utils + +logger = logging.getLogger(__name__) + + +def show_deprecated(deprecated, recommended): + logger.warning(_LW('"%(old)s" is deprecated, ' + 'please use "%(new)s" instead.'), + {'old': deprecated, + 'new': recommended} + ) + + +def do_build_info(service, args=None): + """Retrieve build information. + + :param sc: Instance of senlinclient. + :param args: Additional command line arguments, if any. + """ + show_deprecated('senlin build-info', 'openstack cluster build info') + result = service.get_build_info() + + formatters = { + 'api': utils.json_formatter, + 'engine': utils.json_formatter, + } + utils.print_dict(result, formatters=formatters) + + +# PROFILE TYPES + + +def do_profile_type_list(service, args=None): + """List the available profile types. + + :param sc: Instance of senlinclient. + :param args: Additional command line arguments, if any. + """ + show_deprecated('senlin profile-type-list', + 'openstack cluster profile type list') + types = service.profile_types() + utils.print_list(types, ['name'], sortby_index=0) + + +@utils.arg('type_name', metavar='', + help=_('Profile type to retrieve.')) +@utils.arg('-F', '--format', metavar='', + choices=utils.supported_formats.keys(), + help=_("The template output format, one of: %s.") + % ', '.join(utils.supported_formats.keys())) +def do_profile_type_show(service, args): + """Get the details about a profile type.""" + show_deprecated('senlin profile-type-show', + 'openstack cluster profile type show') + try: + res = service.get_profile_type(args.type_name) + except sdk_exc.ResourceNotFound: + raise exc.CommandError( + _('Profile Type not found: %s') % args.type_name) + + pt = res.to_dict() + + if args.format: + print(utils.format_output(pt, format=args.format)) + else: + print(utils.format_output(pt)) + + +# PROFILES + +@utils.arg('-f', '--filters', metavar='', + help=_('Filter parameters to apply on returned profiles. ' + 'This can be specified multiple times, or once with ' + 'parameters separated by a semicolon.'), + action='append') +@utils.arg('-l', '--limit', metavar='', + help=_('Limit the number of profiles returned.')) +@utils.arg('-m', '--marker', metavar='', + help=_('Only return profiles that appear after the given ID.')) +@utils.arg('-o', '--sort', metavar='', + help=_('Sorting option which is a string containing a list of keys ' + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) +@utils.arg('-g', '--global-project', default=False, action="store_true", + help=_('Indicate that the list should include profiles from' + ' all projects. This option is subject to access policy ' + 'checking. Default is False.')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +def do_profile_list(service, args=None): + """List profiles that meet the criteria.""" + show_deprecated('senlin profile-list', 'openstack cluster profile list') + fields = ['id', 'name', 'type', 'created_at'] + queries = { + 'limit': args.limit, + 'marker': args.marker, + 'sort': args.sort, + 'global_project': args.global_project, + } + if args.filters: + queries.update(utils.format_parameters(args.filters)) + + sortby_index = None if args.sort else 1 + + profiles = service.profiles(**queries) + formatters = {} + if not args.full_id: + formatters = { + 'id': lambda x: x.id[:8], + } + utils.print_list(profiles, fields, formatters=formatters, + sortby_index=sortby_index) + + +def _show_profile(service, profile_id): + try: + profile = service.get_profile(profile_id) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Profile not found: %s') % profile_id) + + formatters = { + 'metadata': utils.json_formatter, + } + + formatters['spec'] = utils.nested_dict_formatter( + ['type', 'version', 'properties'], + ['property', 'value']) + + utils.print_dict(profile.to_dict(), formatters=formatters) + + +@utils.arg('-s', '--spec-file', metavar='', required=True, + help=_('The spec file used to create the profile.')) +@utils.arg('-M', '--metadata', metavar='', + help=_('Metadata values to be attached to the profile. ' + 'This can be specified multiple times, or once with ' + 'key-value pairs separated by a semicolon.'), + action='append') +@utils.arg('name', metavar='', + help=_('Name of the profile to create.')) +def do_profile_create(service, args): + """Create a profile.""" + show_deprecated('senlin profile-create', + 'openstack cluster profile create') + spec = utils.get_spec_content(args.spec_file) + type_name = spec.get('type', None) + type_version = spec.get('version', None) + properties = spec.get('properties', None) + if type_name is None: + raise exc.CommandError(_("Missing 'type' key in spec file.")) + if type_version is None: + raise exc.CommandError(_("Missing 'version' key in spec file.")) + if properties is None: + raise exc.CommandError(_("Missing 'properties' key in spec file.")) + + if type_name == 'os.heat.stack': + stack_properties = utils.process_stack_spec(properties) + spec['properties'] = stack_properties + + params = { + 'name': args.name, + 'spec': spec, + 'metadata': utils.format_parameters(args.metadata), + } + + profile = service.create_profile(**params) + _show_profile(service, profile.id) + + +@utils.arg('id', metavar='', + help=_('Name or ID of profile to show.')) +def do_profile_show(service, args): + """Show the profile details.""" + show_deprecated('senlin profile-show', 'openstack cluster profile show') + _show_profile(service, args.id) + + +@utils.arg('-n', '--name', metavar='', + help=_('The new name for the profile.')) +@utils.arg('-M', '--metadata', metavar='', + help=_('Metadata values to be attached to the profile. ' + 'This can be specified multiple times, or once with ' + 'key-value pairs separated by a semicolon.'), + action='append') +@utils.arg('id', metavar='', + help=_('Name or ID of the profile to update.')) +def do_profile_update(service, args): + """Update a profile.""" + show_deprecated('senlin profile-update', + 'openstack cluster profile update') + params = { + 'name': args.name, + } + if args.metadata: + params['metadata'] = utils.format_parameters(args.metadata) + + # Find the profile first, we need its id + try: + profile = service.get_profile(args.id) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Profile not found: %s') % args.id) + service.update_profile(profile.id, **params) + _show_profile(service, profile.id) + + +@utils.arg('id', metavar='', nargs='+', + help=_('Name or ID of profile(s) to delete.')) +def do_profile_delete(service, args): + """Delete profile(s).""" + show_deprecated('senlin profile-delete', + 'openstack cluster profile delete') + failure_count = 0 + + for pid in args.id: + try: + service.delete_profile(pid, False) + except Exception as ex: + failure_count += 1 + print(ex) + if failure_count > 0: + msg = _('Failed to delete some of the specified profile(s).') + raise exc.CommandError(msg) + print('Profile deleted: %s' % args.id) + + +# POLICY TYPES + + +def do_policy_type_list(service, args): + """List the available policy types.""" + show_deprecated('senlin policy-type-list', + 'openstack cluster policy type list') + types = service.policy_types() + utils.print_list(types, ['name'], sortby_index=0) + + +@utils.arg('type_name', metavar='', + help=_('Policy type to retrieve.')) +@utils.arg('-F', '--format', metavar='', + choices=utils.supported_formats.keys(), + help=_("The template output format, one of: %s.") + % ', '.join(utils.supported_formats.keys())) +def do_policy_type_show(service, args): + """Get the details about a policy type.""" + show_deprecated('senlin policy-type-show', + 'openstack cluster policy type show') + try: + res = service.get_policy_type(args.type_name) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Policy type not found: %s') % args.type_name) + + pt = res.to_dict() + if args.format: + print(utils.format_output(pt, format=args.format)) + else: + print(utils.format_output(pt)) + + +# POLICIES + +@utils.arg('-f', '--filters', metavar='', + help=_('Filter parameters to apply on returned policies. ' + 'This can be specified multiple times, or once with ' + 'parameters separated by a semicolon.'), + action='append') +@utils.arg('-l', '--limit', metavar='', + help=_('Limit the number of policies returned.')) +@utils.arg('-m', '--marker', metavar='', + help=_('Only return policies that appear after the given ID.')) +@utils.arg('-o', '--sort', metavar='', + help=_('Sorting option which is a string containing a list of keys ' + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) +@utils.arg('-g', '--global-project', default=False, action="store_true", + help=_('Indicate that the list should include policies from' + ' all projects. This option is subject to access policy ' + 'checking. Default is False.')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +def do_policy_list(service, args=None): + """List policies that meet the criteria.""" + show_deprecated('senlin policy-list', 'openstack cluster policy list') + fields = ['id', 'name', 'type', 'created_at'] + queries = { + 'limit': args.limit, + 'marker': args.marker, + 'sort': args.sort, + 'global_project': args.global_project, + } + if args.filters: + queries.update(utils.format_parameters(args.filters)) + + sortby_index = None if args.sort else 1 + policies = service.policies(**queries) + formatters = {} + if not args.full_id: + formatters = { + 'id': lambda x: x.id[:8] + } + utils.print_list(policies, fields, formatters=formatters, + sortby_index=sortby_index) + + +def _show_policy(service, policy_id): + try: + policy = service.get_policy(policy_id) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Policy not found: %s') % policy_id) + + formatters = { + 'metadata': utils.json_formatter, + 'spec': utils.json_formatter, + } + utils.print_dict(policy.to_dict(), formatters=formatters) + + +@utils.arg('-s', '--spec-file', metavar='', required=True, + help=_('The spec file used to create the policy.')) +@utils.arg('name', metavar='', + help=_('Name of the policy to create.')) +def do_policy_create(service, args): + """Create a policy.""" + show_deprecated('senlin policy-create', 'openstack cluster policy create') + spec = utils.get_spec_content(args.spec_file) + attrs = { + 'name': args.name, + 'spec': spec, + } + + policy = service.create_policy(**attrs) + _show_policy(service, policy.id) + + +@utils.arg('id', metavar='', + help=_('Name of the policy to be updated.')) +def do_policy_show(service, args): + """Show the policy details.""" + show_deprecated('senlin policy-show', 'openstack cluster policy show') + _show_policy(service, policy_id=args.id) + + +@utils.arg('-n', '--name', metavar='', + help=_('New name of the policy to be updated.')) +@utils.arg('id', metavar='', + help=_('Name of the policy to be updated.')) +def do_policy_update(service, args): + """Update a policy.""" + show_deprecated('senlin policy-update', 'openstack cluster policy update') + params = { + 'name': args.name, + } + + policy = service.get_policy(args.id) + if policy is not None: + service.update_policy(policy.id, **params) + _show_policy(service, policy_id=policy.id) + + +@utils.arg('id', metavar='', nargs='+', + help=_('Name or ID of policy(s) to delete.')) +def do_policy_delete(service, args): + """Delete policy(s).""" + show_deprecated('senlin policy-delete', 'openstack cluster policy delete') + failure_count = 0 + + for pid in args.id: + try: + service.delete_policy(pid, False) + except Exception as ex: + failure_count += 1 + print(ex) + if failure_count > 0: + msg = _('Failed to delete some of the specified policy(s).') + raise exc.CommandError(msg) + print('Policy deleted: %s' % args.id) + + +# CLUSTERS + + +@utils.arg('-f', '--filters', metavar='', + help=_('Filter parameters to apply on returned clusters. ' + 'This can be specified multiple times, or once with ' + 'parameters separated by a semicolon.'), + action='append') +@utils.arg('-o', '--sort', metavar='', + help=_('Sorting option which is a string containing a list of keys ' + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) +@utils.arg('-l', '--limit', metavar='', + help=_('Limit the number of clusters returned.')) +@utils.arg('-m', '--marker', metavar='', + help=_('Only return clusters that appear after the given cluster ' + 'ID.')) +@utils.arg('-g', '--global-project', default=False, action="store_true", + help=_('Indicate that the cluster list should include clusters from' + ' all projects. This option is subject to access policy ' + 'checking. Default is False.')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +def do_cluster_list(service, args=None): + """List the user's clusters.""" + show_deprecated('senlin cluster-list', 'openstack cluster list') + fields = ['id', 'name', 'status', 'created_at', 'updated_at'] + queries = { + 'limit': args.limit, + 'marker': args.marker, + 'sort': args.sort, + 'global_project': args.global_project, + } + if args.filters: + queries.update(utils.format_parameters(args.filters)) + + sortby_index = None if args.sort else 3 + + clusters = service.clusters(**queries) + formatters = {} + if not args.full_id: + formatters = { + 'id': lambda x: x.id[:8] + } + utils.print_list(clusters, fields, formatters=formatters, + sortby_index=sortby_index) + + +def _show_cluster(service, cluster_id): + try: + cluster = service.get_cluster(cluster_id) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Cluster not found: %s') % cluster_id) + + formatters = { + 'metadata': utils.json_formatter, + 'nodes': utils.list_formatter, + } + utils.print_dict(cluster.to_dict(), formatters=formatters) + + +@utils.arg('-p', '--profile', metavar='', required=True, + help=_('Profile Id used for this cluster.')) +@utils.arg('-n', '--min-size', metavar='', default=0, + help=_('Min size of the cluster. Default to 0.')) +@utils.arg('-m', '--max-size', metavar='', default=-1, + help=_('Max size of the cluster. Default to -1, means unlimited.')) +@utils.arg('-c', '--desired-capacity', metavar='', default=0, + help=_('Desired capacity of the cluster. Default to min_size if ' + 'min_size is specified else 0.')) +@utils.arg('-t', '--timeout', metavar='', type=int, + help=_('Cluster creation timeout in seconds.')) +@utils.arg('-M', '--metadata', metavar='', + help=_('Metadata values to be attached to the cluster. ' + 'This can be specified multiple times, or once with ' + 'key-value pairs separated by a semicolon.'), + action='append') +@utils.arg('name', metavar='', + help=_('Name of the cluster to create.')) +def do_cluster_create(service, args): + """Create the cluster.""" + show_deprecated('senlin cluster-create', 'openstack cluster create') + if args.min_size and not args.desired_capacity: + args.desired_capacity = args.min_size + attrs = { + 'name': args.name, + 'profile_id': args.profile, + 'min_size': args.min_size, + 'max_size': args.max_size, + 'desired_capacity': args.desired_capacity, + 'metadata': utils.format_parameters(args.metadata), + 'timeout': args.timeout + } + + cluster = service.create_cluster(**attrs) + _show_cluster(service, cluster.id) + + +@utils.arg('id', metavar='', nargs='+', + help=_('Name or ID of cluster(s) to delete.')) +def do_cluster_delete(service, args): + """Delete the cluster(s).""" + show_deprecated('senlin cluster-delete', 'openstack cluster delete') + failure_count = 0 + + for cid in args.id: + try: + service.delete_cluster(cid, False) + except Exception as ex: + failure_count += 1 + print(ex) + if failure_count > 0: + msg = _('Failed to delete some of the specified clusters.') + raise exc.CommandError(msg) + print('Request accepted') + + +@utils.arg('-p', '--profile', metavar='', + help=_('ID of new profile to use.')) +@utils.arg('-t', '--timeout', metavar='', + help=_('New timeout (in seconds) value for the cluster.')) +@utils.arg('-M', '--metadata', metavar='', + help=_('Metadata values to be attached to the cluster. ' + 'This can be specified multiple times, or once with ' + 'key-value pairs separated by a semicolon.'), + action='append') +@utils.arg('-n', '--name', metavar='', + help=_('New name for the cluster to update.')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to be updated.')) +def do_cluster_update(service, args): + """Update the cluster.""" + show_deprecated('senlin cluster-update', 'openstack cluster update') + cluster = service.get_cluster(args.id) + attrs = { + 'name': args.name, + 'profile_id': args.profile, + 'metadata': utils.format_parameters(args.metadata), + 'timeout': args.timeout, + } + + service.update_cluster(cluster.id, **attrs) + _show_cluster(service, cluster.id) + + +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to show.')) +def do_cluster_show(service, args): + """Show details of the cluster.""" + show_deprecated('senlin cluster-show', 'openstack cluster show') + _show_cluster(service, args.id) + + +@utils.arg('-f', '--filters', metavar='', + help=_('Filter parameters to apply on returned nodes. ' + 'This can be specified multiple times, or once with ' + 'parameters separated by a semicolon.'), + action='append') +@utils.arg('-l', '--limit', metavar='', + help=_('Limit the number of nodes returned.')) +@utils.arg('-m', '--marker', metavar='', + help=_('Only return nodes that appear after the given node ID.')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to nodes from.')) +def do_cluster_node_list(service, args): + """List nodes from cluster.""" + show_deprecated('senlin cluster-node-list', + 'openstack cluster node members list') + queries = { + 'cluster_id': args.id, + 'limit': args.limit, + 'marker': args.marker, + } + if args.filters: + queries.update(utils.format_parameters(args.filters)) + + nodes = service.nodes(**queries) + if not args.full_id: + formatters = { + 'id': lambda x: x.id[:8], + 'physical_id': lambda x: x.physical_id[:8] if x.physical_id else '' + } + else: + formatters = {} + + fields = ['id', 'name', 'index', 'status', 'physical_id', 'created_at'] + utils.print_list(nodes, fields, formatters=formatters, sortby_index=5) + + +@utils.arg('-n', '--nodes', metavar='', required=True, + help=_('ID of nodes to be added; multiple nodes can be separated ' + 'with ","')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to operate on.')) +def do_cluster_node_add(service, args): + """Add specified nodes to cluster.""" + show_deprecated('senlin cluster-node-add', + 'openstack cluster node members add') + node_ids = args.nodes.split(',') + resp = service.cluster_add_nodes(args.id, node_ids) + print('Request accepted by action: %s' % resp['action']) + + +@utils.arg('-n', '--nodes', metavar='', required=True, + help=_('ID of nodes to be deleted; multiple nodes can be separated ' + 'with ",".')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to operate on.')) +def do_cluster_node_del(service, args): + """Delete specified nodes from cluster.""" + show_deprecated('senlin cluster-node-del', + 'openstack cluster node members del') + node_ids = args.nodes.split(',') + resp = service.cluster_del_nodes(args.id, node_ids) + print('Request accepted by action: %s' % resp['action']) + + +@utils.arg('-c', '--capacity', metavar='', type=int, + help=_('The desired number of nodes of the cluster.')) +@utils.arg('-a', '--adjustment', metavar='', type=int, + help=_('A positive integer meaning the number of nodes to add, ' + 'or a negative integer indicating the number of nodes to ' + 'remove.')) +@utils.arg('-p', '--percentage', metavar='', type=float, + help=_('A value that is interpreted as the percentage of size ' + 'adjustment. This value can be positive or negative.')) +@utils.arg('-t', '--min-step', metavar='', type=int, + help=_('An integer specifying the number of nodes for adjustment ' + 'when is specified.')) +@utils.arg('-s', '--strict', action='store_true', default=False, + help=_('A boolean specifying whether the resize should be ' + 'performed on a best-effort basis when the new capacity ' + 'may go beyond size constraints.')) +@utils.arg('-n', '--min-size', metavar='MIN', type=int, + help=_('New lower bound of cluster size.')) +@utils.arg('-m', '--max-size', metavar='MAX', type=int, + help=_('New upper bound of cluster size. A value of -1 indicates ' + 'no upper limit on cluster size.')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to operate on.')) +def do_cluster_resize(service, args): + """Resize a cluster.""" + # validate parameters + # NOTE: this will be much simpler if cliutils supports exclusive groups + show_deprecated('senlin cluster-resize', 'openstack cluster resize') + action_args = {} + + capacity = args.capacity + adjustment = args.adjustment + percentage = args.percentage + min_size = args.min_size + max_size = args.max_size + min_step = args.min_step + + if sum(v is not None for v in (capacity, adjustment, percentage)) > 1: + raise exc.CommandError(_("Only one of 'capacity', 'adjustment' and " + "'percentage' can be specified.")) + + action_args['adjustment_type'] = None + action_args['number'] = None + + if capacity is not None: + if capacity < 0: + raise exc.CommandError(_('Cluster capacity must be larger than ' + ' or equal to zero.')) + action_args['adjustment_type'] = 'EXACT_CAPACITY' + action_args['number'] = capacity + + if adjustment is not None: + if adjustment == 0: + raise exc.CommandError(_('Adjustment cannot be zero.')) + action_args['adjustment_type'] = 'CHANGE_IN_CAPACITY' + action_args['number'] = adjustment + + if percentage is not None: + if (percentage == 0 or percentage == 0.0): + raise exc.CommandError(_('Percentage cannot be zero.')) + action_args['adjustment_type'] = 'CHANGE_IN_PERCENTAGE' + action_args['number'] = percentage + + if min_step is not None: + if percentage is None: + raise exc.CommandError(_('Min step is only used with percentage.')) + + if min_size is not None: + if min_size < 0: + raise exc.CommandError(_('Min size cannot be less than zero.')) + if max_size is not None and max_size >= 0 and min_size > max_size: + raise exc.CommandError(_('Min size cannot be larger than ' + 'max size.')) + if capacity is not None and min_size > capacity: + raise exc.CommandError(_('Min size cannot be larger than the ' + 'specified capacity')) + + if max_size is not None: + if capacity is not None and max_size > 0 and max_size < capacity: + raise exc.CommandError(_('Max size cannot be less than the ' + 'specified capacity.')) + # do a normalization + if max_size < 0: + max_size = -1 + + action_args['min_size'] = min_size + action_args['max_size'] = max_size + action_args['min_step'] = min_step + action_args['strict'] = args.strict + + resp = service.cluster_resize(args.id, **action_args) + print('Request accepted by action: %s' % resp['action']) + + +@utils.arg('-c', '--count', metavar='', + help=_('Number of nodes to be added to the specified cluster.')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to operate on.')) +def do_cluster_scale_out(service, args): + """Scale out a cluster by the specified number of nodes.""" + show_deprecated('senlin cluster-scale-out', 'openstack cluster scale out') + resp = service.cluster_scale_out(args.id, args.count) + print('Request accepted by action %s' % resp['action']) + + +@utils.arg('-c', '--count', metavar='', + help=_('Number of nodes to be deleted from the specified cluster.')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to operate on.')) +def do_cluster_scale_in(service, args): + """Scale in a cluster by the specified number of nodes.""" + show_deprecated('senlin cluster-scale-in', 'openstack cluster scale in') + resp = service.cluster_scale_in(args.id, args.count) + print('Request accepted by action %s' % resp['action']) + + +@utils.arg('-f', '--filters', metavar='', + help=_('Filter parameters to apply on returned results. ' + 'This can be specified multiple times, or once with ' + 'parameters separated by a semicolon.'), + action='append') +@utils.arg('-o', '--sort', metavar='', + help=_('Sorting option which is a string containing a list of keys ' + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to query on.')) +def do_cluster_policy_list(service, args): + """List policies from cluster.""" + show_deprecated('senlin cluster-policy-list', + 'openstack cluster policy binding list') + fields = ['policy_id', 'policy_name', 'policy_type', 'enabled'] + + cluster = service.get_cluster(args.id) + queries = { + 'sort': args.sort, + } + + if args.filters: + queries.update(utils.format_parameters(args.filters)) + + sortby_index = None if args.sort else 3 + policies = service.cluster_policies(cluster.id, **queries) + formatters = {} + if not args.full_id: + formatters = { + 'policy_id': lambda x: x.id[:8] + } + + utils.print_list(policies, fields, formatters=formatters, + sortby_index=sortby_index) + + +@utils.arg('-p', '--policy', metavar='', required=True, + help=_('ID or name of the policy to query on.')) +@utils.arg('id', metavar='', + help=_('ID or name of the cluster to query on.')) +def do_cluster_policy_show(service, args): + """Show a specific policy that is bound to the specified cluster.""" + show_deprecated('senlin cluster-policy-show', + 'openstack cluster policy binding show') + binding = service.get_cluster_policy(args.policy, args.id) + utils.print_dict(binding.to_dict()) + + +@utils.arg('-p', '--policy', metavar='', required=True, + help=_('ID or name of policy to be attached.')) +@utils.arg('-e', '--enabled', default=True, action="store_true", + help=_('Whether the policy should be enabled once attached. ' + 'Default to enabled.')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to operate on.')) +def do_cluster_policy_attach(service, args): + """Attach policy to cluster.""" + show_deprecated('senlin cluster-policy-attach', + 'openstack cluster policy attach') + kwargs = { + 'enabled': args.enabled, + } + + resp = service.cluster_attach_policy(args.id, args.policy, **kwargs) + print('Request accepted by action: %s' % resp['action']) + + +@utils.arg('-p', '--policy', metavar='', required=True, + help=_('ID or name of policy to be detached.')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to operate on.')) +def do_cluster_policy_detach(service, args): + """Detach policy from cluster.""" + show_deprecated('senlin cluster-policy-detach', + 'openstack cluster policy detach') + resp = service.cluster_detach_policy(args.id, args.policy) + print('Request accepted by action %s' % resp['action']) + + +@utils.arg('-p', '--policy', metavar='', required=True, + help=_('ID or name of policy to be updated.')) +@utils.arg('-e', '--enabled', metavar='', + help=_('Whether the policy should be enabled.')) +@utils.arg('id', metavar='', + help=_('Name or ID of cluster to operate on.')) +def do_cluster_policy_update(service, args): + """Update a policy's properties on a cluster.""" + show_deprecated('senlin cluster-policy-update', + 'openstack cluster policy binding update') + kwargs = { + 'enabled': args.enabled, + } + + resp = service.cluster_update_policy(args.id, args.policy, **kwargs) + print('Request accepted by action: %s' % resp['action']) + + +@utils.arg('id', metavar='', nargs='+', + help=_('ID or name of cluster(s) to operate on.')) +def do_cluster_check(service, args): + """Check the cluster(s).""" + show_deprecated('senlin cluster-check', 'openstack cluster check') + for cid in args.id: + resp = service.check_cluster(cid) + print('Cluster check request on cluster %(cid)s is accepted by ' + 'action %(action)s.' % {'cid': cid, 'action': resp['action']}) + + +@utils.arg('id', metavar='', nargs='+', + help=_('ID or name of cluster(s) to operate on.')) +def do_cluster_recover(service, args): + """Recover the cluster(s).""" + show_deprecated('senlin cluster-recover', 'openstack cluster recover') + for cid in args.id: + resp = service.recover_cluster(cid) + print('Cluster recover request on cluster %(cid)s is accepted by ' + 'action %(action)s.' % {'cid': cid, 'action': resp['action']}) + + +# NODES + + +@utils.arg('-c', '--cluster', metavar='', + help=_('ID or name of cluster from which nodes are to be listed.')) +@utils.arg('-f', '--filters', metavar='', + help=_('Filter parameters to apply on returned nodes. ' + 'This can be specified multiple times, or once with ' + 'parameters separated by a semicolon.'), + action='append') +@utils.arg('-o', '--sort', metavar='', + help=_('Sorting option which is a string containing a list of keys ' + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) +@utils.arg('-l', '--limit', metavar='', + help=_('Limit the number of nodes returned.')) +@utils.arg('-m', '--marker', metavar='', + help=_('Only return nodes that appear after the given node ID.')) +@utils.arg('-g', '--global-project', default=False, action="store_true", + help=_('Indicate that this node list should include nodes from ' + 'all projects. This option is subject to access policy ' + 'checking. Default is False.')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +def do_node_list(service, args): + """Show list of nodes.""" + show_deprecated('senlin node-list', 'openstack cluster node list') + + fields = ['id', 'name', 'index', 'status', 'cluster_id', 'physical_id', + 'profile_name', 'created_at', 'updated_at'] + queries = { + 'cluster_id': args.cluster, + 'sort': args.sort, + 'limit': args.limit, + 'marker': args.marker, + 'global_project': args.global_project, + } + + if args.filters: + queries.update(utils.format_parameters(args.filters)) + + sortby_index = None if args.sort else 6 + + nodes = service.nodes(**queries) + + if not args.full_id: + formatters = { + 'id': lambda x: x.id[:8], + 'cluster_id': lambda x: x.cluster_id[:8] if x.cluster_id else '', + 'physical_id': lambda x: x.physical_id[:8] if x.physical_id else '' + } + else: + formatters = {} + + utils.print_list(nodes, fields, formatters=formatters, + sortby_index=sortby_index) + + +def _show_node(service, node_id, show_details=False): + """Show detailed info about the specified node.""" + args = {'show_details': True} if show_details else None + try: + node = service.get_node(node_id, args=args) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Node not found: %s') % node_id) + + formatters = { + 'metadata': utils.json_formatter, + 'data': utils.json_formatter, + } + data = node.to_dict() + if show_details: + formatters['details'] = utils.nested_dict_formatter( + list(node['details'].keys()), ['property', 'value']) + + utils.print_dict(data, formatters=formatters) + + +@utils.arg('-p', '--profile', metavar='', required=True, + help=_('Profile Id used for this node.')) +@utils.arg('-c', '--cluster', metavar='', + help=_('Cluster Id for this node.')) +@utils.arg('-r', '--role', metavar='', + help=_('Role for this node in the specific cluster.')) +@utils.arg('-M', '--metadata', metavar='', + help=_('Metadata values to be attached to the node. ' + 'This can be specified multiple times, or once with ' + 'key-value pairs separated by a semicolon.'), + action='append') +@utils.arg('name', metavar='', + help=_('Name of the node to create.')) +def do_node_create(service, args): + """Create the node.""" + show_deprecated('senlin node-create', 'openstack cluster node create') + attrs = { + 'name': args.name, + 'cluster_id': args.cluster, + 'profile_id': args.profile, + 'role': args.role, + 'metadata': utils.format_parameters(args.metadata), + } + + node = service.create_node(**attrs) + _show_node(service, node.id) + + +@utils.arg('-D', '--details', default=False, action="store_true", + help=_('Include physical object details.')) +@utils.arg('id', metavar='', + help=_('Name or ID of the node to show the details for.')) +def do_node_show(service, args): + """Show detailed info about the specified node.""" + show_deprecated('senlin node-show', 'openstack cluster node show') + _show_node(service, args.id, args.details) + + +@utils.arg('id', metavar='', nargs='+', + help=_('Name or ID of node(s) to delete.')) +def do_node_delete(service, args): + """Delete the node(s).""" + show_deprecated('senlin node-delete', 'openstack cluster node delete') + failure_count = 0 + + for nid in args.id: + try: + service.delete_node(nid, False) + except Exception as ex: + failure_count += 1 + print(ex) + if failure_count > 0: + msg = _('Failed to delete some of the specified nodes.') + raise exc.CommandError(msg) + print('Request accepted') + + +@utils.arg('-n', '--name', metavar='', + help=_('New name for the node.')) +@utils.arg('-p', '--profile', metavar='', + help=_('ID of new profile to use.')) +@utils.arg('-r', '--role', metavar='', + help=_('Role for this node in the specific cluster.')) +@utils.arg('-M', '--metadata', metavar='', + help=_('Metadata values to be attached to the node. ' + 'Metadata can be specified multiple times, or once with ' + 'key-value pairs separated by a semicolon.'), + action='append') +@utils.arg('id', metavar='', + help=_('Name or ID of node to update.')) +def do_node_update(service, args): + """Update the node.""" + show_deprecated('senlin node-update', 'openstack cluster node update') + # Find the node first, we need its UUID + try: + node = service.get_node(args.id) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Node not found: %s') % args.id) + + attrs = { + 'name': args.name, + 'role': args.role, + 'profile_id': args.profile, + 'metadata': utils.format_parameters(args.metadata), + } + + service.update_node(args.id, **attrs) + _show_node(service, node.id) + + +@utils.arg('id', metavar='', nargs='+', + help=_('ID of node(s) to check.')) +def do_node_check(service, args): + """Check the node(s).""" + show_deprecated('senlin node-check', 'openstack cluster node check') + failure_count = 0 + + for nid in args.id: + try: + service.check_node(nid) + except exc.HTTPNotFound: + failure_count += 1 + print('Node id "%s" not found' % nid) + if failure_count > 0: + msg = _('Failed to check some of the specified nodes.') + raise exc.CommandError(msg) + print('Request accepted') + + +@utils.arg('id', metavar='', nargs='+', + help=_('ID of node(s) to recover.')) +def do_node_recover(service, args): + """Recover the node(s).""" + show_deprecated('senlin node-recover', 'openstack cluster node recover') + failure_count = 0 + + for nid in args.id: + try: + service.recover_node(nid) + except exc.HTTPNotFound: + failure_count += 1 + print('Node id "%s" not found' % nid) + if failure_count > 0: + msg = _('Failed to recover some of the specified nodes.') + raise exc.CommandError(msg) + print('Request accepted') + + +# RECEIVERS + + +@utils.arg('-f', '--filters', metavar='', + help=_('Filter parameters to apply on returned receivers. ' + 'This can be specified multiple times, or once with ' + 'parameters separated by a semicolon.'), + action='append') +@utils.arg('-l', '--limit', metavar='', + help=_('Limit the number of receivers returned.')) +@utils.arg('-m', '--marker', metavar='', + help=_('Only return receivers that appear after the given ID.')) +@utils.arg('-o', '--sort', metavar='', + help=_('Sorting option which is a string containing a list of keys ' + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) +@utils.arg('-g', '--global-project', default=False, action="store_true", + help=_('Indicate that the list should include receivers from' + ' all projects. This option is subject to access policy ' + 'checking. Default is False.')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +def do_receiver_list(service, args): + """List receivers that meet the criteria.""" + show_deprecated('senlin receiver-list', 'openstack cluster receiver list') + fields = ['id', 'name', 'type', 'cluster_id', 'action', 'created_at'] + queries = { + 'limit': args.limit, + 'marker': args.marker, + 'sort': args.sort, + 'global_project': args.global_project, + } + + if args.filters: + queries.update(utils.format_parameters(args.filters)) + + sortby_index = None if args.sort else 0 + + receivers = service.receivers(**queries) + formatters = {} + if not args.full_id: + formatters = { + 'id': lambda x: x.id[:8], + 'cluster_id': lambda x: x.cluster_id[:8], + } + utils.print_list(receivers, fields, formatters=formatters, + sortby_index=sortby_index) + + +def _show_receiver(service, receiver_id): + try: + receiver = service.get_receiver(receiver_id) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Receiver not found: %s') % receiver_id) + + formatters = { + 'actor': utils.json_formatter, + 'params': utils.json_formatter, + 'channel': utils.json_formatter, + } + + utils.print_dict(receiver.to_dict(), formatters=formatters) + + +@utils.arg('id', metavar='', + help=_('Name or ID of the receiver to show.')) +def do_receiver_show(service, args): + """Show the receiver details.""" + show_deprecated('senlin receiver-show', 'openstack cluster receiver show') + _show_receiver(service, receiver_id=args.id) + + +@utils.arg('-t', '--type', metavar='', default='webhook', + help=_('Type of the receiver to create.')) +@utils.arg('-c', '--cluster', metavar='', required=True, + help=_('Targeted cluster for this receiver.')) +@utils.arg('-a', '--action', metavar='', required=True, + help=_('Name or ID of the targeted action to be triggered.')) +@utils.arg('-P', '--params', metavar='', + help=_('A dictionary of parameters that will be passed to target ' + 'action when the receiver is triggered.'), + action='append') +@utils.arg('name', metavar='', + help=_('Name of the receiver to create.')) +def do_receiver_create(service, args): + """Create a receiver.""" + show_deprecated('senlin receiver-create', + 'openstack cluster receiver create') + + params = { + 'name': args.name, + 'type': args.type, + 'cluster_id': args.cluster, + 'action': args.action, + 'params': utils.format_parameters(args.params) + } + + receiver = service.create_receiver(**params) + _show_receiver(service, receiver.id) + + +@utils.arg('id', metavar='', nargs='+', + help=_('Name or ID of receiver(s) to delete.')) +def do_receiver_delete(service, args): + """Delete receiver(s).""" + show_deprecated('senlin receiver-delete', + 'openstack cluster receiver delete') + failure_count = 0 + + for wid in args.id: + try: + service.delete_receiver(wid, False) + except Exception as ex: + failure_count += 1 + print(ex) + if failure_count > 0: + msg = _('Failed to delete some of the specified receiver(s).') + raise exc.CommandError(msg) + print('Receivers deleted: %s' % args.id) + + +# EVENTS + + +@utils.arg('-f', '--filters', metavar='', + help=_('Filter parameters to apply on returned events. ' + 'This can be specified multiple times, or once with ' + 'parameters separated by a semicolon.'), + action='append') +@utils.arg('-l', '--limit', metavar='', + help=_('Limit the number of events returned.')) +@utils.arg('-m', '--marker', metavar='', + help=_('Only return events that appear after the given event ID.')) +@utils.arg('-o', '--sort', metavar='', + help=_('Sorting option which is a string containing a list of keys ' + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) +@utils.arg('-g', '--global-project', default=False, action="store_true", + help=_('Whether events from all projects should be listed. ' + ' Default to False. Setting this to True may demand ' + 'for an admin privilege.')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +def do_event_list(service, args): + """List events.""" + show_deprecated('senlin event-list', 'openstack cluster event list') + fields = ['id', 'timestamp', 'obj_type', 'obj_id', 'obj_name', 'action', + 'status', 'status_reason', 'level'] + queries = { + 'sort': args.sort, + 'limit': args.limit, + 'marker': args.marker, + 'global_project': args.global_project, + } + + if args.filters: + queries.update(utils.format_parameters(args.filters)) + + formatters = {} + if not args.full_id: + formatters['id'] = lambda x: x.id[:8] + formatters['obj_id'] = lambda x: x.obj_id[:8] if x.obj_id else '' + + events = service.events(**queries) + utils.print_list(events, fields, formatters=formatters) + + +@utils.arg('id', metavar='', + help=_('ID of event to display details for.')) +def do_event_show(service, args): + """Describe the event.""" + show_deprecated('senlin event-show', 'openstack cluster event show') + try: + event = service.get_event(args.id) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_("Event not found: %s") % args.id) + + utils.print_dict(event.to_dict()) + + +# ACTIONS + + +@utils.arg('-f', '--filters', metavar='', + help=_('Filter parameters to apply on returned actions. ' + 'This can be specified multiple times, or once with ' + 'parameters separated by a semicolon.'), + action='append') +@utils.arg('-o', '--sort', metavar='', + help=_('Sorting option which is a string containing a list of keys ' + 'separated by commas. Each key can be optionally appended ' + 'by a sort direction (:asc or :desc)')) +@utils.arg('-l', '--limit', metavar='', + help=_('Limit the number of actions returned.')) +@utils.arg('-m', '--marker', metavar='', + help=_('Only return actions that appear after the given node ID.')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +def do_action_list(service, args): + """List actions.""" + show_deprecated('senlin action-list', 'openstack cluster action list') + fields = ['id', 'name', 'action', 'status', 'target', 'depends_on', + 'depended_by', 'created_at'] + + queries = { + 'sort': args.sort, + 'limit': args.limit, + 'marker': args.marker, + } + + if args.filters: + queries.update(utils.format_parameters(args.filters)) + + sortby_index = None if args.sort else 0 + + actions = service.actions(**queries) + + formatters = {} + if args.full_id: + f_depon = lambda x: '\n'.join(a for a in x.depends_on) + f_depby = lambda x: '\n'.join(a for a in x.depended_by) + + formatters['depends_on'] = f_depon + formatters['depended_by'] = f_depby + else: + formatters['id'] = lambda x: x.id[:8] + formatters['target'] = lambda x: x.target[:8] + f_depon = lambda x: '\n'.join(a[:8] for a in x.depends_on) + f_depby = lambda x: '\n'.join(a[:8] for a in x.depended_by) + formatters['depends_on'] = f_depon + formatters['depended_by'] = f_depby + + utils.print_list(actions, fields, formatters=formatters, + sortby_index=sortby_index) + + +@utils.arg('id', metavar='', + help=_('Name or ID of the action to show the details for.')) +def do_action_show(service, args): + """Show detailed info about the specified action.""" + show_deprecated('senlin action-show', 'openstack cluster action show') + try: + action = service.get_action(args.id) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Action not found: %s') % args.id) + + formatters = { + 'inputs': utils.json_formatter, + 'outputs': utils.json_formatter, + 'metadata': utils.json_formatter, + 'data': utils.json_formatter, + 'depends_on': utils.list_formatter, + 'depended_by': utils.list_formatter, + } + + utils.print_dict(action.to_dict(), formatters=formatters) diff --git a/setup.cfg b/setup.cfg index b4027d4..3f4141c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,6 +23,9 @@ packages = senlinclient [entry_points] +console_scripts = + senlin = senlinclient.shell:main + openstack.cli.extension = clustering = senlinclient.plugin From e42edf4d6190e4a24604081db1b1c194190e6e3c Mon Sep 17 00:00:00 2001 From: yanyanhu Date: Fri, 1 Apr 2016 01:58:39 -0400 Subject: [PATCH 09/25] Add link to API doc in client module This patch adds link to Senlin API document in client module to give developers a guide to invoke senlinclient interfaces. Change-Id: I1671e0f0f3df15cd9667ff2b039e61d5b462e8e7 --- senlinclient/v1/client.py | 237 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) diff --git a/senlinclient/v1/client.py b/senlinclient/v1/client.py index 334f1d9..944eada 100644 --- a/senlinclient/v1/client.py +++ b/senlinclient/v1/client.py @@ -27,139 +27,376 @@ class Client(object): ###################################################################### def profile_types(self, **query): + """List profile types + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html + #listProfileTypes + """ return self.service.profile_types(**query) def get_profile_type(self, profile_type): + """Show profile type details + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#showProfileType + """ return self.service.get_profile_type(profile_type) def profiles(self, **query): + """List profiles + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#listProfiles + """ return self.service.profiles(**query) def create_profile(self, **attrs): + """Create a profile + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#createProfile + """ return self.service.create_profile(**attrs) def get_profile(self, profile): + """Show profile details + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#showProfile + """ return self.service.get_profile(profile) def update_profile(self, profile, **attrs): + """Update a profile + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#updateProfile + """ return self.service.update_profile(profile, **attrs) def delete_profile(self, profile, ignore_missing=True): + """Delete a profile + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#deleteProfile + """ return self.service.delete_profile(profile, ignore_missing) def policy_types(self, **query): + """List policy types + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html + #listPolicyType + """ return self.service.policy_types(**query) def get_policy_type(self, policy_type): + """Show policy type details + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html + #showPolicyType + """ return self.service.get_policy_type(policy_type) def policies(self, **query): + """List policies + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#listPolicies + """ return self.service.policies(**query) def create_policy(self, **attrs): + """Create a policy + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#createPolicy + """ return self.service.create_policy(**attrs) def get_policy(self, policy): + """Show policy details + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#showPolicy + """ return self.service.get_policy(policy) def update_policy(self, policy, **attrs): + """Update policy + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#updatePolicy + """ return self.service.update_policy(policy, **attrs) def delete_policy(self, policy, ignore_missing=True): + """Delete policy + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#deletePolicy + """ return self.service.delete_policy(policy, ignore_missing) def clusters(self, **queries): + """List clusters + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#listClusters + """ return self.service.clusters(**queries) def create_cluster(self, **attrs): + """Create a cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#createCluster + """ return self.service.create_cluster(**attrs) def get_cluster(self, cluster): + """Show cluster details + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#showCluster + """ return self.service.get_cluster(cluster) def update_cluster(self, cluster, **attrs): + """Update cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#updateCluster + """ return self.service.update_cluster(cluster, **attrs) def delete_cluster(self, cluster, ignore_missing=True): + """Delete cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#deleteCluster + """ return self.service.delete_cluster(cluster, ignore_missing) def cluster_add_nodes(self, cluster, nodes): + """Add a node to cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.cluster_add_nodes(cluster, nodes) def cluster_del_nodes(self, cluster, nodes): + """Delete a node belongs to cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.cluster_del_nodes(cluster, nodes) def cluster_resize(self, cluster, **params): + """Resize cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.cluster_resize(cluster, **params) def cluster_scale_out(self, cluster, count): + """Scale out cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.cluster_scale_out(cluster, count) def cluster_scale_in(self, cluster, count): + """Scale in cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.cluster_scale_in(cluster, count) def cluster_policies(self, cluster, **queries): + """List all policies attached to clusdter + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html + #listClusterPolicies + """ return self.service.cluster_policies(cluster, **queries) def get_cluster_policy(self, policy, cluster): + """Show details of a policy attached to cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html + #showClusterPolicy + """ return self.service.get_cluster_policy(policy, cluster) def cluster_attach_policy(self, cluster, policy, **attrs): + """Attach a policy to cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.cluster_attach_policy(cluster, policy, **attrs) def cluster_detach_policy(self, cluster, policy): + """Detach a policy from cluster + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.cluster_detach_policy(cluster, policy) def cluster_update_policy(self, cluster, policy, **attrs): + """Update the policy attachment + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.cluster_update_policy(cluster, policy, **attrs) def check_cluster(self, cluster, **params): + """Check cluster's health status + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.check_cluster(cluster, **params) def recover_cluster(self, cluster, **params): + """Recover cluster from failure state + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#clusterAction + """ return self.service.recover_cluster(cluster, **params) def nodes(self, **queries): + """List nodes + + Doc link: http://developer.openstack.org/api-ref-clustering-v1.html + #listNodes + """ return self.service.nodes(**queries) def create_node(self, **attrs): + """Create a node + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#createNode + """ return self.service.create_node(**attrs) def get_node(self, node, args=None): + """Show node details + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#showNode + """ return self.service.get_node(node, args=args) def update_node(self, node, **attrs): + """Update node + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#updateNode + """ return self.service.update_node(node, **attrs) def delete_node(self, node, ignore_missing=True): + """Delete node + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#deleteNode + """ return self.service.delete_node(node, ignore_missing) def check_node(self, node, **params): + """Check node's health status + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#nodeAction + """ return self.service.check_node(node, **params) def recover_node(self, node, **params): + """Recover node from failure state + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#nodeAction + """ return self.service.recover_node(node, **params) def receivers(self, **queries): + """List receivers + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#listReceivers + """ return self.service.receivers(**queries) def create_receiver(self, **attrs): + """Creare a receiver + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html + #createReceiver + """ return self.service.create_receiver(**attrs) def get_receiver(self, receiver): + """Show receiver details + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#showReceiver + """ return self.service.get_receiver(receiver) def delete_receiver(self, receiver, ignore_missing=True): + """Delete receiver + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html + #deleteReceiver + """ return self.service.delete_receiver(receiver, ignore_missing) def events(self, **queries): + """List events + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#listEvents + """ return self.service.events(**queries) def get_event(self, event): + """Show event details + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#showEvent + """ return self.service.get_event(event) def actions(self, **queries): + """List actions + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#listActions + """ return self.service.actions(**queries) def get_action(self, action): + """Show action details + + Doc link: + http://developer.openstack.org/api-ref-clustering-v1.html#showAction + """ return self.service.get_action(action) From d1de6ec0d5a49721d962e006bd120626012de6d3 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Mon, 11 Apr 2016 18:16:03 +0800 Subject: [PATCH 10/25] Removed the invalid link for Module Index Same as bug 1538511, this also should be removed. Change-Id: I0ea57e6f8274ee29e66b6d4494cd3255c1999685 --- doc/source/index.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/index.rst b/doc/source/index.rst index 1558c90..d373e69 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -17,6 +17,5 @@ Indices and tables ================== * :ref:`genindex` -* :ref:`modindex` * :ref:`search` From 7bf8bb34726a09aadf0710a90e68077b6d47d3dd Mon Sep 17 00:00:00 2001 From: zzxwill Date: Mon, 11 Apr 2016 20:45:59 +0800 Subject: [PATCH 11/25] Pramater doesn't align to comments Change param 'details' to 'exc' to match the parameter of function parse_exception Change-Id: I82cb3bca09fceecb1807fc7f39a8fbfc57e22e81 --- senlinclient/common/exc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/senlinclient/common/exc.py b/senlinclient/common/exc.py index e5e0a6a..7712283 100644 --- a/senlinclient/common/exc.py +++ b/senlinclient/common/exc.py @@ -230,7 +230,7 @@ _EXCEPTION_MAP = { def parse_exception(exc): """Parse exception code and yield useful information. - :param details: details of the exception. + :param exc: details of the exception. """ if isinstance(exc, sdkexc.HttpException): if exc.details is None: From 18b54945cdd0f1a556a1863ce48d77f058d2c0a8 Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Mon, 11 Apr 2016 18:32:33 +0000 Subject: [PATCH 12/25] Updated from global requirements Change-Id: I207df03375af121e282165aa6387a1c2d994dca8 --- requirements.txt | 2 +- test-requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 1b408d1..7707428 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,7 @@ Babel>=1.3 # BSD pbr>=1.6 # Apache-2.0 cliff!=1.16.0,!=1.17.0,>=1.15.0 # Apache-2.0 PrettyTable<0.8,>=0.7 # BSD -openstacksdk>=0.8.1 # Apache-2.0 +openstacksdk>=0.8.4 # Apache-2.0 oslo.i18n>=2.1.0 # Apache-2.0 oslo.serialization>=1.10.0 # Apache-2.0 oslo.utils>=3.5.0 # Apache-2.0 diff --git a/test-requirements.txt b/test-requirements.txt index fcf1a68..baa40d0 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,7 +6,7 @@ hacking<0.11,>=0.10.0 coverage>=3.6 # Apache-2.0 discover # BSD -fixtures>=1.3.1 # Apache-2.0/BSD +fixtures<2.0,>=1.3.1 # Apache-2.0/BSD requests-mock>=0.7.0 # Apache-2.0 mock>=1.2 # BSD mox3>=0.7.0 # Apache-2.0 From c67fe187667d1bde9df505d6102f914afc9763ae Mon Sep 17 00:00:00 2001 From: zzxwill Date: Sun, 10 Apr 2016 22:15:36 +0800 Subject: [PATCH 13/25] Correct some typos During my review of the project, I corrected these typos to make it more clear. If we will generate docs based on the code, this will help. Change-Id: Iee3d9210431e0489c4872637fe04409fe0addc0e --- senlinclient/common/sdk.py | 4 ++-- senlinclient/v1/client.py | 2 +- tools/senlinrc | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/senlinclient/common/sdk.py b/senlinclient/common/sdk.py index 4850744..65caead 100644 --- a/senlinclient/common/sdk.py +++ b/senlinclient/common/sdk.py @@ -27,9 +27,9 @@ prop = base.prop class ProfileAction(argparse.Action): - """A custom action to parse user proferences as key=value pairs + """A custom action to parse user preferences as key=value pairs - Stores results in users proferences object. + Stores results in users preferences object. """ prof = profile.Profile() diff --git a/senlinclient/v1/client.py b/senlinclient/v1/client.py index 944eada..4667fa9 100644 --- a/senlinclient/v1/client.py +++ b/senlinclient/v1/client.py @@ -222,7 +222,7 @@ class Client(object): return self.service.cluster_scale_in(cluster, count) def cluster_policies(self, cluster, **queries): - """List all policies attached to clusdter + """List all policies attached to cluster Doc link: http://developer.openstack.org/api-ref-clustering-v1.html diff --git a/tools/senlinrc b/tools/senlinrc index 1fae0e8..59e898e 100644 --- a/tools/senlinrc +++ b/tools/senlinrc @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# IMPORTANT: Cleanse existing environment varible first +# IMPORTANT: Cleanse existing environment variable first unset OS_USERNAME unset OS_PASSWORD unset OS_REGION_NAME From a336d4efb902fbb5eaf0cdb979b2249d3114470b Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Wed, 13 Apr 2016 12:48:40 +0000 Subject: [PATCH 14/25] Updated from global requirements Change-Id: I393273f7f4617ee7d04cbd75b8cc0fbde33e39c9 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 7707428..8160135 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. -Babel>=1.3 # BSD +Babel!=2.3.0,!=2.3.1,!=2.3.2,!=2.3.3,>=1.3 # BSD pbr>=1.6 # Apache-2.0 cliff!=1.16.0,!=1.17.0,>=1.15.0 # Apache-2.0 PrettyTable<0.8,>=0.7 # BSD From c5d8f363b0935600ea6bbb8a05a48bbf58dedaa6 Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Mon, 18 Apr 2016 16:35:05 +0000 Subject: [PATCH 15/25] Updated from global requirements Change-Id: I8db5c5506db00b462e578312e77cdf609270c9dc --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 8160135..ab87166 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,7 @@ Babel!=2.3.0,!=2.3.1,!=2.3.2,!=2.3.3,>=1.3 # BSD pbr>=1.6 # Apache-2.0 cliff!=1.16.0,!=1.17.0,>=1.15.0 # Apache-2.0 PrettyTable<0.8,>=0.7 # BSD -openstacksdk>=0.8.4 # Apache-2.0 +openstacksdk>=0.8.5 # Apache-2.0 oslo.i18n>=2.1.0 # Apache-2.0 oslo.serialization>=1.10.0 # Apache-2.0 oslo.utils>=3.5.0 # Apache-2.0 From 4b7c6eb4c88083d8da2554085aacbbb87242c1bd Mon Sep 17 00:00:00 2001 From: zhurong Date: Sat, 7 May 2016 13:14:04 -0400 Subject: [PATCH 16/25] Trival fix: Update README 1.Delete the README.md 2.Update the git repo Change-Id: I1dacb2fd33edc8d55566c3c82c4d45e3fa67fd19 --- README.md | 4 ---- README.rst | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 348ba23..0000000 --- a/README.md +++ /dev/null @@ -1,4 +0,0 @@ -python-senlinclient -=================== - -Client library for OpenStack Clustering Service API diff --git a/README.rst b/README.rst index d1e59a6..01f5550 100644 --- a/README.rst +++ b/README.rst @@ -7,4 +7,4 @@ provides a Python API (the ``senlinclient`` module) and a command-line tool Development takes place via the usual OpenStack processes as outlined in the `developer guide `_. -The master repository is in `Git `_. +The master repository is in `Git `_. From 31214654ceb17e49e99dffaee17be8a6f12afada Mon Sep 17 00:00:00 2001 From: zhurong Date: Sat, 7 May 2016 13:50:41 -0400 Subject: [PATCH 17/25] Add reno for release notes management An initial patch to add reno and create a base directory for release notes Change-Id: I88aa00c28bf9565879fbce4fc854a4b24133ab5a --- .gitignore | 6 + releasenotes/notes/.placeholder | 0 releasenotes/source/_static/.placeholder | 0 releasenotes/source/_templates/.placeholder | 0 releasenotes/source/conf.py | 277 ++++++++++++++++++++ releasenotes/source/index.rst | 8 + releasenotes/source/unreleased.rst | 5 + test-requirements.txt | 1 + tox.ini | 5 +- 9 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/.placeholder create mode 100644 releasenotes/source/_static/.placeholder create mode 100644 releasenotes/source/_templates/.placeholder create mode 100644 releasenotes/source/conf.py create mode 100644 releasenotes/source/index.rst create mode 100644 releasenotes/source/unreleased.rst diff --git a/.gitignore b/.gitignore index 9652773..1b349fe 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,9 @@ doc/build/ # PyBuilder target/ + +# Files created by releasenotes build +releasenotes/build + +# swap file +*.swp diff --git a/releasenotes/notes/.placeholder b/releasenotes/notes/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/releasenotes/source/_static/.placeholder b/releasenotes/source/_static/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/releasenotes/source/_templates/.placeholder b/releasenotes/source/_templates/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/releasenotes/source/conf.py b/releasenotes/source/conf.py new file mode 100644 index 0000000..5a2629f --- /dev/null +++ b/releasenotes/source/conf.py @@ -0,0 +1,277 @@ +# -*- coding: utf-8 -*- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Senlin Release Notes documentation build configuration file, created by +# sphinx-quickstart on Tue Nov 3 17:40:50 2015. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# sys.path.insert(0, os.path.abspath('.')) + +# -- General configuration ------------------------------------------------ + +# If your documentation needs a minimal Sphinx version, state it here. +# needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + 'oslosphinx', + 'reno.sphinxext', +] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +# source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'Senlin Client Release Notes' +copyright = u'2015, Senlin Developers' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +import pbr.version +senlin_version = pbr.version.VersionInfo('python-muranoclient') +# The full version, including alpha/beta/rc tags. +release = senlin_version.version_string_with_vcs() +# The short X.Y version. +version = senlin_version.canonical_version_string() + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +# today = '' +# Else, today_fmt is used as the format for a strftime call. +# today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = [] + +# The reST default role (used for this markup: `text`) to use for all +# documents. +# default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +# add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +# add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +# show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +# modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +# keep_warnings = False + + +# -- Options for HTML output ---------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'default' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +# html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +# html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +# html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +# html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +# html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +# html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# Add any extra paths that contain custom files (such as robots.txt or +# .htaccess) here, relative to this directory. These files are copied +# directly to the root of the documentation. +# html_extra_path = [] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +# html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +# html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +# html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +# html_additional_pages = {} + +# If false, no module index is generated. +# html_domain_indices = True + +# If false, no index is generated. +# html_use_index = True + +# If true, the index is split into individual pages for each letter. +# html_split_index = False + +# If true, links to the reST sources are added to the pages. +# html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +# html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +# html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +# html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +# html_file_suffix = None + +# Output file base name for HTML help builder. +htmlhelp_basename = 'SenlinClientReleaseNotesdoc' + + +# -- Options for LaTeX output --------------------------------------------- + +latex_elements = { + # The paper size ('letterpaper' or 'a4paper'). + # 'papersize': 'letterpaper', + + # The font size ('10pt', '11pt' or '12pt'). + # 'pointsize': '10pt', + + # Additional stuff for the LaTeX preamble. + # 'preamble': '', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + ('index', 'SenlinClientReleaseNotes.tex', + u'Senlin Client Release Notes Documentation', + u'Senlin Developers', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +# latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +# latex_use_parts = False + +# If true, show page references after internal links. +# latex_show_pagerefs = False + +# If true, show URL addresses after external links. +# latex_show_urls = False + +# Documents to append as an appendix to all manuals. +# latex_appendices = [] + +# If false, no module index is generated. +# latex_domain_indices = True + + +# -- Options for manual page output --------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'senlinclientreleasenotes', + u'Senlin Client Release Notes Documentation', + [u'Senlin Developers'], 1) +] + +# If true, show URL addresses after external links. +# man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ('index', 'SenlinClientReleaseNotes', + u'Senlin Client Release Notes Documentation', + u'Senlin Developers', 'SenlinClientReleaseNotes', + 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +# texinfo_appendices = [] + +# If false, no module index is generated. +# texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +# texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +# texinfo_no_detailmenu = False diff --git a/releasenotes/source/index.rst b/releasenotes/source/index.rst new file mode 100644 index 0000000..72aa37f --- /dev/null +++ b/releasenotes/source/index.rst @@ -0,0 +1,8 @@ +============================= +Senlin Client Release Notes +============================= + +.. toctree:: + :maxdepth: 1 + + unreleased diff --git a/releasenotes/source/unreleased.rst b/releasenotes/source/unreleased.rst new file mode 100644 index 0000000..cd22aab --- /dev/null +++ b/releasenotes/source/unreleased.rst @@ -0,0 +1,5 @@ +============================== + Current Series Release Notes +============================== + +.. release-notes:: diff --git a/test-requirements.txt b/test-requirements.txt index baa40d0..6340ae5 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -16,3 +16,4 @@ sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 # BSD testrepository>=0.0.18 # Apache-2.0/BSD testscenarios>=0.4 # Apache-2.0/BSD testtools>=1.4.0 # MIT +reno>=1.6.2 # Apache2 diff --git a/tox.ini b/tox.ini index 312bc0c..23c94bb 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py34,py27,pypy,pep8 +envlist = py34,py27,pypy,pep8,releasenotes minversion = 1.6 skipsdist = True @@ -31,6 +31,9 @@ commands = python setup.py testr --coverage --testr-args='{posargs}' commands= python setup.py build_sphinx +[testenv:releasenotes] +commands = sphinx-build -a -E -W -d releasenotes/build/doctrees -b html releasenotes/source releasenotes/build/html + [flake8] show-source = True exclude=.venv,.git,.tox,dist,*openstack/common*,*lib/python*,*egg,build From d82d836531b225221cee7d6b389397e49826017c Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Tue, 10 May 2016 00:05:18 +0000 Subject: [PATCH 18/25] Updated from global requirements Change-Id: I91d5996018028c840d353d4b5842030e91114ac4 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index ab87166..8891b51 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. -Babel!=2.3.0,!=2.3.1,!=2.3.2,!=2.3.3,>=1.3 # BSD +Babel>=2.3.4 # BSD pbr>=1.6 # Apache-2.0 cliff!=1.16.0,!=1.17.0,>=1.15.0 # Apache-2.0 PrettyTable<0.8,>=0.7 # BSD From 10cac8c0b5c2a77af52ae3be5602ba485c25d2ed Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Fri, 13 May 2016 17:08:54 +0000 Subject: [PATCH 19/25] Updated from global requirements Change-Id: If8528ef8e81fbd482aa5c39180e1845a8ef1a804 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 8891b51..b21ebc6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,7 @@ openstacksdk>=0.8.5 # Apache-2.0 oslo.i18n>=2.1.0 # Apache-2.0 oslo.serialization>=1.10.0 # Apache-2.0 oslo.utils>=3.5.0 # Apache-2.0 -python-heatclient>=0.6.0 # Apache-2.0 +python-heatclient>=1.1.0 # Apache-2.0 python-openstackclient>=2.1.0 # Apache-2.0 PyYAML>=3.1.0 # MIT requests!=2.9.0,>=2.8.1 # Apache-2.0 From a7a12f45bdd1f918c026386e576b3e15b56ff6fb Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Tue, 17 May 2016 14:10:18 +0000 Subject: [PATCH 20/25] Updated from global requirements Change-Id: I9ec00686eb050187bc2efc60b0f1bd528c971166 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index b21ebc6..cc539cf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,5 +13,5 @@ oslo.utils>=3.5.0 # Apache-2.0 python-heatclient>=1.1.0 # Apache-2.0 python-openstackclient>=2.1.0 # Apache-2.0 PyYAML>=3.1.0 # MIT -requests!=2.9.0,>=2.8.1 # Apache-2.0 +requests>=2.10.0 # Apache-2.0 six>=1.9.0 # MIT From c225f4c03098b53770ed15a043d59dd45f920850 Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Tue, 24 May 2016 03:16:13 +0000 Subject: [PATCH 21/25] Updated from global requirements Change-Id: I1a6840150c8a987cd6c72b28f7c9de6d79900df3 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index cc539cf..fb42253 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,7 @@ Babel>=2.3.4 # BSD pbr>=1.6 # Apache-2.0 cliff!=1.16.0,!=1.17.0,>=1.15.0 # Apache-2.0 PrettyTable<0.8,>=0.7 # BSD -openstacksdk>=0.8.5 # Apache-2.0 +openstacksdk>=0.8.6 # Apache-2.0 oslo.i18n>=2.1.0 # Apache-2.0 oslo.serialization>=1.10.0 # Apache-2.0 oslo.utils>=3.5.0 # Apache-2.0 From 761c5ed61a75158ab4e609d7132757f1725a41c9 Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Thu, 26 May 2016 17:05:39 +0000 Subject: [PATCH 22/25] Updated from global requirements Change-Id: Ie5295fd1f048340ed37578228782384b5c1f7f6a --- test-requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-requirements.txt b/test-requirements.txt index 6340ae5..2f4cf85 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -8,7 +8,7 @@ coverage>=3.6 # Apache-2.0 discover # BSD fixtures<2.0,>=1.3.1 # Apache-2.0/BSD requests-mock>=0.7.0 # Apache-2.0 -mock>=1.2 # BSD +mock>=2.0 # BSD mox3>=0.7.0 # Apache-2.0 oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0 oslotest>=1.10.0 # Apache-2.0 From 6114da4ed2a4bd8af953010215883a0f7f3f7280 Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Wed, 1 Jun 2016 13:54:33 +0000 Subject: [PATCH 23/25] Updated from global requirements Change-Id: I43de10a4f53ae76f0e111f4cbd34b10ad7252176 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index fb42253..e0318fe 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,7 +9,7 @@ PrettyTable<0.8,>=0.7 # BSD openstacksdk>=0.8.6 # Apache-2.0 oslo.i18n>=2.1.0 # Apache-2.0 oslo.serialization>=1.10.0 # Apache-2.0 -oslo.utils>=3.5.0 # Apache-2.0 +oslo.utils>=3.11.0 # Apache-2.0 python-heatclient>=1.1.0 # Apache-2.0 python-openstackclient>=2.1.0 # Apache-2.0 PyYAML>=3.1.0 # MIT From f8fb80693d8ddded0bbb67d0e522f555df417df7 Mon Sep 17 00:00:00 2001 From: OpenStack Proposal Bot Date: Thu, 2 Jun 2016 21:11:59 +0000 Subject: [PATCH 24/25] Updated from global requirements Change-Id: I0e62e66544f2c7c6743f37ce53b374fdca2dc881 --- test-requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-requirements.txt b/test-requirements.txt index 2f4cf85..c2cf2a4 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,7 +6,7 @@ hacking<0.11,>=0.10.0 coverage>=3.6 # Apache-2.0 discover # BSD -fixtures<2.0,>=1.3.1 # Apache-2.0/BSD +fixtures>=3.0.0 # Apache-2.0/BSD requests-mock>=0.7.0 # Apache-2.0 mock>=2.0 # BSD mox3>=0.7.0 # Apache-2.0 From 685ab4d317745811254b7e1eb347cffaafc26f8b Mon Sep 17 00:00:00 2001 From: tengqm Date: Fri, 3 Jun 2016 05:41:46 -0400 Subject: [PATCH 25/25] Added release notes for a new release This patch adds release notes for 0.4.2 release. Change-Id: I110c01119552b9d8c9ca52355d06e085496affd5 --- .../notes/cluster-scaling-command-e0d96f2cd0c7ca5f.yaml | 5 +++++ releasenotes/notes/node-check-recover-469bf81db9f9f1ec.yaml | 3 +++ 2 files changed, 8 insertions(+) create mode 100644 releasenotes/notes/cluster-scaling-command-e0d96f2cd0c7ca5f.yaml create mode 100644 releasenotes/notes/node-check-recover-469bf81db9f9f1ec.yaml diff --git a/releasenotes/notes/cluster-scaling-command-e0d96f2cd0c7ca5f.yaml b/releasenotes/notes/cluster-scaling-command-e0d96f2cd0c7ca5f.yaml new file mode 100644 index 0000000..3856593 --- /dev/null +++ b/releasenotes/notes/cluster-scaling-command-e0d96f2cd0c7ca5f.yaml @@ -0,0 +1,5 @@ +--- +upgrade: + - OSC commands for cluster scaling are changed from 'cluster scale in' + and 'cluster scale out' to 'cluster shrink' and 'cluster expand' + respectively. diff --git a/releasenotes/notes/node-check-recover-469bf81db9f9f1ec.yaml b/releasenotes/notes/node-check-recover-469bf81db9f9f1ec.yaml new file mode 100644 index 0000000..5bc21a0 --- /dev/null +++ b/releasenotes/notes/node-check-recover-469bf81db9f9f1ec.yaml @@ -0,0 +1,3 @@ +--- +features: + - Added command for node-check and node-recover.