mirror of https://github.com/kubernetes/kops.git
Refactor rendering Terraform functions into constructor
This commit is contained in:
parent
46ece64893
commit
1b99cae617
|
|
@ -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"
|
||||
|
|
@ -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{
|
||||
{
|
||||
|
|
@ -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{
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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"`
|
||||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue