From 41542eb7f6b0a049f04452d5bd87b457d0111794 Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Fri, 23 May 2025 18:16:27 +0200
Subject: [PATCH] Wrap 'Hypervisor.libvirt' in a 'FutureCache'

---
 src/util.py |  7 ++++++-
 src/vm.py   | 17 +++++++++++------
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/src/util.py b/src/util.py
index c8adf06..3ef298e 100644
--- a/src/util.py
+++ b/src/util.py
@@ -96,7 +96,12 @@ class FutureCache:
     def clear_cache(self):
         self._cache = None
         pass
-        
+
+    def __repr__(self):
+        return 'FutureCache(%s)' % self._cache
+
+    pass
+
 class InvalidHostException(Exception):
 
     def __init__(self, allowed=[]):
diff --git a/src/vm.py b/src/vm.py
index b1726a8..1a0651a 100644
--- a/src/vm.py
+++ b/src/vm.py
@@ -118,10 +118,9 @@ class Hypervisor:
     def __init__(self, host, db):
         self.host = host
         self.db = db
-        self.libvirt = libvirt.open('qemu+ssh://root@%s/system' % host)
-        def get_storage_pool(name):
-            return self.libvirt.storagePoolLookupByName(name)
-            pass
+        def libvirt_open():
+            return libvirt.open('qemu+ssh://root@%s/system' % host)
+        self.libvirt = util.FutureCache(libvirt_open)
         def get_storage_pool_list(): 
             return dict([ (p.name(), p)
                           for p in self.libvirt.listAllStoragePools() ])
@@ -150,8 +149,14 @@ class Hypervisor:
         if not attr.startswith('__'):
             return getattr(self.libvirt, attr)
         raise AttributeError()
-                
-    
+
+    def __repr__(self):
+        return 'Hypervisor(%s, libvirt=%s)' % (
+            self.host,
+            self.libvirt._cache)
+
+    pass
+
 def get_hypervisors(db):
     hosts = set()
     for d in db.tree._host_._qemu_._disk_:
-- 
GitLab