diff --git a/cratonclient/shell/v1/devices_shell.py b/cratonclient/shell/v1/devices_shell.py index bf98503..66ee15f 100644 --- a/cratonclient/shell/v1/devices_shell.py +++ b/cratonclient/shell/v1/devices_shell.py @@ -14,15 +14,38 @@ from __future__ import print_function from cratonclient.common import cliutils from cratonclient import exceptions as exc -from cratonclient.v1 import devices + +DEFAULT_DEVICE_FIELDS = [ + 'id', + 'name', + 'device_type', + 'ip_address', + 'cloud_id', + 'region_id', + 'cell_id', +] + + +DEVICE_FIELDS = DEFAULT_DEVICE_FIELDS + [ + 'parent_id', + 'note', + 'created_at', + 'updated_at', + 'project_id', +] @cliutils.arg('--fields', nargs='+', metavar='', - default=[], + default=DEFAULT_DEVICE_FIELDS, help='Space-separated list of fields to display. ' - 'Only these fields will be fetched from the server.') + 'Only these fields will be fetched from the server. ' + 'This cannot be combined with --detail.') +@cliutils.arg('--detail', + action='store_true', + default=False, + help='Retrieve and show all detail about devices in listing.') @cliutils.arg('--all', action='store_true', default=False, @@ -73,10 +96,6 @@ from cratonclient.v1 import devices def do_device_list(cc, args): """List all devices.""" params = {} - default_fields = [ - 'cloud_id', 'region_id', 'cell_id', 'parent_id', 'id', 'name', - 'device_type', 'active', - ] if args.limit is not None: if args.limit < 0: raise exc.CommandError('Invalid limit specified. Expected ' @@ -86,20 +105,29 @@ def do_device_list(cc, args): if args.all is True: params['limit'] = 100 - if args.fields: - try: - fields = {x: devices.DEVICE_FIELDS[x] for x in args.fields} - except KeyError as err: - raise exc.CommandError('Invalid field "{}"'.format(err.args[0])) - else: - fields = default_fields + if args.detail: + if args.fields and args.fields == DEFAULT_DEVICE_FIELDS: + args.fields = DEVICE_FIELDS + else: + raise exc.CommandError( + 'Cannot specify both --fields and --detail.' + ) + params['detail'] = args.detail + + fields = args.fields + for field in fields: + if field not in DEVICE_FIELDS: + raise exc.CommandError( + 'Invalid field "{}"'.format(field) + ) + sort_key = args.sort_key and args.sort_key.lower() if sort_key is not None: - if sort_key not in devices.DEVICE_FIELDS: + if sort_key not in DEVICE_FIELDS: raise exc.CommandError( '{0} is an invalid key for sorting, valid values for ' '--sort-key are: {1}'.format( - args.sort_key, devices.DEVICE_FIELDS.keys() + args.sort_key, DEVICE_FIELDS ) ) params['sort_keys'] = sort_key @@ -119,4 +147,4 @@ def do_device_list(cc, args): params['active'] = args.active devices_list = cc.devices.list(**params) - args.formatter.configure(fields=list(fields)).handle(devices_list) + args.formatter.configure(fields=fields).handle(devices_list) diff --git a/cratonclient/tests/unit/shell/v1/test_devices_shell.py b/cratonclient/tests/unit/shell/v1/test_devices_shell.py index 6e82687..a7fab40 100644 --- a/cratonclient/tests/unit/shell/v1/test_devices_shell.py +++ b/cratonclient/tests/unit/shell/v1/test_devices_shell.py @@ -21,6 +21,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): def args_for(self, **kwargs): """Generate a Namespace for do_device_list.""" + kwargs.setdefault('detail', False) kwargs.setdefault('cloud', None) kwargs.setdefault('region', None) kwargs.setdefault('cell', None) @@ -30,7 +31,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): kwargs.setdefault('limit', None) kwargs.setdefault('sort_key', None) kwargs.setdefault('sort_dir', 'asc') - kwargs.setdefault('fields', []) + kwargs.setdefault('fields', devices_shell.DEFAULT_DEVICE_FIELDS) kwargs.setdefault('marker', None) kwargs.setdefault('all', False) return super(TestDoDeviceList, self).args_for(**kwargs) @@ -47,10 +48,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): autopaginate=False, marker=None, ) - self.assertSortedPrintListFieldsEqualTo([ - 'active', 'cell_id', 'cloud_id', 'device_type', 'id', 'name', - 'parent_id', 'region_id', - ]) + self.assertFieldsEqualTo(devices_shell.DEFAULT_DEVICE_FIELDS) def test_with_parent_id(self): """Verify that we include the parent_id in the params.""" @@ -65,10 +63,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): autopaginate=False, marker=None, ) - self.assertSortedPrintListFieldsEqualTo([ - 'active', 'cell_id', 'cloud_id', 'device_type', 'id', 'name', - 'parent_id', 'region_id', - ]) + self.assertFieldsEqualTo(devices_shell.DEFAULT_DEVICE_FIELDS) def test_with_parent_id_and_descendants(self): """Verify that the parent_id and descendants is in the params.""" @@ -83,10 +78,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): autopaginate=False, marker=None, ) - self.assertSortedPrintListFieldsEqualTo([ - 'active', 'cell_id', 'cloud_id', 'device_type', 'id', 'name', - 'parent_id', 'region_id', - ]) + self.assertFieldsEqualTo(devices_shell.DEFAULT_DEVICE_FIELDS) def test_with_region_id(self): """Verify that we include the region_id in the params.""" @@ -101,10 +93,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): autopaginate=False, marker=None, ) - self.assertSortedPrintListFieldsEqualTo([ - 'active', 'cell_id', 'cloud_id', 'device_type', 'id', 'name', - 'parent_id', 'region_id', - ]) + self.assertFieldsEqualTo(devices_shell.DEFAULT_DEVICE_FIELDS) def test_with_cell_id(self): """Verify that we include the cell_id in the params.""" @@ -119,10 +108,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): autopaginate=False, marker=None, ) - self.assertSortedPrintListFieldsEqualTo([ - 'active', 'cell_id', 'cloud_id', 'device_type', 'id', 'name', - 'parent_id', 'region_id', - ]) + self.assertFieldsEqualTo(devices_shell.DEFAULT_DEVICE_FIELDS) def test_with_cloud_id(self): """Verify that we include the cell_id in the params.""" @@ -137,10 +123,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): autopaginate=False, marker=None, ) - self.assertSortedPrintListFieldsEqualTo([ - 'active', 'cell_id', 'cloud_id', 'device_type', 'id', 'name', - 'parent_id', 'region_id', - ]) + self.assertFieldsEqualTo(devices_shell.DEFAULT_DEVICE_FIELDS) def test_with_limit(self): """Verify the behaviour with --limit specified.""" @@ -155,10 +138,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): autopaginate=False, marker=None, ) - self.assertSortedPrintListFieldsEqualTo([ - 'active', 'cell_id', 'cloud_id', 'device_type', 'id', 'name', - 'parent_id', 'region_id', - ]) + self.assertFieldsEqualTo(devices_shell.DEFAULT_DEVICE_FIELDS) def test_negative_limit_raises_command_error(self): """Verify that we forbid negative limit values.""" @@ -179,9 +159,7 @@ class TestDoDeviceList(base.TestShellCommandUsingPrintList): autopaginate=False, marker=None, ) - self.assertSortedPrintListFieldsEqualTo([ - 'cell_id', 'id', 'name', - ]) + self.assertFieldsEqualTo(['id', 'name', 'cell_id']) def test_invalid_sort_key(self): """Verify that we disallow invalid sort keys.""" diff --git a/cratonclient/v1/devices.py b/cratonclient/v1/devices.py index 4e11e65..8704877 100644 --- a/cratonclient/v1/devices.py +++ b/cratonclient/v1/devices.py @@ -31,18 +31,3 @@ class DeviceManager(crud.CRUDClient): def list(self, **kwargs): """Generate the items from this endpoint.""" return super(DeviceManager, self).list(nested=True, **kwargs) - -DEVICE_FIELDS = { - 'id': 'ID', - 'project_id': 'Project ID', - 'cloud_id': 'Cloud ID', - 'region_id': 'Region ID', - 'cell_id': 'Cell ID', - 'parent_id': 'Parent ID', - 'name': 'Name', - 'ip_address': 'IP Address', - 'device_type': 'Device Type', - 'note': 'Note', - 'created_at': 'Created At', - 'updated_at': 'Updated At' -}