pulumi/pkg/codegen/hcl2/rewrite_properties.go
Pat Gavlin 69ba47cff2
[codegen/*] Add support for resource options. (#4925)
The PCL binder has supported resource options for some time, but these
options haven't been used or processed by the various code generators.
These options--particularly the parent and provider options0--are
critical for import codegen. These changes implement the basic set of
options, and add a note about fleshing out the rest as necessary.

One component of these changes is a new rewriter that rewrites property
references into property paths that are understood by the Pulumi engine.
This rewriter is used to preprocess the contents of the `ignoreChanges`
resource option.

These changes also hack around a weakness in the HCL2 type system:
In Go, references to resources should be typed as `hcl2.ResourceType`.
Unfortunately, this breaks the existing collection semantics associated
with resources. Because of this, the Go code generator does not have
enough information to know that it should generate a `[]pulumi.Resource`
for lists of resources. These changes hack around that limitation using
a Go-specific opaque type and some hardcoded comparisons in
`argumentTypeName`.

Fixes #4923.
2020-06-29 16:33:52 -07:00

70 lines
1.8 KiB
Go

package hcl2
import (
"bytes"
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/pulumi/pulumi/pkg/v2/codegen/hcl2/model"
"github.com/pulumi/pulumi/pkg/v2/codegen/hcl2/syntax"
"github.com/pulumi/pulumi/sdk/v2/go/common/util/contract"
"github.com/zclconf/go-cty/cty"
)
func RewritePropertyReferences(expr model.Expression) model.Expression {
rewriter := func(expr model.Expression) (model.Expression, hcl.Diagnostics) {
traversal, ok := expr.(*model.ScopeTraversalExpression)
if !ok {
return expr, nil
}
p, ok := traversal.Parts[len(traversal.Parts)-1].(*ResourceProperty)
if !ok {
return expr, nil
}
var buffer bytes.Buffer
for _, t := range p.Path {
var err error
switch t := t.(type) {
case hcl.TraverseRoot:
_, err = fmt.Fprint(&buffer, t.Name)
case hcl.TraverseAttr:
_, err = fmt.Fprintf(&buffer, ".%s", t.Name)
case hcl.TraverseIndex:
switch t.Key.Type() {
case cty.String:
_, err = fmt.Fprintf(&buffer, ".%s", t.Key.AsString())
case cty.Number:
idx, _ := t.Key.AsBigFloat().Int64()
_, err = fmt.Fprintf(&buffer, "[%d]", idx)
default:
contract.Failf("unexpected traversal index of type %v", t.Key.Type())
}
}
contract.IgnoreError(err)
}
// TODO: transfer internal trivia
propertyPath := cty.StringVal(buffer.String())
value := &model.TemplateExpression{
Parts: []model.Expression{
&model.LiteralValueExpression{
Tokens: syntax.NewLiteralValueTokens(propertyPath),
Value: propertyPath,
},
},
}
value.SetLeadingTrivia(expr.GetLeadingTrivia())
value.SetTrailingTrivia(expr.GetTrailingTrivia())
diags := value.Typecheck(false)
contract.Assert(len(diags) == 0)
return value, nil
}
expr, diags := model.VisitExpression(expr, model.IdentityVisitor, rewriter)
contract.Assert(len(diags) == 0)
return expr
}