libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [libcloud] d-mo commented on a change in pull request #1282: Packet driver extensions
Date Wed, 03 Jul 2019 11:06:48 GMT
d-mo commented on a change in pull request #1282: Packet driver extensions
URL: https://github.com/apache/libcloud/pull/1282#discussion_r299899543
 
 

 ##########
 File path: libcloud/compute/drivers/packet.py
 ##########
 @@ -88,11 +98,88 @@ class PacketNodeDriver(NodeDriver):
                       'failed': NodeState.ERROR,
                       'active': NodeState.RUNNING}
 
-    def list_nodes(self, ex_project_id):
-        data = self.connection.request('/projects/%s/devices' %
-                                       (ex_project_id),
-                                       params={'include': 'plan'}
-                                       ).object['devices']
+    def __init__(self, key, project=None):
+        # initialize a NodeDriver for Packet using the API token
+        # and optionally the project (name or id)
+        # If project specified we need to be sure this is a valid project
+        # so we create the variable self.project_id
+        super(PacketNodeDriver, self).__init__(key=key, project=None)
+        self.project_name = project
+        self.project_id = None
+        self.projects = self.ex_list_projects()
+        if project:
+            for project_obj in self.projects:
+                if project in [project_obj.name, project_obj.id]:
+                    self.project_id = project_obj.id
+                    break
+            if not self.project_id:
+                self.project_name = None
+
+    def ex_list_projects(self):
+        projects = []
+        data = self.connection.request('/projects').object
+        projects = data.get('projects')
+        if projects:
+            projects = [Project(project) for project in projects]
+        return projects
+
+    def list_nodes(self, ex_project_id=None):
+        if ex_project_id:
+            return self.list_nodes_for_project(ex_project_id=ex_project_id)
+
+        # if project has been specified during driver initialization, then
+        # return nodes for this project only
+        if self.project_id:
+            return self.list_nodes_for_project(
+                ex_project_id=self.project_id)
+
+        # In case of Python2 perform requests serially
+        if asyncio is None:
+            nodes = []
+            for project in self.projects:
+                nodes.extend(
+                    self.list_nodes_for_project(ex_project_id=project.id)
+                )
+            return nodes
+        # In case of Python3 use asyncio to perform requests in parallel
+        return self.list_resources_async('nodes')
+
+    def list_resources_async(self, resource_type):
+        # The _list_nodes function is defined dynamically using exec in
+        # order to prevent a SyntaxError in Python2 due to "yield from".
+        # This cruft can be removed once Python2 support is no longer
+        # required.
+        assert resource_type in ['nodes', 'volumes']
+        glob = globals()
+        loc = locals()
+        exec("""
+import asyncio
+@asyncio.coroutine
+def _list_async(driver):
+    projects = [project.id for project in driver.projects]
+    loop = asyncio.get_event_loop()
+    futures = [
+        loop.run_in_executor(None, driver.list_%s_for_project, p)
+        for p in projects
+    ]
+    retval = []
+    for future in futures:
+        result = yield from future
+        retval.extend(result)
+    return retval""" % resource_type, glob, loc)
+        loop = asyncio.get_event_loop()
+        return loop.run_until_complete(loc['_list_async'](loc['self']))
+
+    def list_nodes_for_project(self, ex_project_id, include='plan', page=1,
 
 Review comment:
   Fixed

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message