diff --git a/cratonclient/shell/v1/cells_shell.py b/cratonclient/shell/v1/cells_shell.py index c312c6e..3a45132 100644 --- a/cratonclient/shell/v1/cells_shell.py +++ b/cratonclient/shell/v1/cells_shell.py @@ -14,6 +14,9 @@ """Cells resource and resource shell wrapper.""" from __future__ import print_function +import argparse +import sys + from cratonclient.common import cliutils from cratonclient import exceptions as exc @@ -221,3 +224,60 @@ def do_cell_delete(cc, args): else: print("Cell {0} was {1} deleted.". format(args.id, 'successfully' if response else 'not')) + + +@cliutils.arg('id', + metavar='', + type=int, + help='ID or name of the cell.') +@cliutils.handle_shell_exception +def do_cell_vars_get(cc, args): + """Get variables for a cell.""" + variables = cc.cells.get(args.id).variables.get() + formatter = args.formatter.configure(dict_property="Variable", wrap=72) + formatter.handle(variables) + + +@cliutils.arg('id', + metavar='', + type=int, + help='ID of the cell.') +@cliutils.arg('variables', nargs=argparse.REMAINDER) +@cliutils.handle_shell_exception +def do_cell_vars_set(cc, args): + """Set variables for a cell.""" + cell_id = args.id + if not args.variables and sys.stdin.isatty(): + raise exc.CommandError( + 'Nothing to update... Please specify variables to set in the ' + 'following format: "key=value". You may also specify variables to ' + 'delete by key using the format: "key="' + ) + adds, deletes = cliutils.variable_updates(args.variables) + variables = cc.cells.get(cell_id).variables + if deletes: + variables.delete(*deletes) + variables.update(**adds) + formatter = args.formatter.configure(wrap=72, dict_property="Variable") + formatter.handle(variables.get()) + + +@cliutils.arg('id', + metavar='', + type=int, + help='ID of the cell.') +@cliutils.arg('variables', nargs=argparse.REMAINDER) +@cliutils.handle_shell_exception +def do_cell_vars_delete(cc, args): + """Delete variables for a cell by key.""" + cell_id = args.id + if not args.variables and sys.stdin.isatty(): + raise exc.CommandError( + 'Nothing to delete... Please specify variables to delete by ' + 'listing the keys you wish to delete separated by spaces.' + ) + deletes = cliutils.variable_deletes(args.variables) + variables = cc.cells.get(cell_id).variables + response = variables.delete(*deletes) + print("Variables {0} deleted.". + format('successfully' if response else 'not')) diff --git a/cratonclient/shell/v1/clouds_shell.py b/cratonclient/shell/v1/clouds_shell.py index 0cc1f3c..86b0a51 100644 --- a/cratonclient/shell/v1/clouds_shell.py +++ b/cratonclient/shell/v1/clouds_shell.py @@ -12,6 +12,9 @@ """Hosts resource and resource shell wrapper.""" from __future__ import print_function +import argparse +import sys + from cratonclient.common import cliutils from cratonclient import exceptions as exc @@ -153,3 +156,60 @@ def do_cloud_delete(cc, args): else: print("Cloud {0} was {1} deleted.". format(args.id, 'successfully' if response else 'not')) + + +@cliutils.arg('id', + metavar='', + type=int, + help='ID or name of the cloud.') +@cliutils.handle_shell_exception +def do_cloud_vars_get(cc, args): + """Get variables for a cloud.""" + variables = cc.clouds.get(args.id).variables.get() + formatter = args.formatter.configure(dict_property="Variable", wrap=72) + formatter.handle(variables) + + +@cliutils.arg('id', + metavar='', + type=int, + help='ID of the cloud.') +@cliutils.arg('variables', nargs=argparse.REMAINDER) +@cliutils.handle_shell_exception +def do_cloud_vars_set(cc, args): + """Set variables for a cloud.""" + cloud_id = args.id + if not args.variables and sys.stdin.isatty(): + raise exc.CommandError( + 'Nothing to update... Please specify variables to set in the ' + 'following format: "key=value". You may also specify variables to ' + 'delete by key using the format: "key="' + ) + adds, deletes = cliutils.variable_updates(args.variables) + variables = cc.clouds.get(cloud_id).variables + if deletes: + variables.delete(*deletes) + variables.update(**adds) + formatter = args.formatter.configure(wrap=72, dict_property="Variable") + formatter.handle(variables.get()) + + +@cliutils.arg('id', + metavar='', + type=int, + help='ID of the cloud.') +@cliutils.arg('variables', nargs=argparse.REMAINDER) +@cliutils.handle_shell_exception +def do_cloud_vars_delete(cc, args): + """Delete variables for a cloud by key.""" + cloud_id = args.id + if not args.variables and sys.stdin.isatty(): + raise exc.CommandError( + 'Nothing to delete... Please specify variables to delete by ' + 'listing the keys you wish to delete separated by spaces.' + ) + deletes = cliutils.variable_deletes(args.variables) + variables = cc.clouds.get(cloud_id).variables + response = variables.delete(*deletes) + print("Variables {0} deleted.". + format('successfully' if response else 'not')) diff --git a/cratonclient/shell/v1/projects_shell.py b/cratonclient/shell/v1/projects_shell.py index 6e0e1be..4d2c728 100644 --- a/cratonclient/shell/v1/projects_shell.py +++ b/cratonclient/shell/v1/projects_shell.py @@ -14,6 +14,9 @@ """Projects resource and resource shell wrapper.""" from __future__ import print_function +import argparse +import sys + from cratonclient.common import cliutils from cratonclient import exceptions as exc @@ -131,3 +134,57 @@ def do_project_delete(cc, args): else: print("Project {0} was {1} deleted.". format(args.id, 'successfully' if response else 'not')) + + +@cliutils.arg('id', + metavar='', + help='ID or name of the project.') +@cliutils.handle_shell_exception +def do_project_vars_get(cc, args): + """Get variables for a project.""" + variables = cc.projects.get(args.id).variables.get() + formatter = args.formatter.configure(dict_property="Variable", wrap=72) + formatter.handle(variables) + + +@cliutils.arg('id', + metavar='', + help='ID of the project.') +@cliutils.arg('variables', nargs=argparse.REMAINDER) +@cliutils.handle_shell_exception +def do_project_vars_set(cc, args): + """Set variables for a project.""" + project_id = args.id + if not args.variables and sys.stdin.isatty(): + raise exc.CommandError( + 'Nothing to update... Please specify variables to set in the ' + 'following format: "key=value". You may also specify variables to ' + 'delete by key using the format: "key="' + ) + adds, deletes = cliutils.variable_updates(args.variables) + variables = cc.projects.get(project_id).variables + if deletes: + variables.delete(*deletes) + variables.update(**adds) + formatter = args.formatter.configure(wrap=72, dict_property="Variable") + formatter.handle(variables.get()) + + +@cliutils.arg('id', + metavar='', + help='ID of the project.') +@cliutils.arg('variables', nargs=argparse.REMAINDER) +@cliutils.handle_shell_exception +def do_project_vars_delete(cc, args): + """Delete variables for a project by key.""" + project_id = args.id + if not args.variables and sys.stdin.isatty(): + raise exc.CommandError( + 'Nothing to delete... Please specify variables to delete by ' + 'listing the keys you wish to delete separated by spaces.' + ) + deletes = cliutils.variable_deletes(args.variables) + variables = cc.projects.get(project_id).variables + response = variables.delete(*deletes) + print("Variables {0} deleted.". + format('successfully' if response else 'not')) diff --git a/cratonclient/shell/v1/regions_shell.py b/cratonclient/shell/v1/regions_shell.py index 8ee172f..7ba5de9 100644 --- a/cratonclient/shell/v1/regions_shell.py +++ b/cratonclient/shell/v1/regions_shell.py @@ -12,6 +12,9 @@ """Hosts resource and resource shell wrapper.""" from __future__ import print_function +import argparse +import sys + from cratonclient.common import cliutils from cratonclient import exceptions as exc @@ -181,3 +184,60 @@ def do_region_delete(cc, args): else: print("Region {0} was {1} deleted.". format(args.id, 'successfully' if response else 'not')) + + +@cliutils.arg('id', + metavar='', + type=int, + help='ID or name of the region.') +@cliutils.handle_shell_exception +def do_region_vars_get(cc, args): + """Get variables for a region.""" + variables = cc.regions.get(args.id).variables.get() + formatter = args.formatter.configure(dict_property="Variable", wrap=72) + formatter.handle(variables) + + +@cliutils.arg('id', + metavar='', + type=int, + help='ID of the region.') +@cliutils.arg('variables', nargs=argparse.REMAINDER) +@cliutils.handle_shell_exception +def do_region_vars_set(cc, args): + """Set variables for a region.""" + region_id = args.id + if not args.variables and sys.stdin.isatty(): + raise exc.CommandError( + 'Nothing to update... Please specify variables to set in the ' + 'following format: "key=value". You may also specify variables to ' + 'delete by key using the format: "key="' + ) + adds, deletes = cliutils.variable_updates(args.variables) + variables = cc.regions.get(region_id).variables + if deletes: + variables.delete(*deletes) + variables.update(**adds) + formatter = args.formatter.configure(wrap=72, dict_property="Variable") + formatter.handle(variables.get()) + + +@cliutils.arg('id', + metavar='', + type=int, + help='ID of the region.') +@cliutils.arg('variables', nargs=argparse.REMAINDER) +@cliutils.handle_shell_exception +def do_region_vars_delete(cc, args): + """Delete variables for a region by key.""" + region_id = args.id + if not args.variables and sys.stdin.isatty(): + raise exc.CommandError( + 'Nothing to delete... Please specify variables to delete by ' + 'listing the keys you wish to delete separated by spaces.' + ) + deletes = cliutils.variable_deletes(args.variables) + variables = cc.regions.get(region_id).variables + response = variables.delete(*deletes) + print("Variables {0} deleted.". + format('successfully' if response else 'not')) diff --git a/cratonclient/tests/unit/v1/test_clouds.py b/cratonclient/tests/unit/v1/test_clouds.py index 3a6b1e1..65602c4 100644 --- a/cratonclient/tests/unit/v1/test_clouds.py +++ b/cratonclient/tests/unit/v1/test_clouds.py @@ -24,6 +24,7 @@ class TestCloud(base.TestCase): def test_is_a_resource_instance(self): """Verify that a Cloud instance is an instance of a Resource.""" manager = mock.Mock() + manager.extra_request_kwargs = {} self.assertIsInstance(clouds.Cloud(manager, {"id": 1234}), crud.Resource) diff --git a/cratonclient/tests/unit/v1/test_regions.py b/cratonclient/tests/unit/v1/test_regions.py index 184b823..b5f08c3 100644 --- a/cratonclient/tests/unit/v1/test_regions.py +++ b/cratonclient/tests/unit/v1/test_regions.py @@ -24,6 +24,7 @@ class TestRegion(base.TestCase): def test_is_a_resource_instance(self): """Verify that a Region instance is an instance of a Resource.""" manager = mock.Mock() + manager.extra_request_kwargs = {} self.assertIsInstance(regions.Region(manager, {"id": 1234}), crud.Resource) diff --git a/cratonclient/v1/cells.py b/cratonclient/v1/cells.py index b161bab..9e3360f 100644 --- a/cratonclient/v1/cells.py +++ b/cratonclient/v1/cells.py @@ -13,12 +13,15 @@ # under the License. """Regions manager code.""" from cratonclient import crud +from cratonclient.v1 import variables class Cell(crud.Resource): """Representation of a Region.""" - pass + subresource_managers = { + 'variables': variables.VariableManager, + } class CellManager(crud.CRUDClient): diff --git a/cratonclient/v1/clouds.py b/cratonclient/v1/clouds.py index f823bc9..fc9fdfc 100644 --- a/cratonclient/v1/clouds.py +++ b/cratonclient/v1/clouds.py @@ -13,12 +13,15 @@ # under the License. """Clouds manager code.""" from cratonclient import crud +from cratonclient.v1 import variables class Cloud(crud.Resource): """Representation of a Cloud.""" - pass + subresource_managers = { + 'variables': variables.VariableManager, + } class CloudManager(crud.CRUDClient): diff --git a/cratonclient/v1/projects.py b/cratonclient/v1/projects.py index ab5fd26..423848b 100644 --- a/cratonclient/v1/projects.py +++ b/cratonclient/v1/projects.py @@ -13,12 +13,15 @@ # under the License. """Regions manager code.""" from cratonclient import crud +from cratonclient.v1 import variables class Project(crud.Resource): """Representation of a Project.""" - pass + subresource_managers = { + 'variables': variables.VariableManager, + } class ProjectManager(crud.CRUDClient): diff --git a/cratonclient/v1/regions.py b/cratonclient/v1/regions.py index cbcc4ae..0a8184a 100644 --- a/cratonclient/v1/regions.py +++ b/cratonclient/v1/regions.py @@ -13,12 +13,15 @@ # under the License. """Regions manager code.""" from cratonclient import crud +from cratonclient.v1 import variables class Region(crud.Resource): """Representation of a Region.""" - pass + subresource_managers = { + 'variables': variables.VariableManager, + } class RegionManager(crud.CRUDClient):