Skip to content

Commit 237ce20

Browse files
committed
Merge branch 'id-resolution-in-check-resource' of https://github.com/posita/docker-py into posita-id-resolution-in-check-resource
2 parents 64dcd5a + 947febc commit 237ce20

File tree

3 files changed

+22
-69
lines changed

3 files changed

+22
-69
lines changed

docker/client.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ def api_version(self):
246246
@check_resource
247247
def attach(self, container, stdout=True, stderr=True,
248248
stream=False, logs=False):
249-
if isinstance(container, dict):
250-
container = container.get('Id')
251249
params = {
252250
'logs': logs and 1 or 0,
253251
'stdout': stdout and 1 or 0,
@@ -292,9 +290,6 @@ def attach_socket(self, container, params=None, ws=False):
292290
if ws:
293291
return self._attach_websocket(container, params)
294292

295-
if isinstance(container, dict):
296-
container = container.get('Id')
297-
298293
u = self._url("/containers/{0}/attach".format(container))
299294
return self._get_raw_response_socket(self.post(
300295
u, None, params=self._attach_params(params), stream=True))
@@ -409,8 +404,6 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
409404
@check_resource
410405
def commit(self, container, repository=None, tag=None, message=None,
411406
author=None, conf=None):
412-
if isinstance(container, dict):
413-
container = container.get('Id')
414407
params = {
415408
'container': container,
416409
'repo': repository,
@@ -447,8 +440,6 @@ def containers(self, quiet=False, all=False, trunc=False, latest=False,
447440

448441
@check_resource
449442
def copy(self, container, resource):
450-
if isinstance(container, dict):
451-
container = container.get('Id')
452443
res = self._post_json(
453444
self._url("/containers/{0}/copy".format(container)),
454445
data={"Resource": resource},
@@ -493,8 +484,6 @@ def create_container_from_config(self, config, name=None):
493484

494485
@check_resource
495486
def diff(self, container):
496-
if isinstance(container, dict):
497-
container = container.get('Id')
498487
return self._result(self._get(self._url("/containers/{0}/changes".
499488
format(container))), True)
500489

@@ -539,8 +528,6 @@ def exec_create(self, container, cmd, stdout=True, stderr=True, tty=False,
539528
raise errors.InvalidVersion(
540529
'Privileged exec is not supported in API < 1.19'
541530
)
542-
if isinstance(container, dict):
543-
container = container.get('Id')
544531
if isinstance(cmd, six.string_types):
545532
cmd = shlex.split(str(cmd))
546533

@@ -605,8 +592,6 @@ def exec_start(self, exec_id, detach=False, tty=False, stream=False):
605592

606593
@check_resource
607594
def export(self, container):
608-
if isinstance(container, dict):
609-
container = container.get('Id')
610595
res = self._get(self._url("/containers/{0}/export".format(container)),
611596
stream=True)
612597
self._raise_for_status(res)
@@ -744,25 +729,19 @@ def insert(self, image, url, path):
744729

745730
@check_resource
746731
def inspect_container(self, container):
747-
if isinstance(container, dict):
748-
container = container.get('Id')
749732
return self._result(
750733
self._get(self._url("/containers/{0}/json".format(container))),
751734
True)
752735

753736
@check_resource
754737
def inspect_image(self, image):
755-
if isinstance(image, dict):
756-
image = image.get('Id')
757738
return self._result(
758739
self._get(self._url("/images/{0}/json".format(image))),
759740
True
760741
)
761742

762743
@check_resource
763744
def kill(self, container, signal=None):
764-
if isinstance(container, dict):
765-
container = container.get('Id')
766745
url = self._url("/containers/{0}/kill".format(container))
767746
params = {}
768747
if signal is not None:
@@ -810,8 +789,6 @@ def login(self, username, password=None, email=None, registry=None,
810789
@check_resource
811790
def logs(self, container, stdout=True, stderr=True, stream=False,
812791
timestamps=False, tail='all'):
813-
if isinstance(container, dict):
814-
container = container.get('Id')
815792
if utils.compare_version('1.11', self._version) >= 0:
816793
params = {'stderr': stderr and 1 or 0,
817794
'stdout': stdout and 1 or 0,
@@ -844,8 +821,6 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
844821

845822
@check_resource
846823
def pause(self, container):
847-
if isinstance(container, dict):
848-
container = container.get('Id')
849824
url = self._url('/containers/{0}/pause'.format(container))
850825
res = self._post(url)
851826
self._raise_for_status(res)
@@ -855,8 +830,6 @@ def ping(self):
855830

856831
@check_resource
857832
def port(self, container, private_port):
858-
if isinstance(container, dict):
859-
container = container.get('Id')
860833
res = self._get(self._url("/containers/{0}/json".format(container)))
861834
self._raise_for_status(res)
862835
json_ = res.json()
@@ -961,17 +934,13 @@ def push(self, repository, tag=None, stream=False,
961934

962935
@check_resource
963936
def remove_container(self, container, v=False, link=False, force=False):
964-
if isinstance(container, dict):
965-
container = container.get('Id')
966937
params = {'v': v, 'link': link, 'force': force}
967938
res = self._delete(self._url("/containers/" + container),
968939
params=params)
969940
self._raise_for_status(res)
970941

971942
@check_resource
972943
def remove_image(self, image, force=False, noprune=False):
973-
if isinstance(image, dict):
974-
image = image.get('Id')
975944
params = {'force': force, 'noprune': noprune}
976945
res = self._delete(self._url("/images/" + image), params=params)
977946
self._raise_for_status(res)
@@ -982,27 +951,20 @@ def rename(self, container, name):
982951
raise errors.InvalidVersion(
983952
'rename was only introduced in API version 1.17'
984953
)
985-
if isinstance(container, dict):
986-
container = container.get('Id')
987954
url = self._url("/containers/{0}/rename".format(container))
988955
params = {'name': name}
989956
res = self._post(url, params=params)
990957
self._raise_for_status(res)
991958

992959
@check_resource
993960
def resize(self, container, height, width):
994-
if isinstance(container, dict):
995-
container = container.get('Id')
996-
997961
params = {'h': height, 'w': width}
998962
url = self._url("/containers/{0}/resize".format(container))
999963
res = self._post(url, params=params)
1000964
self._raise_for_status(res)
1001965

1002966
@check_resource
1003967
def restart(self, container, timeout=10):
1004-
if isinstance(container, dict):
1005-
container = container.get('Id')
1006968
params = {'t': timeout}
1007969
url = self._url("/containers/{0}/restart".format(container))
1008970
res = self._post(url, params=params)
@@ -1067,9 +1029,6 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
10671029
ipc_mode=ipc_mode, security_opt=security_opt, ulimits=ulimits
10681030
)
10691031

1070-
if isinstance(container, dict):
1071-
container = container.get('Id')
1072-
10731032
url = self._url("/containers/{0}/start".format(container))
10741033
if not start_config:
10751034
start_config = None
@@ -1088,15 +1047,11 @@ def stats(self, container, decode=None):
10881047
raise errors.InvalidVersion(
10891048
'Stats retrieval is not supported in API < 1.17!')
10901049

1091-
if isinstance(container, dict):
1092-
container = container.get('Id')
10931050
url = self._url("/containers/{0}/stats".format(container))
10941051
return self._stream_helper(self._get(url, stream=True), decode=decode)
10951052

10961053
@check_resource
10971054
def stop(self, container, timeout=10):
1098-
if isinstance(container, dict):
1099-
container = container.get('Id')
11001055
params = {'t': timeout}
11011056
url = self._url("/containers/{0}/stop".format(container))
11021057

@@ -1118,8 +1073,6 @@ def tag(self, image, repository, tag=None, force=False):
11181073

11191074
@check_resource
11201075
def top(self, container):
1121-
if isinstance(container, dict):
1122-
container = container.get('Id')
11231076
u = self._url("/containers/{0}/top".format(container))
11241077
return self._result(self._get(u), True)
11251078

@@ -1129,16 +1082,12 @@ def version(self, api_version=True):
11291082

11301083
@check_resource
11311084
def unpause(self, container):
1132-
if isinstance(container, dict):
1133-
container = container.get('Id')
11341085
url = self._url('/containers/{0}/unpause'.format(container))
11351086
res = self._post(url)
11361087
self._raise_for_status(res)
11371088

11381089
@check_resource
11391090
def wait(self, container, timeout=None):
1140-
if isinstance(container, dict):
1141-
container = container.get('Id')
11421091
url = self._url("/containers/{0}/wait".format(container))
11431092
res = self._post(url, timeout=timeout)
11441093
self._raise_for_status(res)

docker/utils/decorators.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def wrapped(self, resource_id=None, *args, **kwargs):
1212
resource_id = kwargs.pop('container')
1313
elif kwargs.get('image'):
1414
resource_id = kwargs.pop('image')
15+
if isinstance(resource_id, dict):
16+
resource_id = resource_id.get('Id')
1517
if not resource_id:
1618
raise errors.NullResource(
1719
'image or container param is undefined'

tests/test.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,15 +1894,16 @@ def test_inspect_container(self):
18941894
timeout=DEFAULT_TIMEOUT_SECONDS
18951895
)
18961896

1897-
def test_inspect_container_empty_id(self):
1898-
try:
1899-
self.client.inspect_container('')
1900-
except docker.errors.NullResource as e:
1901-
self.assertEqual(
1902-
e.args[0], 'image or container param is undefined'
1903-
)
1904-
else:
1905-
self.fail('Command expected NullResource exception')
1897+
def test_inspect_container_undefined_id(self):
1898+
for arg in None, '', {True: True}:
1899+
try:
1900+
self.client.inspect_container(arg)
1901+
except docker.errors.NullResource as e:
1902+
self.assertEqual(
1903+
e.args[0], 'image or container param is undefined'
1904+
)
1905+
else:
1906+
self.fail('Command expected NullResource exception')
19061907

19071908
def test_container_stats(self):
19081909
try:
@@ -2075,15 +2076,16 @@ def test_inspect_image(self):
20752076
timeout=DEFAULT_TIMEOUT_SECONDS
20762077
)
20772078

2078-
def test_inspect_image_empty_id(self):
2079-
try:
2080-
self.client.inspect_image('')
2081-
except docker.errors.NullResource as e:
2082-
self.assertEqual(
2083-
e.args[0], 'image or container param is undefined'
2084-
)
2085-
else:
2086-
self.fail('Command expected NullResource exception')
2079+
def test_inspect_image_undefined_id(self):
2080+
for arg in None, '', {True: True}:
2081+
try:
2082+
self.client.inspect_image(arg)
2083+
except docker.errors.NullResource as e:
2084+
self.assertEqual(
2085+
e.args[0], 'image or container param is undefined'
2086+
)
2087+
else:
2088+
self.fail('Command expected NullResource exception')
20872089

20882090
def test_insert_image(self):
20892091
try:

0 commit comments

Comments
 (0)