cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bhais...@apache.org
Subject [2/4] git commit: cloudmonkey: add param completer heuristic
Date Wed, 29 Oct 2014 11:35:51 GMT
cloudmonkey: add param completer heuristic

- Adds a list API heuristic to autocomplete for arguments that are some kind of
  id, for example in deploy virtualmachine command, for an argument like
  templateid, this heuristic tries to find an API that lists templates and
  returns options that may qualify for this argument

- Adds a relative search approximation heuristic. This uses cache apis returned
  from CloudStack mgmt server, in which the API discovery plugin creates a map
  of related APIs for each API's argument/options. We search this information
  of related APIs for an API arg that we're trying to autocomplete but only after
  the list API heuristic has failed

- Since, heuristic based approximations are used, parameter autocompletion may
  not be foolproof. So debug logs are added so users can report issues and corner
  cases that may be fixed.

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/commit/633152bf
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/tree/633152bf
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/diff/633152bf

Branch: refs/heads/master
Commit: 633152bf9067ff96e2236b3f9771291cced22dc8
Parents: 1184509
Author: Rohit Yadav <rohit.yadav@shapeblue.com>
Authored: Wed Oct 29 16:34:11 2014 +0530
Committer: Rohit Yadav <rohit.yadav@shapeblue.com>
Committed: Wed Oct 29 16:37:18 2014 +0530

----------------------------------------------------------------------
 cloudmonkey/cloudmonkey.py | 63 ++++++++++++++++++++++++++++-------------
 1 file changed, 43 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/blob/633152bf/cloudmonkey/cloudmonkey.py
----------------------------------------------------------------------
diff --git a/cloudmonkey/cloudmonkey.py b/cloudmonkey/cloudmonkey.py
index fd0e865..edadfed 100644
--- a/cloudmonkey/cloudmonkey.py
+++ b/cloudmonkey/cloudmonkey.py
@@ -383,36 +383,55 @@ class CloudMonkeyShell(cmd.Cmd, object):
                 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 = ""
-                    apis = filter(lambda x: 'list' in x, related)
-                    if len(apis) > 0:
-                        api = apis[0]
-                    else:
+                    api = None
+                    logger.debug("[Paramcompl] For %s %s %s=" % (verb, subject,
+                                                                 param))
+                    if "id" in param:
+                        logger.debug("[Paramcompl] Using 'list' heuristics")
                         entity = param.replace("id", "")
-                        for verb in self.apicache["list"]:
-                            if verb.startswith(entity):
-                                api = self.apicache["list"][verb]['name']
-                                break
+                        if not entity:
+                            entity = subject
+                        apis = []
+                        for resource in self.apicache["list"]:
+                            if resource.startswith(entity):
+                                api = self.apicache["list"][resource]['name']
+                                if (entity + "s") == resource.lower():
+                                    break
+                                apis.append(api)
+                                api = None
+                        if len(apis) > 0 and not api:
+                            logger.debug("[Paramcompl] APIs: %s" % apis)
+                            api = min(apis, key=len)
+                        logger.debug("[Paramcompl] Possible API: %s" % api)
                     if not api:
-                        return
+                        logger.debug("[Paramcompl] Using relative approx")
+                        params = self.apicache[verb][subject]['params']
+                        related = filter(lambda x: x['name'] == param,
+                                         params)[0]['related']
+                        apis = filter(lambda x: 'list' in x, related)
+                        logger.debug("[Paramcompl] Related APIs: %s" % apis)
+                        if len(apis) > 0:
+                            api = apis[0]
+                        else:
+                            return
+                    logger.debug("Trying paramcompletion using API: %s" % api)
                     uuids = []
-                    if api in self.param_cache.keys() and self.param_cache[api]["ts"] >
(int(time.time()) - 1000):
+                    cache_burst_ts = int(time.time()) - 900
+                    if api in self.param_cache.keys() and \
+                        len(self.param_cache[api]["options"]) > 0 and \
+                            self.param_cache[api]["ts"] > cache_burst_ts:
                         for option in self.param_cache[api]["options"]:
                             uuid = option[0]
                             name = option[1]
                             if uuid.startswith(value):
                                 uuids.append(uuid)
                     else:
-                        response = self.make_request(api, args={'listall': 'true', 'templatefilter':
'all'})
+                        api_args = {'listall': 'true', 'templatefilter': 'all'}
+                        response = self.make_request(api, args=api_args)
                         responsekey = filter(lambda x: 'response' in x,
-                                            response.keys())[0]
+                                             response.keys())[0]
                         result = response[responsekey]
-                        self.param_cache[api] = {}
-                        self.param_cache[api]["ts"] = int(time.time())
-                        self.param_cache[api]["options"] = []
+                        options = []
                         for key in result.keys():
                             if isinstance(result[key], list):
                                 for element in result[key]:
@@ -423,8 +442,12 @@ class CloudMonkeyShell(cmd.Cmd, object):
                                             name = element['name']
                                         elif 'username' in element.keys():
                                             name = element['username']
-                                        self.param_cache[api]["options"].append((uuid, name,))
+                                        options.append((uuid, name,))
                                         uuids.append(uuid)
+                        self.param_cache[api] = {}
+                        self.param_cache[api]["ts"] = int(time.time())
+                        self.param_cache[api]["options"] = options
+
                     if len(uuids) > 0:
                         print
                         for option in self.param_cache[api]["options"]:


Mime
View raw message