Skip to content

Commit

Permalink
[Container] Fix #20280: az container create: Allow environment vari…
Browse files Browse the repository at this point in the history
…able interpolation in container group yaml (#23148)
  • Loading branch information
avinashupadhya99 authored Jul 27, 2022
1 parent 92fe38a commit 98ea29b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/azure-cli/azure/cli/command_modules/container/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys
import threading
import time
import re
try:
import termios
import tty
Expand Down Expand Up @@ -356,6 +357,28 @@ def _get_diagnostics_from_workspace(cli_ctx, log_analytics_workspace):
return None, {}


yaml_env_var_matcher = re.compile(r'.*\$\{([^}^{]+)\}')


def yaml_env_var_constructor(loader, node):
''' Extract the matched value, expand env variable, and replace the match '''
env_matcher = re.compile(r"\$\{([^}^{]+)\}")
value = node.value
match = env_matcher.findall(value)
if match:
full_value = value
for env_var in match:
full_value = full_value.replace(
f'${{{env_var}}}', os.environ.get(env_var, env_var)
)
return full_value
return value


yaml.add_implicit_resolver('!env_var', yaml_env_var_matcher, None, yaml.SafeLoader)
yaml.add_constructor('!env_var', yaml_env_var_constructor, yaml.SafeLoader)


# pylint: disable=unsupported-assignment-operation,protected-access
def _create_update_from_file(cli_ctx, resource_group_name, name, location, file, no_wait):
resource_client = cf_resource(cli_ctx)
Expand Down

0 comments on commit 98ea29b

Please sign in to comment.