Add filter params to host-list command

$craton host-list needs to allow some of the filtering
parameters to users to be able to filer host listing.

This patch adds --ip, --device-type, --vars and --label
parameters as allowed filters to host-list.

Closes Bug: 1659235

Change-Id: I663079ca454a3451c2688c518884243da1488b85
This commit is contained in:
Sulochan Acharya 2017-03-01 11:41:35 +00:00
parent 4ae51367ff
commit 94456b6617
3 changed files with 148 additions and 0 deletions

View File

@ -76,6 +76,22 @@ def do_host_show(cc, args):
metavar='<marker>',
default=None,
help='ID of the cell to use to resume listing hosts.')
@cliutils.arg('--device-type',
metavar='<device_type>',
default=None,
help='Device type to use as filter.')
@cliutils.arg('--vars',
metavar='<vars>',
default=None,
help='Variables to use as filter in the form of key:value.')
@cliutils.arg('--label',
metavar='<label>',
default=None,
help='Label to use as filter.')
@cliutils.arg('--ip',
metavar='<ip_address>',
default=None,
help='IP address to use as filter.')
def do_host_list(cc, args):
"""Print list of hosts which are registered with the Craton service."""
params = {}
@ -84,6 +100,14 @@ def do_host_list(cc, args):
params['cell_id'] = args.cell
if args.cloud is not None:
params['cloud_id'] = args.cloud
if args.device_type is not None:
params['device_type'] = args.device_type
if args.vars is not None:
params['vars'] = args.vars
if args.label is not None:
params['label'] = args.label
if args.ip is not None:
params['ip_address'] = args.ip
if args.limit is not None:
if args.limit < 0:
raise exc.CommandError('Invalid limit specified. Expected '

View File

@ -116,6 +116,58 @@ class TestHostsShell(base.ShellTestCase):
)
mock_list.reset_mock()
@mock.patch('cratonclient.v1.hosts.HostManager.list')
def test_host_list_vars_success(self, mock_list):
"""Verify --vars arguments successfully pass cell to Client."""
self.shell('host-list -r 1 --vars a:b')
mock_list.assert_called_once_with(
vars='a:b',
sort_dir='asc',
region_id=1,
marker=None,
autopaginate=False,
)
mock_list.reset_mock()
@mock.patch('cratonclient.v1.hosts.HostManager.list')
def test_host_list_ip_success(self, mock_list):
"""Verify --ip arguments successfully pass cell to Client."""
self.shell('host-list -r 1 --ip 10.10.1.1')
mock_list.assert_called_once_with(
ip_address='10.10.1.1',
sort_dir='asc',
region_id=1,
marker=None,
autopaginate=False,
)
mock_list.reset_mock()
@mock.patch('cratonclient.v1.hosts.HostManager.list')
def test_host_list_label_success(self, mock_list):
"""Verify --label arguments successfully pass cell to Client."""
self.shell('host-list -r 1 --label compute')
mock_list.assert_called_once_with(
label='compute',
sort_dir='asc',
region_id=1,
marker=None,
autopaginate=False,
)
mock_list.reset_mock()
@mock.patch('cratonclient.v1.hosts.HostManager.list')
def test_host_list_device_type_success(self, mock_list):
"""Verify --device-type arguments successfully pass cell to Client."""
self.shell('host-list -r 1 --device-type compute')
mock_list.assert_called_once_with(
device_type='compute',
sort_dir='asc',
region_id=1,
marker=None,
autopaginate=False,
)
mock_list.reset_mock()
@mock.patch('cratonclient.v1.hosts.HostManager.list')
def test_host_list_detail_success(self, mock_list):
"""Verify --detail argument successfully pass detail to Client."""

View File

@ -49,6 +49,10 @@ class TestDoHostList(base.TestShellCommandUsingPrintList):
kwargs.setdefault('fields', [])
kwargs.setdefault('marker', None)
kwargs.setdefault('all', False)
kwargs.setdefault('vars', None)
kwargs.setdefault('label', None)
kwargs.setdefault('device_type', None)
kwargs.setdefault('ip', None)
return super(TestDoHostList, self).args_for(**kwargs)
def test_only_required_parameters(self):
@ -155,6 +159,74 @@ class TestDoHostList(base.TestShellCommandUsingPrintList):
self.assertRaisesCommandErrorWith(hosts_shell.do_host_list, args)
self.assertNothingWasCalled()
def test_with_vars(self):
"""Verify the behaviour with --vars specified."""
args = self.args_for(vars='a:b')
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.hosts.list.assert_called_once_with(
vars='a:b',
sort_dir='asc',
region_id=246,
autopaginate=False,
marker=None,
)
self.assertSortedPrintListFieldsEqualTo([
'active', 'cell_id', 'device_type', 'id', 'name'
])
def test_with_label(self):
"""Verify the behaviour with --label specified."""
args = self.args_for(label='compute')
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.hosts.list.assert_called_once_with(
label='compute',
sort_dir='asc',
region_id=246,
autopaginate=False,
marker=None,
)
self.assertSortedPrintListFieldsEqualTo([
'active', 'cell_id', 'device_type', 'id', 'name'
])
def test_with_device_type(self):
"""Verify the behaviour with --device-type specified."""
args = self.args_for(device_type='compute')
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.hosts.list.assert_called_once_with(
device_type='compute',
sort_dir='asc',
region_id=246,
autopaginate=False,
marker=None,
)
self.assertSortedPrintListFieldsEqualTo([
'active', 'cell_id', 'device_type', 'id', 'name'
])
def test_with_ip(self):
"""Verify the behaviour with --ip specified."""
args = self.args_for(ip='10.10.1.1')
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.hosts.list.assert_called_once_with(
ip_address='10.10.1.1',
sort_dir='asc',
region_id=246,
autopaginate=False,
marker=None,
)
self.assertSortedPrintListFieldsEqualTo([
'active', 'cell_id', 'device_type', 'id', 'name'
])
def test_fields(self):
"""Verify that we can specify custom fields."""
args = self.args_for(fields=['id', 'name', 'cell_id'])