-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AutoAliasing code migration (#39)
- Loading branch information
Showing
8 changed files
with
411 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package upgrade | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/ast" | ||
"go/format" | ||
"go/parser" | ||
"go/printer" | ||
"go/token" | ||
"os" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/ast/astutil" | ||
) | ||
|
||
const ( | ||
TfBridgeXPkg = "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/x" | ||
ContractPkg = "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" | ||
) | ||
|
||
func AutoAliasingMigration(resourcesFilePath, providerName string) (bool, error) { | ||
// Create the AST by parsing src | ||
fset := token.NewFileSet() | ||
file, err := parser.ParseFile(fset, resourcesFilePath, nil, parser.ParseComments) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
applied, initMetadata, errAssigned := false, true, false | ||
// check to see if already implemented | ||
astutil.Apply(file, nil, func(c *astutil.Cursor) bool { | ||
n := c.Node() | ||
switch x := n.(type) { | ||
case *ast.AssignStmt: | ||
if len(x.Rhs) == 1 { | ||
if c, ok := x.Rhs[0].(*ast.CallExpr); ok { | ||
if s, ok := c.Fun.(*ast.SelectorExpr); ok && s.Sel.Name == "AutoAliasing" { | ||
applied = true | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
}) | ||
if applied { | ||
return false, nil | ||
} | ||
|
||
astutil.AddImport(fset, file, TfBridgeXPkg) | ||
astutil.AddImport(fset, file, ContractPkg) | ||
astutil.AddNamedImport(fset, file, "EMBED_COMMENT_ANCHOR", "embed") | ||
|
||
astutil.Apply(file, nil, func(c *astutil.Cursor) bool { | ||
n := c.Node() | ||
switch x := n.(type) { | ||
case *ast.GenDecl: | ||
if x.Tok == token.VAR { | ||
if s, ok := x.Specs[0].(*ast.ValueSpec); ok && s.Names[0].Name == "metadata" { | ||
initMetadata = false | ||
} | ||
} | ||
case *ast.CompositeLit: | ||
if s, ok := x.Type.(*ast.SelectorExpr); ok && s.Sel.Name == "ProviderInfo" { | ||
x.Elts = append(x.Elts, &ast.KeyValueExpr{ | ||
Key: &ast.Ident{Name: "MetadataInfo"}, | ||
Value: &ast.CallExpr{ | ||
Fun: &ast.SelectorExpr{ | ||
X: &ast.Ident{Name: "tfbridge"}, | ||
Sel: &ast.Ident{Name: "NewProviderMetadata"}, | ||
}, | ||
Args: []ast.Expr{&ast.Ident{Name: "metadata"}}, | ||
}, | ||
}) | ||
} | ||
case *ast.AssignStmt: | ||
for _, l := range x.Lhs { | ||
if e, ok := l.(*ast.Ident); ok && e.Name == "err" { | ||
errAssigned = true | ||
} | ||
} | ||
case *ast.ExprStmt: | ||
var id *ast.SelectorExpr | ||
call, ok := x.X.(*ast.CallExpr) | ||
if ok { | ||
id, ok = call.Fun.(*ast.SelectorExpr) | ||
} | ||
if ok { | ||
if id.Sel.Name == "SetAutonaming" { | ||
tok := token.DEFINE | ||
if errAssigned { | ||
tok = token.ASSIGN | ||
} | ||
c.InsertBefore(&ast.AssignStmt{ | ||
Tok: tok, | ||
Lhs: []ast.Expr{&ast.Ident{Name: "err", Obj: &ast.Object{Kind: ast.Var, Name: "err"}}}, | ||
Rhs: []ast.Expr{&ast.CallExpr{ | ||
Fun: &ast.SelectorExpr{ | ||
X: &ast.Ident{Name: "x"}, | ||
Sel: &ast.Ident{Name: "AutoAliasing"}, | ||
}, | ||
Args: []ast.Expr{ | ||
&ast.UnaryExpr{ | ||
Op: token.AND, | ||
X: &ast.Ident{Name: "prov", Obj: &ast.Object{Kind: ast.Var, Name: "prov"}}, | ||
}, | ||
&ast.CallExpr{ | ||
Fun: &ast.SelectorExpr{ | ||
X: &ast.Ident{Name: "prov", Obj: &ast.Object{Kind: ast.Var, Name: "prov"}}, | ||
Sel: &ast.Ident{Name: "GetMetadata"}, | ||
}, | ||
}, | ||
}, | ||
}}, | ||
}) | ||
c.InsertBefore(&ast.ExprStmt{ | ||
X: &ast.CallExpr{ | ||
Fun: &ast.SelectorExpr{ | ||
X: &ast.Ident{Name: "contract"}, | ||
Sel: &ast.Ident{Name: "AssertNoErrorf"}, | ||
}, | ||
Args: []ast.Expr{ | ||
&ast.Ident{Name: "err", Obj: &ast.Object{Kind: ast.Var, Name: "err"}}, | ||
&ast.BasicLit{Kind: token.STRING, Value: "\"auto aliasing apply failed\""}, | ||
}, | ||
}}) | ||
} | ||
} | ||
} | ||
|
||
return true | ||
}) | ||
|
||
// TODO: figure out how to properly append comments so everything can be done via AST manipulation | ||
if initMetadata { | ||
file.Decls = append(file.Decls, &ast.GenDecl{ | ||
Tok: token.VAR, | ||
Specs: []ast.Spec{ | ||
&ast.ValueSpec{ | ||
Names: []*ast.Ident{{Name: "metadata", Obj: &ast.Object{Kind: ast.Var, Name: "metadata"}}}, | ||
Type: &ast.ArrayType{Elt: &ast.Ident{Name: "byte // EMBED_DIRECTIVE_ANCHOR"}}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
err = printer.Fprint(buf, fset, file) | ||
if err != nil { | ||
return false, err | ||
} | ||
s := buf.String() | ||
s = strings.Replace(s, `EMBED_COMMENT_ANCHOR "embed"`, | ||
"// embed is used to store bridge-metadata.json in the compiled binary\n _ \"embed\"", 1) | ||
s = strings.Replace(s, `var metadata []byte // EMBED_DIRECTIVE_ANCHOR`, | ||
fmt.Sprintf("//go:embed cmd/pulumi-resource-%s/bridge-metadata.json\nvar metadata []byte", | ||
providerName), 1) | ||
// format output | ||
formatted, err := format.Source([]byte(s)) | ||
if err != nil { | ||
return false, err | ||
} | ||
err = os.WriteFile(resourcesFilePath, formatted, 0600) | ||
if err != nil { | ||
return false, err | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package upgrade | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAutoAliasingMigration(t *testing.T) { | ||
origProgram := `package test | ||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
"unicode" | ||
"github.com/mrparkers/terraform-test/provider" | ||
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge" | ||
) | ||
// all of the token components used below. | ||
const ( | ||
// packages: | ||
mainPkg = "test" | ||
// modules: | ||
mainMod = "index" // the y module | ||
) | ||
// Provider returns additional overlaid schema and metadata associated with the provider.. | ||
func Provider() tfbridge.ProviderInfo { | ||
// Instantiate the Terraform provider | ||
p := shimv2.NewProvider(provider.KeycloakProvider(nil)) | ||
// Create a Pulumi provider mapping | ||
prov := tfbridge.ProviderInfo{ | ||
P: p, | ||
Name: "test", | ||
GitHubOrg: "testing", | ||
Description: "A Pulumi package for creating and managing test cloud resources.", | ||
Keywords: []string{"pulumi", "test"}, | ||
License: "Apache-2.0", | ||
Homepage: "https://pulumi.io", | ||
Repository: "https://github.com/pulumi/pulumi-keycloak", | ||
TFProviderLicense: refProviderLicense(tfbridge.MITLicenseType), | ||
UpstreamRepoPath: ".", | ||
} | ||
prov.SetAutonaming(255, "-") | ||
return prov | ||
} | ||
` | ||
|
||
// Write original program to temporary file | ||
tmpDir := t.TempDir() | ||
origPath := filepath.Join(tmpDir, "original.go") | ||
orig, err := os.Create(origPath) | ||
assert.Nil(t, err) | ||
_, err = orig.Write([]byte(origProgram)) | ||
assert.Nil(t, err) | ||
|
||
// Perform auto aliasing migration | ||
changesMade, err := AutoAliasingMigration(origPath, "test") | ||
assert.Nil(t, err) | ||
assert.True(t, changesMade) | ||
|
||
modified, err := os.ReadFile(origPath) | ||
assert.Nil(t, err) | ||
|
||
expected := `package test | ||
import ( | ||
"fmt" | ||
// embed is used to store bridge-metadata.json in the compiled binary | ||
_ "embed" | ||
"path/filepath" | ||
"strings" | ||
"unicode" | ||
"github.com/mrparkers/terraform-test/provider" | ||
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge" | ||
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/x" | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" | ||
) | ||
// all of the token components used below. | ||
const ( | ||
// packages: | ||
mainPkg = "test" | ||
// modules: | ||
mainMod = "index" // the y module | ||
) | ||
// Provider returns additional overlaid schema and metadata associated with the provider.. | ||
func Provider() tfbridge.ProviderInfo { | ||
// Instantiate the Terraform provider | ||
p := shimv2.NewProvider(provider.KeycloakProvider(nil)) | ||
// Create a Pulumi provider mapping | ||
prov := tfbridge.ProviderInfo{ | ||
P: p, | ||
Name: "test", | ||
GitHubOrg: "testing", | ||
Description: "A Pulumi package for creating and managing test cloud resources.", | ||
Keywords: []string{"pulumi", "test"}, | ||
License: "Apache-2.0", | ||
Homepage: "https://pulumi.io", | ||
Repository: "https://github.com/pulumi/pulumi-keycloak", | ||
TFProviderLicense: refProviderLicense(tfbridge.MITLicenseType), | ||
UpstreamRepoPath: ".", MetadataInfo: tfbridge.NewProviderMetadata(metadata), | ||
} | ||
err := x.AutoAliasing(&prov, prov.GetMetadata()) | ||
contract.AssertNoErrorf(err, "auto aliasing apply failed") | ||
prov.SetAutonaming(255, "-") | ||
return prov | ||
} | ||
//go:embed cmd/pulumi-resource-test/bridge-metadata.json | ||
var metadata []byte | ||
` | ||
// Compare against expected program | ||
assert.Equal(t, string(modified), expected) | ||
|
||
// Test running AutoAliasing twice doesn't change output | ||
changesMade, err = AutoAliasingMigration(origPath, "test") | ||
assert.Nil(t, err) | ||
assert.False(t, changesMade) | ||
|
||
modified2, err := os.ReadFile(origPath) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, string(modified2), expected) | ||
} |
Oops, something went wrong.