diff options
| author | shin- <joffrey@dotcloud.com> | 2013-12-09 17:45:12 +0100 |
|---|---|---|
| committer | shin- <joffrey@dotcloud.com> | 2013-12-09 17:45:12 +0100 |
| commit | 89f2c5891815b6485fd805151a568f82449970bb (patch) | |
| tree | 45df82daaabb1a43cea6cdeebc1dc0a353321057 /docker/utils/utils.py | |
| parent | ef615f5549c98d3deda4333853d5a545276c947a (diff) | |
| download | docker-py-89f2c5891815b6485fd805151a568f82449970bb.tar.gz | |
Improved port binding conversion rules, fixed bugs, added unit tests
Diffstat (limited to 'docker/utils/utils.py')
| -rw-r--r-- | docker/utils/utils.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 1aa86aa..8fd9e94 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -60,3 +60,37 @@ def ping(url): return res.status >= 400 except Exception: return False + + +def _convert_port_binding(binding): + result = {'HostIp': '', 'HostPort': ''} + if isinstance(binding, tuple): + if len(binding) == 2: + result['HostPort'] = binding[1] + result['HostIp'] = binding[0] + elif isinstance(binding[0], six.string_types): + result['HostIp'] = binding[0] + else: + result['HostPort'] = binding[0] + else: + result['HostPort'] = binding + + if result['HostPort'] is None: + result['HostPort'] = '' + else: + result['HostPort'] = str(result['HostPort']) + + return result + + +def convert_port_bindings(port_bindings): + result = {} + for k, v in six.iteritems(port_bindings): + key = str(k) + if '/' not in key: + key = key + '/tcp' + if isinstance(v, list): + result[key] = [_convert_port_binding(binding) for binding in v] + else: + result[key] = [_convert_port_binding(v)] + return result |
