Remove dependency on vendored libjuju
[osm/N2VC.git] / modules / libjuju / juju / client / facade.py
index 9e2aabf..ec20c38 100644 (file)
@@ -16,10 +16,10 @@ from . import codegen
 
 _marker = object()
 
-JUJU_VERSION = re.compile('[0-9]+\.[0-9-]+[\.\-][0-9a-z]+(\.[0-9]+)?')
+JUJU_VERSION = re.compile(r'[0-9]+\.[0-9-]+[\.\-][0-9a-z]+(\.[0-9]+)?')
 # Workaround for https://bugs.launchpad.net/juju/+bug/1683906
 NAUGHTY_CLASSES = ['ClientFacade', 'Client', 'FullStatus', 'ModelStatusInfo',
-                   'ModelInfo']
+                   'ModelInfo', 'ApplicationDeploy']
 
 
 # Map basic types to Python's typing with a callable
@@ -49,14 +49,15 @@ def lookup_facade(name, version):
     of the correct client<version>.py file.
 
     """
-    try:
-        facade = getattr(CLIENTS[str(version)], name)
-    except KeyError:
-        raise ImportError("No facades found for version {}".format(version))
-    except AttributeError:
-        raise ImportError(
-            "No facade with name '{}' in version {}".format(name, version))
-    return facade
+    for _version in range(int(version), 0, -1):
+        try:
+            facade = getattr(CLIENTS[str(_version)], name)
+            return facade
+        except (KeyError, AttributeError):
+            continue
+    else:
+        raise ImportError("No supported version for facade: "
+                          "{}".format(name))
 
 
 '''
@@ -73,7 +74,14 @@ class TypeFactory:
         @param connection: initialized Connection object.
 
         """
-        version = connection.facades[cls.__name__[:-6]]
+        facade_name = cls.__name__
+        if not facade_name.endswith('Facade'):
+           raise TypeError('Unexpected class name: {}'.format(facade_name))
+        facade_name = facade_name[:-len('Facade')]
+        version = connection.facades.get(facade_name)
+        if version is None:
+            raise Exception('No facade {} in facades {}'.format(facade_name,
+                                                                connection.facades))
 
         c = lookup_facade(cls.__name__, version)
         c = c()