Skip to content

Commit

Permalink
Merge pull request #12346 from rifelpet/automated-cherry-pick-of-#123…
Browse files Browse the repository at this point in the history
…16-origin-release-1.22

Automated cherry pick of #12316: Upgrade hcl to 2.10.1
  • Loading branch information
k8s-ci-robot committed Sep 18, 2021
2 parents 1ce42aa + 7787893 commit 2ca5474
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 95 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ require (
github.com/google/uuid v1.2.0
github.com/gophercloud/gophercloud v0.18.0
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/hcl/v2 v2.10.0
github.com/hashicorp/hcl/v2 v2.10.1
github.com/hashicorp/vault/api v1.1.0
github.com/jacksontj/memberlistmesh v0.0.0-20190905163944-93462b9d2bb7
github.com/jetstack/cert-manager v1.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,8 @@ github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+l
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl/v2 v2.10.0 h1:1S1UnuhDGlv3gRFV4+0EdwB+znNP5HmcGbIqwnSCByg=
github.com/hashicorp/hcl/v2 v2.10.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hcl/v2 v2.10.1 h1:h4Xx4fsrRE26ohAk/1iGF/JBqRQbyUqu5Lvj60U54ys=
github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uG
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl/v2 v2.10.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
Expand Down
27 changes: 15 additions & 12 deletions upup/pkg/fi/cloudup/terraform/hcl2.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ func writeLiteral(body *hclwrite.Body, key string, literal *terraformWriter.Lite
},
}
body.SetAttributeRaw(key, tokens)
} else if literal.ResourceType == "" || literal.ResourceName == "" || literal.ResourceProp == "" {
} else if len(literal.Tokens) == 0 {
body.SetAttributeValue(key, cty.StringVal(literal.Value))
} else {
traversal := hcl.Traversal{
hcl.TraverseRoot{Name: literal.ResourceType},
hcl.TraverseAttr{Name: literal.ResourceName},
hcl.TraverseAttr{Name: literal.ResourceProp},
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)
}
Expand All @@ -134,20 +136,21 @@ func writeLiteralList(body *hclwrite.Body, key string, literals []*terraformWrit
{Type: hclsyntax.TokenOBrack, Bytes: []byte("["), SpacesBefore: 1},
}
for i, literal := range literals {
if literal.ResourceType == "" || literal.ResourceName == "" || literal.ResourceProp == "" {
if len(literal.Tokens) == 0 {
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.ResourceType), SpacesBefore: 1},
{Type: hclsyntax.TokenDot, Bytes: []byte(".")},
{Type: hclsyntax.TokenStringLit, Bytes: []byte(literal.ResourceName)},
{Type: hclsyntax.TokenDot, Bytes: []byte(".")},
{Type: hclsyntax.TokenStringLit, Bytes: []byte(literal.ResourceProp)},
}...)
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(",")})
Expand Down
32 changes: 13 additions & 19 deletions upup/pkg/fi/cloudup/terraform/hcl2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,20 @@ func TestWriteLiteral(t *testing.T) {
}{
{
name: "string",
literal: &terraformWriter.Literal{Value: "value"},
literal: terraformWriter.LiteralFromStringValue("value"),
expected: `foo = "value"`,
},
{
name: "traversal",
literal: &terraformWriter.Literal{
ResourceType: "type",
ResourceName: "name",
ResourceProp: "prop",
},
name: "traversal",
literal: terraformWriter.LiteralProperty("type", "name", "prop"),
expected: "foo = type.name.prop",
},
{
name: "provider alias",
literal: terraformWriter.LiteralTokens("aws", "files"),
expected: "foo = aws.files",
},

{
name: "file",
literal: terraformWriter.LiteralFunctionExpression("file", []string{"\"${path.module}/foo\""}),
Expand Down Expand Up @@ -164,9 +166,7 @@ func TestWriteLiteralList(t *testing.T) {
name: "one literal",
literals: []*terraformWriter.Literal{
{
ResourceType: "type",
ResourceName: "name",
ResourceProp: "prop",
Tokens: []string{"type", "name", "prop"},
},
},
expected: "foo = [type.name.prop]",
Expand All @@ -175,14 +175,10 @@ func TestWriteLiteralList(t *testing.T) {
name: "two literals",
literals: []*terraformWriter.Literal{
{
ResourceType: "type1",
ResourceName: "name1",
ResourceProp: "prop1",
Tokens: []string{"type1", "name1", "prop1"},
},
{
ResourceType: "type2",
ResourceName: "name2",
ResourceProp: "prop2",
Tokens: []string{"type2", "name2", "prop2"},
},
},
expected: "foo = [type1.name1.prop1, type2.name2.prop2]",
Expand All @@ -191,9 +187,7 @@ func TestWriteLiteralList(t *testing.T) {
name: "one traversal literal, one string literal",
literals: []*terraformWriter.Literal{
{
ResourceType: "type",
ResourceName: "name",
ResourceProp: "prop",
Tokens: []string{"type", "name", "prop"},
},
{
Value: "foobar",
Expand Down
23 changes: 13 additions & 10 deletions upup/pkg/fi/cloudup/terraformWriter/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ type Literal struct {
// "${}" interpolation is supported.
Value string `cty:"value"`

// ResourceType represents the type of a resource in a literal reference
ResourceType string `cty:"resource_type"`
// ResourceName represents the name of a resource in a literal reference
ResourceName string `cty:"resource_name"`
// ResourceProp represents the property of a resource in a literal reference
ResourceProp string `cty:"resource_prop"`
// Tokens are portions of a literal reference joined by periods.
// example: {"aws_vpc", "foo", "id"}
Tokens []string `cty:"tokens"`

// FnName represents the name of a terraform function.
FnName string `cty:"fn_name"`
Expand Down Expand Up @@ -67,10 +64,16 @@ func LiteralProperty(resourceType, resourceName, prop string) *Literal {
tfName := sanitizeName(resourceName)
expr := "${" + resourceType + "." + tfName + "." + prop + "}"
return &Literal{
Value: expr,
ResourceType: resourceType,
ResourceName: tfName,
ResourceProp: prop,
Value: expr,
Tokens: []string{resourceType, tfName, prop},
}
}

func LiteralTokens(tokens ...string) *Literal {
expr := "${" + strings.Join(tokens, ".") + "}"
return &Literal{
Value: expr,
Tokens: tokens,
}
}

Expand Down
7 changes: 7 additions & 0 deletions vendor/github.com/hashicorp/hcl/v2/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 48 additions & 15 deletions vendor/github.com/hashicorp/hcl/v2/hclsyntax/expression.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions vendor/github.com/hashicorp/hcl/v2/hclsyntax/parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions vendor/github.com/hashicorp/hcl/v2/hclsyntax/spec.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2ca5474

Please sign in to comment.