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

Refactor rendering Terraform functions into constructor #14659

Merged
merged 2 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions upup/pkg/fi/cloudup/awstasks/targetgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ type terraformTargetGroup struct {
Name string `cty:"name"`
Port int64 `cty:"port"`
Protocol string `cty:"protocol"`
VPCID terraformWriter.Literal `cty:"vpc_id"`
VPCID *terraformWriter.Literal `cty:"vpc_id"`
Tags map[string]string `cty:"tags"`
HealthCheck terraformTargetGroupHealthCheck `cty:"health_check"`
}
Expand All @@ -240,7 +240,7 @@ func (_ *TargetGroup) RenderTerraform(t *terraform.TerraformTarget, a, e, change
Name: *e.Name,
Port: *e.Port,
Protocol: *e.Protocol,
VPCID: *e.VPC.TerraformLink(),
VPCID: e.VPC.TerraformLink(),
Tags: e.Tags,
HealthCheck: terraformTargetGroupHealthCheck{
Interval: *e.Interval,
Expand All @@ -253,7 +253,7 @@ func (_ *TargetGroup) RenderTerraform(t *terraform.TerraformTarget, a, e, change
return t.RenderResource("aws_lb_target_group", *e.Name, tf)
}

func (e *TargetGroup) TerraformLink(params ...string) *terraformWriter.Literal {
func (e *TargetGroup) TerraformLink() *terraformWriter.Literal {
shared := fi.ValueOf(e.Shared)
if shared {
if e.ARN != nil {
Expand Down
15 changes: 6 additions & 9 deletions upup/pkg/fi/cloudup/terraform/hcl2.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ limitations under the License.
package terraform

import (
"fmt"
"reflect"
"sort"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
Expand Down Expand Up @@ -104,14 +102,13 @@ func writeValue(body *hclwrite.Body, key string, value cty.Value) {
// key = res_type.res_name.res_prop
// key = file("${module.path}/foo")
func writeLiteral(body *hclwrite.Body, key string, literal *terraformWriter.Literal) {
if literal.FnName != "" {
tokens := hclwrite.Tokens{
if literal.String != "" {
body.SetAttributeRaw(key, hclwrite.Tokens{
{
Type: hclsyntax.TokenIdent,
Bytes: []byte(fmt.Sprintf("%v(%v)", literal.FnName, strings.Join(literal.FnArgs, ", "))),
Bytes: []byte(literal.String),
},
}
body.SetAttributeRaw(key, tokens)
})
} else if literal.Index {
tokens := hclwrite.Tokens{
{
Expand Down Expand Up @@ -242,10 +239,10 @@ func writeMap(body *hclwrite.Body, key string, values map[string]cty.Value) {
errLiteralSlice := gocty.FromCtyValue(v, refLiteralSlice.Interface())
// If this is a map of literals then do not surround the value with quotes
if literal, ok := refLiteral.Interface().(*terraformWriter.Literal); errLiteral == nil && ok {
if literal.FnName != "" {
if literal.String != "" {
tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenIdent,
Bytes: []byte(fmt.Sprintf("%v(%v)", literal.FnName, strings.Join(literal.FnArgs, ", "))),
Bytes: []byte(literal.String),
})
} else if literal.Value != "" {
tokens = append(tokens, []*hclwrite.Token{
Expand Down
6 changes: 3 additions & 3 deletions upup/pkg/fi/cloudup/terraformWriter/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

// Literal represents a literal in terraform syntax
type Literal struct {
// String is the Terraform representation.
String string `cty:"string"`
// Value is used to support Terraform's "${}" interpolation.
Value string `cty:"value"`
// Index to support the index of the count meta-argument.
Expand All @@ -36,8 +38,6 @@ type Literal struct {
// example: {"aws_vpc", "foo", "id"}
Tokens []string `cty:"tokens"`

// FnName represents the name of a terraform function.
FnName string `cty:"fn_name"`
// FnArgs contains string representations of arguments to the function call.
// Any string arguments must be quoted.
FnArgs []string `cty:"fn_arg"`
Expand All @@ -51,8 +51,8 @@ func (l *Literal) MarshalJSON() ([]byte, error) {

func LiteralFunctionExpression(functionName string, args ...string) *Literal {
return &Literal{
String: fmt.Sprintf("%v(%v)", functionName, strings.Join(args, ", ")),
Value: fmt.Sprintf("${%v(%v)}", functionName, strings.Join(args, ", ")),
FnName: functionName,
FnArgs: args,
}
}
Expand Down