forked from dvulpe/bazel-terraform-rules
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module path to working dir provider
Allows modification of a module prior to apply. Added test to verify capability.
- Loading branch information
1 parent
23a2d6b
commit f9e3175
Showing
4 changed files
with
98 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
load("@tf_modules//rules:module.bzl", "terraform_module") | ||
load("@tf_modules//rules:terraform.bzl", "terraform_working_directory") | ||
load(":test.bzl", "execute_test") | ||
|
||
terraform_module( | ||
name = "provider", | ||
srcs = ["main.tf"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
terraform_working_directory( | ||
name = "terraform", | ||
module = ":provider", | ||
allow_provider_download = True, | ||
) | ||
|
||
execute_test( | ||
name = "terraform_test", | ||
terraform_working_directory = ":terraform", | ||
) |
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,13 @@ | ||
terraform { | ||
required_providers { | ||
local = { | ||
source = "hashicorp/local" | ||
version = ">= 2.4.1" | ||
} | ||
} | ||
} | ||
|
||
resource "local_file" "foo" { | ||
source = "${path.module}/content.txt" | ||
filename = "${path.module}/foo.bar" | ||
} |
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,56 @@ | ||
load("@tf_modules//rules:terraform.bzl", "TerraformWorkingDirInfo") | ||
|
||
def _execute_test_impl(ctx): | ||
output = ctx.actions.declare_file(ctx.label.name + ".sh") | ||
working_dir = ctx.attr.terraform_working_directory[DefaultInfo] | ||
working_dir_info = ctx.attr.terraform_working_directory[TerraformWorkingDirInfo] | ||
|
||
ctx.actions.write( | ||
output = output, | ||
content = """ | ||
#!/bin/bash | ||
MODULE_DIR="./$(dirname {terraform_binary})/{module_path}" | ||
echo "hello, world" > $MODULE_DIR/content.txt | ||
OUT=$(./{terraform_binary} apply -auto-approve -no-color) | ||
if [ $? -ne 0 ]; | ||
then | ||
echo 'Plan failed'; | ||
exit 1 | ||
fi | ||
PASS=1 | ||
if [[ $(cat "$MODULE_DIR/foo.bar") != "hello, world" ]]; then | ||
echo "FAIL: content of generated file was not as expected" | ||
PASS=0 | ||
fi | ||
if [[ $PASS == 0 ]]; then | ||
echo "$OUT" | ||
exit 1 | ||
fi | ||
""".format( | ||
terraform_binary=working_dir.files_to_run.executable.short_path, | ||
module_path=working_dir_info.module_path, | ||
), | ||
is_executable = True, | ||
) | ||
|
||
return [ | ||
DefaultInfo( | ||
executable = output, | ||
runfiles = working_dir.default_runfiles, | ||
), | ||
] | ||
|
||
execute_test = rule( | ||
implementation = _execute_test_impl, | ||
executable = True, | ||
test = True, | ||
attrs = { | ||
"terraform_working_directory": attr.label(mandatory=True), | ||
} | ||
) |