From 08422adc0f75dd58f62adc741a354aff507848e8 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Fri, 19 Jan 2024 06:39:54 +0300 Subject: [PATCH 1/8] Add the transformer "resolver" to remove API group imports for cross-resource reference resolver modules Signed-off-by: Alper Rifat Ulucinar --- cmd/resolver/main.go | 24 +++ cmd/resolver/resolver.go | 409 +++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- 3 files changed, 434 insertions(+), 1 deletion(-) create mode 100644 cmd/resolver/main.go create mode 100644 cmd/resolver/resolver.go diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go new file mode 100644 index 00000000..66bdf80c --- /dev/null +++ b/cmd/resolver/main.go @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: 2023 The Crossplane Authors +// +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "os" + "path/filepath" + + "gopkg.in/alecthomas/kingpin.v2" +) + +func main() { + var ( + app = kingpin.New(filepath.Base(os.Args[0]), "Transformer for the generated resolvers by the crossplane-tools so that cross API-group imports are removed.").DefaultEnvars() + apiGroupSuffix = app.Flag("apiGroupSuffix", "Resource API group suffix, such as aws.upbound.io. The resource API group names are suffixed with this to get the canonical API group name.").Short('g').Required().String() + pattern = app.Flag("pattern", "List patterns for the packages to process, such as ./apis/...").Short('p').Default("./apis/...").Strings() + resolverFilePattern = app.Flag("resolver", "Name of the generated resolver files to process.").Short('r').Default("zz_generated.resolvers.go").String() + ignorePackageLoadErrors = app.Flag("ignoreLoadErrors", "Ignore errors encountered while loading the packages.").Short('s').Bool() + ) + kingpin.MustParse(app.Parse(os.Args[1:])) + kingpin.FatalIfError(transformPackages(*apiGroupSuffix, *resolverFilePattern, *ignorePackageLoadErrors, *pattern...), "Failed to transform the resolver files in the specified packages.") +} diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go new file mode 100644 index 00000000..5dc92745 --- /dev/null +++ b/cmd/resolver/resolver.go @@ -0,0 +1,409 @@ +// SPDX-FileCopyrightText: 2023 The Crossplane Authors +// +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "fmt" + "go/ast" + "go/format" + "go/parser" + "go/token" + "os" + "path/filepath" + "strings" + + "github.com/pkg/errors" + "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/go/packages" +) + +const ( + varManagedResource = "m" + varManagedResourceList = "l" +) + +func transformPackages(apiGroupSuffix, resolverFilePattern string, ignorePackageLoadErrors bool, patterns ...string) error { + pkgs, err := packages.Load(&packages.Config{ + Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax, + }, patterns...) + if err != nil { + return errors.Wrapf(err, "failed to load the packages using the patterns %q", strings.Join(patterns, ",")) + } + + for _, p := range pkgs { + if err := toError(p); err != nil && !ignorePackageLoadErrors { + return errors.Wrapf(err, "failed to load the package %q", p.Name) + } + for i, f := range p.GoFiles { + if filepath.Base(f) != resolverFilePattern { + continue + } + if err := transformResolverFile(p.Fset, p.Syntax[i], f, strings.Trim(apiGroupSuffix, ".")); err != nil { + return errors.Wrapf(err, "failed to transform the resolver file %s", f) + } + } + } + return nil +} + +func toError(p *packages.Package) error { + if p == nil || len(p.Errors) == 0 { + return nil + } + sb := &strings.Builder{} + for _, e := range p.Errors { + if _, err := fmt.Fprintln(sb, e); err != nil { + return errors.Wrap(err, "failed to write the package parse error to the string builder") + } + } + return errors.New(sb.String()) +} + +type importUsage struct { + path string + used bool +} + +func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGroupSuffix string) error { //nolint:gocyclo // Arguably, easier to follow + importMap, err := addMRVariableDeclarations(node) + if err != nil { + return errors.Wrapf(err, "failed to add the managed resource variable declarations to the file %s", filePath) + } + + // Map to track imports used in reference.To structs + importsUsed := make(map[string]importUsage) + // assign is the assignment statement that assigns the values returned from + // `APIResolver.Resolve` or `APIResolver.ResolveMultiple` to the local + // variables in the MR kind's `ResolveReferences` function. + var assign *ast.AssignStmt + // block is the MR kind's `ResolveReferences` function's body block. + // We use this to find the correct place to inject MR variable + // declarations, calls to the type registry and error checks, etc. + var block *ast.BlockStmt + // these are the GVKs for the MR kind and the associated list kind + var group, version, kind, listKind string + + // traverse the AST loaded from the given source file to remove the + // cross API-group import statements from it. This helps in avoiding + // the import cycles related to the cross-resource references. + var inspectErr error + ast.Inspect(node, func(n ast.Node) bool { + switch x := n.(type) { + // this transformer takes care of removing the unneeded import statements + // (after the transformation), which are the target cross API-group + // references we are trying to avoid to prevent import cycles and appear + // in cross-resource reference targets. + case *ast.ImportSpec: + // initially, mark all imports as needed + key := "" + if x.Name != nil { + key = x.Name.Name + } else { + key = x.Path.Value + } + importsUsed[key] = importUsage{ + path: strings.Trim(x.Path.Value, `"`), + used: true, + } + + // keep a hold of the `ResolveReferences` function body so that we can + // properly inject variable declarations, error checks, etc. into the + // correct positions. + case *ast.FuncDecl: + block = x.Body + + // keep a hold of the `APIResolver.Resolve` and + // `APIResolver.ResolveMultiple` return value assignments as we will + // inject code right above it. + case *ast.AssignStmt: + assign = x + + // we will attempt to transform expressions such as + // `reference.To{List: &v1beta1.MRList{}, Managed: &v1beta1.MR{}}` + // into: + // `reference.To{List: l, Managed: m}`, where + // l and m are local variables holding the correctly types MR kind + // and MR list kind objects as the reference targets. + // Such expressions are the primary sources of cross API-group + // import statements. + // Cross API-group extractors are rare, and they should be + // handled when they're being added, this transformer does not + // consider them. + case *ast.KeyValueExpr: + // check if the key is "To" and the value is a CompositeLit + if key, ok := x.Key.(*ast.Ident); ok && key.Name == "To" { + // prevent a previous GVK from being reused + group, version, kind, listKind = "", "", "", "" + if cl, ok := x.Value.(*ast.CompositeLit); ok { + // check if there are any package qualifiers in the CompositeLit + for _, elt := range cl.Elts { + if kv, ok := elt.(*ast.KeyValueExpr); ok { + if uexpr, ok := kv.Value.(*ast.UnaryExpr); ok { + if cl, ok := uexpr.X.(*ast.CompositeLit); ok { + // then the reference target resides in another API group + // and the composite literal is a selector expression such as + // v1beta1.MR. In this case, we deduce the GV of the MR and + // list using the selector expression and the corresponding + // import statements (with the name as the expression). + // Kind and list kind are determined from the field selector. + key := kv.Key.(*ast.Ident).Name + if sexpr, ok := cl.Type.(*ast.SelectorExpr); ok { + if ident, ok := sexpr.X.(*ast.Ident); ok { + path := importsUsed[ident.Name].path + importsUsed[ident.Name] = importUsage{ + path: path, + used: false, + } + // we will parse the import path such as: + // github.com/upbound/provider-aws/apis/ec2/v1beta1 + // and extract the GV information from it. + tokens := strings.Split(path, "/") + // e.g., v1beta1 + version = tokens[len(tokens)-1] + // e.g., ec2.aws.upbound.io + group = fmt.Sprintf("%s.%s", tokens[len(tokens)-2], apiGroupSuffix) + // extract the kind and list kind names from the field + // selector. + if sexpr.Sel != nil { + if key == "List" { + listKind = sexpr.Sel.Name + } else { + kind = sexpr.Sel.Name + } + } + } + } else { + // then the reference target is in the same package as the + // source. We still transform it for uniformity and + // in the future, the source and target might still be + // moved to different packages. + // The GV information comes from file name in this case: + // apis/cur/v1beta1/zz_generated.resolvers.go + tokens := strings.Split(filePath, "/") + // e.g., v1beta1 + version = tokens[len(tokens)-2] + // e.g., cur.aws.upbound.io + group = fmt.Sprintf("%s.%s", tokens[len(tokens)-3], apiGroupSuffix) + if ident, ok := cl.Type.(*ast.Ident); ok { + if key == "List" { + listKind = ident.Name + } else { + kind = ident.Name + } + } + } + } + } + } + } + + // we will error if we could not determine the reference target GVKs + // for the MR and its list kind. + if group == "" || version == "" || kind == "" || listKind == "" { + inspectErr = errors.Errorf("failed to extract the GVKs for the reference targets. Group: %q, Version: %q, Kind: %q, List Kind: %q", group, version, kind, listKind) + return false + } + + // replace the value with a CompositeLit of type reference.To + // It's transformed into: + // reference.To{List: l, Managed: m} + x.Value = &ast.CompositeLit{ + Type: &ast.SelectorExpr{ + X: ast.NewIdent("reference"), + Sel: ast.NewIdent("To"), + }, + // here, l & m + Elts: []ast.Expr{ + &ast.KeyValueExpr{ + Key: ast.NewIdent("List"), + Value: ast.NewIdent(varManagedResourceList), + }, + &ast.KeyValueExpr{ + Key: ast.NewIdent("Managed"), + Value: ast.NewIdent(varManagedResource), + }, + }, + } + + // get the statements including the import statements we need to make + // calls to the type registry. + mrImports, stmts := getManagedResourceStatements(group, version, kind, listKind) + // insert the statements that implement type registry lookups + if !insertStatements(stmts, block, assign) { + inspectErr = errors.Errorf("failed to insert the type registry lookup statements for Group: %q, Version: %q, Kind: %q, List Kind: %q", group, version, kind, listKind) + return false + } + // add the new import statements we need to implement the + // type registry lookups. + for k, v := range mrImports { + importMap[k] = v + } + } + } + } + return true + }) + + if inspectErr != nil { + return errors.Wrap(inspectErr, "failed to inspect the resolver file for transformation") + } + + // remove the imports that are no longer used. + for _, decl := range node.Decls { + if gd, ok := decl.(*ast.GenDecl); ok && gd.Tok == token.IMPORT { + var newSpecs []ast.Spec + for _, spec := range gd.Specs { + if imp, ok := spec.(*ast.ImportSpec); ok { + var name string + if imp.Name != nil { + name = imp.Name.Name + } else { + name = imp.Path.Value + } + if usage, exists := importsUsed[name]; !exists || usage.used { + newSpecs = append(newSpecs, spec) + } + } + } + gd.Specs = newSpecs + + for path, name := range importMap { + gd.Specs = append(gd.Specs, &ast.ImportSpec{ + Name: &ast.Ident{ + Name: name, + }, + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: path, + }, + }) + } + } + } + + // dump the transformed resolver file + outFile, err := os.Create(filepath.Clean(filePath)) + if err != nil { + return errors.Wrap(err, "failed to open the resolver file for writing the transformed AST") + } + defer outFile.Close() //nolint:errcheck + + // write the modified AST back to the resolver file + return errors.Wrap(format.Node(outFile, fset, node), "failed to dump the transformed AST back into the resolver file") +} + +func insertStatements(stmts []ast.Stmt, block *ast.BlockStmt, assign *ast.AssignStmt) bool { + astutil.Apply(block, nil, func(c *astutil.Cursor) bool { + n := c.Node() + if n != assign { + return true + } + c.Replace(&ast.BlockStmt{ + List: append(stmts, assign), + }) + return false + }) + return true +} + +func addMRVariableDeclarations(f *ast.File) (map[string]string, error) { //nolint:gocyclo + varSrc := `package main + +import ( + xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" +) + +// reference resolver source objects +var m xpresource.Managed +var l xpresource.ManagedList +` + fset := token.NewFileSet() + varFile, err := parser.ParseFile(fset, "", varSrc, parser.ParseComments) + if err != nil { + return nil, errors.Wrap(err, "failed to parse the managed resource variables file") + } + var varDecls []ast.Stmt + importMap := make(map[string]string, 0) + for _, decl := range varFile.Decls { + if genDecl, ok := decl.(*ast.GenDecl); ok { + switch genDecl.Tok { //nolint:exhaustive + case token.VAR: + varDecls = append(varDecls, &ast.DeclStmt{Decl: genDecl}) + + case token.IMPORT: + for _, spec := range genDecl.Specs { + if importSpec, ok := spec.(*ast.ImportSpec); ok { + name := "" + if importSpec.Name != nil { + name = importSpec.Name.Name + } + importMap[importSpec.Path.Value] = name + } + } + } + } + } + + ast.Inspect(f, func(n ast.Node) bool { + fn, ok := n.(*ast.FuncDecl) + if !ok { + return true + } + + if fn.Name.Name == "ResolveReferences" && len(fn.Recv.List) > 0 { + fn.Body.List = append(varDecls, fn.Body.List...) + } + + return true + }) + + return importMap, nil +} + +func getManagedResourceStatements(group, version, kind, listKind string) (map[string]string, []ast.Stmt) { + stmtSrc := `package main + +import ( + apisresolver "github.com/upbound/provider-aws/internal/apis" +) + +func f() { + m, l, err = apisresolver.GetManagedResource("%s", "%s", "%s", "%s") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } +} +` + stmtSrc = fmt.Sprintf(stmtSrc, group, version, kind, listKind) + + fset := token.NewFileSet() + stmtFile, err := parser.ParseFile(fset, "", stmtSrc, parser.ParseComments) + if err != nil { + panic(err) + } + importMap := make(map[string]string, 0) + var stmts []ast.Stmt + for _, decl := range stmtFile.Decls { + switch x := decl.(type) { + case *ast.GenDecl: + if x.Tok == token.IMPORT { + for _, spec := range x.Specs { + if importSpec, ok := spec.(*ast.ImportSpec); ok { + name := "" + if importSpec.Name != nil { + name = importSpec.Name.Name + } + importMap[importSpec.Path.Value] = name + } + } + } + + case *ast.FuncDecl: + stmts = x.Body.List + } + + } + return importMap, stmts +} diff --git a/go.mod b/go.mod index 6f9fd35d..66fa03f1 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( github.com/zclconf/go-cty v1.14.1 github.com/zclconf/go-cty-yaml v1.0.3 golang.org/x/net v0.17.0 + golang.org/x/tools v0.13.0 gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.1 @@ -113,7 +114,6 @@ require ( golang.org/x/term v0.13.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.3.0 // indirect - golang.org/x/tools v0.13.0 // indirect gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.31.0 // indirect From eb38f3f7c82d507a9b72f63b209cc9353321f41a Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Tue, 23 Jan 2024 23:47:58 +0300 Subject: [PATCH 2/8] Do not transform already transformed files - Fix the floating comment issue for the very first function declaration Signed-off-by: Alper Rifat Ulucinar --- cmd/resolver/resolver.go | 44 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go index 5dc92745..b0ceefa8 100644 --- a/cmd/resolver/resolver.go +++ b/cmd/resolver/resolver.go @@ -22,6 +22,7 @@ import ( const ( varManagedResource = "m" varManagedResourceList = "l" + commentFileTransformed = "// Code transformed by upjet. DO NOT EDIT." ) func transformPackages(apiGroupSuffix, resolverFilePattern string, ignorePackageLoadErrors bool, patterns ...string) error { @@ -66,7 +67,43 @@ type importUsage struct { used bool } +func addTransformedComment(fset *token.FileSet, node *ast.File) bool { + cMap := ast.NewCommentMap(fset, node, node.Comments) + cgl := cMap[node] + for _, cg := range cgl { + for _, c := range cg.List { + if c != nil && c.Text == commentFileTransformed { + return false + } + } + } + switch { + case len(cgl) == 0: + cgl = []*ast.CommentGroup{ + { + List: []*ast.Comment{ + { + Text: commentFileTransformed, + Slash: node.FileStart, + }, + }, + }, + } + + default: + cgl[0].List = append(cgl[0].List, &ast.Comment{ + Text: commentFileTransformed, + Slash: cgl[0].End(), + }) + } + cMap[node] = cgl + return true +} + func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGroupSuffix string) error { //nolint:gocyclo // Arguably, easier to follow + if !addTransformedComment(fset, node) { + return nil + } importMap, err := addMRVariableDeclarations(node) if err != nil { return errors.Wrapf(err, "failed to add the managed resource variable declarations to the file %s", filePath) @@ -284,16 +321,21 @@ func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGro } // dump the transformed resolver file + adjustFunctionDocs(fset, node) outFile, err := os.Create(filepath.Clean(filePath)) if err != nil { return errors.Wrap(err, "failed to open the resolver file for writing the transformed AST") } - defer outFile.Close() //nolint:errcheck + defer func() { _ = outFile.Close() }() // write the modified AST back to the resolver file return errors.Wrap(format.Node(outFile, fset, node), "failed to dump the transformed AST back into the resolver file") } +func adjustFunctionDocs(fset *token.FileSet, node *ast.File) { + node.Decls[1].(*ast.FuncDecl).Doc.List[0].Slash = node.Decls[1].(*ast.FuncDecl).Name.Pos() +} + func insertStatements(stmts []ast.Stmt, block *ast.BlockStmt, assign *ast.AssignStmt) bool { astutil.Apply(block, nil, func(c *astutil.Cursor) bool { n := c.Node() From 2b1d5dfa7945ffa710e5ff4dcc6366a82d33ee71 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Wed, 24 Jan 2024 01:40:59 +0300 Subject: [PATCH 3/8] Set the default value of the transformer command-line option --pattern to "./..." Signed-off-by: Alper Rifat Ulucinar --- cmd/resolver/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go index 66bdf80c..77137f62 100644 --- a/cmd/resolver/main.go +++ b/cmd/resolver/main.go @@ -15,7 +15,7 @@ func main() { var ( app = kingpin.New(filepath.Base(os.Args[0]), "Transformer for the generated resolvers by the crossplane-tools so that cross API-group imports are removed.").DefaultEnvars() apiGroupSuffix = app.Flag("apiGroupSuffix", "Resource API group suffix, such as aws.upbound.io. The resource API group names are suffixed with this to get the canonical API group name.").Short('g').Required().String() - pattern = app.Flag("pattern", "List patterns for the packages to process, such as ./apis/...").Short('p').Default("./apis/...").Strings() + pattern = app.Flag("pattern", "List patterns for the packages to process, such as ./...").Short('p').Default("./...").Strings() resolverFilePattern = app.Flag("resolver", "Name of the generated resolver files to process.").Short('r').Default("zz_generated.resolvers.go").String() ignorePackageLoadErrors = app.Flag("ignoreLoadErrors", "Ignore errors encountered while loading the packages.").Short('s').Bool() ) From 3674eb5c0b3407fd2ba8b288289581d3213e0686 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Wed, 24 Jan 2024 02:50:21 +0300 Subject: [PATCH 4/8] Use a hard-coded AST while generating the reference source statements in function `getManagedResourceStatements` - Use a hard-coded AST while generating the reference source variable declarations in function `addMRVariableDeclarations` Signed-off-by: Alper Rifat Ulucinar --- cmd/resolver/resolver.go | 173 +++++++++++++++++++++------------------ 1 file changed, 92 insertions(+), 81 deletions(-) diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go index b0ceefa8..f1213869 100644 --- a/cmd/resolver/resolver.go +++ b/cmd/resolver/resolver.go @@ -8,7 +8,6 @@ import ( "fmt" "go/ast" "go/format" - "go/parser" "go/token" "os" "path/filepath" @@ -321,7 +320,7 @@ func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGro } // dump the transformed resolver file - adjustFunctionDocs(fset, node) + adjustFunctionDocs(node) outFile, err := os.Create(filepath.Clean(filePath)) if err != nil { return errors.Wrap(err, "failed to open the resolver file for writing the transformed AST") @@ -332,7 +331,7 @@ func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGro return errors.Wrap(format.Node(outFile, fset, node), "failed to dump the transformed AST back into the resolver file") } -func adjustFunctionDocs(fset *token.FileSet, node *ast.File) { +func adjustFunctionDocs(node *ast.File) { node.Decls[1].(*ast.FuncDecl).Doc.List[0].Slash = node.Decls[1].(*ast.FuncDecl).Name.Pos() } @@ -351,41 +350,34 @@ func insertStatements(stmts []ast.Stmt, block *ast.BlockStmt, assign *ast.Assign } func addMRVariableDeclarations(f *ast.File) (map[string]string, error) { //nolint:gocyclo - varSrc := `package main - -import ( - xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" -) - -// reference resolver source objects -var m xpresource.Managed -var l xpresource.ManagedList -` - fset := token.NewFileSet() - varFile, err := parser.ParseFile(fset, "", varSrc, parser.ParseComments) - if err != nil { - return nil, errors.Wrap(err, "failed to parse the managed resource variables file") + // prepare the first variable declaration: + // `var m xpresource.Managed` + varDecl1 := &ast.GenDecl{ + Tok: token.VAR, + Specs: []ast.Spec{ + &ast.ValueSpec{ + Names: []*ast.Ident{ast.NewIdent("m")}, + Type: &ast.SelectorExpr{ + X: ast.NewIdent("xpresource"), + Sel: ast.NewIdent("Managed"), + }, + }, + }, } - var varDecls []ast.Stmt - importMap := make(map[string]string, 0) - for _, decl := range varFile.Decls { - if genDecl, ok := decl.(*ast.GenDecl); ok { - switch genDecl.Tok { //nolint:exhaustive - case token.VAR: - varDecls = append(varDecls, &ast.DeclStmt{Decl: genDecl}) - - case token.IMPORT: - for _, spec := range genDecl.Specs { - if importSpec, ok := spec.(*ast.ImportSpec); ok { - name := "" - if importSpec.Name != nil { - name = importSpec.Name.Name - } - importMap[importSpec.Path.Value] = name - } - } - } - } + + // prepare the second variable declaration: + // `var l xpresource.ManagedList` + varDecl2 := &ast.GenDecl{ + Tok: token.VAR, + Specs: []ast.Spec{ + &ast.ValueSpec{ + Names: []*ast.Ident{ast.NewIdent("l")}, + Type: &ast.SelectorExpr{ + X: ast.NewIdent("xpresource"), + Sel: ast.NewIdent("ManagedList"), + }, + }, + }, } ast.Inspect(f, func(n ast.Node) bool { @@ -395,57 +387,76 @@ var l xpresource.ManagedList } if fn.Name.Name == "ResolveReferences" && len(fn.Recv.List) > 0 { - fn.Body.List = append(varDecls, fn.Body.List...) + fn.Body.List = append([]ast.Stmt{ + &ast.DeclStmt{Decl: varDecl1}, + &ast.DeclStmt{Decl: varDecl2}, + }, fn.Body.List...) } - return true }) - - return importMap, nil + return map[string]string{ + `"github.com/crossplane/crossplane-runtime/pkg/resource"`: "xpresource", + }, nil } func getManagedResourceStatements(group, version, kind, listKind string) (map[string]string, []ast.Stmt) { - stmtSrc := `package main - -import ( - apisresolver "github.com/upbound/provider-aws/internal/apis" -) - -func f() { - m, l, err = apisresolver.GetManagedResource("%s", "%s", "%s", "%s") - if err != nil { - return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + // prepare the assignment statement: + // `m, l, err = apisresolver.GetManagedResource("group", "version", "kind", "listKind")` + assignStmt := &ast.AssignStmt{ + Lhs: []ast.Expr{ + ast.NewIdent("m"), + ast.NewIdent("l"), + ast.NewIdent("err"), + }, + Tok: token.ASSIGN, + Rhs: []ast.Expr{ + &ast.CallExpr{ + Fun: &ast.SelectorExpr{ + X: ast.NewIdent("apisresolver"), + Sel: ast.NewIdent("GetManagedResource"), + }, + Args: []ast.Expr{ + &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s"`, group)}, + &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s"`, version)}, + &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s"`, kind)}, + &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf(`"%s"`, listKind)}, + }, + }, + }, } -} -` - stmtSrc = fmt.Sprintf(stmtSrc, group, version, kind, listKind) - - fset := token.NewFileSet() - stmtFile, err := parser.ParseFile(fset, "", stmtSrc, parser.ParseComments) - if err != nil { - panic(err) - } - importMap := make(map[string]string, 0) - var stmts []ast.Stmt - for _, decl := range stmtFile.Decls { - switch x := decl.(type) { - case *ast.GenDecl: - if x.Tok == token.IMPORT { - for _, spec := range x.Specs { - if importSpec, ok := spec.(*ast.ImportSpec); ok { - name := "" - if importSpec.Name != nil { - name = importSpec.Name.Name - } - importMap[importSpec.Path.Value] = name - } - } - } - - case *ast.FuncDecl: - stmts = x.Body.List - } + // prepare the if statement: + // ``` + // if err != nil { + // return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + // } + // ``` + ifStmt := &ast.IfStmt{ + Cond: &ast.BinaryExpr{ + X: ast.NewIdent("err"), + Op: token.NEQ, + Y: &ast.Ident{Name: "nil"}, + }, + Body: &ast.BlockStmt{ + List: []ast.Stmt{ + &ast.ReturnStmt{ + Results: []ast.Expr{ + &ast.CallExpr{ + Fun: &ast.SelectorExpr{ + X: ast.NewIdent("errors"), + Sel: ast.NewIdent("Wrap"), + }, + Args: []ast.Expr{ + ast.NewIdent("err"), + &ast.BasicLit{Kind: token.STRING, Value: `"failed to get the reference target managed resource and its list for reference resolution"`}, + }, + }, + }, + }, + }, + }, } - return importMap, stmts + return map[string]string{ + `"github.com/upbound/provider-aws/internal/apis"`: "apisresolver", + }, []ast.Stmt{assignStmt, ifStmt} } From bc7c1793a71f26056641b652cd54b5a75a44e58a Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Wed, 24 Jan 2024 04:15:13 +0300 Subject: [PATCH 5/8] Add new imports to the resolver files in stable order Signed-off-by: Alper Rifat Ulucinar --- cmd/resolver/resolver.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go index f1213869..688be9b5 100644 --- a/cmd/resolver/resolver.go +++ b/cmd/resolver/resolver.go @@ -11,6 +11,7 @@ import ( "go/token" "os" "path/filepath" + "slices" "strings" "github.com/pkg/errors" @@ -305,10 +306,16 @@ func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGro } gd.Specs = newSpecs - for path, name := range importMap { + newImportKeys := make([]string, 0, len(importMap)) + for k := range importMap { + newImportKeys = append(newImportKeys, k) + } + slices.Sort(newImportKeys) + + for _, path := range newImportKeys { gd.Specs = append(gd.Specs, &ast.ImportSpec{ Name: &ast.Ident{ - Name: name, + Name: importMap[path], }, Path: &ast.BasicLit{ Kind: token.STRING, From 14930173cbe0689760cc59609f87c617a7e8e1fb Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Wed, 24 Jan 2024 17:09:44 +0300 Subject: [PATCH 6/8] Move main.TransformPackages to transformers.Resolver.TransformPackages - Bump Go version to 1.21 - Bump golangci-lint to v1.55.2 - Add tests for the Resolver.TransformPackages Signed-off-by: Alper Rifat Ulucinar --- .github/workflows/ci.yml | 4 +- Makefile | 4 +- cmd/resolver/main.go | 11 +- go.mod | 2 +- go.sum | 11 + .../resolver => pkg/transformers}/resolver.go | 164 ++- pkg/transformers/resolver_test.go | 114 ++ .../testdata/SuccessfulTransformation.go.txt | 872 ++++++++++++++ ...uccessfulTransformation.transformed.go.txt | 1032 +++++++++++++++++ 9 files changed, 2188 insertions(+), 26 deletions(-) rename {cmd/resolver => pkg/transformers}/resolver.go (69%) create mode 100644 pkg/transformers/resolver_test.go create mode 100644 pkg/transformers/testdata/SuccessfulTransformation.go.txt create mode 100644 pkg/transformers/testdata/SuccessfulTransformation.transformed.go.txt diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dac05e1f..0a0bffaa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,8 +14,8 @@ on: env: # Common versions - GO_VERSION: "1.20" - GOLANGCI_VERSION: "v1.53.3" + GO_VERSION: "1.21" + GOLANGCI_VERSION: "v1.55.2" DOCKER_BUILDX_VERSION: "v0.8.2" # Common users. We can't run a step 'if secrets.AWS_USR != ""' but we can run diff --git a/Makefile b/Makefile index 940c7a81..cda2468e 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,8 @@ PROJECT_REPO := github.com/crossplane/$(PROJECT_NAME) # GOLANGCILINT_VERSION is inherited from build submodule by default. # Uncomment below if you need to override the version. -# GOLANGCILINT_VERSION ?= 1.54.0 -GO_REQUIRED_VERSION ?= 1.20 +GOLANGCILINT_VERSION ?= 1.55.2 +GO_REQUIRED_VERSION ?= 1.21 PLATFORMS ?= linux_amd64 linux_arm64 # -include will silently skip missing files, which allows us diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go index 77137f62..ccf8b1c2 100644 --- a/cmd/resolver/main.go +++ b/cmd/resolver/main.go @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2023 The Crossplane Authors +// SPDX-FileCopyrightText: 2024 The Crossplane Authors // // SPDX-License-Identifier: Apache-2.0 @@ -8,7 +8,12 @@ import ( "os" "path/filepath" + "github.com/crossplane/crossplane-runtime/pkg/logging" + "github.com/spf13/afero" "gopkg.in/alecthomas/kingpin.v2" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + + "github.com/crossplane/upjet/pkg/transformers" ) func main() { @@ -20,5 +25,7 @@ func main() { ignorePackageLoadErrors = app.Flag("ignoreLoadErrors", "Ignore errors encountered while loading the packages.").Short('s').Bool() ) kingpin.MustParse(app.Parse(os.Args[1:])) - kingpin.FatalIfError(transformPackages(*apiGroupSuffix, *resolverFilePattern, *ignorePackageLoadErrors, *pattern...), "Failed to transform the resolver files in the specified packages.") + logger := logging.NewLogrLogger(zap.New().WithName("transformer-resolver")) + r := transformers.NewResolver(afero.NewOsFs(), *apiGroupSuffix, *ignorePackageLoadErrors, logger) + kingpin.FatalIfError(r.TransformPackages(*resolverFilePattern, *pattern...), "Failed to transform the resolver files in the specified packages.") } diff --git a/go.mod b/go.mod index 66fa03f1..65eaef97 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ module github.com/crossplane/upjet -go 1.20 +go 1.21 require ( dario.cat/mergo v1.0.0 diff --git a/go.sum b/go.sum index 5ea7e3de..48362ec2 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,7 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 h1:EKPd1INOIyr5hWOWhvpmQpY6tKjeG0hT1s3AMC/9fic= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1/go.mod h1:VzwV+t+dZ9j/H867F1M2ziD+yLHtB46oM35FxxMJ4d0= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= @@ -78,6 +79,7 @@ github.com/crossplane/crossplane v1.13.2/go.mod h1:jjYHNF5j2JidsrFZ7sfTZoVnBDVEv github.com/crossplane/crossplane-runtime v1.14.0-rc.0.0.20231011070344-cc691421c2e5 h1:K1Km6NCu9+VlZB3CmWSjrs09cDSbwQxJd2Qw2002dFs= github.com/crossplane/crossplane-runtime v1.14.0-rc.0.0.20231011070344-cc691421c2e5/go.mod h1:kCS5576be8g++HhiDGEBUw+8nkW8p4jhURYeC0zx8jM= github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI= +github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -117,6 +119,7 @@ github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g= github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M= github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8= @@ -191,6 +194,7 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98 h1:pUa4ghanp6q4IJHwE9RwLgmVFfReJN+KbQ8ExNEUUoQ= +github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= @@ -244,6 +248,7 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -289,7 +294,9 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m github.com/muvaf/typewriter v0.0.0-20210910160850-80e49fe1eb32 h1:yBQlHXLeUJL3TWVmzup5uT3wG5FLxhiTAiTsmNVocys= github.com/muvaf/typewriter v0.0.0-20210910160850-80e49fe1eb32/go.mod h1:SAAdeMEiFXR8LcHffvIdiLI1w243DCH2DuHq7UrA5YQ= github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU= +github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM= github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= +github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -309,8 +316,10 @@ github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+Pymzi github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY= github.com/spf13/afero v1.10.0/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= @@ -331,6 +340,7 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tmccombs/hcl2json v0.3.3 h1:+DLNYqpWE0CsOQiEZu+OZm5ZBImake3wtITYxQ8uLFQ= github.com/tmccombs/hcl2json v0.3.3/go.mod h1:Y2chtz2x9bAeRTvSibVRVgbLJhLJXKlUeIvjeVdnm4w= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= @@ -370,6 +380,7 @@ go.starlark.net v0.0.0-20230525235612-a134d8f9ddca/go.mod h1:jxU+3+j+71eXOW14274 go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= +go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= diff --git a/cmd/resolver/resolver.go b/pkg/transformers/resolver.go similarity index 69% rename from cmd/resolver/resolver.go rename to pkg/transformers/resolver.go index 688be9b5..9a0ff9e9 100644 --- a/cmd/resolver/resolver.go +++ b/pkg/transformers/resolver.go @@ -1,20 +1,21 @@ -// SPDX-FileCopyrightText: 2023 The Crossplane Authors +// SPDX-FileCopyrightText: 2024 The Crossplane Authors // // SPDX-License-Identifier: Apache-2.0 -package main +package transformers import ( "fmt" "go/ast" "go/format" "go/token" - "os" "path/filepath" "slices" "strings" + "github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/pkg/errors" + "github.com/spf13/afero" "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/packages" ) @@ -23,25 +24,149 @@ const ( varManagedResource = "m" varManagedResourceList = "l" commentFileTransformed = "// Code transformed by upjet. DO NOT EDIT." + + defaultLoadMode = packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax ) -func transformPackages(apiGroupSuffix, resolverFilePattern string, ignorePackageLoadErrors bool, patterns ...string) error { - pkgs, err := packages.Load(&packages.Config{ - Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax, - }, patterns...) +// Resolver transforms the source resolver implementations so that +// the resolution source managed resources are no longer statically typed +// and thus, the implementations no longer need to import the corresponding +// API packages. This transformer is helpful in preventing the import cycles +// described in https://github.com/crossplane/upjet/issues/96 +// and elsewhere. Please see TransformPackages for the details of the +// transformation applied. +type Resolver struct { + // the FS implementation used for storing the transformed output + fs afero.Fs + // the API group suffix to be used for the resolution source + // managed resources, such as aws.upbound.io. Then a sample + // API group for a resource is ec2.aws.upbound.io. + apiGroupSuffix string + // When set, any errors encountered while loading the source packages is + // silently ignored if a logger is not configured, + // or logged via the configured logger. + // We need to set this when, for example, loading resolver implementations + // with import cycles, or when transforming just one package and not loading + // the referenced typed. + ignorePackageLoadErrors bool + logger logging.Logger + config *packages.Config +} + +// NewResolver initializes a new Resolver with the specified configuration. +func NewResolver(fs afero.Fs, apiGroupSuffix string, ignorePackageLoadErrors bool, logger logging.Logger, opts ...ResolverOption) *Resolver { + if logger == nil { + logger = logging.NewNopLogger() + } + r := &Resolver{ + fs: fs, + apiGroupSuffix: apiGroupSuffix, + ignorePackageLoadErrors: ignorePackageLoadErrors, + logger: logger, + config: &packages.Config{ + Mode: defaultLoadMode, + }, + } + for _, o := range opts { + o(r) + } + return r +} + +// ResolverOption is an option used to configure the Resolver. +type ResolverOption func(resolver *Resolver) + +// WithLoaderConfig configures the package loader config for a Resolver. +func WithLoaderConfig(c *packages.Config) ResolverOption { + return func(r *Resolver) { + r.config = c + } +} + +// TransformPackages applies the dynamic resolver transformation to +// the resolver modules loaded from the specified patterns and +// implemented in the specified resolver files. If `r.ignorePackageLoadErrors` +// is set, any errors encountered while loading the source packages are +// ignored. This may be required when the transformation source files have +// compile errors, such as import cycles. The transformed resolver +// implementations will use the specified API group suffix, such as, +// "aws.upbound.io" when determining the API groups of the resolution +// source managed resources. +// A sample transformation implemented by this transformer is from: +// ``` +// +// func (mg *Subnet) ResolveReferences(ctx context.Context, c client.Reader) error { +// r := reference.NewAPIResolver(c, mg) +// +// var rsp reference.ResolutionResponse +// var err error +// +// rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ +// CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.VPCID), +// Extract: reference.ExternalName(), +// Reference: mg.Spec.ForProvider.VPCIDRef, +// Selector: mg.Spec.ForProvider.VPCIDSelector, +// To: reference.To{ +// List: &VPCList{}, +// Managed: &VPC{}, +// }, +// }) +// if err != nil { +// return errors.Wrap(err, "mg.Spec.ForProvider.VPCID") +// } +// mg.Spec.ForProvider.VPCID = reference.ToPtrValue(rsp.ResolvedValue) +// mg.Spec.ForProvider.VPCIDRef = rsp.ResolvedReference +// +// ``` +// to the following: +// ``` +// +// func (mg *Subnet) ResolveReferences(ctx context.Context, c client.Reader) error { +// var m xpresource.Managed +// var l xpresource.ManagedList +// r := reference.NewAPIResolver(c, mg) +// +// var rsp reference.ResolutionResponse +// var err error +// { +// m, l, err = apisresolver.GetManagedResource("ec2.aws.upbound.io", "v1beta1", "VPC", "VPCList") +// if err != nil { +// return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") +// } +// +// rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ +// CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.VPCID), +// Extract: reference.ExternalName(), +// Reference: mg.Spec.ForProvider.VPCIDRef, +// Selector: mg.Spec.ForProvider.VPCIDSelector, +// To: reference.To{List: l, Managed: m}, +// }) +// } +// if err != nil { +// return errors.Wrap(err, "mg.Spec.ForProvider.VPCID") +// } +// mg.Spec.ForProvider.VPCID = reference.ToPtrValue(rsp.ResolvedValue) +// mg.Spec.ForProvider.VPCIDRef = rsp.ResolvedReference +// +// ``` +func (r *Resolver) TransformPackages(resolverFilePattern string, patterns ...string) error { + pkgs, err := packages.Load(r.config, patterns...) if err != nil { return errors.Wrapf(err, "failed to load the packages using the patterns %q", strings.Join(patterns, ",")) } for _, p := range pkgs { - if err := toError(p); err != nil && !ignorePackageLoadErrors { - return errors.Wrapf(err, "failed to load the package %q", p.Name) + if err := toError(p); err != nil { + if !r.ignorePackageLoadErrors { + return errors.Wrapf(err, "failed to load the package %q", p.Name) + } + r.logger.Info("Encounter the following issues when loading a package", "package", p.Name, "issues", err.Error()) } for i, f := range p.GoFiles { if filepath.Base(f) != resolverFilePattern { continue } - if err := transformResolverFile(p.Fset, p.Syntax[i], f, strings.Trim(apiGroupSuffix, ".")); err != nil { + if err := r.transformResolverFile(p.Fset, p.Syntax[i], f, strings.Trim(r.apiGroupSuffix, ".")); err != nil { return errors.Wrapf(err, "failed to transform the resolver file %s", f) } } @@ -100,15 +225,13 @@ func addTransformedComment(fset *token.FileSet, node *ast.File) bool { return true } -func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGroupSuffix string) error { //nolint:gocyclo // Arguably, easier to follow +func (r *Resolver) transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGroupSuffix string) error { //nolint:gocyclo // Arguably, easier to follow if !addTransformedComment(fset, node) { return nil } - importMap, err := addMRVariableDeclarations(node) - if err != nil { - return errors.Wrapf(err, "failed to add the managed resource variable declarations to the file %s", filePath) - } - + // add resolution source variable declarations to the `ResolveReferences` + // function bodies. + importMap := addMRVariableDeclarations(node) // Map to track imports used in reference.To structs importsUsed := make(map[string]importUsage) // assign is the assignment statement that assigns the values returned from @@ -325,10 +448,13 @@ func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGro } } } + return r.dumpTransformed(fset, node, filePath) +} +func (r *Resolver) dumpTransformed(fset *token.FileSet, node *ast.File, filePath string) error { // dump the transformed resolver file adjustFunctionDocs(node) - outFile, err := os.Create(filepath.Clean(filePath)) + outFile, err := r.fs.Create(filepath.Clean(filePath)) if err != nil { return errors.Wrap(err, "failed to open the resolver file for writing the transformed AST") } @@ -356,7 +482,7 @@ func insertStatements(stmts []ast.Stmt, block *ast.BlockStmt, assign *ast.Assign return true } -func addMRVariableDeclarations(f *ast.File) (map[string]string, error) { //nolint:gocyclo +func addMRVariableDeclarations(f *ast.File) map[string]string { // prepare the first variable declaration: // `var m xpresource.Managed` varDecl1 := &ast.GenDecl{ @@ -403,7 +529,7 @@ func addMRVariableDeclarations(f *ast.File) (map[string]string, error) { //nolin }) return map[string]string{ `"github.com/crossplane/crossplane-runtime/pkg/resource"`: "xpresource", - }, nil + } } func getManagedResourceStatements(group, version, kind, listKind string) (map[string]string, []ast.Stmt) { diff --git a/pkg/transformers/resolver_test.go b/pkg/transformers/resolver_test.go new file mode 100644 index 00000000..19249e5a --- /dev/null +++ b/pkg/transformers/resolver_test.go @@ -0,0 +1,114 @@ +// SPDX-FileCopyrightText: 2024 The Crossplane Authors +// +// SPDX-License-Identifier: Apache-2.0 + +package transformers + +import ( + "path/filepath" + "testing" + + "github.com/crossplane/crossplane-runtime/pkg/test" + "github.com/google/go-cmp/cmp" + "github.com/spf13/afero" + "golang.org/x/tools/go/packages/packagestest" +) + +func TestTransformPackages(t *testing.T) { + // args struct to define input arguments for each test case + type args struct { + apiGroupSuffix string + resolverFilePattern string + ignorePackageLoadErrors bool + patterns []string + inputFilePath string + } + + // want struct to define the expected outcome for each test case + type want struct { + err error + transformedPath string + } + + // testCase struct for defining test cases + type testCase struct { + reason string + args args + want want + } + + cases := map[string]testCase{ + "SuccessfulTransformation": { + reason: "Transformation of the source file that has been generated by crossplane-tool's angryjet succeeds with the expected transformed file.", + args: args{ + apiGroupSuffix: "aws.upbound.io", + resolverFilePattern: "zz_generated.resolvers.go", + inputFilePath: "testdata/SuccessfulTransformation.go.txt", + ignorePackageLoadErrors: true, + patterns: []string{"./testdata"}, + }, + want: want{ + transformedPath: "testdata/SuccessfulTransformation.transformed.go.txt", + }, + }, + "TransformationIdempotency": { + reason: "The applied transformation is idempotent, i.e., applying the transformer on an already transformed file does not change the transformed file.", + args: args{ + apiGroupSuffix: "aws.upbound.io", + resolverFilePattern: "zz_generated.resolvers.go", + inputFilePath: "testdata/SuccessfulTransformation.transformed.go.txt", + ignorePackageLoadErrors: true, + patterns: []string{"./testdata"}, + }, + want: want{ + transformedPath: "testdata/SuccessfulTransformation.transformed.go.txt", + }, + }, + // Other test cases + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + inputFileContents := readFile(t, afero.NewOsFs(), tc.args.inputFilePath, tc.reason) + exported := packagestest.Export(t, packagestest.Modules, []packagestest.Module{{ + Name: "fake", + Files: map[string]interface{}{ + filepath.Join("testdata", tc.args.resolverFilePattern): inputFileContents, + }}}) + defer exported.Cleanup() + exported.Config.Mode = defaultLoadMode + memFS := afero.NewMemMapFs() + transformedFilePath := filepath.Join(exported.Temp(), "fake", "testdata", tc.args.resolverFilePattern) + writeFile(t, memFS, transformedFilePath, []byte(inputFileContents), tc.reason) + + r := NewResolver(memFS, tc.args.apiGroupSuffix, tc.args.ignorePackageLoadErrors, nil, WithLoaderConfig(exported.Config)) + err := r.TransformPackages("zz_generated.resolvers.go", tc.args.patterns...) + if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { + t.Errorf("\n%s\nResolver.TransformPackages(...): -wantErr, +gotErr:\n%s", tc.reason, diff) + } + if tc.want.err != nil { + return + } + if diff := cmp.Diff(readFile(t, afero.NewOsFs(), tc.want.transformedPath, tc.reason), + readFile(t, memFS, transformedFilePath, tc.reason)); diff != "" { + t.Errorf("\n%s\nResolver.TransformPackages(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func readFile(t *testing.T, fs afero.Fs, filePath string, reason string) string { + buff, err := afero.ReadFile(fs, filePath) + if err != nil { + t.Fatalf("\n%s\n: Failed to write the test artifact to the path %s: %v", reason, filePath, err) + } + return string(buff) +} + +func writeFile(t *testing.T, fs afero.Fs, filePath string, buff []byte, reason string) string { + err := afero.WriteFile(fs, filePath, buff, 0o600) + if err != nil { + t.Fatalf("\n%s\n: Failed to load the test artifact from the path %s: %v", reason, filePath, err) + } + return string(buff) +} diff --git a/pkg/transformers/testdata/SuccessfulTransformation.go.txt b/pkg/transformers/testdata/SuccessfulTransformation.go.txt new file mode 100644 index 00000000..2f5b9e9a --- /dev/null +++ b/pkg/transformers/testdata/SuccessfulTransformation.go.txt @@ -0,0 +1,872 @@ +// SPDX-FileCopyrightText: 2024 The Crossplane Authors +// +// SPDX-License-Identifier: Apache-2.0 +// Code generated by angryjet. DO NOT EDIT. + +package v1beta1 + +import ( + "context" + reference "github.com/crossplane/crossplane-runtime/pkg/reference" + resource "github.com/crossplane/upjet/pkg/resource" + errors "github.com/pkg/errors" + v1beta11 "github.com/upbound/provider-aws/apis/acm/v1beta1" + v1beta13 "github.com/upbound/provider-aws/apis/ec2/v1beta1" + v1beta12 "github.com/upbound/provider-aws/apis/iam/v1beta1" + v1beta1 "github.com/upbound/provider-aws/apis/lambda/v1beta1" + common "github.com/upbound/provider-aws/config/common" + apis "github.com/upbound/provider-aws/config/common/apis" + lambda "github.com/upbound/provider-aws/config/common/apis/lambda" + client "sigs.k8s.io/controller-runtime/pkg/client" +) + +// ResolveReferences of this APIMapping. +func (mg *APIMapping) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.DomainName), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.DomainNameRef, + Selector: mg.Spec.ForProvider.DomainNameSelector, + To: reference.To{ + List: &DomainNameList{}, + Managed: &DomainName{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.DomainName") + } + mg.Spec.ForProvider.DomainName = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.DomainNameRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.Stage), + Extract: common.TerraformID(), + Reference: mg.Spec.ForProvider.StageRef, + Selector: mg.Spec.ForProvider.StageSelector, + To: reference.To{ + List: &StageList{}, + Managed: &Stage{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.Stage") + } + mg.Spec.ForProvider.Stage = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.StageRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.DomainName), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.DomainNameRef, + Selector: mg.Spec.InitProvider.DomainNameSelector, + To: reference.To{ + List: &DomainNameList{}, + Managed: &DomainName{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.DomainName") + } + mg.Spec.InitProvider.DomainName = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.DomainNameRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.Stage), + Extract: common.TerraformID(), + Reference: mg.Spec.InitProvider.StageRef, + Selector: mg.Spec.InitProvider.StageSelector, + To: reference.To{ + List: &StageList{}, + Managed: &Stage{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.Stage") + } + mg.Spec.InitProvider.Stage = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.StageRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Authorizer. +func (mg *Authorizer) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.AuthorizerURI), + Extract: lambda.FunctionInvokeARN(), + Reference: mg.Spec.ForProvider.AuthorizerURIRef, + Selector: mg.Spec.ForProvider.AuthorizerURISelector, + To: reference.To{ + List: &v1beta1.FunctionList{}, + Managed: &v1beta1.Function{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.AuthorizerURI") + } + mg.Spec.ForProvider.AuthorizerURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.AuthorizerURIRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.AuthorizerURI), + Extract: lambda.FunctionInvokeARN(), + Reference: mg.Spec.InitProvider.AuthorizerURIRef, + Selector: mg.Spec.InitProvider.AuthorizerURISelector, + To: reference.To{ + List: &v1beta1.FunctionList{}, + Managed: &v1beta1.Function{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.AuthorizerURI") + } + mg.Spec.InitProvider.AuthorizerURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.AuthorizerURIRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Deployment. +func (mg *Deployment) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this DomainName. +func (mg *DomainName) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + for i3 := 0; i3 < len(mg.Spec.ForProvider.DomainNameConfiguration); i3++ { + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArn), + Extract: common.ARNExtractor(), + Reference: mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArnRef, + Selector: mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArnSelector, + To: reference.To{ + List: &v1beta11.CertificateList{}, + Managed: &v1beta11.Certificate{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArn") + } + mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArnRef = rsp.ResolvedReference + + } + for i3 := 0; i3 < len(mg.Spec.InitProvider.DomainNameConfiguration); i3++ { + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArn), + Extract: common.ARNExtractor(), + Reference: mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArnRef, + Selector: mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArnSelector, + To: reference.To{ + List: &v1beta11.CertificateList{}, + Managed: &v1beta11.Certificate{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArn") + } + mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArnRef = rsp.ResolvedReference + + } + + return nil +} + +// ResolveReferences of this Integration. +func (mg *Integration) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.ConnectionID), + Extract: resource.ExtractResourceID(), + Reference: mg.Spec.ForProvider.ConnectionIDRef, + Selector: mg.Spec.ForProvider.ConnectionIDSelector, + To: reference.To{ + List: &VPCLinkList{}, + Managed: &VPCLink{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.ConnectionID") + } + mg.Spec.ForProvider.ConnectionID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.ConnectionIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.CredentialsArn), + Extract: resource.ExtractParamPath("arn", true), + Reference: mg.Spec.ForProvider.CredentialsArnRef, + Selector: mg.Spec.ForProvider.CredentialsArnSelector, + To: reference.To{ + List: &v1beta12.RoleList{}, + Managed: &v1beta12.Role{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.CredentialsArn") + } + mg.Spec.ForProvider.CredentialsArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.CredentialsArnRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.IntegrationURI), + Extract: resource.ExtractParamPath("invoke_arn", true), + Reference: mg.Spec.ForProvider.IntegrationURIRef, + Selector: mg.Spec.ForProvider.IntegrationURISelector, + To: reference.To{ + List: &v1beta1.FunctionList{}, + Managed: &v1beta1.Function{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.IntegrationURI") + } + mg.Spec.ForProvider.IntegrationURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.IntegrationURIRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.ConnectionID), + Extract: resource.ExtractResourceID(), + Reference: mg.Spec.InitProvider.ConnectionIDRef, + Selector: mg.Spec.InitProvider.ConnectionIDSelector, + To: reference.To{ + List: &VPCLinkList{}, + Managed: &VPCLink{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.ConnectionID") + } + mg.Spec.InitProvider.ConnectionID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.ConnectionIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.CredentialsArn), + Extract: resource.ExtractParamPath("arn", true), + Reference: mg.Spec.InitProvider.CredentialsArnRef, + Selector: mg.Spec.InitProvider.CredentialsArnSelector, + To: reference.To{ + List: &v1beta12.RoleList{}, + Managed: &v1beta12.Role{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.CredentialsArn") + } + mg.Spec.InitProvider.CredentialsArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.CredentialsArnRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.IntegrationURI), + Extract: resource.ExtractParamPath("invoke_arn", true), + Reference: mg.Spec.InitProvider.IntegrationURIRef, + Selector: mg.Spec.InitProvider.IntegrationURISelector, + To: reference.To{ + List: &v1beta1.FunctionList{}, + Managed: &v1beta1.Function{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.IntegrationURI") + } + mg.Spec.InitProvider.IntegrationURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.IntegrationURIRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this IntegrationResponse. +func (mg *IntegrationResponse) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.IntegrationID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.IntegrationIDRef, + Selector: mg.Spec.ForProvider.IntegrationIDSelector, + To: reference.To{ + List: &IntegrationList{}, + Managed: &Integration{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.IntegrationID") + } + mg.Spec.ForProvider.IntegrationID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.IntegrationIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.IntegrationID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.IntegrationIDRef, + Selector: mg.Spec.InitProvider.IntegrationIDSelector, + To: reference.To{ + List: &IntegrationList{}, + Managed: &Integration{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.IntegrationID") + } + mg.Spec.InitProvider.IntegrationID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.IntegrationIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Model. +func (mg *Model) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Route. +func (mg *Route) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.AuthorizerID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.AuthorizerIDRef, + Selector: mg.Spec.ForProvider.AuthorizerIDSelector, + To: reference.To{ + List: &AuthorizerList{}, + Managed: &Authorizer{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.AuthorizerID") + } + mg.Spec.ForProvider.AuthorizerID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.AuthorizerIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.Target), + Extract: apis.IntegrationIDPrefixed(), + Reference: mg.Spec.ForProvider.TargetRef, + Selector: mg.Spec.ForProvider.TargetSelector, + To: reference.To{ + List: &IntegrationList{}, + Managed: &Integration{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.Target") + } + mg.Spec.ForProvider.Target = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.TargetRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.AuthorizerID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.AuthorizerIDRef, + Selector: mg.Spec.InitProvider.AuthorizerIDSelector, + To: reference.To{ + List: &AuthorizerList{}, + Managed: &Authorizer{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.AuthorizerID") + } + mg.Spec.InitProvider.AuthorizerID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.AuthorizerIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.Target), + Extract: apis.IntegrationIDPrefixed(), + Reference: mg.Spec.InitProvider.TargetRef, + Selector: mg.Spec.InitProvider.TargetSelector, + To: reference.To{ + List: &IntegrationList{}, + Managed: &Integration{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.Target") + } + mg.Spec.InitProvider.Target = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.TargetRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this RouteResponse. +func (mg *RouteResponse) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.RouteID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.RouteIDRef, + Selector: mg.Spec.ForProvider.RouteIDSelector, + To: reference.To{ + List: &RouteList{}, + Managed: &Route{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.RouteID") + } + mg.Spec.ForProvider.RouteID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.RouteIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.RouteID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.RouteIDRef, + Selector: mg.Spec.InitProvider.RouteIDSelector, + To: reference.To{ + List: &RouteList{}, + Managed: &Route{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.RouteID") + } + mg.Spec.InitProvider.RouteID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.RouteIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Stage. +func (mg *Stage) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.DeploymentID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.DeploymentIDRef, + Selector: mg.Spec.ForProvider.DeploymentIDSelector, + To: reference.To{ + List: &DeploymentList{}, + Managed: &Deployment{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.DeploymentID") + } + mg.Spec.ForProvider.DeploymentID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.DeploymentIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{ + List: &APIList{}, + Managed: &API{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.DeploymentID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.DeploymentIDRef, + Selector: mg.Spec.InitProvider.DeploymentIDSelector, + To: reference.To{ + List: &DeploymentList{}, + Managed: &Deployment{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.DeploymentID") + } + mg.Spec.InitProvider.DeploymentID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.DeploymentIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this VPCLink. +func (mg *VPCLink) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var mrsp reference.MultiResolutionResponse + var err error + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.ForProvider.SecurityGroupIds), + Extract: reference.ExternalName(), + References: mg.Spec.ForProvider.SecurityGroupIDRefs, + Selector: mg.Spec.ForProvider.SecurityGroupIDSelector, + To: reference.To{ + List: &v1beta13.SecurityGroupList{}, + Managed: &v1beta13.SecurityGroup{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.SecurityGroupIds") + } + mg.Spec.ForProvider.SecurityGroupIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.ForProvider.SecurityGroupIDRefs = mrsp.ResolvedReferences + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.ForProvider.SubnetIds), + Extract: reference.ExternalName(), + References: mg.Spec.ForProvider.SubnetIDRefs, + Selector: mg.Spec.ForProvider.SubnetIDSelector, + To: reference.To{ + List: &v1beta13.SubnetList{}, + Managed: &v1beta13.Subnet{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.SubnetIds") + } + mg.Spec.ForProvider.SubnetIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.ForProvider.SubnetIDRefs = mrsp.ResolvedReferences + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.InitProvider.SecurityGroupIds), + Extract: reference.ExternalName(), + References: mg.Spec.InitProvider.SecurityGroupIDRefs, + Selector: mg.Spec.InitProvider.SecurityGroupIDSelector, + To: reference.To{ + List: &v1beta13.SecurityGroupList{}, + Managed: &v1beta13.SecurityGroup{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.SecurityGroupIds") + } + mg.Spec.InitProvider.SecurityGroupIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.InitProvider.SecurityGroupIDRefs = mrsp.ResolvedReferences + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.InitProvider.SubnetIds), + Extract: reference.ExternalName(), + References: mg.Spec.InitProvider.SubnetIDRefs, + Selector: mg.Spec.InitProvider.SubnetIDSelector, + To: reference.To{ + List: &v1beta13.SubnetList{}, + Managed: &v1beta13.Subnet{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.SubnetIds") + } + mg.Spec.InitProvider.SubnetIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.InitProvider.SubnetIDRefs = mrsp.ResolvedReferences + + return nil +} diff --git a/pkg/transformers/testdata/SuccessfulTransformation.transformed.go.txt b/pkg/transformers/testdata/SuccessfulTransformation.transformed.go.txt new file mode 100644 index 00000000..6115fe39 --- /dev/null +++ b/pkg/transformers/testdata/SuccessfulTransformation.transformed.go.txt @@ -0,0 +1,1032 @@ +// SPDX-FileCopyrightText: 2024 The Crossplane Authors +// +// SPDX-License-Identifier: Apache-2.0 +// Code generated by angryjet. DO NOT EDIT. +// Code transformed by upjet. DO NOT EDIT. + +package v1beta1 + +import ( + "context" + reference "github.com/crossplane/crossplane-runtime/pkg/reference" + resource "github.com/crossplane/upjet/pkg/resource" + errors "github.com/pkg/errors" + + xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" + common "github.com/upbound/provider-aws/config/common" + apis "github.com/upbound/provider-aws/config/common/apis" + lambda "github.com/upbound/provider-aws/config/common/apis/lambda" + apisresolver "github.com/upbound/provider-aws/internal/apis" + client "sigs.k8s.io/controller-runtime/pkg/client" +) + +func (mg *APIMapping) ResolveReferences( // ResolveReferences of this APIMapping. + ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "DomainName", "DomainNameList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.DomainName), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.DomainNameRef, + Selector: mg.Spec.ForProvider.DomainNameSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.DomainName") + } + mg.Spec.ForProvider.DomainName = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.DomainNameRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Stage", "StageList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.Stage), + Extract: common.TerraformID(), + Reference: mg.Spec.ForProvider.StageRef, + Selector: mg.Spec.ForProvider.StageSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.Stage") + } + mg.Spec.ForProvider.Stage = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.StageRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "DomainName", "DomainNameList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.DomainName), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.DomainNameRef, + Selector: mg.Spec.InitProvider.DomainNameSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.DomainName") + } + mg.Spec.InitProvider.DomainName = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.DomainNameRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Stage", "StageList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.Stage), + Extract: common.TerraformID(), + Reference: mg.Spec.InitProvider.StageRef, + Selector: mg.Spec.InitProvider.StageSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.Stage") + } + mg.Spec.InitProvider.Stage = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.StageRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Authorizer. +func (mg *Authorizer) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("lambda.aws.upbound.io", "v1beta1", "Function", "FunctionList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.AuthorizerURI), + Extract: lambda.FunctionInvokeARN(), + Reference: mg.Spec.ForProvider.AuthorizerURIRef, + Selector: mg.Spec.ForProvider.AuthorizerURISelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.AuthorizerURI") + } + mg.Spec.ForProvider.AuthorizerURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.AuthorizerURIRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("lambda.aws.upbound.io", "v1beta1", "Function", "FunctionList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.AuthorizerURI), + Extract: lambda.FunctionInvokeARN(), + Reference: mg.Spec.InitProvider.AuthorizerURIRef, + Selector: mg.Spec.InitProvider.AuthorizerURISelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.AuthorizerURI") + } + mg.Spec.InitProvider.AuthorizerURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.AuthorizerURIRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Deployment. +func (mg *Deployment) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this DomainName. +func (mg *DomainName) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + for i3 := 0; i3 < len(mg.Spec.ForProvider.DomainNameConfiguration); i3++ { + { + m, l, err = apisresolver.GetManagedResource("acm.aws.upbound.io", "v1beta1", "Certificate", "CertificateList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArn), + Extract: common.ARNExtractor(), + Reference: mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArnRef, + Selector: mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArnSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArn") + } + mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArnRef = rsp.ResolvedReference + + } + for i3 := 0; i3 < len(mg.Spec.InitProvider.DomainNameConfiguration); i3++ { + { + m, l, err = apisresolver.GetManagedResource("acm.aws.upbound.io", "v1beta1", "Certificate", "CertificateList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArn), + Extract: common.ARNExtractor(), + Reference: mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArnRef, + Selector: mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArnSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArn") + } + mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArnRef = rsp.ResolvedReference + + } + + return nil +} + +// ResolveReferences of this Integration. +func (mg *Integration) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "VPCLink", "VPCLinkList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.ConnectionID), + Extract: resource.ExtractResourceID(), + Reference: mg.Spec.ForProvider.ConnectionIDRef, + Selector: mg.Spec.ForProvider.ConnectionIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.ConnectionID") + } + mg.Spec.ForProvider.ConnectionID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.ConnectionIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("iam.aws.upbound.io", "v1beta1", "Role", "RoleList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.CredentialsArn), + Extract: resource.ExtractParamPath("arn", true), + Reference: mg.Spec.ForProvider.CredentialsArnRef, + Selector: mg.Spec.ForProvider.CredentialsArnSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.CredentialsArn") + } + mg.Spec.ForProvider.CredentialsArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.CredentialsArnRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("lambda.aws.upbound.io", "v1beta1", "Function", "FunctionList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.IntegrationURI), + Extract: resource.ExtractParamPath("invoke_arn", true), + Reference: mg.Spec.ForProvider.IntegrationURIRef, + Selector: mg.Spec.ForProvider.IntegrationURISelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.IntegrationURI") + } + mg.Spec.ForProvider.IntegrationURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.IntegrationURIRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "VPCLink", "VPCLinkList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.ConnectionID), + Extract: resource.ExtractResourceID(), + Reference: mg.Spec.InitProvider.ConnectionIDRef, + Selector: mg.Spec.InitProvider.ConnectionIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.ConnectionID") + } + mg.Spec.InitProvider.ConnectionID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.ConnectionIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("iam.aws.upbound.io", "v1beta1", "Role", "RoleList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.CredentialsArn), + Extract: resource.ExtractParamPath("arn", true), + Reference: mg.Spec.InitProvider.CredentialsArnRef, + Selector: mg.Spec.InitProvider.CredentialsArnSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.CredentialsArn") + } + mg.Spec.InitProvider.CredentialsArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.CredentialsArnRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("lambda.aws.upbound.io", "v1beta1", "Function", "FunctionList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.IntegrationURI), + Extract: resource.ExtractParamPath("invoke_arn", true), + Reference: mg.Spec.InitProvider.IntegrationURIRef, + Selector: mg.Spec.InitProvider.IntegrationURISelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.IntegrationURI") + } + mg.Spec.InitProvider.IntegrationURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.IntegrationURIRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this IntegrationResponse. +func (mg *IntegrationResponse) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Integration", "IntegrationList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.IntegrationID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.IntegrationIDRef, + Selector: mg.Spec.ForProvider.IntegrationIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.IntegrationID") + } + mg.Spec.ForProvider.IntegrationID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.IntegrationIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Integration", "IntegrationList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.IntegrationID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.IntegrationIDRef, + Selector: mg.Spec.InitProvider.IntegrationIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.IntegrationID") + } + mg.Spec.InitProvider.IntegrationID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.IntegrationIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Model. +func (mg *Model) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Route. +func (mg *Route) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Authorizer", "AuthorizerList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.AuthorizerID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.AuthorizerIDRef, + Selector: mg.Spec.ForProvider.AuthorizerIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.AuthorizerID") + } + mg.Spec.ForProvider.AuthorizerID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.AuthorizerIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Integration", "IntegrationList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.Target), + Extract: apis.IntegrationIDPrefixed(), + Reference: mg.Spec.ForProvider.TargetRef, + Selector: mg.Spec.ForProvider.TargetSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.Target") + } + mg.Spec.ForProvider.Target = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.TargetRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Authorizer", "AuthorizerList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.AuthorizerID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.AuthorizerIDRef, + Selector: mg.Spec.InitProvider.AuthorizerIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.AuthorizerID") + } + mg.Spec.InitProvider.AuthorizerID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.AuthorizerIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Integration", "IntegrationList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.Target), + Extract: apis.IntegrationIDPrefixed(), + Reference: mg.Spec.InitProvider.TargetRef, + Selector: mg.Spec.InitProvider.TargetSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.Target") + } + mg.Spec.InitProvider.Target = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.TargetRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this RouteResponse. +func (mg *RouteResponse) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Route", "RouteList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.RouteID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.RouteIDRef, + Selector: mg.Spec.ForProvider.RouteIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.RouteID") + } + mg.Spec.ForProvider.RouteID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.RouteIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Route", "RouteList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.RouteID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.RouteIDRef, + Selector: mg.Spec.InitProvider.RouteIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.RouteID") + } + mg.Spec.InitProvider.RouteID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.RouteIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Stage. +func (mg *Stage) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Deployment", "DeploymentList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.DeploymentID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.DeploymentIDRef, + Selector: mg.Spec.ForProvider.DeploymentIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.DeploymentID") + } + mg.Spec.ForProvider.DeploymentID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.DeploymentIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Deployment", "DeploymentList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.DeploymentID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.DeploymentIDRef, + Selector: mg.Spec.InitProvider.DeploymentIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.DeploymentID") + } + mg.Spec.InitProvider.DeploymentID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.DeploymentIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this VPCLink. +func (mg *VPCLink) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var mrsp reference.MultiResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("ec2.aws.upbound.io", "v1beta1", "SecurityGroup", "SecurityGroupList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.ForProvider.SecurityGroupIds), + Extract: reference.ExternalName(), + References: mg.Spec.ForProvider.SecurityGroupIDRefs, + Selector: mg.Spec.ForProvider.SecurityGroupIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.SecurityGroupIds") + } + mg.Spec.ForProvider.SecurityGroupIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.ForProvider.SecurityGroupIDRefs = mrsp.ResolvedReferences + { + m, l, err = apisresolver.GetManagedResource("ec2.aws.upbound.io", "v1beta1", "Subnet", "SubnetList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.ForProvider.SubnetIds), + Extract: reference.ExternalName(), + References: mg.Spec.ForProvider.SubnetIDRefs, + Selector: mg.Spec.ForProvider.SubnetIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.SubnetIds") + } + mg.Spec.ForProvider.SubnetIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.ForProvider.SubnetIDRefs = mrsp.ResolvedReferences + { + m, l, err = apisresolver.GetManagedResource("ec2.aws.upbound.io", "v1beta1", "SecurityGroup", "SecurityGroupList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.InitProvider.SecurityGroupIds), + Extract: reference.ExternalName(), + References: mg.Spec.InitProvider.SecurityGroupIDRefs, + Selector: mg.Spec.InitProvider.SecurityGroupIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.SecurityGroupIds") + } + mg.Spec.InitProvider.SecurityGroupIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.InitProvider.SecurityGroupIDRefs = mrsp.ResolvedReferences + { + m, l, err = apisresolver.GetManagedResource("ec2.aws.upbound.io", "v1beta1", "Subnet", "SubnetList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.InitProvider.SubnetIds), + Extract: reference.ExternalName(), + References: mg.Spec.InitProvider.SubnetIDRefs, + Selector: mg.Spec.InitProvider.SubnetIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.SubnetIds") + } + mg.Spec.InitProvider.SubnetIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.InitProvider.SubnetIDRefs = mrsp.ResolvedReferences + + return nil +} From 994c07aef2f9a83ef64264e456277227cab34be3 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Thu, 25 Jan 2024 15:35:39 +0300 Subject: [PATCH 7/8] Add failure tests for the resolver transformer Signed-off-by: Alper Rifat Ulucinar --- pkg/transformers/resolver_test.go | 40 ++++++++++++++---- ...n.go.txt => apigatewayv2.resolvers.go.txt} | 0 ...apigatewayv2.resolvers.transformed.go.txt} | 0 .../missing_list_type.resolvers.go.txt | 42 +++++++++++++++++++ 4 files changed, 74 insertions(+), 8 deletions(-) rename pkg/transformers/testdata/{SuccessfulTransformation.go.txt => apigatewayv2.resolvers.go.txt} (100%) rename pkg/transformers/testdata/{SuccessfulTransformation.transformed.go.txt => apigatewayv2.resolvers.transformed.go.txt} (100%) create mode 100644 pkg/transformers/testdata/missing_list_type.resolvers.go.txt diff --git a/pkg/transformers/resolver_test.go b/pkg/transformers/resolver_test.go index 19249e5a..8feb2b30 100644 --- a/pkg/transformers/resolver_test.go +++ b/pkg/transformers/resolver_test.go @@ -10,6 +10,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" + "github.com/pkg/errors" "github.com/spf13/afero" "golang.org/x/tools/go/packages/packagestest" ) @@ -26,7 +27,8 @@ func TestTransformPackages(t *testing.T) { // want struct to define the expected outcome for each test case type want struct { - err error + // errFunc receives the transformed resolver file's path + errFunc func(string) error transformedPath string } @@ -43,12 +45,12 @@ func TestTransformPackages(t *testing.T) { args: args{ apiGroupSuffix: "aws.upbound.io", resolverFilePattern: "zz_generated.resolvers.go", - inputFilePath: "testdata/SuccessfulTransformation.go.txt", + inputFilePath: "testdata/apigatewayv2.resolvers.go.txt", ignorePackageLoadErrors: true, patterns: []string{"./testdata"}, }, want: want{ - transformedPath: "testdata/SuccessfulTransformation.transformed.go.txt", + transformedPath: "testdata/apigatewayv2.resolvers.transformed.go.txt", }, }, "TransformationIdempotency": { @@ -56,15 +58,33 @@ func TestTransformPackages(t *testing.T) { args: args{ apiGroupSuffix: "aws.upbound.io", resolverFilePattern: "zz_generated.resolvers.go", - inputFilePath: "testdata/SuccessfulTransformation.transformed.go.txt", + inputFilePath: "testdata/apigatewayv2.resolvers.transformed.go.txt", ignorePackageLoadErrors: true, patterns: []string{"./testdata"}, }, want: want{ - transformedPath: "testdata/SuccessfulTransformation.transformed.go.txt", + transformedPath: "testdata/apigatewayv2.resolvers.transformed.go.txt", + }, + }, + "TransformationFailure": { + reason: "The transformation source is not a valid angryjet-generated resolver file: List type is missing for a resolution source.", + args: args{ + apiGroupSuffix: "aws.upbound.io", + resolverFilePattern: "zz_generated.resolvers.go", + inputFilePath: "testdata/missing_list_type.resolvers.go.txt", + ignorePackageLoadErrors: true, + patterns: []string{"./testdata"}, + }, + want: want{ + errFunc: func(transformedPath string) error { + return errors.Wrapf( + errors.Wrap( + errors.New(`failed to extract the GVKs for the reference targets. Group: "fake.aws.upbound.io", Version: "testdata", Kind: "Project", List Kind: ""`), + "failed to inspect the resolver file for transformation"), + "failed to transform the resolver file %s", transformedPath) + }, }, }, - // Other test cases } for name, tc := range cases { @@ -83,10 +103,14 @@ func TestTransformPackages(t *testing.T) { r := NewResolver(memFS, tc.args.apiGroupSuffix, tc.args.ignorePackageLoadErrors, nil, WithLoaderConfig(exported.Config)) err := r.TransformPackages("zz_generated.resolvers.go", tc.args.patterns...) - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { + var wantErr error + if tc.want.errFunc != nil { + wantErr = tc.want.errFunc(transformedFilePath) + } + if diff := cmp.Diff(wantErr, err, test.EquateErrors()); diff != "" { t.Errorf("\n%s\nResolver.TransformPackages(...): -wantErr, +gotErr:\n%s", tc.reason, diff) } - if tc.want.err != nil { + if wantErr != nil { return } if diff := cmp.Diff(readFile(t, afero.NewOsFs(), tc.want.transformedPath, tc.reason), diff --git a/pkg/transformers/testdata/SuccessfulTransformation.go.txt b/pkg/transformers/testdata/apigatewayv2.resolvers.go.txt similarity index 100% rename from pkg/transformers/testdata/SuccessfulTransformation.go.txt rename to pkg/transformers/testdata/apigatewayv2.resolvers.go.txt diff --git a/pkg/transformers/testdata/SuccessfulTransformation.transformed.go.txt b/pkg/transformers/testdata/apigatewayv2.resolvers.transformed.go.txt similarity index 100% rename from pkg/transformers/testdata/SuccessfulTransformation.transformed.go.txt rename to pkg/transformers/testdata/apigatewayv2.resolvers.transformed.go.txt diff --git a/pkg/transformers/testdata/missing_list_type.resolvers.go.txt b/pkg/transformers/testdata/missing_list_type.resolvers.go.txt new file mode 100644 index 00000000..094e562d --- /dev/null +++ b/pkg/transformers/testdata/missing_list_type.resolvers.go.txt @@ -0,0 +1,42 @@ +// SPDX-FileCopyrightText: 2024 The Crossplane Authors +// +// SPDX-License-Identifier: Apache-2.0 +// Code generated by angryjet. DO NOT EDIT. + +package v1beta1 + +import ( + "context" + reference "github.com/crossplane/crossplane-runtime/pkg/reference" + resource "github.com/crossplane/upjet/pkg/resource" + errors "github.com/pkg/errors" + client "sigs.k8s.io/controller-runtime/pkg/client" +) + +// ResolveReferences of this Feature. +func (mg *Feature) ResolveReferences(ctx context.Context, c client.Reader) error { + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.Project), + Extract: resource.ExtractParamPath("name", false), + Reference: mg.Spec.ForProvider.ProjectRef, + Selector: mg.Spec.ForProvider.ProjectSelector, + // Please note the missing List type in the below expression + // This is not expected for the generated resolver + // implementations by angryjet. + To: reference.To{ + Managed: &Project{}, + }, + }) + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.Project") + } + mg.Spec.ForProvider.Project = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.ProjectRef = rsp.ResolvedReference + + return nil +} From bbac0a6bdd8836a8c6d65690526e38cc4ca61f4d Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Fri, 26 Jan 2024 19:10:25 +0300 Subject: [PATCH 8/8] Add the --api-group-override command-line option to the resolver transformation to be able to override the generated API group names. - Add the --api-resolver-package command-line option to the resolver transformation to properly configure the package of the GetManagedResource function. Signed-off-by: Alper Rifat Ulucinar --- cmd/resolver/main.go | 4 +- pkg/transformers/resolver.go | 51 +- pkg/transformers/resolver_test.go | 26 +- ...pigatewayv2.resolvers.withoverrides.go.txt | 1032 +++++++++++++++++ 4 files changed, 1107 insertions(+), 6 deletions(-) create mode 100644 pkg/transformers/testdata/apigatewayv2.resolvers.withoverrides.go.txt diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go index ccf8b1c2..6b769747 100644 --- a/cmd/resolver/main.go +++ b/cmd/resolver/main.go @@ -20,12 +20,14 @@ func main() { var ( app = kingpin.New(filepath.Base(os.Args[0]), "Transformer for the generated resolvers by the crossplane-tools so that cross API-group imports are removed.").DefaultEnvars() apiGroupSuffix = app.Flag("apiGroupSuffix", "Resource API group suffix, such as aws.upbound.io. The resource API group names are suffixed with this to get the canonical API group name.").Short('g').Required().String() + apiGroupOverride = app.Flag("apiGroupOverride", "API group overrides").Short('o').StringMap() + apiResolverPackage = app.Flag("apiResolverPackage", "The package that contains the implementation for the GetManagedResource function, such as github.com/upbound/provider-aws/internal/apis.").Short('a').Required().String() pattern = app.Flag("pattern", "List patterns for the packages to process, such as ./...").Short('p').Default("./...").Strings() resolverFilePattern = app.Flag("resolver", "Name of the generated resolver files to process.").Short('r').Default("zz_generated.resolvers.go").String() ignorePackageLoadErrors = app.Flag("ignoreLoadErrors", "Ignore errors encountered while loading the packages.").Short('s').Bool() ) kingpin.MustParse(app.Parse(os.Args[1:])) logger := logging.NewLogrLogger(zap.New().WithName("transformer-resolver")) - r := transformers.NewResolver(afero.NewOsFs(), *apiGroupSuffix, *ignorePackageLoadErrors, logger) + r := transformers.NewResolver(afero.NewOsFs(), *apiGroupSuffix, *apiResolverPackage, *ignorePackageLoadErrors, logger, transformers.WithAPIGroupOverrides(*apiGroupOverride)) kingpin.FatalIfError(r.TransformPackages(*resolverFilePattern, *pattern...), "Failed to transform the resolver files in the specified packages.") } diff --git a/pkg/transformers/resolver.go b/pkg/transformers/resolver.go index 9a0ff9e9..8078616b 100644 --- a/pkg/transformers/resolver.go +++ b/pkg/transformers/resolver.go @@ -42,6 +42,21 @@ type Resolver struct { // managed resources, such as aws.upbound.io. Then a sample // API group for a resource is ec2.aws.upbound.io. apiGroupSuffix string + // API group overrides for the provider. Certain providers need + // to rename the short API group names they use, breaking the + // convention that the short group name matches the package name. + // An example is upbound/provider-azure, where the ResourceGroup.azure + // resource's short API group is the empty string. This map allows such + // providers to control the names of the generated API group names by this + // Resolver transformer. + apiGroupOverrides map[string]string + // the API resolver package that contains the + // `GetManagedResource("group", "version", "kind", "listKind")` + // function. This function is used to initialize a managed resource and + // its list type, owned by the provider, with the given API group, version, + // kind and list kind. Signature of the resolver function is as follows: + // func GetManagedResource(group, version, kind, listKind string) (xpresource.Managed, xpresource.ManagedList, error) + apiResolverPackage string // When set, any errors encountered while loading the source packages is // silently ignored if a logger is not configured, // or logged via the configured logger. @@ -54,13 +69,14 @@ type Resolver struct { } // NewResolver initializes a new Resolver with the specified configuration. -func NewResolver(fs afero.Fs, apiGroupSuffix string, ignorePackageLoadErrors bool, logger logging.Logger, opts ...ResolverOption) *Resolver { +func NewResolver(fs afero.Fs, apiGroupSuffix, apiResolverPackage string, ignorePackageLoadErrors bool, logger logging.Logger, opts ...ResolverOption) *Resolver { if logger == nil { logger = logging.NewNopLogger() } r := &Resolver{ fs: fs, apiGroupSuffix: apiGroupSuffix, + apiResolverPackage: apiResolverPackage, ignorePackageLoadErrors: ignorePackageLoadErrors, logger: logger, config: &packages.Config{ @@ -83,6 +99,17 @@ func WithLoaderConfig(c *packages.Config) ResolverOption { } } +// WithAPIGroupOverrides configures the API group overrides for a Resolver. +// Certain providers need to rename the short API group names they use, +// breaking the convention that the short group name matches the package name. +// An example is upbound/provider-azure, where the ResourceGroup.azure +// resource's short API group is the empty string. +func WithAPIGroupOverrides(overrides map[string]string) ResolverOption { + return func(r *Resolver) { + r.apiGroupOverrides = overrides + } +} + // TransformPackages applies the dynamic resolver transformation to // the resolver modules loaded from the specified patterns and // implemented in the specified resolver files. If `r.ignorePackageLoadErrors` @@ -324,6 +351,8 @@ func (r *Resolver) transformResolverFile(fset *token.FileSet, node *ast.File, fi version = tokens[len(tokens)-1] // e.g., ec2.aws.upbound.io group = fmt.Sprintf("%s.%s", tokens[len(tokens)-2], apiGroupSuffix) + // apply any configured group name overrides + group = r.overrideGroupName(group) // extract the kind and list kind names from the field // selector. if sexpr.Sel != nil { @@ -346,6 +375,8 @@ func (r *Resolver) transformResolverFile(fset *token.FileSet, node *ast.File, fi version = tokens[len(tokens)-2] // e.g., cur.aws.upbound.io group = fmt.Sprintf("%s.%s", tokens[len(tokens)-3], apiGroupSuffix) + // apply any configured group name overrides + group = r.overrideGroupName(group) if ident, ok := cl.Type.(*ast.Ident); ok { if key == "List" { listKind = ident.Name @@ -389,7 +420,7 @@ func (r *Resolver) transformResolverFile(fset *token.FileSet, node *ast.File, fi // get the statements including the import statements we need to make // calls to the type registry. - mrImports, stmts := getManagedResourceStatements(group, version, kind, listKind) + mrImports, stmts := getManagedResourceStatements(group, version, kind, listKind, r.apiResolverPackage) // insert the statements that implement type registry lookups if !insertStatements(stmts, block, assign) { inspectErr = errors.Errorf("failed to insert the type registry lookup statements for Group: %q, Version: %q, Kind: %q, List Kind: %q", group, version, kind, listKind) @@ -532,7 +563,7 @@ func addMRVariableDeclarations(f *ast.File) map[string]string { } } -func getManagedResourceStatements(group, version, kind, listKind string) (map[string]string, []ast.Stmt) { +func getManagedResourceStatements(group, version, kind, listKind, apiResolverPackage string) (map[string]string, []ast.Stmt) { // prepare the assignment statement: // `m, l, err = apisresolver.GetManagedResource("group", "version", "kind", "listKind")` assignStmt := &ast.AssignStmt{ @@ -590,6 +621,18 @@ func getManagedResourceStatements(group, version, kind, listKind string) (map[st }, } return map[string]string{ - `"github.com/upbound/provider-aws/internal/apis"`: "apisresolver", + // TODO: we may need to parameterize the import alias in the future, if + // any provider that uses the transformer has an import alias collision + // which is not very likely. + fmt.Sprintf(`"%s"`, apiResolverPackage): "apisresolver", }, []ast.Stmt{assignStmt, ifStmt} } + +func (r *Resolver) overrideGroupName(group string) string { + g, ok := r.apiGroupOverrides[group] + // we need to allow overrides with an empty string + if !ok { + return group + } + return g +} diff --git a/pkg/transformers/resolver_test.go b/pkg/transformers/resolver_test.go index 8feb2b30..efd6f949 100644 --- a/pkg/transformers/resolver_test.go +++ b/pkg/transformers/resolver_test.go @@ -19,6 +19,8 @@ func TestTransformPackages(t *testing.T) { // args struct to define input arguments for each test case type args struct { apiGroupSuffix string + apiResolverPackage string + apiGroupOverrides map[string]string resolverFilePattern string ignorePackageLoadErrors bool patterns []string @@ -44,6 +46,7 @@ func TestTransformPackages(t *testing.T) { reason: "Transformation of the source file that has been generated by crossplane-tool's angryjet succeeds with the expected transformed file.", args: args{ apiGroupSuffix: "aws.upbound.io", + apiResolverPackage: "github.com/upbound/provider-aws/internal/apis", resolverFilePattern: "zz_generated.resolvers.go", inputFilePath: "testdata/apigatewayv2.resolvers.go.txt", ignorePackageLoadErrors: true, @@ -57,6 +60,7 @@ func TestTransformPackages(t *testing.T) { reason: "The applied transformation is idempotent, i.e., applying the transformer on an already transformed file does not change the transformed file.", args: args{ apiGroupSuffix: "aws.upbound.io", + apiResolverPackage: "github.com/upbound/provider-aws/internal/apis", resolverFilePattern: "zz_generated.resolvers.go", inputFilePath: "testdata/apigatewayv2.resolvers.transformed.go.txt", ignorePackageLoadErrors: true, @@ -70,6 +74,7 @@ func TestTransformPackages(t *testing.T) { reason: "The transformation source is not a valid angryjet-generated resolver file: List type is missing for a resolution source.", args: args{ apiGroupSuffix: "aws.upbound.io", + apiResolverPackage: "github.com/upbound/provider-aws/internal/apis", resolverFilePattern: "zz_generated.resolvers.go", inputFilePath: "testdata/missing_list_type.resolvers.go.txt", ignorePackageLoadErrors: true, @@ -85,6 +90,23 @@ func TestTransformPackages(t *testing.T) { }, }, }, + "SuccessfulTransformationWithGroupOverrides": { + reason: "Transformation of the source file with a group overrides configuration succeeds with the expected transformed file.", + args: args{ + apiGroupSuffix: "aws.upbound.io", + apiResolverPackage: "github.com/upbound/provider-aws/internal/resolver", + apiGroupOverrides: map[string]string{ + "ec2.aws.upbound.io": "replacedec2.aws.upbound.io", + }, + resolverFilePattern: "zz_generated.resolvers.go", + inputFilePath: "testdata/apigatewayv2.resolvers.go.txt", + ignorePackageLoadErrors: true, + patterns: []string{"./testdata"}, + }, + want: want{ + transformedPath: "testdata/apigatewayv2.resolvers.withoverrides.go.txt", + }, + }, } for name, tc := range cases { @@ -101,7 +123,9 @@ func TestTransformPackages(t *testing.T) { transformedFilePath := filepath.Join(exported.Temp(), "fake", "testdata", tc.args.resolverFilePattern) writeFile(t, memFS, transformedFilePath, []byte(inputFileContents), tc.reason) - r := NewResolver(memFS, tc.args.apiGroupSuffix, tc.args.ignorePackageLoadErrors, nil, WithLoaderConfig(exported.Config)) + r := NewResolver(memFS, tc.args.apiGroupSuffix, tc.args.apiResolverPackage, tc.args.ignorePackageLoadErrors, nil, + WithLoaderConfig(exported.Config), + WithAPIGroupOverrides(tc.args.apiGroupOverrides)) err := r.TransformPackages("zz_generated.resolvers.go", tc.args.patterns...) var wantErr error if tc.want.errFunc != nil { diff --git a/pkg/transformers/testdata/apigatewayv2.resolvers.withoverrides.go.txt b/pkg/transformers/testdata/apigatewayv2.resolvers.withoverrides.go.txt new file mode 100644 index 00000000..fcf1d5d8 --- /dev/null +++ b/pkg/transformers/testdata/apigatewayv2.resolvers.withoverrides.go.txt @@ -0,0 +1,1032 @@ +// SPDX-FileCopyrightText: 2024 The Crossplane Authors +// +// SPDX-License-Identifier: Apache-2.0 +// Code generated by angryjet. DO NOT EDIT. +// Code transformed by upjet. DO NOT EDIT. + +package v1beta1 + +import ( + "context" + reference "github.com/crossplane/crossplane-runtime/pkg/reference" + resource "github.com/crossplane/upjet/pkg/resource" + errors "github.com/pkg/errors" + + xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" + common "github.com/upbound/provider-aws/config/common" + apis "github.com/upbound/provider-aws/config/common/apis" + lambda "github.com/upbound/provider-aws/config/common/apis/lambda" + apisresolver "github.com/upbound/provider-aws/internal/resolver" + client "sigs.k8s.io/controller-runtime/pkg/client" +) + +func (mg *APIMapping) ResolveReferences( // ResolveReferences of this APIMapping. + ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "DomainName", "DomainNameList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.DomainName), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.DomainNameRef, + Selector: mg.Spec.ForProvider.DomainNameSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.DomainName") + } + mg.Spec.ForProvider.DomainName = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.DomainNameRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Stage", "StageList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.Stage), + Extract: common.TerraformID(), + Reference: mg.Spec.ForProvider.StageRef, + Selector: mg.Spec.ForProvider.StageSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.Stage") + } + mg.Spec.ForProvider.Stage = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.StageRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "DomainName", "DomainNameList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.DomainName), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.DomainNameRef, + Selector: mg.Spec.InitProvider.DomainNameSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.DomainName") + } + mg.Spec.InitProvider.DomainName = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.DomainNameRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Stage", "StageList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.Stage), + Extract: common.TerraformID(), + Reference: mg.Spec.InitProvider.StageRef, + Selector: mg.Spec.InitProvider.StageSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.Stage") + } + mg.Spec.InitProvider.Stage = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.StageRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Authorizer. +func (mg *Authorizer) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("lambda.aws.upbound.io", "v1beta1", "Function", "FunctionList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.AuthorizerURI), + Extract: lambda.FunctionInvokeARN(), + Reference: mg.Spec.ForProvider.AuthorizerURIRef, + Selector: mg.Spec.ForProvider.AuthorizerURISelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.AuthorizerURI") + } + mg.Spec.ForProvider.AuthorizerURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.AuthorizerURIRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("lambda.aws.upbound.io", "v1beta1", "Function", "FunctionList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.AuthorizerURI), + Extract: lambda.FunctionInvokeARN(), + Reference: mg.Spec.InitProvider.AuthorizerURIRef, + Selector: mg.Spec.InitProvider.AuthorizerURISelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.AuthorizerURI") + } + mg.Spec.InitProvider.AuthorizerURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.AuthorizerURIRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Deployment. +func (mg *Deployment) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this DomainName. +func (mg *DomainName) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + + for i3 := 0; i3 < len(mg.Spec.ForProvider.DomainNameConfiguration); i3++ { + { + m, l, err = apisresolver.GetManagedResource("acm.aws.upbound.io", "v1beta1", "Certificate", "CertificateList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArn), + Extract: common.ARNExtractor(), + Reference: mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArnRef, + Selector: mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArnSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArn") + } + mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.DomainNameConfiguration[i3].CertificateArnRef = rsp.ResolvedReference + + } + for i3 := 0; i3 < len(mg.Spec.InitProvider.DomainNameConfiguration); i3++ { + { + m, l, err = apisresolver.GetManagedResource("acm.aws.upbound.io", "v1beta1", "Certificate", "CertificateList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArn), + Extract: common.ARNExtractor(), + Reference: mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArnRef, + Selector: mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArnSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArn") + } + mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.DomainNameConfiguration[i3].CertificateArnRef = rsp.ResolvedReference + + } + + return nil +} + +// ResolveReferences of this Integration. +func (mg *Integration) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "VPCLink", "VPCLinkList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.ConnectionID), + Extract: resource.ExtractResourceID(), + Reference: mg.Spec.ForProvider.ConnectionIDRef, + Selector: mg.Spec.ForProvider.ConnectionIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.ConnectionID") + } + mg.Spec.ForProvider.ConnectionID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.ConnectionIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("iam.aws.upbound.io", "v1beta1", "Role", "RoleList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.CredentialsArn), + Extract: resource.ExtractParamPath("arn", true), + Reference: mg.Spec.ForProvider.CredentialsArnRef, + Selector: mg.Spec.ForProvider.CredentialsArnSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.CredentialsArn") + } + mg.Spec.ForProvider.CredentialsArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.CredentialsArnRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("lambda.aws.upbound.io", "v1beta1", "Function", "FunctionList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.IntegrationURI), + Extract: resource.ExtractParamPath("invoke_arn", true), + Reference: mg.Spec.ForProvider.IntegrationURIRef, + Selector: mg.Spec.ForProvider.IntegrationURISelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.IntegrationURI") + } + mg.Spec.ForProvider.IntegrationURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.IntegrationURIRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "VPCLink", "VPCLinkList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.ConnectionID), + Extract: resource.ExtractResourceID(), + Reference: mg.Spec.InitProvider.ConnectionIDRef, + Selector: mg.Spec.InitProvider.ConnectionIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.ConnectionID") + } + mg.Spec.InitProvider.ConnectionID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.ConnectionIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("iam.aws.upbound.io", "v1beta1", "Role", "RoleList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.CredentialsArn), + Extract: resource.ExtractParamPath("arn", true), + Reference: mg.Spec.InitProvider.CredentialsArnRef, + Selector: mg.Spec.InitProvider.CredentialsArnSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.CredentialsArn") + } + mg.Spec.InitProvider.CredentialsArn = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.CredentialsArnRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("lambda.aws.upbound.io", "v1beta1", "Function", "FunctionList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.IntegrationURI), + Extract: resource.ExtractParamPath("invoke_arn", true), + Reference: mg.Spec.InitProvider.IntegrationURIRef, + Selector: mg.Spec.InitProvider.IntegrationURISelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.IntegrationURI") + } + mg.Spec.InitProvider.IntegrationURI = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.IntegrationURIRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this IntegrationResponse. +func (mg *IntegrationResponse) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Integration", "IntegrationList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.IntegrationID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.IntegrationIDRef, + Selector: mg.Spec.ForProvider.IntegrationIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.IntegrationID") + } + mg.Spec.ForProvider.IntegrationID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.IntegrationIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Integration", "IntegrationList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.IntegrationID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.IntegrationIDRef, + Selector: mg.Spec.InitProvider.IntegrationIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.IntegrationID") + } + mg.Spec.InitProvider.IntegrationID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.IntegrationIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Model. +func (mg *Model) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Route. +func (mg *Route) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Authorizer", "AuthorizerList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.AuthorizerID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.AuthorizerIDRef, + Selector: mg.Spec.ForProvider.AuthorizerIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.AuthorizerID") + } + mg.Spec.ForProvider.AuthorizerID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.AuthorizerIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Integration", "IntegrationList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.Target), + Extract: apis.IntegrationIDPrefixed(), + Reference: mg.Spec.ForProvider.TargetRef, + Selector: mg.Spec.ForProvider.TargetSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.Target") + } + mg.Spec.ForProvider.Target = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.TargetRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Authorizer", "AuthorizerList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.AuthorizerID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.AuthorizerIDRef, + Selector: mg.Spec.InitProvider.AuthorizerIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.AuthorizerID") + } + mg.Spec.InitProvider.AuthorizerID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.AuthorizerIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Integration", "IntegrationList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.Target), + Extract: apis.IntegrationIDPrefixed(), + Reference: mg.Spec.InitProvider.TargetRef, + Selector: mg.Spec.InitProvider.TargetSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.Target") + } + mg.Spec.InitProvider.Target = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.TargetRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this RouteResponse. +func (mg *RouteResponse) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Route", "RouteList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.RouteID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.RouteIDRef, + Selector: mg.Spec.ForProvider.RouteIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.RouteID") + } + mg.Spec.ForProvider.RouteID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.RouteIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Route", "RouteList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.RouteID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.RouteIDRef, + Selector: mg.Spec.InitProvider.RouteIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.RouteID") + } + mg.Spec.InitProvider.RouteID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.RouteIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this Stage. +func (mg *Stage) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var rsp reference.ResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.APIIDRef, + Selector: mg.Spec.ForProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.APIID") + } + mg.Spec.ForProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Deployment", "DeploymentList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.DeploymentID), + Extract: reference.ExternalName(), + Reference: mg.Spec.ForProvider.DeploymentIDRef, + Selector: mg.Spec.ForProvider.DeploymentIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.DeploymentID") + } + mg.Spec.ForProvider.DeploymentID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.DeploymentIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "API", "APIList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.APIID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.APIIDRef, + Selector: mg.Spec.InitProvider.APIIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.APIID") + } + mg.Spec.InitProvider.APIID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.APIIDRef = rsp.ResolvedReference + { + m, l, err = apisresolver.GetManagedResource("fake.aws.upbound.io", "testdata", "Deployment", "DeploymentList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + rsp, err = r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.InitProvider.DeploymentID), + Extract: reference.ExternalName(), + Reference: mg.Spec.InitProvider.DeploymentIDRef, + Selector: mg.Spec.InitProvider.DeploymentIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.DeploymentID") + } + mg.Spec.InitProvider.DeploymentID = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.InitProvider.DeploymentIDRef = rsp.ResolvedReference + + return nil +} + +// ResolveReferences of this VPCLink. +func (mg *VPCLink) ResolveReferences(ctx context.Context, c client.Reader) error { + var m xpresource.Managed + var l xpresource.ManagedList + r := reference.NewAPIResolver(c, mg) + + var mrsp reference.MultiResolutionResponse + var err error + { + m, l, err = apisresolver.GetManagedResource("replacedec2.aws.upbound.io", "v1beta1", "SecurityGroup", "SecurityGroupList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.ForProvider.SecurityGroupIds), + Extract: reference.ExternalName(), + References: mg.Spec.ForProvider.SecurityGroupIDRefs, + Selector: mg.Spec.ForProvider.SecurityGroupIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.SecurityGroupIds") + } + mg.Spec.ForProvider.SecurityGroupIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.ForProvider.SecurityGroupIDRefs = mrsp.ResolvedReferences + { + m, l, err = apisresolver.GetManagedResource("replacedec2.aws.upbound.io", "v1beta1", "Subnet", "SubnetList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.ForProvider.SubnetIds), + Extract: reference.ExternalName(), + References: mg.Spec.ForProvider.SubnetIDRefs, + Selector: mg.Spec.ForProvider.SubnetIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.ForProvider.SubnetIds") + } + mg.Spec.ForProvider.SubnetIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.ForProvider.SubnetIDRefs = mrsp.ResolvedReferences + { + m, l, err = apisresolver.GetManagedResource("replacedec2.aws.upbound.io", "v1beta1", "SecurityGroup", "SecurityGroupList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.InitProvider.SecurityGroupIds), + Extract: reference.ExternalName(), + References: mg.Spec.InitProvider.SecurityGroupIDRefs, + Selector: mg.Spec.InitProvider.SecurityGroupIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.SecurityGroupIds") + } + mg.Spec.InitProvider.SecurityGroupIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.InitProvider.SecurityGroupIDRefs = mrsp.ResolvedReferences + { + m, l, err = apisresolver.GetManagedResource("replacedec2.aws.upbound.io", "v1beta1", "Subnet", "SubnetList") + if err != nil { + return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution") + } + + mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{ + CurrentValues: reference.FromPtrValues(mg.Spec.InitProvider.SubnetIds), + Extract: reference.ExternalName(), + References: mg.Spec.InitProvider.SubnetIDRefs, + Selector: mg.Spec.InitProvider.SubnetIDSelector, + To: reference.To{List: l, Managed: m}, + }) + } + if err != nil { + return errors.Wrap(err, "mg.Spec.InitProvider.SubnetIds") + } + mg.Spec.InitProvider.SubnetIds = reference.ToPtrValues(mrsp.ResolvedValues) + mg.Spec.InitProvider.SubnetIDRefs = mrsp.ResolvedReferences + + return nil +}