From f6bba7b4222a142c233c1825438e5f8c02378fb3 Mon Sep 17 00:00:00 2001 From: Kyle Kotowick Date: Sun, 26 Mar 2023 14:03:49 -0400 Subject: [PATCH] Update to switch internal data source to non-deprecated provider (#3) --- .github/workflows/CICD.yml | 59 ++++++++++++++++++++++++++++++++++ main.tf | 9 +++--- outputs.tf | 2 +- tests/fail/.terraform.lock.hcl | 22 +++++++++++++ tests/fail/main.tf | 5 +++ tests/pass/.terraform.lock.hcl | 22 +++++++++++++ tests/pass/main.tf | 5 +++ versions.tf | 6 ++-- 8 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/CICD.yml create mode 100644 tests/fail/.terraform.lock.hcl create mode 100644 tests/fail/main.tf create mode 100644 tests/pass/.terraform.lock.hcl create mode 100644 tests/pass/main.tf diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml new file mode 100644 index 0000000..20b5f22 --- /dev/null +++ b/.github/workflows/CICD.yml @@ -0,0 +1,59 @@ +name: "Build" + +on: [push, pull_request] + +jobs: + Matrix: + runs-on: ubuntu-latest + + steps: + - name: Generate Matrix + id: matrix + uses: Invicton-Labs/terraform-module-testing/matrix@v0.1.0 + with: + minimum_tf_version: '0.13.0' + + - name: Output Matrix + run: | + echo "Strategy: ${{ steps.matrix.outputs.strategy }}" + + outputs: + strategy: ${{ steps.matrix.outputs.strategy }} + + Test: + needs: [Matrix] + strategy: ${{ fromJSON(needs.Matrix.outputs.strategy)}} + runs-on: ${{ matrix.runs-on }} + container: ${{ matrix.container }} + steps: + - name: Initialize - Pass + id: init-pass + uses: Invicton-Labs/terraform-module-testing/initialize@v0.1.0 + with: + tf_path: tests/pass + - name: Run Tests - Pass + id: tests-pass + uses: Invicton-Labs/terraform-module-testing/apply-destroy@v0.1.0 + with: + tf_path: tests/pass + + - name: Initialize - Fail + id: init-fail + uses: Invicton-Labs/terraform-module-testing/initialize@v0.1.0 + with: + tf_path: tests/fail + - name: Run Tests - Fail + id: tests-fail + uses: Invicton-Labs/terraform-module-testing/apply-failure@v0.1.0 + with: + tf_path: tests/fail + + # This job just waits for all other jobs to pass. We have it here + # so our branch protection rule can reference a single job, instead + # of needing to list every matrix value of every job above. + Passed: + runs-on: ubuntu-latest + needs: [Test] + steps: + - name: Mark tests as passed + run: echo "🎉" \ No newline at end of file diff --git a/main.tf b/main.tf index 8373e4d..a019ad6 100644 --- a/main.tf +++ b/main.tf @@ -3,12 +3,11 @@ // Therefore, unless the "checked" output parameter was actually used somewhere in the parent module, // the assertion check would never actually run. By putting it in a data source, it will always run // regardless of whether any outputs of this module are used in the parent module. The `null` data -// source would be ideal, but it throws a deprecation warning. This template data source is the +// source would be ideal, but it throws a deprecation warning. This CloudInit data source is the // next best alternative, since it is fast and doesn't interact with the operating system at all -// (unlike an external data source, for example). -data "template_file" "check" { - template = "" - vars = { +// (unlike an external data source, for example), and isn't deprecated. +data "cloudinit_config" "check" { + part { content = var.condition ? "" : SEE_ABOVE_ERROR_MESSAGE(true ? null : "ERROR: ${var.error_message}") } } diff --git a/outputs.tf b/outputs.tf index 0b93c50..d2bce0e 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,4 +1,4 @@ output "checked" { description = "Whether the condition has been checked (used for assertion dependencies)." - value = data.template_file.check.rendered == "" ? true : true + value = data.cloudinit_config.check.rendered == "" ? true : true } diff --git a/tests/fail/.terraform.lock.hcl b/tests/fail/.terraform.lock.hcl new file mode 100644 index 0000000..db7a7c3 --- /dev/null +++ b/tests/fail/.terraform.lock.hcl @@ -0,0 +1,22 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/cloudinit" { + version = "2.3.2" + constraints = ">= 2.3.1" + hashes = [ + "h1:2jb+BfT5T96dXxUD2LQ6MtVHpXErd7ZybmMvdWE2jd4=", + "zh:2487e498736ed90f53de8f66fe2b8c05665b9f8ff1506f751c5ee227c7f457d1", + "zh:3d8627d142942336cf65eea6eb6403692f47e9072ff3fa11c3f774a3b93130b3", + "zh:434b643054aeafb5df28d5529b72acc20c6f5ded24decad73b98657af2b53f4f", + "zh:436aa6c2b07d82aa6a9dd746a3e3a627f72787c27c80552ceda6dc52d01f4b6f", + "zh:458274c5aabe65ef4dbd61d43ce759287788e35a2da004e796373f88edcaa422", + "zh:54bc70fa6fb7da33292ae4d9ceef5398d637c7373e729ed4fce59bd7b8d67372", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:893ba267e18749c1a956b69be569f0d7bc043a49c3a0eb4d0d09a8e8b2ca3136", + "zh:95493b7517bce116f75cdd4c63b7c82a9d0d48ec2ef2f5eb836d262ef96d0aa7", + "zh:9ae21ab393be52e3e84e5cce0ef20e690d21f6c10ade7d9d9d22b39851bfeddc", + "zh:cc3b01ac2472e6d59358d54d5e4945032efbc8008739a6d4946ca1b621a16040", + "zh:f23bfe9758f06a1ec10ea3a81c9deedf3a7b42963568997d84a5153f35c5839a", + ] +} diff --git a/tests/fail/main.tf b/tests/fail/main.tf new file mode 100644 index 0000000..749cea6 --- /dev/null +++ b/tests/fail/main.tf @@ -0,0 +1,5 @@ +module "fail" { + source = "../../" + condition = false + error_message = "sample error" +} diff --git a/tests/pass/.terraform.lock.hcl b/tests/pass/.terraform.lock.hcl new file mode 100644 index 0000000..db7a7c3 --- /dev/null +++ b/tests/pass/.terraform.lock.hcl @@ -0,0 +1,22 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/cloudinit" { + version = "2.3.2" + constraints = ">= 2.3.1" + hashes = [ + "h1:2jb+BfT5T96dXxUD2LQ6MtVHpXErd7ZybmMvdWE2jd4=", + "zh:2487e498736ed90f53de8f66fe2b8c05665b9f8ff1506f751c5ee227c7f457d1", + "zh:3d8627d142942336cf65eea6eb6403692f47e9072ff3fa11c3f774a3b93130b3", + "zh:434b643054aeafb5df28d5529b72acc20c6f5ded24decad73b98657af2b53f4f", + "zh:436aa6c2b07d82aa6a9dd746a3e3a627f72787c27c80552ceda6dc52d01f4b6f", + "zh:458274c5aabe65ef4dbd61d43ce759287788e35a2da004e796373f88edcaa422", + "zh:54bc70fa6fb7da33292ae4d9ceef5398d637c7373e729ed4fce59bd7b8d67372", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:893ba267e18749c1a956b69be569f0d7bc043a49c3a0eb4d0d09a8e8b2ca3136", + "zh:95493b7517bce116f75cdd4c63b7c82a9d0d48ec2ef2f5eb836d262ef96d0aa7", + "zh:9ae21ab393be52e3e84e5cce0ef20e690d21f6c10ade7d9d9d22b39851bfeddc", + "zh:cc3b01ac2472e6d59358d54d5e4945032efbc8008739a6d4946ca1b621a16040", + "zh:f23bfe9758f06a1ec10ea3a81c9deedf3a7b42963568997d84a5153f35c5839a", + ] +} diff --git a/tests/pass/main.tf b/tests/pass/main.tf new file mode 100644 index 0000000..b1ac4e3 --- /dev/null +++ b/tests/pass/main.tf @@ -0,0 +1,5 @@ +module "pass" { + source = "../../" + condition = true + error_message = "sample error" +} diff --git a/versions.tf b/versions.tf index cfe9f63..eb46da1 100644 --- a/versions.tf +++ b/versions.tf @@ -1,9 +1,9 @@ terraform { required_version = ">= 0.13.0" required_providers { - template = { - source = "hashicorp/template" - version = ">= 2.2.0" + cloudinit = { + source = "hashicorp/cloudinit" + version = ">= 2.3.1" } } }