Skip to content

Commit

Permalink
Merge pull request #33 from affinipay/ca/null/add-template-vars-to-ho…
Browse files Browse the repository at this point in the history
…ok-env

Add template_vars to environment variables when executing a hook
  • Loading branch information
crallen authored Sep 25, 2023
2 parents 7de9725 + 0ec147d commit eb52854
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions tfworker/commands/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ def _run(
self._terraform_bin,
debug=debug,
b64_encode=self._b64_encode,
extra_vars=definition.template_vars,
)
except HookError as e:
click.secho(
Expand Down Expand Up @@ -487,6 +488,7 @@ def _run(
self._terraform_bin,
debug=debug,
b64_encode=self._b64_encode,
extra_vars=definition.template_vars,
)
except HookError as e:
click.secho(
Expand All @@ -504,6 +506,7 @@ def hook_exec(
terraform_path,
debug=False,
b64_encode=False,
extra_vars={},
):
"""
hook_exec executes a hook script.
Expand Down Expand Up @@ -559,9 +562,7 @@ def hook_exec(

if state_value is not None:
if b64_encode:
state_value = base64.b64encode(
state_value.encode("utf-8")
).decode()
state_value = base64.b64encode(state_value.encode("utf-8"))
local_env[f"TF_REMOTE_{state}_{item}".upper()] = state_value

# populate environment with terraform variables
Expand All @@ -578,7 +579,7 @@ def hook_exec(
tf_var[1] = tf_var[1].replace(k, v)

if b64_encode:
tf_var[1] = base64.b64encode(tf_var[1].encode("utf-8")).decode()
tf_var[1] = base64.b64encode(tf_var[1].encode("utf-8"))

local_env[f"TF_VAR_{tf_var[0].upper()}"] = tf_var[1]
else:
Expand All @@ -587,6 +588,11 @@ def hook_exec(
fg="red",
)

for k, v in extra_vars.items():
if b64_encode:
v = base64.b64encode(v.encode("utf-8"))
local_env[f"TF_EXTRA_{k.upper()}"] = v

# execute the hook
(exit_code, stdout, stderr) = pipe_exec(
f"{hook_script} {phase} {command}",
Expand Down
4 changes: 4 additions & 0 deletions tfworker/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ def provider_names(self):
def plan_file(self):
return self._plan_file

@property
def template_vars(self):
return self._template_vars

@plan_file.setter
def plan_file(self, value: Path):
if type(value) not in [PosixPath, WindowsPath, Path]:
Expand Down

0 comments on commit eb52854

Please sign in to comment.