-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use EnvConfigValue for passing env-configured arguments to services (#…
…1704) Contributes to #1701 ## By Submitting this PR I confirm: - I am familiar with the [Contributing Guidelines](https://github.com/nv-morpheus/Morpheus/blob/main/docs/source/developer_guide/contributing.md). - When the PR is ready for review, new or existing tests cover these changes. - When the PR is ready for review, the documentation is up to date with these changes. Authors: - Christopher Harris (https://github.com/cwharris) Approvers: - Michael Demoret (https://github.com/mdemoret-nv) URL: #1704
- Loading branch information
Showing
5 changed files
with
283 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
from abc import ABC | ||
from enum import Enum | ||
|
||
|
||
class EnvConfigValueSource(Enum): | ||
ENV_DEFAULT = 1 | ||
CONSTRUCTOR = 2 | ||
ENV_OVERRIDE = 3 | ||
|
||
|
||
class EnvConfigValue(ABC): | ||
""" | ||
A wrapper for a string used as a configuration value which can be loaded from the system environment or injected via | ||
the constructor. This class should be subclassed and the class fields `_ENV_KEY` and `_ENV_KEY_OVERRIDE` can be set | ||
to enable environment-loading functionality. Convienience properties are available to check from where the value was | ||
loaded. | ||
""" | ||
|
||
_ENV_KEY: str | None = None | ||
_ENV_KEY_OVERRIDE: str | None = None | ||
_ALLOW_NONE: bool = False | ||
|
||
def __init__(self, value: str | None = None, use_env: bool = True): | ||
""" | ||
Parameters | ||
---------- | ||
value : str, optional | ||
The value to be contained in the EnvConfigValue. If the value is `None`, an attempt will be made to load it | ||
from the environment using `_ENV_KEY`. if the `_ENV_KEY_OVERRIDE` field is not `None`, an attempt will be | ||
made to load that environment variable in place of the passed-in value. | ||
use_env : bool | ||
If False, all environment-loading logic will be bypassed and the passed-in value will be used as-is. | ||
defaults to True. | ||
""" | ||
|
||
self._source = EnvConfigValueSource.CONSTRUCTOR | ||
|
||
if use_env: | ||
if value is None and self.__class__._ENV_KEY is not None: | ||
value = os.environ.get(self.__class__._ENV_KEY, None) | ||
self._source = EnvConfigValueSource.ENV_DEFAULT | ||
|
||
if self.__class__._ENV_KEY_OVERRIDE is not None and self.__class__._ENV_KEY_OVERRIDE in os.environ: | ||
value = os.environ[self.__class__._ENV_KEY_OVERRIDE] | ||
self._source = EnvConfigValueSource.ENV_OVERRIDE | ||
|
||
if not self.__class__._ALLOW_NONE and value is None: | ||
|
||
message = ("value must not be None, but provided value was None and no environment-based default or " | ||
"override was found.") | ||
|
||
if self.__class__._ENV_KEY is None: | ||
raise ValueError(message) | ||
|
||
raise ValueError( | ||
f"{message} Try passing a value to the constructor, or setting the `{self.__class__._ENV_KEY}` " | ||
"environment variable.") | ||
|
||
else: | ||
if not self.__class__._ALLOW_NONE and value is None: | ||
raise ValueError("value must not be none") | ||
|
||
assert isinstance(value, str) or value is None | ||
|
||
self._value = value | ||
self._use_env = use_env | ||
|
||
@property | ||
def source(self) -> EnvConfigValueSource: | ||
return self._source | ||
|
||
@property | ||
def use_env(self) -> bool: | ||
return self._use_env | ||
|
||
@property | ||
def value(self) -> str | None: | ||
return self._value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from morpheus.utils.env_config_value import EnvConfigValue | ||
from morpheus.utils.env_config_value import EnvConfigValueSource | ||
|
||
|
||
class EnvDrivenValue(EnvConfigValue): | ||
_ENV_KEY = "DEFAULT" | ||
_ENV_KEY_OVERRIDE = "OVERRIDE" | ||
|
||
|
||
def test_env_driven_value(): | ||
with mock.patch.dict(os.environ, clear=True, values={"DEFAULT": "default.api.com"}): | ||
|
||
config = EnvDrivenValue() | ||
assert config.value == "default.api.com" | ||
assert config.source == EnvConfigValueSource.ENV_DEFAULT | ||
assert config.use_env | ||
|
||
with pytest.raises(ValueError): | ||
config = EnvDrivenValue(use_env=False) | ||
|
||
config = EnvDrivenValue("api.com") | ||
assert config.value == "api.com" | ||
assert config.source == EnvConfigValueSource.CONSTRUCTOR | ||
assert config.use_env | ||
|
||
with mock.patch.dict(os.environ, clear=True, values={"OVERRIDE": "override.api.com"}): | ||
|
||
config = EnvDrivenValue("api.com") | ||
assert config.value == "override.api.com" | ||
assert config.source == EnvConfigValueSource.ENV_OVERRIDE | ||
assert config.use_env | ||
|
||
config = EnvDrivenValue("api.com", use_env=False) | ||
assert config.value == "api.com" | ||
assert config.source == EnvConfigValueSource.CONSTRUCTOR | ||
assert not config.use_env | ||
|
||
|
||
class EnvDriverValueNoOverride(EnvConfigValue): | ||
_ENV_KEY = "DEFAULT" | ||
|
||
|
||
def test_env_driven_value_no_override(): | ||
with mock.patch.dict(os.environ, clear=True, values={"DEFAULT": "default.api.com"}): | ||
|
||
config = EnvDriverValueNoOverride() | ||
assert config.value == "default.api.com" | ||
assert config.source == EnvConfigValueSource.ENV_DEFAULT | ||
assert config.use_env | ||
|
||
with pytest.raises(ValueError): | ||
config = EnvDriverValueNoOverride(use_env=False) | ||
|
||
config = EnvDriverValueNoOverride("api.com") | ||
assert config.value == "api.com" | ||
assert config.source == EnvConfigValueSource.CONSTRUCTOR | ||
assert config.use_env | ||
|
||
with mock.patch.dict(os.environ, clear=True, values={"OVERRIDE": "override.api.com"}): | ||
|
||
config = EnvDriverValueNoOverride("api.com") | ||
assert config.value == "api.com" | ||
assert config.source == EnvConfigValueSource.CONSTRUCTOR | ||
assert config.use_env | ||
|
||
|
||
class EnvDrivenValueNoDefault(EnvConfigValue): | ||
_ENV_KEY_OVERRIDE = "OVERRIDE" | ||
|
||
|
||
def test_env_driven_value_no_default(): | ||
with mock.patch.dict(os.environ, clear=True, values={"DEFAULT": "default.api.com"}): | ||
|
||
with pytest.raises(ValueError): | ||
config = EnvDrivenValueNoDefault() | ||
|
||
config = EnvDrivenValueNoDefault("api.com") | ||
assert config.value == "api.com" | ||
assert config.source == EnvConfigValueSource.CONSTRUCTOR | ||
assert config.use_env | ||
|
||
with mock.patch.dict(os.environ, clear=True, values={"OVERRIDE": "override.api.com"}): | ||
|
||
config = EnvDrivenValueNoDefault("api.com") | ||
assert config.value == "override.api.com" | ||
assert config.source == EnvConfigValueSource.ENV_OVERRIDE | ||
assert config.use_env | ||
|
||
|
||
class EnvOptionalValue(EnvConfigValue): | ||
_ALLOW_NONE = True | ||
|
||
|
||
def test_env_optional_value(): | ||
config = EnvOptionalValue() | ||
assert config.value is None | ||
assert config.source == EnvConfigValueSource.CONSTRUCTOR | ||
assert config.use_env |