diff --git a/doc/source/cli/v2/plans.rst b/doc/source/cli/v2/plans.rst index 3bf7701..a3b6536 100644 --- a/doc/source/cli/v2/plans.rst +++ b/doc/source/cli/v2/plans.rst @@ -136,6 +136,31 @@ Usage example: This will unassign Role from a Plan. This will not delete the Role from Tuskar. Output of this command is the same as for plan-show. +Show Plan’s scale +----------------------- +*tuskar plan-show-scale plan_uuid* + +Usage example: + +:: + + tuskar plan-show-scale c367b394-7179-4c44-85ed-bf84baaf9fee + +Output of this command is a table containing role names with versions and their counts. + +Scaling a Plan +----------------------- +*tuskar plan-scale --count= plan_uuid* + +Usage example: + +:: + + tuskar plan-scale compute-1 --count=2 c367b394-7179-4c44-85ed-bf84baaf9fee + +This will scale given Plan’s role with specified count of nodes. +Output of this command is a short summary of changed values. + Changing a Plan’s Configuration Values -------------------------------------- *tuskar plan-update [-h] [-A ] plan_uuid* diff --git a/tuskarclient/tests/v2/test_plans_shell.py b/tuskarclient/tests/v2/test_plans_shell.py index 332b79b..fff4b4c 100644 --- a/tuskarclient/tests/v2/test_plans_shell.py +++ b/tuskarclient/tests/v2/test_plans_shell.py @@ -29,6 +29,7 @@ def mock_plan(): plan.uuid = '5' plan.name = 'My Plan' plan.to_dict.return_value = {'uuid': 5, 'name': 'My Plan'} + plan.parameters = [{'name': 'compute-1::count', 'value': '2'}] return plan @@ -66,6 +67,18 @@ class PlansShellTest(BasePlansShellTest): mock_print_detail.assert_called_with(mock_find_resource.return_value, outfile=self.outfile) + @mock.patch('tuskarclient.common.utils.find_resource') + @mock.patch('tuskarclient.common.formatting.print_dict') + def test_plan_show_scale(self, mock_print_dict, mock_find_resource): + mock_find_resource.return_value = mock_plan() + args = empty_args() + args.plan = '5' + + self.shell.do_plan_show_scale(self.tuskar, args, outfile=self.outfile) + mock_find_resource.assert_called_with(self.tuskar.plans, '5') + mock_print_dict.assert_called_with({'compute-1': '2'}, + outfile=self.outfile) + @mock.patch('tuskarclient.common.utils.find_resource') def test_plan_delete(self, mock_find_resource): mock_find_resource.return_value = mock_plan() @@ -115,6 +128,29 @@ class PlansShellTest(BasePlansShellTest): mock_print_detail.assert_called_with( self.tuskar.plans.remove_role.return_value, outfile=self.outfile) + @mock.patch('tuskarclient.common.utils.find_resource') + def test_plan_scale(self, mock_find_resource): + mock_find_resource.return_value = mock_plan() + role = mock.Mock() + role.name = 'compute' + role.version = 1 + self.tuskar.roles.list.return_value = [role] + + args = empty_args() + args.plan_uuid = 'plan_uuid' + args.role_name = 'compute-1' + args.count = '9' + + attributes = [{'name': 'compute-1::count', 'value': '9'}] + + self.shell.do_plan_scale(self.tuskar, args, outfile=self.outfile) + self.tuskar.plans.patch.assert_called_once() + self.assertEqual('plan_uuid', self.tuskar.plans.patch.call_args[0][0]) + self.assertEqual( + sorted(attributes, key=lambda k: k['name']), + sorted(self.tuskar.plans.patch.call_args[0][1], + key=lambda k: k['name'])) + @mock.patch('tuskarclient.v2.plans_shell.print_plan_detail') def test_plan_patch(self, mock_print_detail): args = empty_args() diff --git a/tuskarclient/v2/plans_shell.py b/tuskarclient/v2/plans_shell.py index 06dd80d..6fead44 100644 --- a/tuskarclient/v2/plans_shell.py +++ b/tuskarclient/v2/plans_shell.py @@ -39,6 +39,20 @@ def do_plan_show(tuskar, args, outfile=sys.stdout): print_plan_detail(plan, outfile=outfile) +@utils.arg('plan', metavar="", + help="UUID of the Plan to show a scale.") +def do_plan_show_scale(tuskar, args, outfile=sys.stdout): + """Show scale counts of Plan.""" + plan = utils.find_resource(tuskar.plans, args.plan) + scales = dict() + + for param in plan.parameters: + if param['name'].endswith('::count'): + scales[param['name'].replace('::count', '')] = param["value"] + + fmt.print_dict(scales, outfile=outfile) + + def print_plan_detail(plan, outfile=sys.stdout): """Print detailed Plan information (for plan-show etc.).""" @@ -95,6 +109,40 @@ def do_plan_remove_role(tuskar, args, outfile=sys.stdout): print_plan_detail(plan, outfile=outfile) +@utils.arg('role_name', help="Name of role which you want scale.") +@utils.arg('plan_uuid', help="UUID of the Plan to modify.") +@utils.arg('-C', '--count', help="Count of nodes to be set.", required=True) +def do_plan_scale(tuskar, args, outfile=sys.stdout): + """Scale plan by changing count of roles.""" + roles = tuskar.roles.list() + plan = utils.find_resource(tuskar.plans, args.plan_uuid) + attributes = [] + + for role in roles: + versioned_name = "{name}-{v}".format(name=role.name, v=role.version) + if versioned_name == args.role_name: + role_name_key = versioned_name + "::count" + attributes.append({'name': role_name_key, + 'value': args.count}) + old_val = [p['value'] for p in plan.parameters + if p['name'] == role_name_key][0] + + if old_val != args.count: + print("Scaling {role} count: {old_val} -> {new_val}".format( + role=args.role_name, old_val=old_val, new_val=args.count + ), file=outfile) + else: + print("Keeping scale {role} count: {count}".format( + role=args.role_name, count=old_val), file=outfile) + return + + if attributes: + return tuskar.plans.patch(args.plan_uuid, attributes) + else: + print("ERROR: no roles were found in the Plan with the name {0}". + format(args.role_name), file=sys.stderr) + + @utils.arg('plan_uuid', help="UUID of the Plan to modify.") @utils.arg('-A', '--attribute', dest='attributes', metavar='', help='This can be specified multiple times.',