Skip to content

Commit 1fd0563

Browse files
committed
CLOUDSTACK-1037: Implement fuzzy parameter completion
Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
1 parent bf1b40e commit 1fd0563

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

tools/cli/cloudmonkey/cachemaker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def savecache(apicache, json_file):
5656
"""
5757
Saves apicache dictionary as json_file, returns dictionary as indented str
5858
"""
59-
if isinstance(type(apicache), types.NoneType) or apicache is None or apicache is {}:
60-
return ""
59+
if apicache is None or apicache is {}:
60+
return ""
6161
apicachestr = json.dumps(apicache, indent=2)
6262
with open(json_file, 'w') as cache_file:
6363
cache_file.write(apicachestr)

tools/cli/cloudmonkey/cloudmonkey.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,13 @@ def default(self, args):
246246
map(lambda x: x.strip(),
247247
args_dict.pop('filter').split(',')))
248248

249-
missing_args = []
249+
missing = []
250250
if verb in self.apicache and subject in self.apicache[verb]:
251-
missing_args = filter(lambda x: x not in args_dict.keys(),
252-
self.apicache[verb][subject]['requiredparams'])
251+
missing = filter(lambda x: x not in args_dict.keys(),
252+
self.apicache[verb][subject]['requiredparams'])
253253

254-
if len(missing_args) > 0:
255-
self.monkeyprint("Missing arguments: ", ' '.join(missing_args))
254+
if len(missing) > 0:
255+
self.monkeyprint("Missing arguments: ", ' '.join(missing))
256256
return
257257

258258
isasync = False
@@ -293,12 +293,33 @@ def completedefault(self, text, line, begidx, endidx):
293293
map(lambda x: x['name'],
294294
self.apicache[verb][subject]['params']))
295295
search_string = text
296+
if self.paramcompletion == 'true':
297+
param = line.split(" ")[-1]
298+
idx = param.find("=")
299+
value = param[idx + 1:]
300+
param = param[:idx]
301+
if len(value) < 36 and idx != -1:
302+
params = self.apicache[verb][subject]['params']
303+
related = filter(lambda x: x['name'] == param,
304+
params)[0]['related']
305+
api = min(filter(lambda x: 'list' in x, related), key=len)
306+
response = self.make_request(api, args={'listall': 'true'})
307+
responsekey = filter(lambda x: 'response' in x,
308+
response.keys())[0]
309+
result = response[responsekey]
310+
uuids = []
311+
for key in result.keys():
312+
if isinstance(result[key], list):
313+
for element in result[key]:
314+
if 'id' in element.keys():
315+
uuids.append(element['id'])
316+
autocompletions = uuids
317+
search_string = value
296318

297319
if self.tabularize == "true" and subject != "":
298320
autocompletions.append("filter=")
299321
return [s for s in autocompletions if s.startswith(search_string)]
300322

301-
302323
def do_sync(self, args):
303324
"""
304325
Asks cloudmonkey to discovery and sync apis available on user specified
@@ -396,7 +417,8 @@ def do_help(self, args):
396417
helpdoc += "\nRequired params are %s" % ' '.join(required)
397418
helpdoc += "\nParameters\n" + "=" * 10
398419
for param in api['params']:
399-
helpdoc += "\n%s = (%s) %s" % (param['name'], param['type'], param['description'])
420+
helpdoc += "\n%s = (%s) %s" % (param['name'],
421+
param['type'], param['description'])
400422
self.monkeyprint(helpdoc)
401423
else:
402424
self.monkeyprint("Error: no such api (%s) on %s" %

tools/cli/cloudmonkey/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
config_fields = {'core': {}, 'ui': {}, 'server': {}, 'user': {}}
4343

4444
# core
45+
config_fields['core']['asyncblock'] = 'true'
46+
config_fields['core']['paramcompletion'] = 'false'
4547
config_fields['core']['history_file'] = expanduser(config_dir + '/history')
4648
config_fields['core']['log_file'] = expanduser(config_dir + '/log')
4749

@@ -51,7 +53,6 @@
5153
config_fields['ui']['tabularize'] = 'false'
5254

5355
# server
54-
config_fields['server']['asyncblock'] = 'true'
5556
config_fields['server']['host'] = 'localhost'
5657
config_fields['server']['path'] = '/client/api'
5758
config_fields['server']['port'] = '8080'

0 commit comments

Comments
 (0)