diff --git a/congressclient/common/utils.py b/congressclient/common/utils.py index d5b9d23..9a381e8 100644 --- a/congressclient/common/utils.py +++ b/congressclient/common/utils.py @@ -71,6 +71,15 @@ def format_long_dict_list(data): return ',\n'.join(newdata) + '\n' +def format_dict(data): + """Return a formatted string. + + :param data: a dict + :rtype: a string formatted to {a:b, c:d} + """ + return str({str(key): str(value) for key, value in data.items()}) + + def format_list(data): """Return a formatted strings diff --git a/congressclient/osc/v1/datasource.py b/congressclient/osc/v1/datasource.py index 1f3d6ab..c79f933 100644 --- a/congressclient/osc/v1/datasource.py +++ b/congressclient/osc/v1/datasource.py @@ -38,7 +38,7 @@ class ListDatasources(lister.Lister): client = self.app.client_manager.congressclient data = client.list_datasources()['results'] columns = ['id', 'name', 'enabled', 'type', 'config'] - formatters = {'Datasources': utils.format_list} + formatters = {'config': utils.format_dict} return (columns, (utils.get_dict_properties(s, columns, formatters=formatters) diff --git a/congressclient/tests/v1/test_datasource.py b/congressclient/tests/v1/test_datasource.py index b0790ea..0908a9f 100644 --- a/congressclient/tests/v1/test_datasource.py +++ b/congressclient/tests/v1/test_datasource.py @@ -43,6 +43,37 @@ class TestListDatasources(common.TestCongressBase): self.assertEqual(['id', 'name', 'enabled', 'type', 'config'], result[0]) + def test_list_datasource_output_not_unicode(self): + # response json string is converted to dict by oslo jsonutils.loads(), + # so the key and value in the dict should be unicode type. + response = { + u"results": [{u"id": u"neutron", + u"name": u"my_name", + u"enabled": True, + u"type": None, + u"config": { + u'username': u'admin', + u'tenant_name': u'admin', + u'poll_time': u'10', + u'password': u'', + u'auth_url': u'http://127.0.0.1:5000/v2.0' + }}] + } + lister = mock.Mock(return_value=response) + self.app.client_manager.congressclient.list_datasources = lister + cmd = datasource.ListDatasources(self.app, self.namespace) + + parsed_args = self.check_parser(cmd, [], []) + result = cmd.take_action(parsed_args) + + lister.assert_called_with() + self.assertEqual(['id', 'name', 'enabled', 'type', 'config'], + result[0]) + # get 'config' column + config = list(result[1])[0][-1] + self.assertIn("'username': 'admin'", config) + self.assertNotIn("u'username': u'admin'", config) + class TestListDatasourceTables(common.TestCongressBase): def test_list_datasource_tables(self):