From 09a2f23e8faecb3aefa0450499e04098bae98a1d Mon Sep 17 00:00:00 2001 From: Dan Slimmon Date: Thu, 25 Jul 2013 16:58:15 +0000 Subject: [PATCH] Made Linode inventory module much faster. Listing no longer makes an API request for every node. --- plugins/inventory/linode.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/plugins/inventory/linode.py b/plugins/inventory/linode.py index 6157c6deccd..472b56c8bcc 100755 --- a/plugins/inventory/linode.py +++ b/plugins/inventory/linode.py @@ -98,6 +98,8 @@ class LinodeInventory(object): self.inventory = {} # Index of label to Linode ID self.index = {} + # Local cache of Datacenter objects populated by populate_datacenter_cache() + self._datacenter_cache = None # Read settings and parse CLI arguments self.read_settings() @@ -162,7 +164,7 @@ class LinodeInventory(object): def get_nodes(self): """Makes an Linode API call to get the list of nodes.""" try: - for node in Linode.search(): + for node in Linode.search(status=Linode.STATUS_RUNNING): self.add_node(node) except api.linode_api.ApiError, e: print "Looks like Linode's API is down:" @@ -180,9 +182,18 @@ class LinodeInventory(object): print e sys.exit(1) + def populate_datacenter_cache(self): + """Creates self._datacenter_cache, containing all Datacenters indexed by ID.""" + self._datacenter_cache = {} + dcs = Datacenter.search() + for dc in dcs: + self._datacenter_cache[dc.api_id] = dc + def get_datacenter_city(self, node): """Returns a the lowercase city name of the node's data center.""" - location = node.datacenter.location + if self._datacenter_cache is None: + self.populate_datacenter_cache() + location = self._datacenter_cache[node.datacenter_id].location location = location.lower() location = location.split(",")[0] return location @@ -190,10 +201,6 @@ class LinodeInventory(object): def add_node(self, node): """Adds an node to the inventory and index.""" - # Only want running nodes - if not node.is_up(): - return - dest = node.label # Add to index