Merge pull request #1873 from docker/hongbin-image-load

Return Image objects in ImageCollection.load
This commit is contained in:
Joffrey F 2018-01-26 14:45:09 -08:00 committed by GitHub
commit deb8222d69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 3 deletions

View File

@ -144,6 +144,10 @@ class BuildError(Exception):
pass
class ImageLoadError(DockerException):
pass
def create_unexpected_kwargs_error(name, kwargs):
quoted_kwargs = ["'{}'".format(k) for k in sorted(kwargs)]
text = ["{}() ".format(name)]

View File

@ -3,7 +3,7 @@ import re
import six
from ..api import APIClient
from ..errors import BuildError
from ..errors import BuildError, ImageLoadError
from ..utils.json_stream import json_stream
from .resource import Collection, Model
@ -241,13 +241,27 @@ class ImageCollection(Collection):
data (binary): Image data to be loaded.
Returns:
(generator): Progress output as JSON objects
(list of :py:class:`Image`): The images.
Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.
"""
return self.client.api.load_image(data)
resp = self.client.api.load_image(data)
images = []
for chunk in resp:
if 'stream' in chunk:
match = re.search(
r'(^Loaded image ID: |^Loaded image: )(.+)$',
chunk['stream']
)
if match:
image_id = match.group(2)
images.append(image_id)
if 'error' in chunk:
raise ImageLoadError(chunk['error'])
return [self.get(i) for i in images]
def pull(self, name, tag=None, **kwargs):
"""

View File

@ -71,6 +71,11 @@ class ImageCollectionTest(BaseIntegrationTest):
image = client.images.pull('alpine', tag='3.3')
assert 'alpine:3.3' in image.attrs['RepoTags']
def test_load_error(self):
client = docker.from_env(version=TEST_API_VERSION)
with pytest.raises(docker.errors.ImageLoadError):
client.images.load('abc')
class ImageTest(BaseIntegrationTest):