Nested keys handled for del operation in fuel2
Change-Id: Id484e9a2893779be62e651ab0ef7e489ae53cad6 Closes-Bug: #1642330
This commit is contained in:
parent
d434d03c35
commit
5b0bdef5d2
|
@ -64,7 +64,8 @@ class Get(show.ShowOne, ResourcesCommand):
|
|||
'-k', '--key',
|
||||
type=str,
|
||||
help="Name of key to get from the resource. For fetching nested "
|
||||
"key value use '.' as delimiter. Example: k1.k2.k3",
|
||||
"key value use '{0}' as delimiter. Example: "
|
||||
"k1{0}k2{0}k3".format(ResourceValues.KEYS_PATH_DELIMITER),
|
||||
)
|
||||
parser.add_argument(
|
||||
'-s', '--show-lookup',
|
||||
|
@ -107,7 +108,8 @@ class Set(ResourcesCommand):
|
|||
'-k', '--key',
|
||||
type=str,
|
||||
help="Name of key to get from the resource. For set nested "
|
||||
"key value use '.' as delimiter. Example: k1.k2.k3",
|
||||
"key value use '{0}' as delimiter. Example: "
|
||||
"k1{0}k2{0}k3".format(ResourceValues.KEYS_PATH_DELIMITER),
|
||||
)
|
||||
parser.add_argument(
|
||||
'-v', '--value',
|
||||
|
@ -225,7 +227,9 @@ class Delete(ResourcesCommand):
|
|||
parser.add_argument(
|
||||
'-k', '--key',
|
||||
type=str,
|
||||
help="Name of key to delete from the resource",
|
||||
help="Name of key to delete from the resource. For nested "
|
||||
"key deletion use '{0}' as delimiter. Example: "
|
||||
"k1{0}k2{0}k3".format(ResourceValues.KEYS_PATH_DELIMITER),
|
||||
required=True
|
||||
)
|
||||
return parser
|
||||
|
@ -242,7 +246,9 @@ class Delete(ResourcesCommand):
|
|||
def take_action(self, parsed_args):
|
||||
client = self.get_client()
|
||||
resource_url = self.get_resource_url(parsed_args, self.url_last_part)
|
||||
result = client.patch(resource_url, [[parsed_args.key]])
|
||||
keys_path = parsed_args.key.split(
|
||||
ResourceValues.KEYS_PATH_DELIMITER)
|
||||
result = client.patch(resource_url, [keys_path])
|
||||
if result is None:
|
||||
result = self.get_deletion_message(parsed_args)
|
||||
self.app.stdout.write(six.text_type(result))
|
||||
|
|
|
@ -165,8 +165,10 @@ class KeysOperationMixin(object):
|
|||
|
||||
try:
|
||||
for key in keys_path[:-1]:
|
||||
key = self._cast_key(key, cur_point)
|
||||
cur_point = cur_point[key]
|
||||
key = keys_path[-1]
|
||||
key = self._cast_key(key, cur_point)
|
||||
self._check_path_is_reachable(cur_point, key, keys_path)
|
||||
del cur_point[key]
|
||||
except (KeyError, IndexError):
|
||||
|
|
|
@ -190,6 +190,8 @@ class TestDelete(testscenarios.WithScenarios, _BaseCLITest):
|
|||
for s in [
|
||||
('k1', ('-k k1', "ResourceValue for key k1 was deleted\n")),
|
||||
('xx', ('-k xx', "ResourceValue for key xx was deleted\n")),
|
||||
('x.x', ('-k x.x', "ResourceValue for key x.x was deleted\n")),
|
||||
('x.0', ('-k x.0', "ResourceValue for key x.0 was deleted\n"))
|
||||
]
|
||||
]
|
||||
|
||||
|
|
|
@ -162,6 +162,30 @@ class TestResourceKeysOperations(BaseTest):
|
|||
actual = res.json
|
||||
self.assertEqual({'key_1': 'val_1'}, actual)
|
||||
|
||||
def test_put_resource_values_delete_nested_keys(self):
|
||||
self._fixture()
|
||||
environment_id = 9
|
||||
res_def_id = 5
|
||||
levels = (('lvl1', 'val1'), ('lvl2', 'val2'))
|
||||
values = {'k0': [{'k1': 'v01'}, 'b'], 'k2': {'k3': 'v23'}}
|
||||
self._add_resource_values(environment_id, res_def_id, levels, values)
|
||||
|
||||
obj_url = self.object_url.format(
|
||||
environment_id,
|
||||
self.get_levels_path(levels),
|
||||
res_def_id
|
||||
)
|
||||
obj_keys_url = obj_url + '/keys/delete'
|
||||
|
||||
data = [['k0', '0'], ['k2', 'k3']]
|
||||
res = self.client.put(obj_keys_url, data=data)
|
||||
self.assertEqual(204, res.status_code)
|
||||
|
||||
res = self.client.get(obj_url)
|
||||
self.assertEqual(200, res.status_code)
|
||||
actual = res.json
|
||||
self.assertEqual({'k0': ['b'], 'k2': {}}, actual)
|
||||
|
||||
def test_put_resource_values_not_found(self):
|
||||
self.app.config["PROPAGATE_EXCEPTIONS"] = True
|
||||
self._fixture()
|
||||
|
|
Loading…
Reference in New Issue