Skip to content

Commit

Permalink
Support user provided literal vars
Browse files Browse the repository at this point in the history
  • Loading branch information
VJftw committed Aug 27, 2022
1 parent d946eb8 commit 3f1c5c3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
16 changes: 14 additions & 2 deletions build/defs/terraform.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ $TOOLS -vvvvv module registry \\
def terraform_root(
name: str,
srcs: list,
vars: dict = {},
var_files: list = [],
modules: list = [],
toolchain: str = None,
Expand All @@ -158,7 +159,8 @@ def terraform_root(
Args:
name: The name of the build rule.
srcs: The source Terraform files for the root module.
vars: The Terraform var files passed into the root module.
vars: The literal Terraform vars to pass into the root module.
var_files: The Terraform var files passed into the root module.
modules: The Terraform modules that the srcs use.
toolchain: The Terraform toolchain to use with against the srcs.
labels: The additonal labels to add to the build rule.
Expand All @@ -169,7 +171,16 @@ def terraform_root(
post_workspace_cmd: Additional commands to run to execute after executing Terraform commands.
"""
_validate_config()
# build a Terraform root workspace

if vars:
json_vars = json(vars)
vars_file=genrule(
name = f"{name}_vars",
outs = [f"{name}_vars.tfvars.json"],
cmd = f"echo '{json_vars}' > $OUTS",
)
var_files += [vars_file]

var_file_flags = [f"--var_files=\"$(location {var_file})\"" for var_file in var_files]
var_files_cmd = " ".join(var_file_flags)

Expand All @@ -179,6 +190,7 @@ def terraform_root(
if CONFIG.TERRAFORM.EXTRA_TERRAFORM_ROOT_SRC:
srcs += [CONFIG.TERRAFORM.EXTRA_TERRAFORM_ROOT_SRC]

# build a Terraform root workspace
root=genrule(
name = f"_{name}_root",
outs = [f"{name}_root"],
Expand Down
12 changes: 12 additions & 0 deletions example/0.12/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ terraform_root(
name = "example",
srcs = [
"main.tf",
"outputs.tf",
"variables.tf",
],
modules = [
Expand All @@ -12,4 +13,15 @@ terraform_root(
],
toolchain = "//example/third_party/terraform:0.12",
var_files = ["my_vars.tfvars"],
vars = {
"single_value": "e",
"list_value": [
"e_1",
"e_2",
],
"dict_value": {
"e_1": "e_1",
"e_2": "e_2",
},
},
)
8 changes: 8 additions & 0 deletions example/0.12/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
output "vars" {
value = {
"hello" = var.hello,
"single_value" = var.single_value,
"list_value" = var.list_value,
"dict_value" = var.dict_value,
}
}
15 changes: 15 additions & 0 deletions example/0.12/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
variable "hello" {
description = "an example tf var"
}

variable "single_value" {
type = string
}

variable "list_value" {
type = list
}

variable "dict_value" {
type = object({
e_1 = string
e_2 = string
})
}

0 comments on commit 3f1c5c3

Please sign in to comment.