From a1e7890d865a8530f0143d890d0e802f8835534f Mon Sep 17 00:00:00 2001 From: Lukas Hoehl Date: Tue, 21 Jan 2025 12:41:44 +0100 Subject: [PATCH] fix(postgresflex): continue if database is not found during read (#629) Signed-off-by: Lukas Hoehl --- .../internal/services/postgresflex/database/resource.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stackit/internal/services/postgresflex/database/resource.go b/stackit/internal/services/postgresflex/database/resource.go index 0bc005ca..3a7e8094 100644 --- a/stackit/internal/services/postgresflex/database/resource.go +++ b/stackit/internal/services/postgresflex/database/resource.go @@ -2,6 +2,7 @@ package postgresflex import ( "context" + "errors" "fmt" "net/http" "strings" @@ -234,7 +235,7 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r databaseResp, err := getDatabase(ctx, r.client, projectId, instanceId, databaseId) if err != nil { oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped - if ok && oapiErr.StatusCode == http.StatusNotFound { + if (ok && oapiErr.StatusCode == http.StatusNotFound) || errors.Is(err, databaseNotFoundErr) { resp.State.RemoveResource(ctx) return } @@ -371,6 +372,8 @@ func toCreatePayload(model *Model) (*postgresflex.CreateDatabasePayload, error) }, nil } +var databaseNotFoundErr = errors.New("database not found") + // The API does not have a GetDatabase endpoint, only ListDatabases func getDatabase(ctx context.Context, client *postgresflex.APIClient, projectId, instanceId, databaseId string) (*postgresflex.InstanceDatabase, error) { resp, err := client.ListDatabases(ctx, projectId, instanceId).Execute() @@ -385,5 +388,5 @@ func getDatabase(ctx context.Context, client *postgresflex.APIClient, projectId, return &database, nil } } - return nil, fmt.Errorf("database not found") + return nil, databaseNotFoundErr }