Skip to content

Commit 8cefa51

Browse files
authored
Merge pull request #258 from linode/bugfix/fix-lke-cluster-create
bug: Fix parsing of nested objects in lists
2 parents 857623d + 20677f3 commit 8cefa51

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

linodecli/operation.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,14 @@ def parse_args(self, args):
189189
type=TYPES[arg.arg_type])
190190

191191
parsed = parser.parse_args(args)
192-
193192
lists = {}
194193
# group list items as expected
195194
for arg_name, list_name in list_items:
196-
item_name = arg_name.split('.')[-1]
195+
item_name = arg_name.split(list_name)[1][1:]
197196
if hasattr(parsed, arg_name):
198197
val = getattr(parsed, arg_name) or []
198+
if not val:
199+
continue
199200
if list_name not in lists:
200201
new_list = [{item_name: c} for c in val]
201202
lists[list_name] = new_list
@@ -204,6 +205,33 @@ def parse_args(self, args):
204205
for obj, item in zip(update_list, val):
205206
obj[item_name] = item
206207

208+
# break out list items with periods in their name into objects. This
209+
# allows supporting nested lists
210+
for _, cur_list in lists.items():
211+
# for each list in lists
212+
for item in cur_list:
213+
# for each item in the list (these are dicts)
214+
new_dicts = {}
215+
remove_keys = []
216+
for k, v in item.items():
217+
# if there's a period in the key, split it into a dict and
218+
# possibly merge it with a dict that came from a prior split
219+
#
220+
# XXX: This only supports one layer of nested dicts in lists
221+
if "." in k:
222+
dict_key, key = k.split('.', 1)
223+
if dict_key in new_dicts:
224+
new_dicts[dict_key][key] = v
225+
else:
226+
new_dicts[dict_key] = {key: v}
227+
remove_keys.append(k)
228+
229+
# remove the original keys
230+
for key in remove_keys:
231+
del item[key]
232+
# and add the combined keys
233+
item.update(new_dicts)
234+
207235
# don't send along empty lists
208236
to_delete = []
209237
for k, v in lists.items():
@@ -220,6 +248,7 @@ def parse_args(self, args):
220248
del parsed[name]
221249
parsed = argparse.Namespace(**parsed)
222250

251+
223252
return parsed
224253

225254
def process_response_json(self, json, handler):

0 commit comments

Comments
 (0)