From 592bcc468159f55b749a6bb743331d8661efd853 Mon Sep 17 00:00:00 2001 From: anthdm Date: Fri, 12 Jan 2024 07:24:36 +0100 Subject: [PATCH] update endpoint api --- cmd/cli/main.go | 4 ++++ internal/api/server.go | 33 +++++++++++++++++++++++++++++++++ internal/api/server_test.go | 19 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index b3e1f9b..e9d2e5e 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -125,6 +125,10 @@ func (c command) handleEndpoint(args []string) { flagset.Var(&env, "env", "Environment variables for this endpoint") _ = flagset.Parse(args) + if len(runtime) == 0 { + fmt.Println("please provide a valid runtime [--runtime go, --runtime js]") + os.Exit(1) + } if !types.ValidRuntime(runtime) { fmt.Printf("invalid runtime %s, only go and js are currently supported\n", runtime) os.Exit(1) diff --git a/internal/api/server.go b/internal/api/server.go index 7b90eee..9fbe54e 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -48,6 +48,7 @@ func (s *Server) initRouter() { s.router.Get("/endpoint/{id}/metrics", makeAPIHandler(s.handleGetEndpointMetrics)) s.router.Post("/endpoint", makeAPIHandler(s.handleCreateEndpoint)) s.router.Post("/endpoint/{id}/deployment", makeAPIHandler(s.handleCreateDeployment)) + s.router.Put("/endpoint/{id}", makeAPIHandler(s.handleUpdateEndpoint)) s.router.Post("/publish", makeAPIHandler(s.handlePublish)) } @@ -84,6 +85,38 @@ func (p CreateEndpointParams) validate() error { return nil } +type UpdateEndpointParams struct { + Environment map[string]string `json:"environment"` +} + +func (s *Server) handleUpdateEndpoint(w http.ResponseWriter, r *http.Request) error { + endpointID, err := uuid.Parse(chi.URLParam(r, "id")) + if err != nil { + return writeJSON(w, http.StatusBadRequest, ErrorResponse(err)) + } + endpoint, err := s.store.GetEndpoint(endpointID) + if err != nil { + return writeJSON(w, http.StatusNotFound, ErrorResponse(err)) + } + var params UpdateEndpointParams + if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { + return writeJSON(w, http.StatusBadRequest, ErrorResponse(err)) + } + defer r.Body.Close() + if len(params.Environment) > 0 { + for k, v := range params.Environment { + endpoint.Environment[k] = v + } + } + updateParams := storage.UpdateEndpointParams{ + Environment: endpoint.Environment, + } + if err := s.store.UpdateEndpoint(endpointID, updateParams); err != nil { + return writeJSON(w, http.StatusUnprocessableEntity, ErrorResponse(err)) + } + return writeJSON(w, http.StatusOK, map[string]string{"status": "OK"}) +} + func (s *Server) handleCreateEndpoint(w http.ResponseWriter, r *http.Request) error { var params CreateEndpointParams if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { diff --git a/internal/api/server_test.go b/internal/api/server_test.go index 3398574..243c488 100644 --- a/internal/api/server_test.go +++ b/internal/api/server_test.go @@ -14,6 +14,25 @@ import ( "github.com/stretchr/testify/require" ) +func TestUpdateEndpoint(t *testing.T) { + s := createServer() + endpoint := seedEndpoint(t, s) + expected := map[string]string{"A": "B", "C": "D", "FOO": "BAR"} + + params := UpdateEndpointParams{ + Environment: map[string]string{"A": "B", "C": "D"}, + } + b, err := json.Marshal(params) + require.Nil(t, err) + + req := httptest.NewRequest("PUT", "/endpoint/"+endpoint.ID.String(), bytes.NewReader(b)) + resp := httptest.NewRecorder() + s.router.ServeHTTP(resp, req) + + require.Equal(t, http.StatusOK, resp.Result().StatusCode) + require.Equal(t, expected, endpoint.Environment) +} + func TestCreateEndpoint(t *testing.T) { s := createServer()