Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use hclwriter in gotemplates #936

Open
vincenthsh opened this issue Sep 17, 2023 · 1 comment
Open

feat: Use hclwriter in gotemplates #936

vincenthsh opened this issue Sep 17, 2023 · 1 comment

Comments

@vincenthsh
Copy link
Contributor

vincenthsh commented Sep 17, 2023

Example Implementation writing a foo = {} assignment

Example Implementation writing a provider "foo" {} block

Usage in go templates:

terraform {
  required_version = "={{ .TerraformVersion }}"
  {{ template "backend" .Backend }}
  required_providers {
    {{- range $k, $v := .ProviderVersions }}
    {{ toHclAssignment $k $v }}
    {{- end }}
  }
}

Result:

terraform {
  required_version = "=1.5.7"
  backend "s3" {
    # snip
    ...
  }
  required_providers {
    archive = {
      source  = "hashicorp/archive"
      version = "~> 2.0"
    }
    assert = {
      source  = "bwoznicki/assert"
      version = "0.0.1"
    }
    ...

Caveat: requires json tags on the golang structs as zclconf/go-cty only supports UnmarshalJSON

// json tags required for toHCLAssignment function
// in util/template.go
type ProviderVersion struct {
	Source  string  `yaml:"source" json:"source"`
	Version *string `yaml:"version" json:"version"`
}

Inspiration from:

@vincenthsh
Copy link
Contributor Author

Just noticed github.com/zclconf/go-cty-yaml - may not need json tags 🤦‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant