terramate icon indicating copy to clipboard operation
terramate copied to clipboard

feat: add tm_dynamic attributes

Open katcipis opened this issue 2 years ago • 1 comments

Reason for This Change

Adding support to attributes inside tm_dynamic as a way to generate blocks using an object/map as the source of the blocks attributes. This is useful in some scenarios where content is not enough, like dynamic generation of terraform.required_providers (or a scenario where each generated block will have different attributes).

Description of Changes

Add support to attributes and its tests. Also added some tests for the overall tm_dynamic that were missing, specially error handling and fixed some issues like tm_dynamic allowing unknown attributes instead of failing.

Test example

Here is a simple test script that serves as an example on how this could be used to generate providers configuration:

#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail

workdir="$(mktemp -d)"
cd "${workdir}"

cat > terramate.tm.hcl <<- EOM
terramate {
  config {
  }
}
EOM

cat > providers.tm.hcl <<- EOM
globals {
  used_providers = ["google", "kubernetes"]
  available_providers = {
    google = {
      source  = "hashicorp/google"
      version = "version 1"
    }
    google-beta = {
      source  = "hashicorp/google-beta"
      version = "version 2"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "version 3"
    }
  }
  required_providers = { for k,v in global.available_providers : k => v if tm_contains(global.used_providers, k)}
}

generate_hcl "providers.tf" {
  content {
    terraform {
      tm_dynamic "required_providers" {
        for_each = tm_length(global.required_providers) > 0 ? [true] : []
        attributes = global.required_providers
      }
    }
  }
}
EOM

terramate create stack
terramate generate

echo
echo "generated providers:"
cat stack/providers.tf

katcipis avatar Aug 09 '22 20:08 katcipis

Codecov Report

Merging #527 (b17a448) into main (bd09ec1) will increase coverage by 0.63%. The diff coverage is 85.14%.

@@            Coverage Diff             @@
##             main     #527      +/-   ##
==========================================
+ Coverage   69.57%   70.20%   +0.63%     
==========================================
  Files          52       52              
  Lines        8979     9099     +120     
==========================================
+ Hits         6247     6388     +141     
+ Misses       2424     2411      -13     
+ Partials      308      300       -8     
Flag Coverage Δ
tests 70.20% <85.14%> (+0.63%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
test/hcl.go 73.05% <ø> (-0.17%) :arrow_down:
hcl/hcl.go 88.88% <84.79%> (+2.77%) :arrow_up:
generate/genhcl/genhcl.go 94.11% <100.00%> (ø)
hcl/eval/eval.go 89.77% <100.00%> (+0.11%) :arrow_up:
hcl/eval/partial.go 80.79% <0.00%> (+0.30%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Aug 09 '22 20:08 codecov[bot]