From ac40be5b267d4ab78890cf3dd908bdf08b5fe25b Mon Sep 17 00:00:00 2001
From: Timo Furrer <tfurrer@gitlab.com>
Date: Fri, 31 May 2024 07:48:05 +0200
Subject: [PATCH] Introduce destroy input for plan job

---
 templates/plan.yml                            | 12 +++++++++++-
 tests/integration-tests/Destroy.gitlab-ci.yml | 16 ++++++++++++++++
 tests/integration.gitlab-ci.yml               |  1 +
 3 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 tests/integration-tests/Destroy.gitlab-ci.yml

diff --git a/templates/plan.yml b/templates/plan.yml
index 7441ebd..bcaed74 100644
--- a/templates/plan.yml
+++ b/templates/plan.yml
@@ -50,6 +50,10 @@ spec:
     state_name:
       default: default
       description: 'Remote OpenTofu state name.'
+    destroy:
+      default: false
+      type: boolean
+      description: 'Indicate if the plan should be a destroy plan'
 
 ---
 
@@ -88,5 +92,11 @@ spec:
   image:
     name: '$[[ inputs.image_registry_base ]]/$[[ inputs.image_name ]]:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
   script:
-    - gitlab-tofu plan
+    - |
+      args=""
+      if [ "$[[ inputs.destroy ]]" == "true" ]; then 
+        echo "Planning for a destroy"
+        args="-destroy"
+      fi
+    - gitlab-tofu plan $args
     - gitlab-tofu plan-json
diff --git a/tests/integration-tests/Destroy.gitlab-ci.yml b/tests/integration-tests/Destroy.gitlab-ci.yml
new file mode 100644
index 0000000..0c89d9f
--- /dev/null
+++ b/tests/integration-tests/Destroy.gitlab-ci.yml
@@ -0,0 +1,16 @@
+include:
+  - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/plan@$CI_COMMIT_SHA
+    inputs:
+      image_registry_base: $GITLAB_OPENTOFU_IMAGE_BASE
+      version: $CI_COMMIT_SHA
+      opentofu_version: $OPENTOFU_VERSION
+      root_dir: $TEST_TF_ROOT
+      state_name: $TEST_TF_STATE_NAME
+      destroy: true
+
+stages: [build]
+
+# Required to run everything immediately, instead of manually.
+
+plan:
+  rules: [{when: always}]
diff --git a/tests/integration.gitlab-ci.yml b/tests/integration.gitlab-ci.yml
index 8f490d7..ca0369b 100644
--- a/tests/integration.gitlab-ci.yml
+++ b/tests/integration.gitlab-ci.yml
@@ -12,3 +12,4 @@ component:
       - PIPELINE_NAME: [Defaults]
       - PIPELINE_NAME: [JobTemplates]
       - PIPELINE_NAME: [TestJob]
+      - PIPELINE_NAME: [Destroy]
-- 
GitLab