Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions localstack-wiremock/localstack_wiremock/extension.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import os

from localstack.utils.container_utils.container_client import Util
from localstack_wiremock.utils.docker import ProxiedDockerContainerExtension


# Environment variable for WireMock Cloud API token - note: if this value is specified, then the
# `wiremock/wiremock-runner` image is being used, otherwise the `wiremock/wiremock` OSS image.
ENV_WIREMOCK_API_TOKEN = "WIREMOCK_API_TOKEN"
# container port for WireMock endpoint - TODO make configurable over time
PORT = 8080


class WireMockExtension(ProxiedDockerContainerExtension):
name = "localstack-wiremock"

HOST = "wiremock.<domain>"
# name of the Docker image to spin up
# name of the OSS Docker image
DOCKER_IMAGE = "wiremock/wiremock"
# name of the WireMock Cloud runner Docker image
DOCKER_IMAGE_RUNNER = "wiremock/wiremock-runner"
# name of the container
CONTAINER_NAME = "ls-wiremock"

def __init__(self):
env_vars = {}
image_name = self.DOCKER_IMAGE
kwargs = {}
if api_token := os.getenv(ENV_WIREMOCK_API_TOKEN):
env_vars["WMC_ADMIN_PORT"] = str(PORT)
# TODO remove?
# env_vars["WMC_DEFAULT_MODE"] = "record-many"
env_vars["WMC_API_TOKEN"] = api_token
env_vars["WMC_RUNNER_ENABLED"] = "true"
image_name = self.DOCKER_IMAGE_RUNNER
settings_file = Util.mountable_tmp_file()
# TODO: set configs in YAML file
kwargs["volumes"] = ([(settings_file, "/work/.wiremock/wiremock.yaml")],)
super().__init__(
image_name=self.DOCKER_IMAGE,
container_ports=[8080],
image_name=image_name,
container_ports=[PORT],
container_name=self.CONTAINER_NAME,
host=self.HOST,
**kwargs,
)
11 changes: 10 additions & 1 deletion localstack-wiremock/localstack_wiremock/utils/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from localstack.utils.docker_utils import DOCKER_CLIENT
from localstack.extensions.api import Extension, http
from localstack.http import Request
from localstack.utils.container_utils.container_client import PortMappings
from localstack.utils.container_utils.container_client import (
PortMappings,
SimpleVolumeBind,
)
from localstack.utils.net import get_addressable_container_host
from localstack.utils.sync import retry
from rolo import route
Expand Down Expand Up @@ -44,6 +47,9 @@ class ProxiedDockerContainerExtension(Extension):
http2_ports: list[int] | None
"""List of ports for which HTTP2 proxy forwarding into the container should be enabled."""

volumes: list[SimpleVolumeBind] | None = (None,)
"""Optional volumes to mount into the container host."""

def __init__(
self,
image_name: str,
Expand All @@ -54,6 +60,7 @@ def __init__(
command: list[str] | None = None,
request_to_port_router: Callable[[Request], int] | None = None,
http2_ports: list[int] | None = None,
volumes: list[SimpleVolumeBind] | None = None,
):
self.image_name = image_name
self.container_ports = container_ports
Expand All @@ -63,6 +70,7 @@ def __init__(
self.command = command
self.request_to_port_router = request_to_port_router
self.http2_ports = http2_ports
self.volumes = volumes

def update_gateway_routes(self, router: http.Router[http.RouteHandler]):
if self.path:
Expand Down Expand Up @@ -106,6 +114,7 @@ def start_container(self) -> None:
remove=True,
name=container_name,
ports=ports,
volumes=self.volumes,
**kwargs,
)
except Exception as e:
Expand Down