Skip to content

Commit

Permalink
Add HTTP protocol to socket
Browse files Browse the repository at this point in the history
Signed-off-by: Sagi Shnaidman <[email protected]>
  • Loading branch information
sshnaidm committed Jun 13, 2024
1 parent d934f4a commit ca70bc8
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions plugins/module_utils/podman/podman_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
DEFAULT_SCHEME = 'http+unix://'


class PodmanAPIError(Exception):
pass


# The following was adapted from some code from docker-py
# https://github.com/docker/docker-py/blob/master/docker/transport/unixconn.py
class UnixHTTPConnection(httplib.HTTPConnection, object):
Expand Down Expand Up @@ -80,7 +84,7 @@ def _new_conn(self):
if HAS_REQUESTS:
class UnixAdapter(HTTPAdapter):

def __init__(self, timeout=60, pool_connections=25, *args, **kwargs):
def __init__(self, *args, timeout=60, pool_connections=25, **kwargs):
super(UnixAdapter, self).__init__(*args, **kwargs)
self.timeout = timeout
self.pools = urllib3._collections.RecentlyUsedContainer(
Expand Down Expand Up @@ -114,16 +118,18 @@ def close(self):

if HAS_REQUESTS:
class APISession(requests.Session):
def __init__(self, url_scheme=DEFAULT_SCHEME, *args, **kwargs):
def __init__(self, *args, url_scheme=DEFAULT_SCHEME, **kwargs):
super(APISession, self).__init__(*args, **kwargs)
self.mount(url_scheme, UnixAdapter())


class PodmanAPIHTTP:
def __init__(self, base_url):
self.api_url = "".join((DEFAULT_SCHEME,
def __init__(self, base_url, scheme=DEFAULT_SCHEME):
self.api_url = "".join((scheme,
quote(base_url, safe=""),
"/v2.0.0/libpod"))
if scheme == "http://":
self.api_url = "".join((scheme, base_url, "/v2.0.0/libpod"))
self.session = APISession()

def request(self, method, url, **kwargs):
Expand Down Expand Up @@ -157,14 +163,17 @@ def options(self, url, **kwargs):
class PodmanAPIClient:
def __init__(self, base_url):
if not HAS_REQUESTS:
raise Exception("requests package is required for podman API")
raise PodmanAPIError("requests package is required for podman API")
socket_opt = urlparse(base_url)
if socket_opt.scheme != "unix":
raise Exception("Scheme %s is not supported! Use %s" % (
if socket_opt.scheme not in ("unix", "http"):
raise PodmanAPIError("Scheme %s is not supported! Use %s" % (
socket_opt.scheme,
DEFAULT_SCHEME
))
self.api = PodmanAPIHTTP(socket_opt.path)
if socket_opt.scheme == "http":
self.api = PodmanAPIHTTP(socket_opt.netloc, "http://")
else:
self.api = PodmanAPIHTTP(socket_opt.path)
self.containers = PodmanAPIContainers(self.api)
self.images = PodmanAPIImages(api=self.api)

Expand Down Expand Up @@ -204,8 +213,8 @@ def create(self, **container_data):
)
if response.ok:
return response.json()
raise Exception("Container %s failed to create! Error: %s" %
(container_data.get('name'), response.text))
raise PodmanAPIError("Container %s failed to create! Error: %s" %
(container_data.get('name'), response.text))

def get(self, name):
response = self.api.get(
Expand Down

0 comments on commit ca70bc8

Please sign in to comment.