Generate deletion policy getters and setters for managed resources.

Signed-off-by: Nic Cope <negz@rk0n.org>
This commit is contained in:
Nic Cope 2020-08-11 18:42:59 -07:00
parent 12a1a1fb07
commit da38fc6e95
3 changed files with 58 additions and 0 deletions

View File

@ -116,6 +116,8 @@ func GenerateManaged(filename, header string, p *packages.Package) error {
"GetWriteConnectionSecretToReference": method.NewGetWriteConnectionSecretToReference(receiver, RuntimeImport),
"SetReclaimPolicy": method.NewSetReclaimPolicy(receiver, RuntimeImport, fields.NameSpec),
"GetReclaimPolicy": method.NewGetReclaimPolicy(receiver, RuntimeImport, fields.NameSpec),
"SetDeletionPolicy": method.NewSetDeletionPolicy(receiver, RuntimeImport),
"GetDeletionPolicy": method.NewGetDeletionPolicy(receiver, RuntimeImport),
}
err := generate.WriteMethods(p, methods, filepath.Join(filepath.Dir(p.GoFiles[0]), filename),

View File

@ -302,6 +302,28 @@ func NewGetReclaimPolicy(receiver, runtime, field string) New {
}
}
// NewSetDeletionPolicy returns a NewMethod that writes a SetDeletionPolicy
// method for the supplied Object to the supplied file.
func NewSetDeletionPolicy(receiver, runtime string) New {
return func(f *jen.File, o types.Object) {
f.Commentf("SetDeletionPolicy of this %s.", o.Name())
f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("SetDeletionPolicy").Params(jen.Id("r").Qual(runtime, "DeletionPolicy")).Block(
jen.Id(receiver).Dot(fields.NameSpec).Dot("DeletionPolicy").Op("=").Id("r"),
)
}
}
// NewGetDeletionPolicy returns a NewMethod that writes a GetDeletionPolicy
// method for the supplied Object to the supplied file.
func NewGetDeletionPolicy(receiver, runtime string) New {
return func(f *jen.File, o types.Object) {
f.Commentf("GetDeletionPolicy of this %s.", o.Name())
f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("GetDeletionPolicy").Params().Qual(runtime, "DeletionPolicy").Block(
jen.Return(jen.Id(receiver).Dot(fields.NameSpec).Dot("DeletionPolicy")),
)
}
}
// NewGetCredentialsSecretReference returns a NewMethod that writes a
// GetCredentialsSecretReference method for the supplied Object to the supplied
// file.

View File

@ -376,6 +376,40 @@ func (t *Type) GetReclaimPolicy() runtime.ReclaimPolicy {
}
}
func TestNewSetDeletionPolicy(t *testing.T) {
want := `package pkg
import runtime "example.org/runtime"
// SetDeletionPolicy of this Type.
func (t *Type) SetDeletionPolicy(r runtime.DeletionPolicy) {
t.Spec.DeletionPolicy = r
}
`
f := jen.NewFile("pkg")
NewSetDeletionPolicy("t", "example.org/runtime")(f, MockObject{Named: "Type"})
if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" {
t.Errorf("NewSetDeletionPolicy(): -want, +got\n%s", diff)
}
}
func TestNewGetDeletionPolicy(t *testing.T) {
want := `package pkg
import runtime "example.org/runtime"
// GetDeletionPolicy of this Type.
func (t *Type) GetDeletionPolicy() runtime.DeletionPolicy {
return t.Spec.DeletionPolicy
}
`
f := jen.NewFile("pkg")
NewGetDeletionPolicy("t", "example.org/runtime")(f, MockObject{Named: "Type"})
if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" {
t.Errorf("NewGetDeletionPolicy(): -want, +got\n%s", diff)
}
}
func TestNewManagedGetItems(t *testing.T) {
want := `package pkg