[lxc-devel] [pylxd/master] Add the ability to get an image by its alias (fixes #152)

rockstar on Github lxc-bot at linuxcontainers.org
Wed Jun 29 19:30:45 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 302 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160629/95bc9607/attachment.bin>
-------------- next part --------------
From bc0c652c7186e269f25f53bc8b06010b52704aaa Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul.hummer at canonical.com>
Date: Wed, 29 Jun 2016 13:29:58 -0600
Subject: [PATCH] Add the ability to get an image by its alias (fixes #152)

---
 pylxd/image.py            |  8 ++++++++
 pylxd/tests/mock_lxd.py   | 14 ++++++++++++++
 pylxd/tests/test_image.py |  7 +++++++
 3 files changed, 29 insertions(+)

diff --git a/pylxd/image.py b/pylxd/image.py
index d4c70e5..746c109 100644
--- a/pylxd/image.py
+++ b/pylxd/image.py
@@ -46,6 +46,14 @@ def get(cls, client, fingerprint):
         return image
 
     @classmethod
+    def get_by_alias(cls, client, alias):
+        """Get an image by its alias."""
+        response = client.api.images.aliases[alias].get()
+
+        fingerprint = response.json()['metadata']['target']
+        return cls.get(client, fingerprint)
+
+    @classmethod
     def all(cls, client):
         """Get all images."""
         response = client.api.images.get()
diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py
index 3acc038..5dbea19 100644
--- a/pylxd/tests/mock_lxd.py
+++ b/pylxd/tests/mock_lxd.py
@@ -366,6 +366,20 @@ def profile_GET(request, context):
         'url': r'^http://pylxd.test/1.0/images$',
     },
     {
+        'json': {
+            'type': 'sync',
+            'status': 'Success',
+            'status_code': 200,
+            'metadata': {
+                'name': 'an-alias',
+                'description': 'an-alias',
+                'target': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',  # NOQA
+            }
+        },
+        'method': 'GET',
+        'url': r'^http://pylxd.test/1.0/images/aliases/an-alias$',
+    },
+    {
         'text': json.dumps({
             'type': 'sync',
             'metadata': {
diff --git a/pylxd/tests/test_image.py b/pylxd/tests/test_image.py
index 9c3686e..756e56f 100644
--- a/pylxd/tests/test_image.py
+++ b/pylxd/tests/test_image.py
@@ -55,6 +55,13 @@ def error(request, context):
             exceptions.LXDAPIException,
             image.Image.get, self.client, fingerprint)
 
+    def test_get_by_alias(self):
+        fingerprint = hashlib.sha256(b'').hexdigest()
+
+        a_image = image.Image.get_by_alias(self.client, 'an-alias')
+
+        self.assertEqual(fingerprint, a_image.fingerprint)
+
     def test_all(self):
         """A list of all images is returned."""
         images = image.Image.all(self.client)


More information about the lxc-devel mailing list