The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/pylxd/pull/155

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===

From bc0c652c7186e269f25f53bc8b06010b52704aaa Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul.hum...@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)
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to