Skip to content

Commit

Permalink
add initialization through env variables functionality (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
kelkawi-a authored Oct 7, 2024
1 parent 62cf497 commit 52b608a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion resource_sample_py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ packages = [
python = "^3.8"
protobuf = "^3.2.0"
PyYAML = "^6.0"
temporal-lib-py = "^1.3.1"
temporal-lib-py = "^1.4.3"
python-json-logger = "^2.0.4"
urllib3 = "^1.26.16"

Expand Down
17 changes: 15 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,22 @@ def _update(self, event): # noqa: C901
context.update({key: value})

context.update(
{convert_env_var(key): value for key, value in self.config.items() if key not in ["environment"]}
{
convert_env_var(key, prefix="TWC_"): value
for key, value in self.config.items()
if key not in ["environment"]
}
)
context.update({"TWC_PROMETHEUS_PORT": PROMETHEUS_PORT})

context.update(
{
convert_env_var(key, prefix="TEMPORAL_"): value
for key, value in self.config.items()
if key not in ["environment"]
}
)

context.update({"TWC_PROMETHEUS_PORT": PROMETHEUS_PORT, "TEMPORAL_PROMETHEUS_PORT": PROMETHEUS_PORT})

pebble_layer = {
"summary": "temporal worker layer",
Expand Down
4 changes: 4 additions & 0 deletions src/environment_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def process_vault_variables(charm, parsed_environment_data):
raise ValueError(f"Unable to read vault secret `{from_key}` at path `{path}`: {e}") from e
charm_env.update({key_name: secret})

for key in charm_env:
if key.startswith("TEMPORAL_") or key.startswith("TWC_"):
raise ValueError("Environment variables cannot use reserved prefix 'TEMPORAL_' or 'TWC_'")

return charm_env


Expand Down
16 changes: 12 additions & 4 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import asyncio
import logging
import os
import time
from datetime import timedelta
from pathlib import Path
Expand Down Expand Up @@ -133,19 +134,26 @@ def unseal_vault(client, endpoint: str, root_token: str, unseal_key: str):
client.sys.submit_unseal_key(unseal_key)


async def run_sample_workflow(ops_test: OpsTest, workflow_type=None):
async def run_sample_workflow(ops_test: OpsTest, workflow_type=None, use_env_variables=False):
"""Connect to a client and runs a basic Temporal workflow.
Args:
ops_test: PyTest object.
workflow_type: set to "vault" to test Vault workflow.
use_env_variables: whether to initialize Temporal client using environment variables or declared parameters.
"""
url = await get_application_url(ops_test, application=APP_NAME_SERVER, port=7233)
logger.info("running workflow on app address: %s", url)

client = await Client.connect(
Options(host=url, queue=BASE_WORKER_CONFIG["queue"], namespace=BASE_WORKER_CONFIG["namespace"])
)
if use_env_variables:
os.environ["TEMPORAL_HOST"] = url
os.environ["TEMPORAL_NAMESPACE"] = BASE_WORKER_CONFIG["namespace"]
os.environ["TEMPORAL_QUEUE"] = BASE_WORKER_CONFIG["queue"]
client = await Client.connect(Options())
else:
client = await Client.connect(
Options(host=url, queue=BASE_WORKER_CONFIG["queue"], namespace=BASE_WORKER_CONFIG["namespace"])
)

workflow_name = "GreetingWorkflow"
if workflow_type == "vault":
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ class TestDeployment:

async def test_basic_client(self, ops_test: OpsTest):
"""Connects a client and runs a basic Temporal workflow."""
await run_sample_workflow(ops_test)
await run_sample_workflow(ops_test, use_env_variables=False)
await run_sample_workflow(ops_test, use_env_variables=True)
28 changes: 28 additions & 0 deletions tests/unit/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@
}

WANT_ENV = {
"TEMPORAL_AUTH_PROVIDER": "candid",
"TEMPORAL_CANDID_PRIVATE_KEY": "test-private-key",
"TEMPORAL_CANDID_PUBLIC_KEY": "test-public-key",
"TEMPORAL_CANDID_URL": "test-url",
"TEMPORAL_CANDID_USERNAME": "test-username",
"TEMPORAL_ENCRYPTION_KEY": "",
"TEMPORAL_HOST": "test-host",
"TEMPORAL_LOG_LEVEL": "debug",
"TEMPORAL_NAMESPACE": "test-namespace",
"TEMPORAL_PROMETHEUS_PORT": 9000,
"TEMPORAL_OIDC_AUTH_CERT_URL": "",
"TEMPORAL_OIDC_AUTH_TYPE": "",
"TEMPORAL_OIDC_AUTH_URI": "",
"TEMPORAL_OIDC_CLIENT_CERT_URL": "",
"TEMPORAL_OIDC_CLIENT_EMAIL": "",
"TEMPORAL_OIDC_CLIENT_ID": "",
"TEMPORAL_OIDC_PRIVATE_KEY": "",
"TEMPORAL_OIDC_PRIVATE_KEY_ID": "",
"TEMPORAL_OIDC_PROJECT_ID": "",
"TEMPORAL_OIDC_TOKEN_URI": "",
"TEMPORAL_QUEUE": "test-queue",
"TEMPORAL_SENTRY_DSN": "",
"TEMPORAL_SENTRY_ENVIRONMENT": "",
"TEMPORAL_SENTRY_RELEASE": "",
"TEMPORAL_SENTRY_SAMPLE_RATE": 1.0,
"TEMPORAL_SENTRY_REDACT_PARAMS": False,
"TEMPORAL_TLS_ROOT_CAS": "",
# The below are kept for backwards-compatibility
"TWC_AUTH_PROVIDER": "candid",
"TWC_CANDID_PRIVATE_KEY": "test-private-key",
"TWC_CANDID_PUBLIC_KEY": "test-public-key",
Expand Down
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ commands =
isort --check-only --diff {[vars]all_path}
black --check --diff {[vars]all_path}
mypy {[vars]all_path} --ignore-missing-imports --install-types --non-interactive --explicit-package-bases
pylint {[vars]all_path} --disable=E0401,W1203,W0613,W0718,R0903,W1514,C0301,C0103,C0104,R0914,R0915,R0801,W0212,W0719,W0511,R0913,R0912,R0902
pylint {[vars]all_path} --disable=E0401,W1203,W0613,W0718,R0903,W1514,C0301,C0103,C0104,R0914,R0915,R0801,W0212,W0719,W0511,R0913,R0912,R0902,R0917

[testenv:unit]
description = Run unit tests
Expand Down Expand Up @@ -102,7 +102,7 @@ deps =
juju==3.5.2.0
pytest==7.1.3
pytest-operator==0.31.1
temporal-lib-py==1.3.1
temporal-lib-py==1.4.3
pytest-asyncio==0.21
poetry==1.8.3
-r{toxinidir}/requirements.txt
Expand All @@ -118,7 +118,7 @@ deps =
juju==3.5.2.0
pytest==7.1.3
pytest-operator==0.31.1
temporal-lib-py==1.3.1
temporal-lib-py==1.4.3
pytest-asyncio==0.21
poetry==1.8.3
hvac==2.3.0
Expand Down

0 comments on commit 52b608a

Please sign in to comment.