Skip to content

Commit

Permalink
vdk-core: detect non vdk_ prefixed environment values for config (#874)
Browse files Browse the repository at this point in the history
* vdk-core: detect non vdk_ prefixed environment values for config

Currently user need to set VDK_ + config name as environment variable so
VDK  can recognize it. The idea was to avoid conflicts with similarly
named env variables. But this proved to be very error prone where users
tend to forget or get confused on where vdk_ prefix is needed and where
it is not.

This is solved by allowing non-prefixed env variable to be set.

For for configuration "team"

Now if "VDK_TEAM" is set as environment variable is set it is used (as
before)
If it is not set but "TEAM" is set as env variable, it is now used.

Prefixed variables do have higher priority.

Testing Done: unit test

Signed-off-by: Antoni Ivanov <[email protected]>
  • Loading branch information
antoniivanov authored Jun 23, 2022
1 parent 4c1a580 commit 8bc6a96
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class EnvironmentVarsConfigPlugin:
Configuration loaded from environment variables.
"""

def __normalize_key(self, key):
return key.replace("-", "_").replace(".", "_").upper()

@hookimpl(trylast=True)
def vdk_configure(self, config_builder: ConfigurationBuilder) -> None:
"""
Expand All @@ -134,10 +137,17 @@ def vdk_configure(self, config_builder: ConfigurationBuilder) -> None:
)
upper_cased_env = {k.upper(): v for k, v in os.environ.items()}
for key in config_keys:
normalized_key = VDK_ + key.replace(".", "_").upper()
normalized_key_with_vdk_prefix = VDK_ + self.__normalize_key(key)
normalized_key = self.__normalize_key(key)
value = upper_cased_env.get(normalized_key)
if value is not None:
log.debug(f"Found environment variable for key {key}")
value_with_vdk_prefix = upper_cased_env.get(normalized_key_with_vdk_prefix)
if value_with_vdk_prefix is not None:
log.debug(
f"Found environment variable {normalized_key_with_vdk_prefix} for key {key}"
)
config_builder.set_value(key, value_with_vdk_prefix)
elif value is not None:
log.debug(f"Found environment variable {normalized_key} for key {key}")
config_builder.set_value(key, value)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2021 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
import os
from unittest import mock

from vdk.internal.builtin_plugins.config.vdk_config import EnvironmentVarsConfigPlugin
from vdk.internal.core.config import ConfigurationBuilder


@mock.patch.dict(
os.environ,
{"VDK_OPTION": "high", "OPTION": "low", "no_prefixed_option": "value"},
)
def test_vdk_config_env_variable_vdk_prefix_prio():
envVarsConfig = EnvironmentVarsConfigPlugin()
configuration_builder = ConfigurationBuilder()
configuration_builder.add("option", "")
configuration_builder.add("no_prefixed_option", "")

envVarsConfig.vdk_configure(configuration_builder)

assert configuration_builder.build().get_value("option") == "high"
assert configuration_builder.build().get_value("no_prefixed_option") == "value"


@mock.patch.dict(
os.environ,
{"VDK_OPTION_WITH_DOTS": "value1", "no_prefix_option_dots": "value2"},
)
def test_vdk_config_env_variable_dots_replaced():
envVarsConfig = EnvironmentVarsConfigPlugin()
configuration_builder = ConfigurationBuilder()
configuration_builder.add("option.with.dots", "")
configuration_builder.add("no.prefix.option.dots", "")

envVarsConfig.vdk_configure(configuration_builder)

assert configuration_builder.build().get_value("option.with.dots") == "value1"
assert configuration_builder.build().get_value("no.prefix.option.dots") == "value2"


@mock.patch.dict(
os.environ,
{"VDK_OPTION_WITH_DASH": "value1", "no_prefix_option_dash": "value2"},
)
def test_vdk_config_env_variable_dash_replaced():
envVarsConfig = EnvironmentVarsConfigPlugin()
configuration_builder = ConfigurationBuilder()
configuration_builder.add("option-with-dash", "")
configuration_builder.add("no-prefix-option-dash", "")

envVarsConfig.vdk_configure(configuration_builder)

assert configuration_builder.build().get_value("option-with-dash") == "value1"
assert configuration_builder.build().get_value("no-prefix-option-dash") == "value2"

0 comments on commit 8bc6a96

Please sign in to comment.