Kaydet (Commit) d19572aa authored tarafından Joffrey F's avatar Joffrey F Kaydeden (comit) GitHub

Merge pull request #1493 from funkyfuture/model_properties

Adds a few model properties
...@@ -13,5 +13,4 @@ html/* ...@@ -13,5 +13,4 @@ html/*
__pycache__ __pycache__
# Compiled Documentation # Compiled Documentation
site/ docs/_build
Makefile
FROM python:3.5 FROM python:3.5
RUN mkdir /src ARG uid=1000
WORKDIR /src ARG gid=1000
COPY requirements.txt /src/requirements.txt RUN addgroup --gid $gid sphinx \
RUN pip install -r requirements.txt && useradd --uid $uid --gid $gid -M sphinx
COPY docs-requirements.txt /src/docs-requirements.txt WORKDIR /src
RUN pip install -r docs-requirements.txt COPY requirements.txt docs-requirements.txt ./
RUN pip install -r requirements.txt -r docs-requirements.txt
COPY . /src USER sphinx
...@@ -16,7 +16,7 @@ build-py3: ...@@ -16,7 +16,7 @@ build-py3:
.PHONY: build-docs .PHONY: build-docs
build-docs: build-docs:
docker build -t docker-sdk-python-docs -f Dockerfile-docs . docker build -t docker-sdk-python-docs -f Dockerfile-docs --build-arg uid=$(shell id -u) --build-arg gid=$(shell id -g) .
.PHONY: build-dind-certs .PHONY: build-dind-certs
build-dind-certs: build-dind-certs:
...@@ -77,7 +77,7 @@ flake8: build ...@@ -77,7 +77,7 @@ flake8: build
.PHONY: docs .PHONY: docs
docs: build-docs docs: build-docs
docker run --rm -it -v `pwd`:/code docker-sdk-python-docs sphinx-build docs ./_build docker run --rm -it -v `pwd`:/src docker-sdk-python-docs sphinx-build docs docs/_build
.PHONY: shell .PHONY: shell
shell: build shell: build
......
...@@ -18,6 +18,24 @@ class Container(Model): ...@@ -18,6 +18,24 @@ class Container(Model):
if self.attrs.get('Name') is not None: if self.attrs.get('Name') is not None:
return self.attrs['Name'].lstrip('/') return self.attrs['Name'].lstrip('/')
@property
def image(self):
"""
The image of the container.
"""
image_id = self.attrs['Image']
if image_id is None:
return None
return self.client.images.get(image_id.split(':')[1])
@property
def labels(self):
"""
The labels of a container as dictionary.
"""
result = self.attrs['Config'].get('Labels')
return result or {}
@property @property
def status(self): def status(self):
""" """
......
...@@ -15,6 +15,14 @@ class Image(Model): ...@@ -15,6 +15,14 @@ class Image(Model):
def __repr__(self): def __repr__(self):
return "<%s: '%s'>" % (self.__class__.__name__, "', '".join(self.tags)) 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 @property
def short_id(self): def short_id(self):
""" """
......
...@@ -21,11 +21,13 @@ Container objects ...@@ -21,11 +21,13 @@ Container objects
.. autoclass:: Container() .. autoclass:: Container()
.. py:attribute:: attrs
.. autoattribute:: id .. autoattribute:: id
.. autoattribute:: short_id .. autoattribute:: image
.. autoattribute:: labels
.. autoattribute:: name .. autoattribute:: name
.. autoattribute:: short_id
.. autoattribute:: status .. autoattribute:: status
.. py:attribute:: attrs
The raw representation of this object from the server. The raw representation of this object from the server.
......
...@@ -26,10 +26,11 @@ Image objects ...@@ -26,10 +26,11 @@ Image objects
.. autoclass:: Image() .. autoclass:: Image()
.. autoattribute:: id .. py:attribute:: attrs
.. autoattribute:: short_id .. autoattribute:: id
.. autoattribute:: tags .. autoattribute:: labels
.. py:attribute:: attrs .. autoattribute:: short_id
.. autoattribute:: tags
The raw representation of this object from the server. The raw representation of this object from the server.
......
...@@ -134,7 +134,7 @@ def get_fake_inspect_container(tty=False): ...@@ -134,7 +134,7 @@ def get_fake_inspect_container(tty=False):
status_code = 200 status_code = 200
response = { response = {
'Id': FAKE_CONTAINER_ID, 'Id': FAKE_CONTAINER_ID,
'Config': {'Privileged': True, 'Tty': tty}, 'Config': {'Labels': {'foo': 'bar'}, 'Privileged': True, 'Tty': tty},
'ID': FAKE_CONTAINER_ID, 'ID': FAKE_CONTAINER_ID,
'Image': 'busybox:latest', 'Image': 'busybox:latest',
'Name': 'foobar', 'Name': 'foobar',
...@@ -158,6 +158,7 @@ def get_fake_inspect_image(): ...@@ -158,6 +158,7 @@ def get_fake_inspect_image():
'Parent': "27cf784147099545", 'Parent': "27cf784147099545",
'Created': "2013-03-23T22:24:18.818426-07:00", 'Created': "2013-03-23T22:24:18.818426-07:00",
'Container': FAKE_CONTAINER_ID, 'Container': FAKE_CONTAINER_ID,
'Config': {'Labels': {'bar': 'foo'}},
'ContainerConfig': 'ContainerConfig':
{ {
"Hostname": "", "Hostname": "",
......
...@@ -384,12 +384,22 @@ class ContainerTest(unittest.TestCase): ...@@ -384,12 +384,22 @@ class ContainerTest(unittest.TestCase):
container.get_archive('foo') container.get_archive('foo')
client.api.get_archive.assert_called_with(FAKE_CONTAINER_ID, 'foo') client.api.get_archive.assert_called_with(FAKE_CONTAINER_ID, 'foo')
def test_image(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)
assert container.image.id == FAKE_IMAGE_ID
def test_kill(self): def test_kill(self):
client = make_fake_client() client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID) container = client.containers.get(FAKE_CONTAINER_ID)
container.kill(signal=5) container.kill(signal=5)
client.api.kill.assert_called_with(FAKE_CONTAINER_ID, signal=5) client.api.kill.assert_called_with(FAKE_CONTAINER_ID, signal=5)
def test_labels(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)
assert container.labels == {'foo': 'bar'}
def test_logs(self): def test_logs(self):
client = make_fake_client() client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID) container = client.containers.get(FAKE_CONTAINER_ID)
......
...@@ -21,6 +21,11 @@ class ImageCollectionTest(unittest.TestCase): ...@@ -21,6 +21,11 @@ class ImageCollectionTest(unittest.TestCase):
assert isinstance(image, Image) assert isinstance(image, Image)
assert image.id == FAKE_IMAGE_ID 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): def test_list(self):
client = make_fake_client() client = make_fake_client()
images = client.images.list(all=True) images = client.images.list(all=True)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment