Skip to content

Commit

Permalink
refactor(datastore): attempts endpoint removal
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan-pad committed Apr 29, 2024
1 parent 137d774 commit fa6c4e2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 82 deletions.
37 changes: 0 additions & 37 deletions internal/datastore/api/attempts.go

This file was deleted.

35 changes: 0 additions & 35 deletions internal/datastore/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Client interface {
PutPlan(namespace string, layer string, run string, attempt string, format string, content []byte) error
GetLogs(namespace string, layer string, run string, attempt string) ([]string, error)
PutLogs(namespace string, layer string, run string, attempt string, content []byte) error
GetAttempts(namespace string, layer string, run string) (int, error)
}

type DefaultClient struct {
Expand Down Expand Up @@ -197,37 +196,3 @@ func (c *DefaultClient) PutLogs(namespace string, layer string, run string, atte
}
return nil
}

func (c *DefaultClient) GetAttempts(namespace string, layer string, run string) (int, error) {
req, err := c.buildRequest(
"/api/attempts",
url.Values{
"namespace": {namespace},
"layer": {layer},
"run": {run},
},
http.MethodGet,
nil,
)
if err != nil {
return 0, err
}
resp, err := c.client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("could not get attempts, there's an issue with the storage backend")
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
jresp := api.GetAttemptsResponse{}
err = json.Unmarshal(b, &jresp)
if err != nil {
return 0, err
}
return jresp.Attempts, nil
}
1 change: 0 additions & 1 deletion internal/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (s *Datastore) Exec() {
api.PUT("/logs", s.API.PutLogsHandler)
api.GET("/plans", s.API.GetPlanHandler)
api.PUT("/plans", s.API.PutPlanHandler)
api.GET("/attempts", s.API.GetAttemptsHandler)
e.Logger.Fatal(e.Start(s.Config.Datastore.Addr))
log.Infof("burrito datastore started on addr %s", s.Config.Datastore.Addr)
}
Expand Down
21 changes: 12 additions & 9 deletions internal/server/api/runs.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
package api

import (
"context"
"fmt"
"net/http"

"github.com/labstack/echo/v4"
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
"k8s.io/apimachinery/pkg/types"
)

type GetAttemptsResponse struct {
Count int `json:"count"`
}

func getRunAttemptArgs(c echo.Context) (string, string, string, error) {
func getRunAttemptArgs(c echo.Context) (string, string, error) {
namespace := c.Param("namespace")
layer := c.Param("layer")
run := c.Param("run")
if namespace == "" || layer == "" || run == "" {
return "", "", "", fmt.Errorf("missing query parameters")
if namespace == "" || run == "" {
return "", "", fmt.Errorf("missing query parameters")
}
return namespace, layer, run, nil
return namespace, run, nil
}

func (a *API) GetAttemptsHandler(c echo.Context) error {
namespace, layer, run, err := getRunAttemptArgs(c)
namespace, run, err := getRunAttemptArgs(c)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
attempts, err := a.Datastore.GetAttempts(namespace, layer, run)
runObject := &configv1alpha1.TerraformRun{}
err = a.Client.Get(context.Background(), types.NamespacedName{Name: run, Namespace: namespace}, runObject)
if err != nil {
return c.String(http.StatusInternalServerError, "could not get run attempt, there's an issue with the storage backend")
return c.String(http.StatusInternalServerError, "could not get run attempt, there's an issue with the cluster: "+err.Error())
}
response := GetAttemptsResponse{Count: attempts}
response := GetAttemptsResponse{Count: len(runObject.Status.Attempts)}
return c.JSON(http.StatusOK, &response)
}

0 comments on commit fa6c4e2

Please sign in to comment.