From 68e538b5cec1775fa1479660eba75ddd6642025d Mon Sep 17 00:00:00 2001 From: Harshit Luthra Date: Thu, 28 Nov 2024 22:47:58 +0530 Subject: [PATCH] refactor(terraform): consolidate output parsing logic into locals.tf for improved maintainability and error handling (#11) * refactor(terraform): consolidate output parsing logic into locals.tf for improved maintainability and error handling * terraform-docs: automated action --------- Co-authored-by: github-actions[bot] --- README.md | 2 -- locals.tf | 19 +++++++++++++++++++ main.tf | 18 ------------------ 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c81cf8a..354c99a 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ | Name | Version | |------|---------| -| [local](#provider\_local) | ~> 2.5 | | [null](#provider\_null) | ~> 3.0 | ## Modules @@ -25,7 +24,6 @@ No modules. | Name | Type | |------|------| | [null_resource.create_cluster](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource | -| [local_file.cluster_output](https://registry.terraform.io/providers/hashicorp/local/latest/docs/data-sources/file) | data source | ## Inputs diff --git a/locals.tf b/locals.tf index 0080a13..acf62fc 100644 --- a/locals.tf +++ b/locals.tf @@ -73,5 +73,24 @@ locals { ) ) + output_file = "${path.module}/cluster_output.txt" + + # Safely read the output file if it exists + raw_output = try( + fileexists(local.output_file) ? file(local.output_file) : "", + "" + ) + + # Parse the output lines safely + output_lines = compact(split("\n", local.raw_output)) + + # Create the output map with proper error handling + output_map = { + for line in local.output_lines : + split("::", line)[0] => split("::", line)[1] + if length(split("::", line)) == 2 + } } + + diff --git a/main.tf b/main.tf index 4714250..3184216 100644 --- a/main.tf +++ b/main.tf @@ -18,21 +18,3 @@ resource "null_resource" "create_cluster" { } } -data "local_file" "cluster_output" { - filename = local.output_file - depends_on = [null_resource.create_cluster] -} - -locals { - output_lines = try( - compact(split("\n", data.local_file.cluster_output.content)), - [] - ) - output_map = try( - { for line in local.output_lines : - split("::", line)[0] => split("::", line)[1] - if length(split("::", line)) == 2 - }, - {} - ) -}