Skip to content

Commit 66c028d

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

File tree

4 files changed

+73
-47
lines changed

4 files changed

+73
-47
lines changed

.github/workflows/pr_ci.yml

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will build and test a golang project
22

33
name: PR CI
4-
run-name: 'CI pipeline for PR - (#${{ github.event.number }}) ${{ github.event.pull_request.title }}'
4+
run-name: "CI pipeline for PR - (#${{ github.event.number }}) ${{ github.event.pull_request.title }}"
55

66
on:
77
pull_request:
@@ -19,7 +19,7 @@ jobs:
1919
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
2020
- uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5
2121
with:
22-
go-version: '1.23'
22+
go-version: "1.23"
2323
- name: golangci-lint
2424
uses: golangci/golangci-lint-action@55c2c1448f86e01eaae002a5a3a9624417608d84 # v6
2525
with:
@@ -34,7 +34,7 @@ jobs:
3434
- name: Set up Go
3535
uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5
3636
with:
37-
go-version: '1.23'
37+
go-version: "1.23"
3838

3939
- name: Download Go dependencies
4040
run: go get ./... && go mod tidy
@@ -65,7 +65,7 @@ jobs:
6565
- name: Set up Go
6666
uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5
6767
with:
68-
go-version: '1.23'
68+
go-version: "1.23"
6969

7070
- name: Download Go dependencies
7171
run: go get ./... && go mod tidy
@@ -98,52 +98,11 @@ jobs:
9898
- name: Setup Terraform
9999
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3
100100
with:
101-
terraform_version: '1.11.2'
101+
terraform_version: "1.11.2"
102102

103103
- name: Validate Terraform Examples
104-
working-directory: assets/terraform/examples
105104
run: |
106-
# Create provider configuration template
107-
cat << EOF > provider.tf.template
108-
terraform {
109-
required_providers {
110-
panos = {
111-
source = "PaloAltoNetworks/panos"
112-
version = "1.0.0"
113-
}
114-
}
115-
}
116-
117-
provider "panos" {
118-
alias = "ci"
119-
}
120-
EOF
121-
122-
# Find all directories containing .tf files except ephemeral-resources
123-
DIRS=$(find . -type f -name "*.tf" -exec dirname {} \; | sort -u)
124-
125-
# Loop through each directory and validate
126-
for dir in $DIRS; do
127-
128-
# Copy provider configuration for validation
129-
cp provider.tf.template $dir/provider.tf
130-
131-
echo "Validating configurations in: $dir"
132-
cd "$dir"
133-
134-
# Initialize and validate
135-
terraform init -no-color
136-
terraform validate -no-color
137-
138-
# Clean up provider configuration
139-
rm provider.tf
140-
141-
# Return to the examples directory
142-
cd - > /dev/null
143-
done
144-
145-
# Clean up template
146-
rm provider.tf.template
105+
./scripts/validate-terraform-examples.sh
147106
148107
- name: Workaround actions/upload-artifact#176
149108
run: |

.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 {} \; | 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)