Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
Add support to alias envs and build_args
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodaher committed Mar 26, 2021
1 parent 05c53fd commit 29cc528
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
55 changes: 40 additions & 15 deletions flamingo/services/alias_engine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import re
from dataclasses import field, dataclass
from typing import Generator, Any, Tuple

from sanic_rest.exceptions import ValidationError
Expand All @@ -8,17 +10,50 @@
logger = logging.getLogger()


ALIAS_REGEX = r"\${(?P<alias_to>.*)}"


@dataclass
class ReplacementEngine:
replacements: KeyValue = field(default_factory=dict)

def add(self, items: KeyValue):
for k, v in items.items():
if AliasEngine.is_virtual(value=v):
continue
self.replacements[k] = v

def get(self, value):
return self.replacements[value]

def replace(self, virtual_value):
alias_to = re.match(ALIAS_REGEX, virtual_value).group('alias_to')

try:
replace_with = self.replacements[alias_to]
except KeyError as e:
raise ValidationError(f"Could not find the referenced env var for {virtual_value.as_kv}") from e

return re.sub(ALIAS_REGEX, replace_with, virtual_value)


class AliasEngine:
def __init__(self, items: KeyValue, replacements: KeyValue = None):
def __init__(self, items: KeyValue, replacements: ReplacementEngine = None):
super().__init__()
self._concrete: KeyValue = dict()
self._virtual: KeyValue = dict()
self._replacements: KeyValue = replacements or items

if not replacements:
replacements = ReplacementEngine()
replacements.add(items=items)
self._replacements = replacements

self.extend(items)

def is_virtual(self, value):
return False # TODO We need an use case for this, the syntax must not be defined too
@classmethod
def is_virtual(cls, value):
matches = re.match(ALIAS_REGEX, str(value))
return bool(matches)

def append(self, key: str, value: Any) -> None:
container = self._virtual if self.is_virtual(value) else self._concrete
Expand All @@ -33,14 +68,4 @@ def extend(self, items: KeyValue) -> None:
def items(self) -> Generator[Tuple[str, Any], None, None]:
yield from self._concrete.items()
for key, virtual_value in self._virtual.items():
yield key, self._to_concrete(virtual_value=virtual_value)

def _to_concrete(self, virtual_value) -> Any:
alias_to = virtual_value[1:]

try:
value = self._replacements[alias_to]
except KeyError as e:
raise ValidationError(f"Could not find the referenced env var for {virtual_value.as_kv}") from e

return value
yield key, self._replacements.replace(virtual_value=virtual_value)
12 changes: 5 additions & 7 deletions flamingo/services/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
from gcp_pilot.build import CloudBuild, Substitutions
from gcp_pilot.exceptions import NotFound
from gcp_pilot.run import CloudRun
from gcp_pilot.dns import CloudDNS, RecordType
from google.cloud.devtools import cloudbuild_v1

import settings
from models.app import App
from models.base import KeyValue
from models.buildpack import Target
from models.schedule import ScheduledInvocation
from services.alias_engine import AliasEngine
from services.alias_engine import AliasEngine, ReplacementEngine

logger = logging.getLogger()

Expand Down Expand Up @@ -63,10 +61,10 @@ def _get_env_and_build_args(self) -> Tuple[KeyValue, KeyValue]:
all_env_vars = {var.key: var.value for var in self.app.get_all_env_vars()}
all_build_args = self.app.get_all_build_args()

replacements = dict()
replacements.update(self._setup_params)
replacements.update(all_env_vars)
replacements.update(all_build_args)
replacements = ReplacementEngine()
replacements.add(items=self._setup_params)
replacements.add(items=all_env_vars)
replacements.add(items=all_build_args)

env_var_engine = AliasEngine(
items=all_env_vars,
Expand Down

0 comments on commit 29cc528

Please sign in to comment.