Skip to content

Commit ca70bc8

Browse files
committed
Add HTTP protocol to socket
Signed-off-by: Sagi Shnaidman <[email protected]>
1 parent d934f4a commit ca70bc8

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

plugins/module_utils/podman/podman_api.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
DEFAULT_SCHEME = 'http+unix://'
3838

3939

40+
class PodmanAPIError(Exception):
41+
pass
42+
43+
4044
# The following was adapted from some code from docker-py
4145
# https://github.com/docker/docker-py/blob/master/docker/transport/unixconn.py
4246
class UnixHTTPConnection(httplib.HTTPConnection, object):
@@ -80,7 +84,7 @@ def _new_conn(self):
8084
if HAS_REQUESTS:
8185
class UnixAdapter(HTTPAdapter):
8286

83-
def __init__(self, timeout=60, pool_connections=25, *args, **kwargs):
87+
def __init__(self, *args, timeout=60, pool_connections=25, **kwargs):
8488
super(UnixAdapter, self).__init__(*args, **kwargs)
8589
self.timeout = timeout
8690
self.pools = urllib3._collections.RecentlyUsedContainer(
@@ -114,16 +118,18 @@ def close(self):
114118

115119
if HAS_REQUESTS:
116120
class APISession(requests.Session):
117-
def __init__(self, url_scheme=DEFAULT_SCHEME, *args, **kwargs):
121+
def __init__(self, *args, url_scheme=DEFAULT_SCHEME, **kwargs):
118122
super(APISession, self).__init__(*args, **kwargs)
119123
self.mount(url_scheme, UnixAdapter())
120124

121125

122126
class PodmanAPIHTTP:
123-
def __init__(self, base_url):
124-
self.api_url = "".join((DEFAULT_SCHEME,
127+
def __init__(self, base_url, scheme=DEFAULT_SCHEME):
128+
self.api_url = "".join((scheme,
125129
quote(base_url, safe=""),
126130
"/v2.0.0/libpod"))
131+
if scheme == "http://":
132+
self.api_url = "".join((scheme, base_url, "/v2.0.0/libpod"))
127133
self.session = APISession()
128134

129135
def request(self, method, url, **kwargs):
@@ -157,14 +163,17 @@ def options(self, url, **kwargs):
157163
class PodmanAPIClient:
158164
def __init__(self, base_url):
159165
if not HAS_REQUESTS:
160-
raise Exception("requests package is required for podman API")
166+
raise PodmanAPIError("requests package is required for podman API")
161167
socket_opt = urlparse(base_url)
162-
if socket_opt.scheme != "unix":
163-
raise Exception("Scheme %s is not supported! Use %s" % (
168+
if socket_opt.scheme not in ("unix", "http"):
169+
raise PodmanAPIError("Scheme %s is not supported! Use %s" % (
164170
socket_opt.scheme,
165171
DEFAULT_SCHEME
166172
))
167-
self.api = PodmanAPIHTTP(socket_opt.path)
173+
if socket_opt.scheme == "http":
174+
self.api = PodmanAPIHTTP(socket_opt.netloc, "http://")
175+
else:
176+
self.api = PodmanAPIHTTP(socket_opt.path)
168177
self.containers = PodmanAPIContainers(self.api)
169178
self.images = PodmanAPIImages(api=self.api)
170179

@@ -204,8 +213,8 @@ def create(self, **container_data):
204213
)
205214
if response.ok:
206215
return response.json()
207-
raise Exception("Container %s failed to create! Error: %s" %
208-
(container_data.get('name'), response.text))
216+
raise PodmanAPIError("Container %s failed to create! Error: %s" %
217+
(container_data.get('name'), response.text))
209218

210219
def get(self, name):
211220
response = self.api.get(

0 commit comments

Comments
 (0)