From 72291ef97fa3f1ebded671f9b46bfaef507cb855 Mon Sep 17 00:00:00 2001 From: Rohit Yadav Date: Thu, 7 Feb 2013 16:00:22 +0530 Subject: [PATCH] CLOUDSTACK-1037: Implement fuzzy parameter completion Signed-off-by: Rohit Yadav (cherry picked from commit 1fd0563137075a8b405bdd5f17d0b821442c4f58) Signed-off-by: Rohit Yadav --- tools/cli/cloudmonkey/cachemaker.py | 4 ++-- tools/cli/cloudmonkey/cloudmonkey.py | 36 ++++++++++++++++++++++------ tools/cli/cloudmonkey/config.py | 3 ++- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/tools/cli/cloudmonkey/cachemaker.py b/tools/cli/cloudmonkey/cachemaker.py index 8827f2984c0..42a077ad928 100644 --- a/tools/cli/cloudmonkey/cachemaker.py +++ b/tools/cli/cloudmonkey/cachemaker.py @@ -56,8 +56,8 @@ def savecache(apicache, json_file): """ Saves apicache dictionary as json_file, returns dictionary as indented str """ - if isinstance(type(apicache), types.NoneType) or apicache is None or apicache is {}: - return "" + if apicache is None or apicache is {}: + return "" apicachestr = json.dumps(apicache, indent=2) with open(json_file, 'w') as cache_file: cache_file.write(apicachestr) diff --git a/tools/cli/cloudmonkey/cloudmonkey.py b/tools/cli/cloudmonkey/cloudmonkey.py index 53f73bd3e72..b5b185f2d6c 100644 --- a/tools/cli/cloudmonkey/cloudmonkey.py +++ b/tools/cli/cloudmonkey/cloudmonkey.py @@ -246,13 +246,13 @@ class CloudMonkeyShell(cmd.Cmd, object): map(lambda x: x.strip(), args_dict.pop('filter').split(','))) - missing_args = [] + missing = [] if verb in self.apicache and subject in self.apicache[verb]: - missing_args = filter(lambda x: x not in args_dict.keys(), - self.apicache[verb][subject]['requiredparams']) + missing = filter(lambda x: x not in args_dict.keys(), + self.apicache[verb][subject]['requiredparams']) - if len(missing_args) > 0: - self.monkeyprint("Missing arguments: ", ' '.join(missing_args)) + if len(missing) > 0: + self.monkeyprint("Missing arguments: ", ' '.join(missing)) return isasync = False @@ -293,12 +293,33 @@ class CloudMonkeyShell(cmd.Cmd, object): map(lambda x: x['name'], self.apicache[verb][subject]['params'])) search_string = text + if self.paramcompletion == 'true': + param = line.split(" ")[-1] + idx = param.find("=") + value = param[idx + 1:] + param = param[:idx] + if len(value) < 36 and idx != -1: + params = self.apicache[verb][subject]['params'] + related = filter(lambda x: x['name'] == param, + params)[0]['related'] + api = min(filter(lambda x: 'list' in x, related), key=len) + response = self.make_request(api, args={'listall': 'true'}) + responsekey = filter(lambda x: 'response' in x, + response.keys())[0] + result = response[responsekey] + uuids = [] + for key in result.keys(): + if isinstance(result[key], list): + for element in result[key]: + if 'id' in element.keys(): + uuids.append(element['id']) + autocompletions = uuids + search_string = value if self.tabularize == "true" and subject != "": autocompletions.append("filter=") return [s for s in autocompletions if s.startswith(search_string)] - def do_sync(self, args): """ Asks cloudmonkey to discovery and sync apis available on user specified @@ -396,7 +417,8 @@ class CloudMonkeyShell(cmd.Cmd, object): helpdoc += "\nRequired params are %s" % ' '.join(required) helpdoc += "\nParameters\n" + "=" * 10 for param in api['params']: - helpdoc += "\n%s = (%s) %s" % (param['name'], param['type'], param['description']) + helpdoc += "\n%s = (%s) %s" % (param['name'], + param['type'], param['description']) self.monkeyprint(helpdoc) else: self.monkeyprint("Error: no such api (%s) on %s" % diff --git a/tools/cli/cloudmonkey/config.py b/tools/cli/cloudmonkey/config.py index 8b718c2d8a3..5dfe09bf85d 100644 --- a/tools/cli/cloudmonkey/config.py +++ b/tools/cli/cloudmonkey/config.py @@ -42,6 +42,8 @@ cache_file = expanduser(config_dir + '/cache') config_fields = {'core': {}, 'ui': {}, 'server': {}, 'user': {}} # core +config_fields['core']['asyncblock'] = 'true' +config_fields['core']['paramcompletion'] = 'false' config_fields['core']['history_file'] = expanduser(config_dir + '/history') config_fields['core']['log_file'] = expanduser(config_dir + '/log') @@ -51,7 +53,6 @@ config_fields['ui']['prompt'] = '> ' config_fields['ui']['tabularize'] = 'false' # server -config_fields['server']['asyncblock'] = 'true' config_fields['server']['host'] = 'localhost' config_fields['server']['path'] = '/client/api' config_fields['server']['port'] = '8080'