Skip to content

Commit

Permalink
Use io.ReadAll() and add Delete() method
Browse files Browse the repository at this point in the history
  • Loading branch information
andmat900 committed Nov 22, 2024
1 parent 41d6d59 commit 0d99b19
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
7 changes: 6 additions & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 11 additions & 9 deletions internal/database/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
}
7 changes: 3 additions & 4 deletions pkg/iut/v1alpha1/v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"runtime"
"sync"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 0d99b19

Please sign in to comment.