Skip to content

Commit 5e331a5

Browse files
committed
Merge branch 'aanand-url-variable-args'
2 parents 02f330d + 29b12cf commit 5e331a5

File tree

4 files changed

+221
-140
lines changed

4 files changed

+221
-140
lines changed

docker/client.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,22 @@ def _get(self, url, **kwargs):
111111
def _delete(self, url, **kwargs):
112112
return self.delete(url, **self._set_request_timeout(kwargs))
113113

114-
def _url(self, pathfmt, resource_id=None, versioned_api=True):
115-
if resource_id and not isinstance(resource_id, six.string_types):
116-
raise ValueError(
117-
'Expected a resource ID string but found {0} ({1}) '
118-
'instead'.format(resource_id, type(resource_id))
119-
)
120-
elif resource_id:
121-
resource_id = six.moves.urllib.parse.quote_plus(resource_id)
114+
def _url(self, pathfmt, *args, **kwargs):
115+
for arg in args:
116+
if not isinstance(arg, six.string_types):
117+
raise ValueError(
118+
'Expected a string but found {0} ({1}) '
119+
'instead'.format(arg, type(arg))
120+
)
121+
122+
args = map(six.moves.urllib.parse.quote_plus, args)
122123

123-
if versioned_api:
124+
if kwargs.get('versioned_api', True):
124125
return '{0}/v{1}{2}'.format(
125-
self.base_url, self._version, pathfmt.format(resource_id)
126+
self.base_url, self._version, pathfmt.format(*args)
126127
)
127128
else:
128-
return '{0}{1}'.format(self.base_url, pathfmt.format(resource_id))
129+
return '{0}{1}'.format(self.base_url, pathfmt.format(*args))
129130

130131
def _raise_for_status(self, response, explanation=None):
131132
"""Raises stored :class:`APIError`, if one occurred."""

docker/utils/utils.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ def create_host_config(
457457
restart_policy=None, cap_add=None, cap_drop=None, devices=None,
458458
extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None,
459459
security_opt=None, ulimits=None, log_config=None, mem_limit=None,
460-
memswap_limit=None, cgroup_parent=None, group_add=None, version=None
460+
memswap_limit=None, cgroup_parent=None, group_add=None, cpu_quota=None,
461+
cpu_period=None, version=None
461462
):
462463
host_config = {}
463464

@@ -518,7 +519,7 @@ def create_host_config(
518519
host_config['Devices'] = parse_devices(devices)
519520

520521
if group_add:
521-
if compare_version(version, '1.20') < 0:
522+
if version_lt(version, '1.20'):
522523
raise errors.InvalidVersion(
523524
'group_add param not supported for API version < 1.20'
524525
)
@@ -601,6 +602,30 @@ def create_host_config(
601602
log_config = LogConfig(**log_config)
602603
host_config['LogConfig'] = log_config
603604

605+
if cpu_quota:
606+
if not isinstance(cpu_quota, int):
607+
raise TypeError(
608+
'Invalid type for cpu_quota param: expected int but'
609+
' found {0}'.format(type(cpu_quota))
610+
)
611+
if version_lt(version, '1.19'):
612+
raise errors.InvalidVersion(
613+
'cpu_quota param not supported for API version < 1.19'
614+
)
615+
host_config['CpuQuota'] = cpu_quota
616+
617+
if cpu_period:
618+
if not isinstance(cpu_period, int):
619+
raise TypeError(
620+
'Invalid type for cpu_period param: expected int but'
621+
' found {0}'.format(type(cpu_period))
622+
)
623+
if version_lt(version, '1.19'):
624+
raise errors.InvalidVersion(
625+
'cpu_period param not supported for API version < 1.19'
626+
)
627+
host_config['CpuPeriod'] = cpu_period
628+
604629
return host_config
605630

606631

tests/test.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ def fake_put(self, url, *args, **kwargs):
104104
def fake_delete(self, url, *args, **kwargs):
105105
return fake_request('DELETE', url, *args, **kwargs)
106106

107-
url_prefix = 'http+docker://localunixsocket/v{0}/'.format(
107+
url_base = 'http+docker://localunixsocket/'
108+
url_prefix = '{0}v{1}/'.format(
109+
url_base,
108110
docker.constants.DEFAULT_DOCKER_API_VERSION)
109111

110112

@@ -174,6 +176,14 @@ def test_url_valid_resource(self):
174176
url, '{0}{1}'.format(url_prefix, 'hello/somename/world')
175177
)
176178

179+
url = self.client._url(
180+
'/hello/{0}/world/{1}', 'somename', 'someothername'
181+
)
182+
self.assertEqual(
183+
url,
184+
'{0}{1}'.format(url_prefix, 'hello/somename/world/someothername')
185+
)
186+
177187
url = self.client._url('/hello/{0}/world', '/some?name')
178188
self.assertEqual(
179189
url, '{0}{1}'.format(url_prefix, 'hello/%2Fsome%3Fname/world')
@@ -187,8 +197,13 @@ def test_url_no_resource(self):
187197
url = self.client._url('/simple')
188198
self.assertEqual(url, '{0}{1}'.format(url_prefix, 'simple'))
189199

190-
url = self.client._url('/simple', None)
191-
self.assertEqual(url, '{0}{1}'.format(url_prefix, 'simple'))
200+
def test_url_unversioned_api(self):
201+
url = self.client._url(
202+
'/hello/{0}/world', 'somename', versioned_api=False
203+
)
204+
self.assertEqual(
205+
url, '{0}{1}'.format(url_base, 'hello/somename/world')
206+
)
192207

193208
#########################
194209
# INFORMATION TESTS #
@@ -202,6 +217,15 @@ def test_version(self):
202217
timeout=DEFAULT_TIMEOUT_SECONDS
203218
)
204219

220+
def test_version_no_api_version(self):
221+
self.client.version(False)
222+
223+
fake_request.assert_called_with(
224+
'GET',
225+
url_base + 'version',
226+
timeout=DEFAULT_TIMEOUT_SECONDS
227+
)
228+
205229
def test_retrieve_server_version(self):
206230
client = docker.Client(version="auto")
207231
self.assertTrue(isinstance(client._version, six.string_types))

0 commit comments

Comments
 (0)