diff --git a/internal/datastore/api/attempts.go b/internal/datastore/api/attempts.go deleted file mode 100644 index 444f78cf..00000000 --- a/internal/datastore/api/attempts.go +++ /dev/null @@ -1,37 +0,0 @@ -package api - -import ( - "fmt" - "net/http" - - "github.com/labstack/echo/v4" -) - -type GetAttemptsResponse struct { - Attempts int `json:"count"` -} - -func getAttemptsArgs(c echo.Context) (string, string, string, error) { - namespace := c.QueryParam("namespace") - layer := c.QueryParam("layer") - run := c.QueryParam("run") - if namespace == "" || layer == "" || run == "" { - return "", "", "", fmt.Errorf("missing query parameters") - } - return namespace, layer, run, nil -} - -func (a *API) GetAttemptsHandler(c echo.Context) error { - namespace, layer, run, err := getAttemptsArgs(c) - if err != nil { - return c.String(http.StatusBadRequest, err.Error()) - } - attempts, err := a.Storage.GetAttempts(namespace, layer, run) - if err != nil { - return c.String(http.StatusInternalServerError, "could not get attempts, there's an issue with the storage backend") - } - response := GetAttemptsResponse{ - Attempts: attempts, - } - return c.JSON(http.StatusOK, &response) -} diff --git a/internal/datastore/client/client.go b/internal/datastore/client/client.go index 8be71cc3..4025c72f 100644 --- a/internal/datastore/client/client.go +++ b/internal/datastore/client/client.go @@ -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 { @@ -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 -} diff --git a/internal/datastore/datastore.go b/internal/datastore/datastore.go index cc00a551..5dc1a9d6 100644 --- a/internal/datastore/datastore.go +++ b/internal/datastore/datastore.go @@ -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) } diff --git a/internal/server/api/runs.go b/internal/server/api/runs.go index 5579fb6f..be4a24e0 100644 --- a/internal/server/api/runs.go +++ b/internal/server/api/runs.go @@ -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) }