Skip to content

Commit b585ec5

Browse files
committed
Adds a 'labels' property to the image model
Signed-off-by: Frank Sachsenheim <[email protected]>
1 parent 1cd56b9 commit b585ec5

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

docker/models/images.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class Image(Model):
1515
def __repr__(self):
1616
return "<%s: '%s'>" % (self.__class__.__name__, "', '".join(self.tags))
1717

18+
@property
19+
def labels(self):
20+
"""
21+
The labels of an image as dictionary.
22+
"""
23+
result = self.attrs['Config'].get('Labels')
24+
return result or {}
25+
1826
@property
1927
def short_id(self):
2028
"""

docs/images.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Image objects
2929
.. autoattribute:: id
3030
.. autoattribute:: short_id
3131
.. autoattribute:: tags
32+
.. autoattribute:: labels
3233
.. py:attribute:: attrs
3334
3435
The raw representation of this object from the server.

tests/unit/fake_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def get_fake_inspect_image():
158158
'Parent': "27cf784147099545",
159159
'Created': "2013-03-23T22:24:18.818426-07:00",
160160
'Container': FAKE_CONTAINER_ID,
161+
'Config': {'Labels': {'bar': 'foo'}},
161162
'ContainerConfig':
162163
{
163164
"Hostname": "",

tests/unit/models_images_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def test_get(self):
2121
assert isinstance(image, Image)
2222
assert image.id == FAKE_IMAGE_ID
2323

24+
def test_labels(self):
25+
client = make_fake_client()
26+
image = client.images.get(FAKE_IMAGE_ID)
27+
assert image.labels == {'bar': 'foo'}
28+
2429
def test_list(self):
2530
client = make_fake_client()
2631
images = client.images.list(all=True)

0 commit comments

Comments
 (0)