From a6c0b14aa9cd6d7ee8628b3929247250a831558a Mon Sep 17 00:00:00 2001 From: Mansour Baitar Date: Fri, 12 Jan 2024 22:44:16 +0100 Subject: [PATCH 1/2] Added List deployment logic --- cmd/cli/main.go | 18 ++++++++++++++++++ internal/api/server.go | 9 +++++++++ internal/storage/memory_store.go | 12 ++++++++++++ internal/storage/sql.go | 18 ++++++++++++++++++ internal/storage/storage.go | 1 + 5 files changed, 58 insertions(+) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index e9d2e5e..7a2f35f 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -27,6 +27,7 @@ Commands: endpoint Create a new endpoint publish Publish a deployment to an endpoint deploy Create a new deployment + list-deploy Lists all deployments help Show usage `, version.Version) @@ -74,6 +75,8 @@ func main() { command.handleEndpoint(args[1:]) case "deploy": command.handleDeploy(args[1:]) + case "list-deploy": + command.handleDeploy(args[1:]) case "serve": if len(args) < 2 { printUsage() @@ -183,6 +186,21 @@ func (c command) handleDeploy(args []string) { fmt.Printf("deploy preview: %s/preview/%s\n", config.IngressUrl(), deploy.ID) } +func (c command) handleListDeploy(args []string) { + flagSet := flag.NewFlagSet("list-deploy", flag.ExitOnError) + _ = flagSet.Parse(args) + + endpoint, err := c.client.ListEndpoints() + if err != nil { + printErrorAndExit(err) + } + b, err := json.MarshalIndent(endpoint, "", " ") + if err != nil { + printErrorAndExit(err) + } + fmt.Println(string(b)) +} + func (c command) handleServeEndpoint(args []string) { fmt.Println("TODO") } diff --git a/internal/api/server.go b/internal/api/server.go index 9fbe54e..7fb929e 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -49,6 +49,7 @@ func (s *Server) initRouter() { 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.Get("/deployments", makeAPIHandler(s.handleGetDeployments)) s.router.Post("/publish", makeAPIHandler(s.handlePublish)) } @@ -187,6 +188,14 @@ func (s *Server) handleGetEndpoints(w http.ResponseWriter, r *http.Request) erro // return writeJSON(w, http.StatusOK, endpoints) } +func (s *Server) handleGetDeployments(w http.ResponseWriter, r *http.Request) error { + deployments, err := s.store.GetDeployments() + if err != nil { + return writeJSON(w, http.StatusNotFound, ErrorResponse(err)) + } + return writeJSON(w, http.StatusOK, deployments) +} + // PublishParams holds all the necessary fields to publish a specific // deployment LIVE to your application. type PublishParams struct { diff --git a/internal/storage/memory_store.go b/internal/storage/memory_store.go index 0c379fa..399065b 100644 --- a/internal/storage/memory_store.go +++ b/internal/storage/memory_store.go @@ -73,6 +73,18 @@ func (s *MemoryStore) GetDeployment(id uuid.UUID) (*types.Deployment, error) { return deploy, nil } +func (s *MemoryStore) GetDeployments() ([]*types.Deployment, error) { + s.mu.RLock() + defer s.mu.RUnlock() + + deployments := make([]*types.Deployment, 0, len(s.deploys)) + for _, deploy := range s.deploys { + deployments = append(deployments, deploy) + } + + return deployments, nil +} + func (s *MemoryStore) CreateRuntimeMetric(_ *types.RuntimeMetric) error { return nil } diff --git a/internal/storage/sql.go b/internal/storage/sql.go index 68bfa0f..b694c90 100644 --- a/internal/storage/sql.go +++ b/internal/storage/sql.go @@ -111,6 +111,24 @@ RETURNING id` return err } +func (s *SQLStore) GetDeployments() ([]*types.Deployment, error) { + rows, err := s.db.Query("SELECT * FROM deployment") + if err != nil { + return nil, err + } + defer rows.Close() + + var deployments []*types.Deployment + for rows.Next() { + var deploy types.Deployment + if err := scanDeploy(rows, &deploy); err != nil { + return nil, err + } + deployments = append(deployments, &deploy) + } + return deployments, nil +} + func (s *SQLStore) CreateRuntimeMetric(metric *types.RuntimeMetric) error { return nil } diff --git a/internal/storage/storage.go b/internal/storage/storage.go index e48fc56..8aeaf89 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -11,6 +11,7 @@ type Store interface { GetEndpoint(uuid.UUID) (*types.Endpoint, error) CreateDeployment(*types.Deployment) error GetDeployment(uuid.UUID) (*types.Deployment, error) + GetDeployments() ([]*types.Deployment, error) } type MetricStore interface { From eb7c3bb5ee022cbf7d1d06d78612d97070647652 Mon Sep 17 00:00:00 2001 From: Mansour Baitar Date: Sat, 13 Jan 2024 05:20:54 +0100 Subject: [PATCH 2/2] Use correct functions --- cmd/cli/main.go | 4 ++-- internal/client/client.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 7a2f35f..26ed843 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -76,7 +76,7 @@ func main() { case "deploy": command.handleDeploy(args[1:]) case "list-deploy": - command.handleDeploy(args[1:]) + command.handleListDeploy(args[1:]) case "serve": if len(args) < 2 { printUsage() @@ -190,7 +190,7 @@ func (c command) handleListDeploy(args []string) { flagSet := flag.NewFlagSet("list-deploy", flag.ExitOnError) _ = flagSet.Parse(args) - endpoint, err := c.client.ListEndpoints() + endpoint, err := c.client.ListDeployments() if err != nil { printErrorAndExit(err) } diff --git a/internal/client/client.go b/internal/client/client.go index 5c5228b..dc19fda 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -132,3 +132,22 @@ func (c *Client) ListEndpoints() ([]types.Endpoint, error) { resp.Body.Close() return endpoints, nil } + +func (c *Client) ListDeployments() ([]types.Deployment, error) { + url := fmt.Sprintf("%s/deployments", c.config.url) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + req.Header.Add("Content-Type", "application/json") + resp, err := c.Do(req) + if err != nil { + return nil, err + } + var deploys []types.Deployment + if err := json.NewDecoder(resp.Body).Decode(&deploys); err != nil { + return nil, err + } + resp.Body.Close() + return deploys, nil +}