Skip to content

Commit

Permalink
Deleter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
andmat900 committed Dec 27, 2024
1 parent 0d99b19 commit 4525627
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/database/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions pkg/iut/v1alpha1/v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 4525627

Please sign in to comment.