From 452562735b78614ccc631a32477ec3690ee0002b Mon Sep 17 00:00:00 2001 From: Andrei Matveyeu Date: Fri, 27 Dec 2024 12:07:44 +0100 Subject: [PATCH] Deleter interface --- internal/database/database.go | 5 ++++- internal/database/etcd/etcd.go | 1 + pkg/iut/v1alpha1/v1alpha1.go | 13 +++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/database/database.go b/internal/database/database.go index 3126ca8..d1e3b1b 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -22,9 +22,12 @@ import ( "github.com/google/uuid" ) +type Deleter interface { + Delete() error +} + type DatabaseReadWriter interface { io.ReadWriter - Delete() error } // Opener is the common interface for database clients diff --git a/internal/database/etcd/etcd.go b/internal/database/etcd/etcd.go index 4475697..5c50aed 100644 --- a/internal/database/etcd/etcd.go +++ b/internal/database/etcd/etcd.go @@ -32,6 +32,7 @@ import ( // TODO: refactor the client so that it does not store data it fetched. // However, without it implementing the database.Opener interface would be more complex (methods readByte, read). type Etcd struct { + database.Deleter cfg config.Config client *clientv3.Client ID uuid.UUID diff --git a/pkg/iut/v1alpha1/v1alpha1.go b/pkg/iut/v1alpha1/v1alpha1.go index b6ec404..5c980ff 100644 --- a/pkg/iut/v1alpha1/v1alpha1.go +++ b/pkg/iut/v1alpha1/v1alpha1.go @@ -229,10 +229,19 @@ func (h V1Alpha1Handler) Stop(w http.ResponseWriter, r *http.Request, ps httprou RespondWithError(w, http.StatusBadRequest, err.Error()) return } + // client := h.database.Open(r.Context(), identifier) + client := h.database.Open(r.Context(), identifier) - err = client.Delete() + deleter, canDelete := client.(database.Deleter) + if !canDelete { + logger.Warning("The database does not support delete. Writing nil.") + _, err = client.Write(nil) + } else { + err = deleter.Delete() + } + if err != nil { - logger.Errorf("Etcd delete failed: %s", err.Error()) + logger.Errorf("Database delete failed: %s", err.Error()) RespondWithError(w, http.StatusInternalServerError, err.Error()) return }