From 8457617ae13d2101b95175a65fb9f88fa93194ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Fern=C3=A1ndez?= <7312236+fernandezcuesta@users.noreply.github.com> Date: Sun, 27 Oct 2024 16:49:54 +0100 Subject: [PATCH] fix: ensure we always have an ID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jesús Fernández <7312236+fernandezcuesta@users.noreply.github.com> --- config/team/config.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/config/team/config.go b/config/team/config.go index 3ae41a0..e25f818 100644 --- a/config/team/config.go +++ b/config/team/config.go @@ -1,11 +1,46 @@ package team -import "github.com/crossplane/upjet/pkg/config" +import ( + "context" + + "github.com/crossplane/upjet/pkg/config" + "github.com/pkg/errors" +) + +const ( + ErrFmtNoAttribute = `"attribute not found: %s` + ErrFmtUnexpectedType = `unexpected type for attribute %s: Expecting a string` + ErrFmtEmptyAttribute = `empty attribute %s` +) + +func getExternalName(tfstate map[string]any) (string, error) { + id, ok := tfstate["id"] + if !ok { + return "", errors.Errorf(ErrFmtNoAttribute, "id") + } + idStr, ok := id.(string) + if !ok { + return "", errors.Errorf(ErrFmtUnexpectedType, "id") + } + if idStr == "" { + return "PENDING", nil + } + return idStr, nil +} + +func getID(ctx context.Context, externalName string, parameters map[string]any, providerConfig map[string]any) (string, error) { + if externalName == "" { + return "PENDING", nil + } + return externalName, nil +} // Configure configures individual resources by adding custom ResourceConfigurators. func Configure(p *config.Provider) { p.AddResourceConfigurator("pagerduty_team", func(r *config.Resource) { - + r.ExternalName = config.IdentifierFromProvider + r.ExternalName.GetExternalNameFn = getExternalName + r.ExternalName.GetIDFn = getID r.ShortGroup = "team" })