Skip to content

Commit

Permalink
refactor(terraform): consolidate output parsing logic into locals.tf …
Browse files Browse the repository at this point in the history
…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] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
sachincool and github-actions[bot] authored Nov 28, 2024
1 parent 4caa0fa commit 68e538b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

| Name | Version |
|------|---------|
| <a name="provider_local"></a> [local](#provider\_local) | ~> 2.5 |
| <a name="provider_null"></a> [null](#provider\_null) | ~> 3.0 |

## Modules
Expand All @@ -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

Expand Down
19 changes: 19 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}


18 changes: 0 additions & 18 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
{}
)
}

0 comments on commit 68e538b

Please sign in to comment.