Skip to content

Commit 8fcff9b

Browse files
committed
test(codegen): refactor examples testing out of GitHub workflows so they can be used as part of Makefile
1 parent bbd3a78 commit 8fcff9b

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
# IDE
3030
.idea/
3131

32+
/assets/terraform/examples/**/provider.tf
33+
/assets/terraform/examples/provider.tf.template
34+
3235
# Local semantic-release configuration if you create one for testing
3336
release.config.local.js
3437
release.config.local.json

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ $(GENERATED_OUT_PATH)/%: assets/%
2828
@mkdir -p $(@D)
2929
cp $< $@
3030

31+
.PHONY: install
32+
install: codegen
33+
cd $(GENERATED_OUT_PATH)/terraform/ && go install
34+
35+
.PHONY: examples
36+
examples: install
37+
TF_CLI_CONFIG_FILE= ./scripts/validate-terraform-examples.sh
38+
3139
.PHONY: test
3240
test: test/codegen test/pango test/terraform
3341

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# This script validates all terraform examples.
6+
# It should be run from the root of the repository.
7+
8+
cd assets/terraform/examples
9+
10+
# Create provider configuration template
11+
cat << EOF > provider.tf.template
12+
terraform {
13+
required_providers {
14+
panos = {
15+
source = "PaloAltoNetworks/panos"
16+
version = "1.0.0"
17+
}
18+
}
19+
}
20+
21+
provider "panos" {
22+
alias = "ci"
23+
}
24+
EOF
25+
26+
# Find all directories containing .tf files
27+
DIRS=$(find . -type f -name "*.tf" -exec dirname {} \; | grep -v "./provider" | sort -u)
28+
29+
# Loop through each directory and validate
30+
for dir in $DIRS; do
31+
32+
# Copy provider configuration for validation
33+
cp provider.tf.template "$dir/provider.tf"
34+
35+
echo "Validating configurations in: $dir"
36+
(
37+
cd "$dir"
38+
39+
# Initialize and validate
40+
[[ $CI == "true" ]] && terraform init -no-color
41+
OUTPUT=$(terraform validate -no-color)
42+
if [ $? -ne 0 ]; then
43+
echo "Vailed to validate examples: $dir"
44+
echo "${OUTPUT}"
45+
exit 1
46+
fi
47+
48+
# Clean up provider configuration
49+
rm provider.tf
50+
)
51+
done
52+
53+
# Clean up template
54+
rm provider.tf.template
55+
56+
echo "All examples validated successfully."

0 commit comments

Comments
 (0)