fix(postgresflex): continue if database is not found during read (#629)

Signed-off-by: Lukas Hoehl <lukas.hoehl@stackit.cloud>
This commit is contained in:
Lukas Hoehl 2025-01-21 12:41:44 +01:00 committed by GitHub
parent 1e0f33b759
commit a1e7890d86
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,6 +2,7 @@ package postgresflex
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"strings" "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) databaseResp, err := getDatabase(ctx, r.client, projectId, instanceId, databaseId)
if err != nil { 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 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) resp.State.RemoveResource(ctx)
return return
} }
@ -371,6 +372,8 @@ func toCreatePayload(model *Model) (*postgresflex.CreateDatabasePayload, error)
}, nil }, nil
} }
var databaseNotFoundErr = errors.New("database not found")
// The API does not have a GetDatabase endpoint, only ListDatabases // 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) { func getDatabase(ctx context.Context, client *postgresflex.APIClient, projectId, instanceId, databaseId string) (*postgresflex.InstanceDatabase, error) {
resp, err := client.ListDatabases(ctx, projectId, instanceId).Execute() 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 &database, nil
} }
} }
return nil, fmt.Errorf("database not found") return nil, databaseNotFoundErr
} }