Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added List deployments #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -74,6 +75,8 @@ func main() {
command.handleEndpoint(args[1:])
case "deploy":
command.handleDeploy(args[1:])
case "list-deploy":
command.handleListDeploy(args[1:])
case "serve":
if len(args) < 2 {
printUsage()
Expand Down Expand Up @@ -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.ListDeployments()
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")
}
Expand Down
9 changes: 9 additions & 0 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions internal/storage/memory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions internal/storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down