-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hook for provider version checking
- Loading branch information
Showing
12 changed files
with
192 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
|
||
# Crash log files | ||
crash.log | ||
|
||
.vscode/ |
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,15 @@ | ||
#!/bin/bash -e | ||
|
||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
AWK_FILE="${SCRIPT_DIR}/required_providers.awk" | ||
|
||
check_files() { | ||
has_error=0 | ||
for file in "$@"; do | ||
awk -f "$AWK_FILE" "$file" || has_error=1 | ||
done | ||
return $has_error | ||
} | ||
|
||
check_files "$@" | ||
exit $has_error |
14 changes: 14 additions & 0 deletions
14
hooks/provider-pinned-versions/fixtures/fail_when_all_constraints.tf
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,14 @@ | ||
terraform { | ||
required_version = ">= 1.0.0, < 2.0.0" | ||
|
||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~>5.78" | ||
} | ||
newrelic = { | ||
source = "newrelic/newrelic" | ||
version = ">=2, <3" | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
hooks/provider-pinned-versions/fixtures/fail_when_mixed.tf
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,14 @@ | ||
terraform { | ||
required_version = ">= 1.0.0, < 2.0.0" | ||
|
||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "5.78.0" | ||
} | ||
newrelic = { | ||
source = "newrelic/newrelic" | ||
version = ">=2, <3" | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
hooks/provider-pinned-versions/fixtures/pass_when_all_pinned.tf
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,22 @@ | ||
terraform { | ||
required_version = ">= 1.0.0, < 2.0.0" | ||
|
||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "5.78.0" | ||
} | ||
cyral = { | ||
source = "cyralinc/cyral" | ||
version = "4.14.1" | ||
} | ||
mysql = { | ||
source = "petoju/mysql" | ||
version = "3.0.67" | ||
} | ||
newrelic = { | ||
source = "newrelic/newrelic" | ||
version = "3.52.1" | ||
} | ||
} | ||
} |
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,8 @@ | ||
terraform { | ||
required_version = ">= 1.0.0, < 2.0.0" | ||
|
||
required_providers { | ||
aws = { source = "hashicorp/aws", version = "5.78.0" } | ||
cyral = { source = "cyralinc/cyral", version = "4.14.1" } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
hooks/provider-pinned-versions/fixtures/pass_when_pre_release.tf
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,14 @@ | ||
terraform { | ||
required_version = ">= 1.0.0, < 2.0.0" | ||
|
||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "5.78.0-beta-some-branch-name" | ||
} | ||
newrelic = { | ||
source = "newrelic/newrelic" | ||
version = "3.52.1" | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
hooks/provider-pinned-versions/fixtures/pass_when_version_is_first.tf
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,14 @@ | ||
terraform { | ||
required_version = ">= 1.0.0, < 2.0.0" | ||
|
||
required_providers { | ||
aws = { | ||
version = "5.78.0" | ||
source = "hashicorp/aws" | ||
} | ||
cyral = { | ||
source = "cyralinc/cyral" | ||
version = "4.14.1" | ||
} | ||
} | ||
} |
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,54 @@ | ||
#! /bin/awk | ||
|
||
BEGIN { | ||
in_required_providers = 0; | ||
brace_count = 0; | ||
version_prefix_regex = ".*version[[:space:]]+=[[:space:]]+"; | ||
provider_prefix_regex = "[a-z_-]+[[:space:]]+=[[:space:]]+\{"; | ||
pinned_version_regex = "[0-9]+\.[0-9]+\.[0-9]+"; | ||
version_constraints_regex = "[!~><]+"; | ||
} | ||
|
||
{ | ||
if ($0 ~ /required_providers/) { | ||
in_required_providers = 1; | ||
} | ||
|
||
# If inside "required_providers" block, count braces | ||
if (in_required_providers) { | ||
brace_count += gsub(/{/, "{"); | ||
brace_count -= gsub(/}/, "}"); | ||
|
||
# If brace count returns to 0, exit the block | ||
if (brace_count == 0) { | ||
in_required_providers = 0; | ||
next; | ||
} | ||
|
||
if ($0 ~ provider_prefix_regex) { | ||
provider = $1; | ||
} | ||
|
||
# If the line doesn't contain a version, skip | ||
if (!($0 ~ version_prefix_regex)) { | ||
next; | ||
} else { | ||
# If the line contains a version, remove the prefix | ||
gsub(version_prefix_regex, "", $0); | ||
if ($0 ~ version_constraints_regex) { | ||
# remove trailing curly brace if any (happens when version is inlined) | ||
gsub(/}/, "", $0); | ||
error[++i] = "ERROR: '" provider "' version not in pinned format: " $0 " in file " FILENAME; | ||
} | ||
} | ||
} | ||
} | ||
|
||
END { | ||
if (length(error) > 0) { | ||
for (j = 1; j <= i; j++) { | ||
print error[j] | ||
} | ||
exit 1; | ||
} | ||
} |
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,26 @@ | ||
#!/bin/bash -e | ||
|
||
# get the directory of the script | ||
script_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | ||
|
||
echo "testing: $script_directory" | ||
|
||
# shellcheck disable=SC2207 | ||
files=($(find "$script_directory"/fixtures -type f -name "pass_*.tf")) | ||
for file in "${files[@]}"; do | ||
echo "testing: check $file" | ||
"$script_directory/check" "$file" | ||
done | ||
|
||
# shellcheck disable=SC2207 | ||
files=($(find "$script_directory"/fixtures -type f -name "fail_*.tf")) | ||
for file in "${files[@]}"; do | ||
echo "testing: check $file" | ||
echo " expecting error" | ||
if "$script_directory/check" "$file"; then | ||
echo "ERROR: should have failed" | ||
exit 1 | ||
fi | ||
done | ||
|
||
echo "testing: PASS" |
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