Update clouds commands to use pluggable formatters

This updates the clouds-* command-line entry points to use the formatter
chosen by the user with --format.

Change-Id: I4d07abc41cb93589889c0599eff426544467a965
This commit is contained in:
Ian Cordasco 2017-02-28 12:03:21 -06:00
parent ab49589345
commit d8e31c39cc
3 changed files with 43 additions and 54 deletions

View File

@ -29,8 +29,7 @@ def do_cloud_create(cc, args):
if k in clouds.CLOUD_FIELDS and not (v is None)}
cloud = cc.clouds.create(**fields)
data = {f: getattr(cloud, f, '') for f in clouds.CLOUD_FIELDS}
cliutils.print_dict(data, wrap=72)
args.formatter.configure(wrap=72).handle(cloud)
@cliutils.arg('--fields',
@ -78,7 +77,7 @@ def do_cloud_list(cc, args):
params['autopaginate'] = args.all
clouds_list = cc.clouds.list(**params)
cliutils.print_list(clouds_list, list(fields))
args.formatter.configure(fields=list(fields)).handle(clouds_list)
@cliutils.arg('id',
@ -88,8 +87,7 @@ def do_cloud_list(cc, args):
def do_cloud_show(cc, args):
"""Show detailed information about a cloud."""
cloud = cc.clouds.get(args.id)
data = {f: getattr(cloud, f, '') for f in clouds.CLOUD_FIELDS}
cliutils.print_dict(data, wrap=72)
args.formatter.configure(wrap=72).handle(cloud)
@cliutils.arg('id',
@ -112,8 +110,7 @@ def do_cloud_update(cc, args):
'--note'
)
cloud = cc.clouds.update(item_id, **fields)
data = {f: getattr(cloud, f, '') for f in clouds.CLOUD_FIELDS}
cliutils.print_dict(data, wrap=72)
args.formatter.configure(wrap=72).handle(cloud)
@cliutils.arg('id',

View File

@ -26,18 +26,26 @@ class TestCloudsShell(base.ShellTestCase):
re_options = re.DOTALL | re.MULTILINE
cloud_valid_fields = None
cloud_invalid_field = None
cloud_invalid_fields = None
def setUp(self):
"""Setup required test fixtures."""
super(TestCloudsShell, self).setUp()
self.cloud_valid_fields = Namespace(project_id=1,
id=1,
name='mock_cloud')
self.cloud_invalid_field = Namespace(project_id=1,
id=1,
name='mock_cloud',
invalid_foo='ignored')
self.cloud_valid_kwargs = {
'project_id': 1,
'id': 1,
'name': 'mock_cloud',
}
self.cloud_invalid_kwargs = {
'project_id': 1,
'id': 1,
'name': 'mock_cloud',
'invalid_foo': 'ignored',
}
self.cloud_valid_fields = Namespace(**self.cloud_valid_kwargs)
self.cloud_valid_fields.formatter = mock.Mock()
self.cloud_invalid_fields = Namespace(**self.cloud_invalid_kwargs)
self.cloud_invalid_fields.formatter = mock.Mock()
def test_cloud_create_missing_required_args(self):
"""Verify that missing required args results in error message."""
@ -59,7 +67,7 @@ class TestCloudsShell(base.ShellTestCase):
session.project_id = 1
client.clouds = clouds.CloudManager(session, 'http://127.0.0.1/')
clouds_shell.do_cloud_create(client, self.cloud_valid_fields)
mock_create.assert_called_once_with(**vars(self.cloud_valid_fields))
mock_create.assert_called_once_with(**self.cloud_valid_kwargs)
@mock.patch('cratonclient.v1.clouds.CloudManager.create')
def test_do_cloud_create_ignores_unknown_fields(self, mock_create):
@ -68,8 +76,8 @@ class TestCloudsShell(base.ShellTestCase):
session = mock.Mock()
session.project_id = 1
client.clouds = clouds.CloudManager(session, 'http://127.0.0.1/')
clouds_shell.do_cloud_create(client, self.cloud_invalid_field)
mock_create.assert_called_once_with(**vars(self.cloud_valid_fields))
clouds_shell.do_cloud_create(client, self.cloud_invalid_fields)
mock_create.assert_called_once_with(**self.cloud_valid_kwargs)
def test_cloud_show_missing_required_args(self):
"""Verify that missing required args results in error message."""
@ -90,7 +98,7 @@ class TestCloudsShell(base.ShellTestCase):
session = mock.Mock()
session.project_id = 1
client.clouds = clouds.CloudManager(session, 'http://127.0.0.1/')
test_args = Namespace(id=1)
test_args = Namespace(id=1, formatter=mock.Mock())
clouds_shell.do_cloud_show(client, test_args)
mock_get.assert_called_once_with(vars(test_args)['id'])
@ -135,7 +143,8 @@ class TestCloudsShell(base.ShellTestCase):
session.project_id = 1
client.clouds = clouds.CloudManager(session, 'http://127.0.0.1/')
valid_input = Namespace(id=1,
name='mock_cloud')
name='mock_cloud',
formatter=mock.Mock())
clouds_shell.do_cloud_update(client, valid_input)
mock_update.assert_called_once_with(1, name='mock_cloud')
@ -148,6 +157,7 @@ class TestCloudsShell(base.ShellTestCase):
client.clouds = clouds.CloudManager(session, 'http://127.0.0.1/')
invalid_input = Namespace(id=1,
name='mock_cloud',
invalid=True)
invalid=True,
formatter=mock.Mock())
clouds_shell.do_cloud_update(client, invalid_input)
mock_update.assert_called_once_with(1, name='mock_cloud')

View File

@ -17,7 +17,6 @@ import mock
from cratonclient import exceptions
from cratonclient.shell.v1 import clouds_shell
from cratonclient.tests.unit.shell import base
from cratonclient.v1 import clouds
class TestDoCloudShow(base.TestShellCommandUsingPrintDict):
@ -30,10 +29,8 @@ class TestDoCloudShow(base.TestShellCommandUsingPrintDict):
clouds_shell.do_cloud_show(self.craton_client, args)
self.craton_client.clouds.get.assert_called_once_with(1234)
self.print_dict.assert_called_once_with(
{field: mock.ANY for field in clouds.CLOUD_FIELDS},
wrap=72,
)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
class TestDoCloudCreate(base.TestShellCommandUsingPrintDict):
@ -54,10 +51,8 @@ class TestDoCloudCreate(base.TestShellCommandUsingPrintDict):
self.craton_client.clouds.create.assert_called_once_with(
name='New cloud',
)
self.print_dict.assert_called_once_with(
{field: mock.ANY for field in clouds.CLOUD_FIELDS},
wrap=72,
)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
def test_accepts_optional_arguments(self):
"""Verify operation with --note passed as well."""
@ -69,10 +64,8 @@ class TestDoCloudCreate(base.TestShellCommandUsingPrintDict):
name='New cloud',
note='This is a note',
)
self.print_dict.assert_called_once_with(
{field: mock.ANY for field in clouds.CLOUD_FIELDS},
wrap=72,
)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
class TestDoCloudUpdate(base.TestShellCommandUsingPrintDict):
@ -106,10 +99,8 @@ class TestDoCloudUpdate(base.TestShellCommandUsingPrintDict):
12345,
name='A New Name',
)
self.print_dict.assert_called_once_with(
{field: mock.ANY for field in clouds.CLOUD_FIELDS},
wrap=72,
)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
def test_note_is_updated(self):
"""Verify the note attribute is updated."""
@ -121,10 +112,8 @@ class TestDoCloudUpdate(base.TestShellCommandUsingPrintDict):
12345,
note='A New Note',
)
self.print_dict.assert_called_once_with(
{field: mock.ANY for field in clouds.CLOUD_FIELDS},
wrap=72,
)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
def test_everything_is_updated(self):
"""Verify the note and name are updated."""
@ -140,10 +129,8 @@ class TestDoCloudUpdate(base.TestShellCommandUsingPrintDict):
note='A New Note',
name='A New Name',
)
self.print_dict.assert_called_once_with(
{field: mock.ANY for field in clouds.CLOUD_FIELDS},
wrap=72,
)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
class TestDoCloudDelete(base.TestShellCommand):
@ -219,9 +206,7 @@ class TestDoCloudList(base.TestShellCommandUsingPrintList):
args = self.args_for()
clouds_shell.do_cloud_list(self.craton_client, args)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
self.assertSortedFieldsEqualTo(['id', 'name'])
def test_negative_limit(self):
"""Ensure we raise an exception for negative limits."""
@ -237,16 +222,13 @@ class TestDoCloudList(base.TestShellCommandUsingPrintList):
marker=None,
autopaginate=False,
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
self.assertSortedFieldsEqualTo(['id', 'name'])
def test_fields(self):
"""Verify that we print out specific fields."""
args = self.args_for(fields=['id', 'name', 'note'])
clouds_shell.do_cloud_list(self.craton_client, args)
self.assertEqual(['id', 'name', 'note'],
sorted(self.print_list.call_args[0][-1]))
self.assertSortedFieldsEqualTo(['id', 'name', 'note'])
def test_invalid_fields(self):
"""Verify that we error out with invalid fields."""