diff --git a/pkg/config/resource.go b/pkg/config/resource.go index e7002629..ab0c6d64 100644 --- a/pkg/config/resource.go +++ b/pkg/config/resource.go @@ -12,6 +12,7 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/fieldpath" + "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" fwresource "github.com/hashicorp/terraform-plugin-framework/resource" @@ -315,7 +316,7 @@ func (t *Tagger) Initialize(ctx context.Context, mg xpresource.Managed) error { if err != nil { return err } - pavedByte, err := setExternalTagsWithPaved(xpresource.GetExternalTags(mg), paved, t.fieldName) + pavedByte, err := setExternalTagsWithPaved(getExternalTags(mg), paved, t.fieldName) if err != nil { return err } @@ -328,11 +329,20 @@ func (t *Tagger) Initialize(ctx context.Context, mg xpresource.Managed) error { return nil } +const externalResourceTagKeyExternalName = "crossplane-external-name" + +func getExternalTags(mg xpresource.Managed) map[string]string { + externalTags := xpresource.GetExternalTags(mg) + externalTags[externalResourceTagKeyExternalName] = mg.GetAnnotations()[meta.AnnotationKeyExternalName] + return externalTags +} + func setExternalTagsWithPaved(externalTags map[string]string, paved *fieldpath.Paved, fieldName string) ([]byte, error) { tags := map[string]*string{ xpresource.ExternalResourceTagKeyKind: ptr.To(externalTags[xpresource.ExternalResourceTagKeyKind]), xpresource.ExternalResourceTagKeyName: ptr.To(externalTags[xpresource.ExternalResourceTagKeyName]), xpresource.ExternalResourceTagKeyProvider: ptr.To(externalTags[xpresource.ExternalResourceTagKeyProvider]), + externalResourceTagKeyExternalName: ptr.To(externalTags[externalResourceTagKeyExternalName]), } if err := paved.SetValue(fmt.Sprintf("spec.forProvider.%s", fieldName), tags); err != nil { diff --git a/pkg/config/resource_test.go b/pkg/config/resource_test.go index a94366c7..d496c509 100644 --- a/pkg/config/resource_test.go +++ b/pkg/config/resource_test.go @@ -11,17 +11,20 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/crossplane-runtime/pkg/fieldpath" + "github.com/crossplane/crossplane-runtime/pkg/meta" xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/resource/fake" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" ) const ( - kind = "ACoolService" - name = "example-service" - provider = "ACoolProvider" + kind = "ACoolService" + name = "example-service" + provider = "ACoolProvider" + externalName = "anExternalName" ) func TestTagger_Initialize(t *testing.T) { @@ -82,18 +85,38 @@ func TestSetExternalTagsWithPaved(t *testing.T) { args want }{ - "Successful": { + "Successful without external-name": { + args: args{ + externalTags: map[string]string{ + xpresource.ExternalResourceTagKeyKind: kind, + xpresource.ExternalResourceTagKeyName: name, + xpresource.ExternalResourceTagKeyProvider: provider, + }, + paved: fieldpath.Pave(map[string]any{}), + fieldName: "tags", + }, + want: want{ + pavedString: fmt.Sprintf(`{"spec":{"forProvider":{"tags":{"%s":"%s","%s":"%s","%s":"%s","%s":"%s"}}}}`, + externalResourceTagKeyExternalName, "", + xpresource.ExternalResourceTagKeyKind, kind, + xpresource.ExternalResourceTagKeyName, name, + xpresource.ExternalResourceTagKeyProvider, provider), + }, + }, + "Successful with external-name": { args: args{ externalTags: map[string]string{ xpresource.ExternalResourceTagKeyKind: kind, xpresource.ExternalResourceTagKeyName: name, xpresource.ExternalResourceTagKeyProvider: provider, + externalResourceTagKeyExternalName: externalName, }, paved: fieldpath.Pave(map[string]any{}), fieldName: "tags", }, want: want{ - pavedString: fmt.Sprintf(`{"spec":{"forProvider":{"tags":{"%s":"%s","%s":"%s","%s":"%s"}}}}`, + pavedString: fmt.Sprintf(`{"spec":{"forProvider":{"tags":{"%s":"%s","%s":"%s","%s":"%s","%s":"%s"}}}}`, + externalResourceTagKeyExternalName, externalName, xpresource.ExternalResourceTagKeyKind, kind, xpresource.ExternalResourceTagKeyName, name, xpresource.ExternalResourceTagKeyProvider, provider), @@ -112,3 +135,47 @@ func TestSetExternalTagsWithPaved(t *testing.T) { }) } } + +func TestGetExternalTags(t *testing.T) { + cases := map[string]struct { + mg xpresource.Managed + want map[string]string + }{ + "Without external-name": { + mg: &fake.Managed{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + }, + }, + want: map[string]string{ + xpresource.ExternalResourceTagKeyKind: "", + xpresource.ExternalResourceTagKeyName: name, + externalResourceTagKeyExternalName: "", + }, + }, + "With external-name": { + mg: &fake.Managed{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Annotations: map[string]string{ + meta.AnnotationKeyExternalName: externalName, + }, + }, + }, + want: map[string]string{ + xpresource.ExternalResourceTagKeyKind: "", + xpresource.ExternalResourceTagKeyName: name, + externalResourceTagKeyExternalName: externalName, + }, + }, + } + + for n, tc := range cases { + t.Run(n, func(t *testing.T) { + got := getExternalTags(tc.mg) + if diff := cmp.Diff(tc.want, got, test.EquateErrors()); diff != "" { + t.Fatalf("generateTypeName(...): -want, +got: %s", diff) + } + }) + } +}