Skip to content

Commit

Permalink
db list: display region and engine
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Nov 8, 2023
1 parent b46526e commit d430b9a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/gofrs/uuid v4.3.0+incompatible
github.com/gorilla/websocket v1.5.0
github.com/iancoleman/strcase v0.2.0
github.com/koyeb/koyeb-api-client-go v0.0.0-20231004123101-b055bd9e4928
github.com/koyeb/koyeb-api-client-go v0.0.0-20231108165300-51115b89df62
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/manifoldco/promptui v0.9.0
github.com/mitchellh/go-homedir v1.1.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/koyeb/koyeb-api-client-go v0.0.0-20231004123101-b055bd9e4928 h1:xFTTl4MmHRZmrZnoi7BvQVPlMroQo98mdFbZuXsVXiA=
github.com/koyeb/koyeb-api-client-go v0.0.0-20231004123101-b055bd9e4928/go.mod h1:+oQfFj2WL3gi9Pb+UHbob4D7xaT52mPfKyH1UvWa4PQ=
github.com/koyeb/koyeb-api-client-go v0.0.0-20231104002024-ddb67d5a4885 h1:XLK+TwMe6J+iEQwUpEbU1+S+U3gZT2laKrjsZ4G/jYk=
github.com/koyeb/koyeb-api-client-go v0.0.0-20231104002024-ddb67d5a4885/go.mod h1:+oQfFj2WL3gi9Pb+UHbob4D7xaT52mPfKyH1UvWa4PQ=
github.com/koyeb/koyeb-api-client-go v0.0.0-20231108165300-51115b89df62 h1:U3Tl3KFl9tJGL+c6BUZvcNQRZaafNKnJFoEgcE794RI=
github.com/koyeb/koyeb-api-client-go v0.0.0-20231108165300-51115b89df62/go.mod h1:+oQfFj2WL3gi9Pb+UHbob4D7xaT52mPfKyH1UvWa4PQ=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand Down
67 changes: 49 additions & 18 deletions pkg/koyeb/databases_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package koyeb

import (
"encoding/json"
"fmt"
"strconv"

Expand All @@ -11,8 +12,14 @@ import (
"github.com/spf13/cobra"
)

// DatabaseInfo wraps a service returned by the services API and it's latest deployment.
type DatabaseInfo struct {
Service koyeb.ServiceListItem `json:"service"`
Deployment koyeb.Deployment `json:"deployment"`
}

func (h *DatabaseHandler) List(ctx *CLIContext, cmd *cobra.Command, args []string) error {
list := []koyeb.ServiceListItem{}
list := []DatabaseInfo{}

page := int64(0)
offset := int64(0)
Expand All @@ -31,7 +38,21 @@ func (h *DatabaseHandler) List(ctx *CLIContext, cmd *cobra.Command, args []strin
)
}

list = append(list, res.GetServices()...)
for _, svc := range res.GetServices() {
res, resp, err := ctx.Client.DeploymentsApi.GetDeployment(ctx.Context, svc.GetLatestDeploymentId()).Execute()
if err != nil {
return errors.NewCLIErrorFromAPIError(
fmt.Sprintf("Error while fetching the deployment for the database service `%s`", svc.GetId()),
err,
resp,
)
}

list = append(list, DatabaseInfo{
Service: svc,
Deployment: *res.Deployment,
})
}

page++
offset = page * limit
Expand All @@ -41,47 +62,57 @@ func (h *DatabaseHandler) List(ctx *CLIContext, cmd *cobra.Command, args []strin
}

full := GetBoolFlags(cmd, "full")
listDatabasesReply := NewListDatabasesReply(ctx.Mapper, &koyeb.ListServicesReply{Services: list}, full)
listDatabasesReply := NewListDatabasesReply(ctx.Mapper, list, full)
ctx.Renderer.Render(listDatabasesReply)
return nil
}

type ListDatabasesReply struct {
mapper *idmapper.Mapper
value *koyeb.ListServicesReply
full bool
mapper *idmapper.Mapper
databases []DatabaseInfo
full bool
}

func NewListDatabasesReply(mapper *idmapper.Mapper, value *koyeb.ListServicesReply, full bool) *ListDatabasesReply {
func NewListDatabasesReply(mapper *idmapper.Mapper, databases []DatabaseInfo, full bool) *ListDatabasesReply {
return &ListDatabasesReply{
mapper: mapper,
value: value,
full: full,
mapper: mapper,
databases: databases,
full: full,
}
}

func (ListDatabasesReply) Title() string {
return "Services"
return "Databases"
}

func (r *ListDatabasesReply) MarshalBinary() ([]byte, error) {
return r.value.MarshalJSON()
return json.Marshal(r.databases)
}

func (r *ListDatabasesReply) Headers() []string {
return []string{"id", "name", "status", "created_at"}
return []string{"id", "name", "region", "engine", "status", "created_at"}
}

func (r *ListDatabasesReply) Fields() []map[string]string {
items := r.value.GetServices()
items := r.databases
resp := make([]map[string]string, 0, len(items))

for _, item := range items {
var region string
var engine string

if item.Deployment.DatabaseInfo.HasNeonPostgres() {
region = item.Deployment.Definition.GetDatabase().NeonPostgres.GetRegion()
engine = fmt.Sprintf("Postgres %d", item.Deployment.Definition.GetDatabase().NeonPostgres.GetPgVersion())
}

fields := map[string]string{
"id": renderer.FormatID(item.GetId(), r.full),
"name": item.GetName(),
"status": formatServiceStatus(item.GetStatus()),
"created_at": renderer.FormatTime(item.GetCreatedAt()),
"id": renderer.FormatID(item.Service.GetId(), r.full),
"name": item.Service.GetName(),
"region": region,
"engine": engine,
"status": formatServiceStatus(item.Service.GetStatus()),
"created_at": renderer.FormatTime(item.Service.GetCreatedAt()),
}
resp = append(resp, fields)
}
Expand Down

0 comments on commit d430b9a

Please sign in to comment.