Adds a 'labels' property to the image model

Signed-off-by: Frank Sachsenheim <funkyfuture@riseup.net>
This commit is contained in:
Frank Sachsenheim 2017-03-04 00:36:11 +01:00
parent 1cd56b9f0c
commit b585ec59a8
4 changed files with 15 additions and 0 deletions

View File

@ -15,6 +15,14 @@ class Image(Model):
def __repr__(self):
return "<%s: '%s'>" % (self.__class__.__name__, "', '".join(self.tags))
@property
def labels(self):
"""
The labels of an image as dictionary.
"""
result = self.attrs['Config'].get('Labels')
return result or {}
@property
def short_id(self):
"""

View File

@ -29,6 +29,7 @@ Image objects
.. autoattribute:: id
.. autoattribute:: short_id
.. autoattribute:: tags
.. autoattribute:: labels
.. py:attribute:: attrs
The raw representation of this object from the server.

View File

@ -158,6 +158,7 @@ def get_fake_inspect_image():
'Parent': "27cf784147099545",
'Created': "2013-03-23T22:24:18.818426-07:00",
'Container': FAKE_CONTAINER_ID,
'Config': {'Labels': {'bar': 'foo'}},
'ContainerConfig':
{
"Hostname": "",

View File

@ -21,6 +21,11 @@ class ImageCollectionTest(unittest.TestCase):
assert isinstance(image, Image)
assert image.id == FAKE_IMAGE_ID
def test_labels(self):
client = make_fake_client()
image = client.images.get(FAKE_IMAGE_ID)
assert image.labels == {'bar': 'foo'}
def test_list(self):
client = make_fake_client()
images = client.images.list(all=True)