CommandError is raised for invalid server fields
When listing servers with fields of invalid type, a TypeError was raised. With this change and in order to ensure the validity of the fields, we are whitelisting them using the keys of the dictionary Resource.to_dict(). For all fields not in the whitelist a CommandError is raised. Change-Id: I647fa611d29745f830daadac1c3f9c1c71c2733a Closes-Bug: #1733917
This commit is contained in:
parent
0de65d26fd
commit
198c0d7327
@ -1395,6 +1395,12 @@ class ShellTest(utils.TestCase):
|
||||
self.assertIn('securitygroup1', output)
|
||||
self.assertIn('OS-EXT-MOD: Some Thing', output)
|
||||
self.assertIn('mod_some_thing_value', output)
|
||||
# Testing the 'networks' field that is explicitly added to the
|
||||
# existing fields list.
|
||||
output, _err = self.run_command('list --fields networks')
|
||||
self.assertIn('Networks', output)
|
||||
self.assertIn('10.11.12.13', output)
|
||||
self.assertIn('5.6.7.8', output)
|
||||
|
||||
@mock.patch(
|
||||
'novaclient.tests.unit.v2.fakes.FakeSessionClient.get_servers_detail')
|
||||
@ -1411,6 +1417,18 @@ class ShellTest(utils.TestCase):
|
||||
self.run_command,
|
||||
'list --fields host,security_groups,'
|
||||
'OS-EXT-MOD:some_thing,invalid')
|
||||
self.assertRaises(exceptions.CommandError,
|
||||
self.run_command,
|
||||
'list --fields __dict__')
|
||||
self.assertRaises(exceptions.CommandError,
|
||||
self.run_command,
|
||||
'list --fields update')
|
||||
self.assertRaises(exceptions.CommandError,
|
||||
self.run_command,
|
||||
'list --fields __init__')
|
||||
self.assertRaises(exceptions.CommandError,
|
||||
self.run_command,
|
||||
'list --fields __module__,updated')
|
||||
|
||||
def test_list_with_marker(self):
|
||||
self.run_command('list --marker some-uuid')
|
||||
|
@ -1009,6 +1009,7 @@ def _expand_dict_attr(collection, attr):
|
||||
delattr(item, attr)
|
||||
for subkey in field.keys():
|
||||
setattr(item, attr + ':' + subkey, field[subkey])
|
||||
item.set_info(attr + ':' + subkey, field[subkey])
|
||||
|
||||
|
||||
def _translate_keys(collection, convert):
|
||||
@ -1018,6 +1019,7 @@ def _translate_keys(collection, convert):
|
||||
for from_key, to_key in convert:
|
||||
if from_key in keys and to_key not in keys:
|
||||
setattr(item, to_key, item_dict[from_key])
|
||||
item.set_info(to_key, item_dict[from_key])
|
||||
|
||||
|
||||
def _translate_extended_states(collection):
|
||||
@ -1042,6 +1044,8 @@ def _translate_extended_states(collection):
|
||||
getattr(item, 'task_state')
|
||||
except AttributeError:
|
||||
setattr(item, 'task_state', "N/A")
|
||||
item.set_info('power_state', item.power_state)
|
||||
item.set_info('task_state', item.task_state)
|
||||
|
||||
|
||||
def _translate_flavor_keys(collection):
|
||||
@ -1706,12 +1710,19 @@ def _get_list_table_columns_and_formatters(fields, objs, exclude_fields=(),
|
||||
|
||||
columns = []
|
||||
formatters = {}
|
||||
existing_fields = set()
|
||||
|
||||
non_existent_fields = []
|
||||
exclude_fields = set(exclude_fields)
|
||||
|
||||
# NOTE(ttsiouts): Bug #1733917. Validating the fields using the keys of
|
||||
# the Resource.to_dict(). Adding also the 'networks' field.
|
||||
if obj:
|
||||
obj_dict = obj.to_dict()
|
||||
existing_fields = set(['networks']) | set(obj_dict.keys())
|
||||
|
||||
for field in fields.split(','):
|
||||
if not hasattr(obj, field):
|
||||
if field not in existing_fields:
|
||||
non_existent_fields.append(field)
|
||||
continue
|
||||
if field in exclude_fields:
|
||||
|
Loading…
Reference in New Issue
Block a user