Skip to content

Commit 947febc

Browse files
committed
Move image/container ID resolution to @check_resource decorator.
1 parent 08444cf commit 947febc

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
@@ -251,8 +251,6 @@ def api_version(self):
251251
@check_resource
252252
def attach(self, container, stdout=True, stderr=True,
253253
stream=False, logs=False):
254-
if isinstance(container, dict):
255-
container = container.get('Id')
256254
params = {
257255
'logs': logs and 1 or 0,
258256
'stdout': stdout and 1 or 0,
@@ -297,9 +295,6 @@ def attach_socket(self, container, params=None, ws=False):
297295
if ws:
298296
return self._attach_websocket(container, params)
299297

300-
if isinstance(container, dict):
301-
container = container.get('Id')
302-
303298
u = self._url("/containers/{0}/attach".format(container))
304299
return self._get_raw_response_socket(self.post(
305300
u, None, params=self._attach_params(params), stream=True))
@@ -410,8 +405,6 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
410405
@check_resource
411406
def commit(self, container, repository=None, tag=None, message=None,
412407
author=None, conf=None):
413-
if isinstance(container, dict):
414-
container = container.get('Id')
415408
params = {
416409
'container': container,
417410
'repo': repository,
@@ -448,8 +441,6 @@ def containers(self, quiet=False, all=False, trunc=False, latest=False,
448441

449442
@check_resource
450443
def copy(self, container, resource):
451-
if isinstance(container, dict):
452-
container = container.get('Id')
453444
res = self._post_json(
454445
self._url("/containers/{0}/copy".format(container)),
455446
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()
@@ -954,17 +927,13 @@ def push(self, repository, tag=None, stream=False,
954927

955928
@check_resource
956929
def remove_container(self, container, v=False, link=False, force=False):
957-
if isinstance(container, dict):
958-
container = container.get('Id')
959930
params = {'v': v, 'link': link, 'force': force}
960931
res = self._delete(self._url("/containers/" + container),
961932
params=params)
962933
self._raise_for_status(res)
963934

964935
@check_resource
965936
def remove_image(self, image, force=False, noprune=False):
966-
if isinstance(image, dict):
967-
image = image.get('Id')
968937
params = {'force': force, 'noprune': noprune}
969938
res = self._delete(self._url("/images/" + image), params=params)
970939
self._raise_for_status(res)
@@ -975,27 +944,20 @@ def rename(self, container, name):
975944
raise errors.InvalidVersion(
976945
'rename was only introduced in API version 1.17'
977946
)
978-
if isinstance(container, dict):
979-
container = container.get('Id')
980947
url = self._url("/containers/{0}/rename".format(container))
981948
params = {'name': name}
982949
res = self._post(url, params=params)
983950
self._raise_for_status(res)
984951

985952
@check_resource
986953
def resize(self, container, height, width):
987-
if isinstance(container, dict):
988-
container = container.get('Id')
989-
990954
params = {'h': height, 'w': width}
991955
url = self._url("/containers/{0}/resize".format(container))
992956
res = self._post(url, params=params)
993957
self._raise_for_status(res)
994958

995959
@check_resource
996960
def restart(self, container, timeout=10):
997-
if isinstance(container, dict):
998-
container = container.get('Id')
999961
params = {'t': timeout}
1000962
url = self._url("/containers/{0}/restart".format(container))
1001963
res = self._post(url, params=params)
@@ -1060,9 +1022,6 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
10601022
ipc_mode=ipc_mode, security_opt=security_opt, ulimits=ulimits
10611023
)
10621024

1063-
if isinstance(container, dict):
1064-
container = container.get('Id')
1065-
10661025
url = self._url("/containers/{0}/start".format(container))
10671026
if not start_config:
10681027
start_config = None
@@ -1081,15 +1040,11 @@ def stats(self, container, decode=None):
10811040
raise errors.InvalidVersion(
10821041
'Stats retrieval is not supported in API < 1.17!')
10831042

1084-
if isinstance(container, dict):
1085-
container = container.get('Id')
10861043
url = self._url("/containers/{0}/stats".format(container))
10871044
return self._stream_helper(self._get(url, stream=True), decode=decode)
10881045

10891046
@check_resource
10901047
def stop(self, container, timeout=10):
1091-
if isinstance(container, dict):
1092-
container = container.get('Id')
10931048
params = {'t': timeout}
10941049
url = self._url("/containers/{0}/stop".format(container))
10951050

@@ -1111,8 +1066,6 @@ def tag(self, image, repository, tag=None, force=False):
11111066

11121067
@check_resource
11131068
def top(self, container):
1114-
if isinstance(container, dict):
1115-
container = container.get('Id')
11161069
u = self._url("/containers/{0}/top".format(container))
11171070
return self._result(self._get(u), True)
11181071

@@ -1122,16 +1075,12 @@ def version(self, api_version=True):
11221075

11231076
@check_resource
11241077
def unpause(self, container):
1125-
if isinstance(container, dict):
1126-
container = container.get('Id')
11271078
url = self._url('/containers/{0}/unpause'.format(container))
11281079
res = self._post(url)
11291080
self._raise_for_status(res)
11301081

11311082
@check_resource
11321083
def wait(self, container, timeout=None):
1133-
if isinstance(container, dict):
1134-
container = container.get('Id')
11351084
url = self._url("/containers/{0}/wait".format(container))
11361085
res = self._post(url, timeout=timeout)
11371086
self._raise_for_status(res)

docker/utils/decorators.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def wrapped(self, resource_id=None, *args, **kwargs):
88
resource_id = kwargs.pop('container')
99
elif kwargs.get('image'):
1010
resource_id = kwargs.pop('image')
11+
if isinstance(resource_id, dict):
12+
resource_id = resource_id.get('Id')
1113
if not resource_id:
1214
raise errors.NullResource(
1315
'image or container param is undefined'

tests/test.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,15 +1786,16 @@ def test_inspect_container(self):
17861786
timeout=DEFAULT_TIMEOUT_SECONDS
17871787
)
17881788

1789-
def test_inspect_container_empty_id(self):
1790-
try:
1791-
self.client.inspect_container('')
1792-
except docker.errors.NullResource as e:
1793-
self.assertEqual(
1794-
e.args[0], 'image or container param is undefined'
1795-
)
1796-
else:
1797-
self.fail('Command expected NullResource exception')
1789+
def test_inspect_container_undefined_id(self):
1790+
for arg in None, '', {True: True}:
1791+
try:
1792+
self.client.inspect_container(arg)
1793+
except docker.errors.NullResource as e:
1794+
self.assertEqual(
1795+
e.args[0], 'image or container param is undefined'
1796+
)
1797+
else:
1798+
self.fail('Command expected NullResource exception')
17981799

17991800
def test_container_stats(self):
18001801
try:
@@ -1967,15 +1968,16 @@ def test_inspect_image(self):
19671968
timeout=DEFAULT_TIMEOUT_SECONDS
19681969
)
19691970

1970-
def test_inspect_image_empty_id(self):
1971-
try:
1972-
self.client.inspect_image('')
1973-
except docker.errors.NullResource as e:
1974-
self.assertEqual(
1975-
e.args[0], 'image or container param is undefined'
1976-
)
1977-
else:
1978-
self.fail('Command expected NullResource exception')
1971+
def test_inspect_image_undefined_id(self):
1972+
for arg in None, '', {True: True}:
1973+
try:
1974+
self.client.inspect_image(arg)
1975+
except docker.errors.NullResource as e:
1976+
self.assertEqual(
1977+
e.args[0], 'image or container param is undefined'
1978+
)
1979+
else:
1980+
self.fail('Command expected NullResource exception')
19791981

19801982
def test_insert_image(self):
19811983
try:

0 commit comments

Comments
 (0)