Skip to content

environment_utils

Environment utils are used to manage environemnt dependencies in the application.

get_keys_yaml_file()

Retrieves the KEYS_FILE.yml and returns it as a dictionary.

Returns:

Name Type Description
dict dict

The contents of the KEYS_FILE.yml as a dictionary.

Raises:

Type Description
AssertionError

If the KEYS_FILE environment variable is not set or not equal to "KEYS_FILE.yml".

Source code in physical_operations_utils/environment_utils.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def get_keys_yaml_file() -> dict:
    """
    Retrieves the KEYS_FILE.yml and returns it as a dictionary.

    Returns:
        dict: The contents of the KEYS_FILE.yml as a dictionary.

    Raises:
        AssertionError: If the KEYS_FILE environment variable is not set or not equal to "KEYS_FILE.yml".
    """
    # Assert that the environment variable KEYS_FILE exists
    assert "KEYS_FILE" in os.environ, "KEYS_FILE environment variable is not set"
    # Assert it equals KEY_FILE.yml
    keys_file_path = os.environ["KEYS_FILE"]
    assert keys_file_path in [
        "KEYS_FILE.yml",
        "/app/KEYS_FILE.yml",
    ], "KEYS_FILE environment variable is not set to KEYS_FILE.yml or /app/KEYS_FILE.yml"
    with open(keys_file_path, "r") as file:
        keys = yaml.load(file, Loader=yaml.FullLoader)
    return keys

setup_environment()

Sets up the environment by determining the correct Azure Key Vault name based on the ENVIRONMENT environment variable. Returns the current environment.

This function: 1. Retrieves the ENVIRONMENT variable from the system (defaults to "nonprod" if not set). 2. Based on the environment, assigns the appropriate Azure Key Vault name: - "prod""axpotn-pro-appl-phys-kv" - "nonprod""axpotn-non-appl-phys-kv" 3. Stores the Key Vault name in the KEY_VAULT_NAME environment variable. 4. Logs a warning with the selected environment and Key Vault name. 5. Returns the environment name.

Returns:

Name Type Description
str str

The selected environment ("prod" or "nonprod").

Raises:

Type Description
ValueError

If an invalid environment is set.

Example
import os
from physical_operations_utils.EnvironmentUtils import setup_environment

os.environ["ENVIRONMENT"] = "prod"
env = setup_environment()
print(env)  # Output: "prod"
print(os.environ["KEY_VAULT_NAME"])  # Output: "axpotn-pro-appl-phys-kv"
Source code in physical_operations_utils/environment_utils.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def setup_environment() -> str:
    """
    Sets up the environment by determining the correct Azure Key Vault name based on
    the `ENVIRONMENT` environment variable. Returns the current environment.

    This function:
    1. Retrieves the `ENVIRONMENT` variable from the system (defaults to `"nonprod"` if not set).
    2. Based on the environment, assigns the appropriate Azure Key Vault name:
       - `"prod"` → `"axpotn-pro-appl-phys-kv"`
       - `"nonprod"` → `"axpotn-non-appl-phys-kv"`
    3. Stores the Key Vault name in the `KEY_VAULT_NAME` environment variable.
    4. Logs a warning with the selected environment and Key Vault name.
    5. Returns the environment name.

    Returns:
        str: The selected environment (`"prod"` or `"nonprod"`).

    Raises:
        ValueError: If an invalid environment is set.

    Example:
        ```python
        import os
        from physical_operations_utils.EnvironmentUtils import setup_environment

        os.environ["ENVIRONMENT"] = "prod"
        env = setup_environment()
        print(env)  # Output: "prod"
        print(os.environ["KEY_VAULT_NAME"])  # Output: "axpotn-pro-appl-phys-kv"
        ```
    """
    environment = os.getenv("ENVIRONMENT", "nonprod")
    if environment == "prod":
        key_vault_name = "axpotn-pro-appl-phys-kv"
    elif environment == "nonprod":
        key_vault_name = "axpotn-non-appl-phys-kv"
    else:
        raise ValueError(f"Invalid environment: {environment}")
    os.environ["KEY_VAULT_NAME"] = key_vault_name
    _logger.info(f"Environment: {environment}; Key vault name: {key_vault_name}")
    return environment