diff --git a/internal/database/database.go b/internal/database/database.go index b34e652..3126ca8 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -22,7 +22,12 @@ import ( "github.com/google/uuid" ) +type DatabaseReadWriter interface { + io.ReadWriter + Delete() error +} + // Opener is the common interface for database clients type Opener interface { - Open(context.Context, uuid.UUID) io.ReadWriter + Open(context.Context, uuid.UUID) DatabaseReadWriter } diff --git a/internal/database/etcd/etcd.go b/internal/database/etcd/etcd.go index 5a78dc1..4475697 100644 --- a/internal/database/etcd/etcd.go +++ b/internal/database/etcd/etcd.go @@ -59,7 +59,7 @@ func New(cfg config.Config, logger *logrus.Logger, treePrefix string) database.O } // Open returns a copy of an Etcd client with ID and context added -func (etcd Etcd) Open(ctx context.Context, id uuid.UUID) io.ReadWriter { +func (etcd Etcd) Open(ctx context.Context, id uuid.UUID) database.DatabaseReadWriter { return &Etcd{ client: etcd.client, cfg: etcd.cfg, @@ -75,14 +75,6 @@ func (etcd Etcd) Write(p []byte) (int, error) { } key := fmt.Sprintf("%s/%s", etcd.treePrefix, etcd.ID.String()) - if p == nil { - _, err := etcd.client.Delete(etcd.ctx, key) - if err != nil { - return 0, fmt.Errorf("Failed to delete key %s: %s", key, err.Error()) - } - return 0, nil - } - _, err := etcd.client.Put(etcd.ctx, key, string(p)) if err != nil { return 0, err @@ -137,3 +129,13 @@ func (etcd *Etcd) Read(p []byte) (n int, err error) { return n, nil } + +// Delete deletes the current key from the database +func (etcd Etcd) Delete() error { + key := fmt.Sprintf("%s/%s", etcd.treePrefix, etcd.ID.String()) + _, err := etcd.client.Delete(etcd.ctx, key) + if err != nil { + return fmt.Errorf("Failed to delete key %s: %s", key, err.Error()) + } + return nil +} diff --git a/pkg/iut/v1alpha1/v1alpha1.go b/pkg/iut/v1alpha1/v1alpha1.go index 5fd1f51..b6ec404 100644 --- a/pkg/iut/v1alpha1/v1alpha1.go +++ b/pkg/iut/v1alpha1/v1alpha1.go @@ -19,6 +19,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "runtime" "sync" @@ -186,9 +187,7 @@ func (h V1Alpha1Handler) Status(w http.ResponseWriter, r *http.Request, ps httpr id, err := uuid.Parse(r.URL.Query().Get("id")) client := h.database.Open(r.Context(), identifier) - data := make([]byte, 4096) - byteCount, err := client.Read(data) - data = data[:byteCount] + data, err := io.ReadAll(client) if err != nil { logger.Errorf("Failed to look up status request id: %s, %s", identifier, err.Error()) @@ -231,7 +230,7 @@ func (h V1Alpha1Handler) Stop(w http.ResponseWriter, r *http.Request, ps httprou return } client := h.database.Open(r.Context(), identifier) - _, err = client.Write(nil) + err = client.Delete() if err != nil { logger.Errorf("Etcd delete failed: %s", err.Error()) RespondWithError(w, http.StatusInternalServerError, err.Error())