Add NewResolveReferences in its most basic form
Signed-off-by: Muvaffak Onus <me@muvaf.com>
This commit is contained in:
parent
1baca298c5
commit
2fee56c588
|
|
@ -45,11 +45,17 @@ const (
|
||||||
CoreAlias = "corev1"
|
CoreAlias = "corev1"
|
||||||
CoreImport = "k8s.io/api/core/v1"
|
CoreImport = "k8s.io/api/core/v1"
|
||||||
|
|
||||||
|
ClientAlias = "client"
|
||||||
|
ClientImport = "sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
|
||||||
RuntimeAlias = "xpv1"
|
RuntimeAlias = "xpv1"
|
||||||
RuntimeImport = "github.com/crossplane/crossplane-runtime/apis/common/v1"
|
RuntimeImport = "github.com/crossplane/crossplane-runtime/apis/common/v1"
|
||||||
|
|
||||||
ResourceAlias = "resource"
|
ResourceAlias = "resource"
|
||||||
ResourceImport = "github.com/crossplane/crossplane-runtime/pkg/resource"
|
ResourceImport = "github.com/crossplane/crossplane-runtime/pkg/resource"
|
||||||
|
|
||||||
|
ReferenceAlias = "reference"
|
||||||
|
ReferenceImport = "github.com/crossplane/crossplane-runtime/pkg/reference"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -209,3 +215,26 @@ func GenerateProviderConfigUsageList(filename, header string, p *packages.Packag
|
||||||
|
|
||||||
return errors.Wrap(err, "cannot write provider config usage list methods")
|
return errors.Wrap(err, "cannot write provider config usage list methods")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateReferences generates reference resolver calls.
|
||||||
|
func GenerateReferences(filename, header string, p *packages.Package) error {
|
||||||
|
receiver := "mg"
|
||||||
|
|
||||||
|
methods := method.Set{
|
||||||
|
"ResolveReferences": method.NewResolveReferences(receiver, ClientImport, ReferenceImport),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := generate.WriteMethods(p, methods, filepath.Join(filepath.Dir(p.GoFiles[0]), filename),
|
||||||
|
generate.WithHeaders(header),
|
||||||
|
generate.WithImportAliases(map[string]string{
|
||||||
|
ClientImport: ClientAlias,
|
||||||
|
ReferenceImport: ReferenceAlias,
|
||||||
|
}),
|
||||||
|
generate.WithMatcher(match.AllOf(
|
||||||
|
match.Managed(),
|
||||||
|
match.DoesNotHaveMarker(comments.In(p), DisableMarker, "false")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
return errors.Wrap(err, "cannot write reference resolver methods")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
/*
|
||||||
|
Copyright 2021 The Crossplane Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package method
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go/types"
|
||||||
|
|
||||||
|
"github.com/dave/jennifer/jen"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewResolveReferences returns a NewMethod that writes a SetProviderConfigReference
|
||||||
|
// method for the supplied Object to the supplied file.
|
||||||
|
func NewResolveReferences(receiver, clientPath, referencePath string) New {
|
||||||
|
return func(f *jen.File, o types.Object) {
|
||||||
|
f.Commentf("ResolveReferences of this %s.", o.Name())
|
||||||
|
f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("ResolveReferences").
|
||||||
|
Params(
|
||||||
|
jen.Id("ctx").Qual("context", "Context"),
|
||||||
|
jen.Id("c").Qual(clientPath, "Reader"),
|
||||||
|
).Block(
|
||||||
|
jen.Id("r").Op(":=").Qual(referencePath, "NewAPIResolver").Call(jen.Id("c"), jen.Id(receiver)),
|
||||||
|
jen.Line(),
|
||||||
|
jen.List(jen.Id("resp"), jen.Err()).Op(":=").Id("r").Dot("Resolve").Call(
|
||||||
|
jen.Id("ctx"),
|
||||||
|
jen.Qual(referencePath, "ResolutionRequest").Values(jen.Dict{
|
||||||
|
jen.Id("CurrentValue"): jen.Id(receiver).Dot("Spec").Dot("ForProvider").Dot("ApiId"),
|
||||||
|
jen.Id("Reference"): jen.Id(receiver).Dot("Spec").Dot("ForProvider").Dot("ApiIdRef"),
|
||||||
|
jen.Id("Selector"): jen.Id(receiver).Dot("Spec").Dot("ForProvider").Dot("ApiIdSelector"),
|
||||||
|
jen.Id("To"): jen.Qual(referencePath, "To").Values(jen.Dict{
|
||||||
|
jen.Id("Managed"): jen.Op("&").Id("Apigatewayv2Api").Values(),
|
||||||
|
jen.Id("List"): jen.Op("&").Id("Apigatewayv2ApiList").Values(),
|
||||||
|
}),
|
||||||
|
jen.Id("Extract"): jen.Qual(referencePath, "ExternalName").Call(),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
jen.If(jen.Err().Op("!=").Nil()).Block(
|
||||||
|
jen.Return(jen.Qual("github.com/pkg/errors", "Wrap").Call(jen.Err(), jen.Lit("Spec.ForProvider.ApiId"))),
|
||||||
|
),
|
||||||
|
jen.Line(),
|
||||||
|
jen.Id(receiver).Dot("Spec").Dot("ForProvider").Dot("ApiId").Op("=").Id("resp").Dot("ResolvedValue"),
|
||||||
|
jen.Id(receiver).Dot("Spec").Dot("ForProvider").Dot("ApiIdRef").Op("=").Id("resp").Dot("ResolvedReference"),
|
||||||
|
jen.Line(),
|
||||||
|
jen.Return(jen.Nil()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
/*
|
||||||
|
Copyright 2021 The Crossplane Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package method
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/dave/jennifer/jen"
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewResolveReferences(t *testing.T) {
|
||||||
|
want := `package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
client "example.org/client"
|
||||||
|
reference "example.org/reference"
|
||||||
|
errors "github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ResolveReferences of this Type.
|
||||||
|
func (t *Type) ResolveReferences(ctx context.Context, c client.Reader) {
|
||||||
|
r := reference.NewAPIResolver(c, t)
|
||||||
|
|
||||||
|
resp, err := r.Resolve(ctx, reference.ResolutionRequest{
|
||||||
|
CurrentValue: t.Spec.ForProvider.ApiId,
|
||||||
|
Extract: reference.ExternalName(),
|
||||||
|
Reference: t.Spec.ForProvider.ApiIdRef,
|
||||||
|
Selector: t.Spec.ForProvider.ApiIdSelector,
|
||||||
|
To: reference.To{
|
||||||
|
List: &Apigatewayv2ApiList{},
|
||||||
|
Managed: &Apigatewayv2Api{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Spec.ForProvider.ApiId")
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Spec.ForProvider.ApiId = resp.ResolvedValue
|
||||||
|
t.Spec.ForProvider.ApiIdRef = resp.ResolvedReference
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
`
|
||||||
|
f := jen.NewFile("pkg")
|
||||||
|
NewResolveReferences("t", "example.org/client", "example.org/reference")(f, MockObject{Named: "Type"})
|
||||||
|
if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" {
|
||||||
|
t.Errorf("NewResolveReferences(): -want, +got\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue