mirror of https://github.com/kubernetes/kops.git
Refactor terraformWriter.Literal's Tokens field
This commit is contained in:
parent
fb07387dd5
commit
cde34d8b56
|
@ -20,7 +20,6 @@ import (
|
|||
"reflect"
|
||||
"sort"
|
||||
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
"github.com/hashicorp/hcl/v2/hclsyntax"
|
||||
"github.com/hashicorp/hcl/v2/hclwrite"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
@ -145,17 +144,8 @@ func writeLiteral(body *hclwrite.Body, key string, literal *terraformWriter.Lite
|
|||
},
|
||||
}
|
||||
body.SetAttributeRaw(key, tokens)
|
||||
} else if len(literal.Tokens) == 0 {
|
||||
body.SetAttributeValue(key, cty.StringVal(literal.Value))
|
||||
} else {
|
||||
traversal := hcl.Traversal{
|
||||
hcl.TraverseRoot{Name: literal.Tokens[0]},
|
||||
}
|
||||
for i := 1; i < len(literal.Tokens); i++ {
|
||||
token := literal.Tokens[i]
|
||||
traversal = append(traversal, hcl.TraverseAttr{Name: token})
|
||||
}
|
||||
body.SetAttributeTraversal(key, traversal)
|
||||
body.SetAttributeValue(key, cty.StringVal(literal.Value))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -167,21 +157,14 @@ func literalListTokens(literals []*terraformWriter.Literal) hclwrite.Tokens {
|
|||
{Type: hclsyntax.TokenOBrack, Bytes: []byte("["), SpacesBefore: 1},
|
||||
}
|
||||
for i, literal := range literals {
|
||||
if len(literal.Tokens) == 0 {
|
||||
if literal.String != "" {
|
||||
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenIdent, Bytes: []byte(literal.String)})
|
||||
} else {
|
||||
tokens = append(tokens, []*hclwrite.Token{
|
||||
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
|
||||
{Type: hclsyntax.TokenQuotedLit, Bytes: []byte(literal.Value)},
|
||||
{Type: hclsyntax.TokenCQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
|
||||
}...)
|
||||
} else {
|
||||
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenStringLit, Bytes: []byte(literal.Tokens[0]), SpacesBefore: 1})
|
||||
for i := 1; i < len(literal.Tokens); i++ {
|
||||
token := literal.Tokens[i]
|
||||
tokens = append(tokens, []*hclwrite.Token{
|
||||
{Type: hclsyntax.TokenDot, Bytes: []byte(".")},
|
||||
{Type: hclsyntax.TokenStringLit, Bytes: []byte(token)},
|
||||
}...)
|
||||
}
|
||||
}
|
||||
if i < len(literals)-1 {
|
||||
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenComma, Bytes: []byte(",")})
|
||||
|
|
|
@ -167,7 +167,7 @@ func TestWriteLiteralList(t *testing.T) {
|
|||
name: "one literal",
|
||||
literals: []*terraformWriter.Literal{
|
||||
{
|
||||
Tokens: []string{"type", "name", "prop"},
|
||||
String: "type.name.prop",
|
||||
},
|
||||
},
|
||||
expected: "foo = [type.name.prop]",
|
||||
|
@ -176,10 +176,10 @@ func TestWriteLiteralList(t *testing.T) {
|
|||
name: "two literals",
|
||||
literals: []*terraformWriter.Literal{
|
||||
{
|
||||
Tokens: []string{"type1", "name1", "prop1"},
|
||||
String: "type1.name1.prop1",
|
||||
},
|
||||
{
|
||||
Tokens: []string{"type2", "name2", "prop2"},
|
||||
String: "type2.name2.prop2",
|
||||
},
|
||||
},
|
||||
expected: "foo = [type1.name1.prop1, type2.name2.prop2]",
|
||||
|
@ -188,7 +188,7 @@ func TestWriteLiteralList(t *testing.T) {
|
|||
name: "one traversal literal, one string literal",
|
||||
literals: []*terraformWriter.Literal{
|
||||
{
|
||||
Tokens: []string{"type", "name", "prop"},
|
||||
String: "type.name.prop",
|
||||
},
|
||||
{
|
||||
Value: "foobar",
|
||||
|
|
|
@ -34,10 +34,6 @@ type Literal struct {
|
|||
// Index to support the index of the count meta-argument.
|
||||
Index bool `cty:"index"`
|
||||
|
||||
// Tokens are portions of a literal reference joined by periods.
|
||||
// example: {"aws_vpc", "foo", "id"}
|
||||
Tokens []string `cty:"tokens"`
|
||||
|
||||
// FnArgs contains string representations of arguments to the function call.
|
||||
// Any string arguments must be quoted.
|
||||
FnArgs []string `cty:"fn_arg"`
|
||||
|
@ -63,27 +59,27 @@ func LiteralSelfLink(resourceType, resourceName string) *Literal {
|
|||
|
||||
func LiteralData(dataSourceType, dataSourceName, prop string) *Literal {
|
||||
tfName := sanitizeName(dataSourceName)
|
||||
expr := "${data." + dataSourceType + "." + tfName + "." + prop + "}"
|
||||
expr := "data." + dataSourceType + "." + tfName + "." + prop + ""
|
||||
return &Literal{
|
||||
Value: expr,
|
||||
Tokens: []string{"data", dataSourceType, tfName, prop},
|
||||
String: expr,
|
||||
Value: "${" + expr + "}",
|
||||
}
|
||||
}
|
||||
|
||||
func LiteralProperty(resourceType, resourceName, prop string) *Literal {
|
||||
tfName := sanitizeName(resourceName)
|
||||
expr := "${" + resourceType + "." + tfName + "." + prop + "}"
|
||||
expr := resourceType + "." + tfName + "." + prop
|
||||
return &Literal{
|
||||
Value: expr,
|
||||
Tokens: []string{resourceType, tfName, prop},
|
||||
String: expr,
|
||||
Value: "${" + expr + "}",
|
||||
}
|
||||
}
|
||||
|
||||
func LiteralTokens(tokens ...string) *Literal {
|
||||
expr := "${" + strings.Join(tokens, ".") + "}"
|
||||
expr := strings.Join(tokens, ".")
|
||||
return &Literal{
|
||||
Value: expr,
|
||||
Tokens: tokens,
|
||||
String: expr,
|
||||
Value: "${" + expr + "}",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue