From bfc6b308cf5cc0f855f8015505277bef424fb6f1 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Wed, 14 Aug 2024 20:06:47 +0200 Subject: [PATCH] parse output --- src/dda/build/terragrunt.clj | 71 ++++++++++++++--------- src/dda/build/terragrunt/domain.clj | 50 +++++++++------- test/dda/build/terragrunt/domain_test.clj | 59 +++++++++++++------ 3 files changed, 114 insertions(+), 66 deletions(-) diff --git a/src/dda/build/terragrunt.clj b/src/dda/build/terragrunt.clj index 76368e2..dfa9b4d 100644 --- a/src/dda/build/terragrunt.clj +++ b/src/dda/build/terragrunt.clj @@ -1,6 +1,7 @@ (ns dda.build.terragrunt (:require [orchestra.core :refer [defn-spec]] [clojure.spec.test.alpha :as st] + [cheshire.core :refer [parse-string]] [dda.build.devops :as d] [dda.build.terragrunt.domain :as domain] [dda.build.infrastructure :as i])) @@ -8,58 +9,70 @@ (st/instrument `clean-build-dir!) (def default - (merge d/default {:autoapply false})) + (merge d/default {:autoapply false + :tg-output-filenname "tg-out.json"})) (defn-spec clean-build-dir! nil? [devops ::d/devops] - (let [final (merge default devops)] - (i/execute! (domain/clean-build-dir-command final) final))) + (let [config (merge default devops)] + (i/execute! (domain/clean-build-dir-command config) config))) (defn-spec copy-terragrunt! nil? [devops ::d/devops] - (let [final (merge default devops)] - (doseq [c (domain/copy-terragrunt-command final)] - (i/execute! c final)))) + (let [config (merge default devops)] + (doseq [c (domain/copy-terragrunt-command config)] + (i/execute! c config)))) (defn-spec terragrunt-plan! nil? [devops ::d/devops] - (let [final (merge default devops)] - (doseq [c (domain/terragrunt-plan-command final)] - (i/execute! c final)))) + (let [config (merge default devops)] + (doseq [c (domain/terragrunt-plan-command config)] + (i/execute! c config)))) (defn-spec terragrunt-apply! nil? [devops ::d/devops] - (let [final (merge default devops)] - (doseq [c (into (domain/terragrunt-apply-command final) - (domain/terragrunt-output-command final))] - (i/execute! c final)))) + (let [config (merge default devops)] + (doseq [c (domain/terragrunt-apply-command config)] + (i/execute! c config)))) + +(defn-spec terragrunt-output! map? + [devops ::d/devops] + (let [config (merge default devops)] + (doseq [c (domain/terragrunt-output-command config)] + (i/execute! c config)) + (parse-string (slurp (domain/output-path config)) + (fn [k] (keyword (.toLowerCase k)))))) (defn-spec terragrunt-destroy! nil? [devops ::d/devops] - (let [final (merge default devops)] - (doseq [c (domain/terragrunt-destroy-command final)] - (i/execute! c final)))) + (let [config (merge default devops)] + (doseq [c (domain/terragrunt-destroy-command config)] + (i/execute! c config)))) (defn-spec plan! nil? [devops ::d/devops] - (clean-build-dir! devops) - (d/create-build-dir! devops) - (copy-terragrunt! devops) - (terragrunt-plan! devops)) + (let [config (merge default devops)] + (clean-build-dir! config) + (d/create-build-dir! config) + (copy-terragrunt! config) + (terragrunt-plan! config))) -(defn-spec apply! nil? +(defn-spec apply! map? [devops ::d/devops] - (clean-build-dir! devops) - (d/create-build-dir! devops) - (copy-terragrunt! devops) - (terragrunt-apply! devops)) + (let [config (merge default devops)] + (clean-build-dir! config) + (d/create-build-dir! config) + (copy-terragrunt! config) + (terragrunt-apply! config) + (terragrunt-output! config))) (defn-spec destroy! nil? [devops ::d/devops] - (clean-build-dir! devops) - (d/create-build-dir! devops) - (copy-terragrunt! devops) - (terragrunt-destroy! devops)) + (let [config (merge default devops)] + (clean-build-dir! config) + (d/create-build-dir! config) + (copy-terragrunt! config) + (terragrunt-destroy! config))) (st/instrument `clean-build-dir!) (st/instrument `copy-terragrunt!) diff --git a/src/dda/build/terragrunt/domain.clj b/src/dda/build/terragrunt/domain.clj index f7d84e9..48e8665 100644 --- a/src/dda/build/terragrunt/domain.clj +++ b/src/dda/build/terragrunt/domain.clj @@ -3,36 +3,46 @@ [orchestra.core :refer [defn-spec]] [dda.build.devops.domain :as d])) -(s/def ::devops - (s/keys :req-un [::name ::stage ::project-root-path ::build-dir-name ::debug ::dry-run ::module] +(s/def ::tg-output-filenname string?) +(s/def ::autoapply boolean?) + +(s/def ::config + (s/keys :req-un [::d/name ::d/stage ::d/project-root-path ::d/build-dir-name ::d/debug + ::d/dry-run ::d/module ::tg-output-filenname ::autoapply] :opt-un [])) (defn-spec clean-build-dir-command seq? - [devops ::devops] - ["rm" "-rf" (d/build-path (dissoc devops :module))]) + [config ::config] + ["rm" "-rf" (d/build-path (dissoc config :module))]) (defn-spec copy-terragrunt-command seq? - [devops ::devops] - (let [{:keys [module]} devops - devops-wo-module (dissoc devops :module)] - [["bash" "-c" (str "cp *.hcl " (d/build-path devops-wo-module))] - ["cp" "-r" module (d/build-path devops-wo-module)]])) + [config ::config] + (let [{:keys [module]} config + config-wo-module (dissoc config :module)] + [["bash" "-c" (str "cp *.hcl " (d/build-path config-wo-module))] + ["cp" "-r" module (d/build-path config-wo-module)]])) (defn-spec terragrunt-plan-command seq? - [devops ::devops] - [[{:dir (d/build-path devops)} "terragrunt" "init"] - [{:dir (d/build-path devops)} "terragrunt" "plan"]]) + [config ::config] + [[{:dir (d/build-path config)} "terragrunt" "init"] + [{:dir (d/build-path config)} "terragrunt" "plan"]]) (defn-spec terragrunt-apply-command seq? - [devops ::devops] - [[{:dir (d/build-path devops)} "terragrunt" "init"] - [{:dir (d/build-path devops)} "terragrunt" "apply" "-auto-approve"]]) + [config ::config] + [[{:dir (d/build-path config)} "terragrunt" "init"] + [{:dir (d/build-path config)} "terragrunt" "apply" "-auto-approve"]]) (defn-spec terragrunt-destroy-command seq? - [devops ::devops] - [[{:dir (d/build-path devops)} "terragrunt" "init"] - [{:dir (d/build-path devops)} "terragrunt" "destroy"]]) + [config ::config] + [[{:dir (d/build-path config)} "terragrunt" "init"] + [{:dir (d/build-path config)} "terragrunt" "destroy"]]) (defn-spec terragrunt-output-command seq? - [devops ::devops] - [[{:dir (d/build-path devops)} "bash" "-c" "terragrunt output -json > terraform.json"]]) + [config ::config] + (let [{:keys [tg-output-filenname]} config] + [[{:dir (d/build-path config)} "bash" "-c" (str "terragrunt output -json > " tg-output-filenname)]])) + +(defn-spec output-path string? + [config ::config] + (let [{:keys [tg-output-filenname]} config] + (str (d/build-path config) "/" tg-output-filenname))) diff --git a/test/dda/build/terragrunt/domain_test.clj b/test/dda/build/terragrunt/domain_test.clj index 76c5b9c..5acc88f 100644 --- a/test/dda/build/terragrunt/domain_test.clj +++ b/test/dda/build/terragrunt/domain_test.clj @@ -19,8 +19,10 @@ :version "4.11.8-dev" :stage "dev" :debug false - :dry-run false})))) - + :dry-run false + :autoapply false + :tg-output-filenname "tg-out.json"})))) + (deftest should-calculate-copy-terragrunt-command (is (= [["bash" "-c" "cp *.hcl ../../target/test"] ["cp" "-r" "statistics" "../../target/test"]] @@ -31,7 +33,9 @@ :version "4.11.8-dev" :stage "dev" :debug false - :dry-run false})))) + :dry-run false + :autoapply false + :tg-output-filenname "tg-out.json"})))) (deftest should-calculate-terragrunt-plan-command (is (= [[{:dir "../../../target/test/statistics"} "terragrunt" "init"] @@ -43,29 +47,50 @@ :version "4.11.8-dev" :stage "dev" :debug false - :dry-run false})))) + :dry-run false + :autoapply false + :tg-output-filenname "tg-out.json"})))) (deftest should-calculate-terragrunt-apply-command (is (= [[{:dir "../../../target/test/statistics"} "terragrunt" "init"] [{:dir "../../../target/test/statistics"} "terragrunt" "apply" "-auto-approve"]] (cut/terragrunt-apply-command {:name "test" - :module "statistics" - :project-root-path "../../.." - :build-dir-name "target" - :version "4.11.8-dev" - :stage "dev" - :debug false - :dry-run false})))) - -(deftest should-calculate-terragrunt-destroy-command - (is (= [[{:dir "../../../target/test/statistics"} "terragrunt" "init"] - [{:dir "../../../target/test/statistics"} "terragrunt" "destroy"]] - (cut/terragrunt-destroy-command {:name "test" :module "statistics" :project-root-path "../../.." :build-dir-name "target" :version "4.11.8-dev" :stage "dev" :debug false - :dry-run false})))) + :dry-run false + :autoapply false + :tg-output-filenname "tg-out.json"})))) + +(deftest should-calculate-terragrunt-destroy-command + (is (= [[{:dir "../../../target/test/statistics"} "terragrunt" "init"] + [{:dir "../../../target/test/statistics"} "terragrunt" "destroy"]] + (cut/terragrunt-destroy-command {:name "test" + :module "statistics" + :project-root-path "../../.." + :build-dir-name "target" + :version "4.11.8-dev" + :stage "dev" + :debug false + :dry-run false + :autoapply false + :tg-output-filenname "tg-out.json"})))) + +(deftest should-calculate-terragrunt-output-command + (is (= [[{:dir "../../../target/test/statistics"} "bash" + "-c" + "terragrunt output -json > tg-out.json"]] + (cut/terragrunt-output-command {:name "test" + :module "statistics" + :project-root-path "../../.." + :build-dir-name "target" + :version "4.11.8-dev" + :stage "dev" + :debug false + :dry-run false + :autoapply false + :tg-output-filenname "tg-out.json"}))))