-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Vendor secrets and session oenvs (part 3) 🗃️ (#3921)
- Loading branch information
Showing
46 changed files
with
1,545 additions
and
133 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 |
---|---|---|
|
@@ -19,4 +19,5 @@ pytest-instafail | |
pytest-mock | ||
pytest-runner | ||
pytest-sugar | ||
python-dotenv | ||
pyyaml |
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
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
76 changes: 76 additions & 0 deletions
76
packages/models-library/src/models_library/utils/specs_substitution.py
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,76 @@ | ||
from typing import Any, TypeAlias | ||
|
||
import yaml | ||
from models_library.utils.string_substitution import ( | ||
SubstitutionsDict, | ||
TextTemplate, | ||
substitute_all_legacy_identifiers, | ||
) | ||
from pydantic import StrictBool, StrictFloat, StrictInt | ||
|
||
from .string_substitution import SubstitutionsDict, TextTemplate | ||
|
||
# This constraint on substitution values is to avoid | ||
# deserialization issues on the TextTemplate substitution! | ||
SubstitutionValue: TypeAlias = StrictBool | StrictInt | StrictFloat | str | ||
|
||
|
||
class SpecsSubstitutionsResolver: | ||
""" | ||
Resolve specs dict by substituting identifiers | ||
'specs' is defined here as dict[str, Any]. E.g. a docker-compose.yml loaded as a dict are 'specs'. | ||
""" | ||
|
||
def __init__(self, specs: dict[str, Any], upgrade: bool): | ||
self._template = self._create_text_template(specs, upgrade=upgrade) | ||
self._substitutions: SubstitutionsDict = SubstitutionsDict() | ||
|
||
@classmethod | ||
def _create_text_template( | ||
cls, specs: dict[str, Any], *, upgrade: bool | ||
) -> TextTemplate: | ||
# convert to yaml (less symbols as in json) | ||
service_spec_str: str = yaml.safe_dump(specs) | ||
|
||
if upgrade: # legacy | ||
service_spec_str = substitute_all_legacy_identifiers(service_spec_str) | ||
|
||
# template | ||
template = TextTemplate(service_spec_str) | ||
assert template.is_valid() # nosec | ||
|
||
return template | ||
|
||
def get_identifiers(self) -> list[str]: | ||
"""lists identifiers in specs in order of apperance. Can have repetitions""" | ||
return self._template.get_identifiers() | ||
|
||
def get_replaced(self) -> set[str]: | ||
return self._substitutions.used | ||
|
||
@property | ||
def substitutions(self): | ||
return self._substitutions | ||
|
||
def set_substitutions( | ||
self, environs: dict[str, SubstitutionValue] | ||
) -> SubstitutionsDict: | ||
"""NOTE: ONLY targets identifiers declared in the specs""" | ||
identifiers_needed = self.get_identifiers() | ||
|
||
# picks only needed for substitution | ||
self._substitutions = SubstitutionsDict( | ||
{ | ||
identifier: environs[identifier] | ||
for identifier in identifiers_needed | ||
if identifier in environs | ||
} | ||
) | ||
return self._substitutions | ||
|
||
def run(self) -> dict[str, Any]: | ||
new_specs_txt: str = self._template.safe_substitute(self._substitutions) | ||
new_specs: dict = yaml.safe_load(new_specs_txt) | ||
return new_specs |
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
Oops, something went wrong.