From c16e306014b9d5934bea01b67d1dd74acbf93d1f Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Wed, 23 Sep 2015 11:25:53 +0200 Subject: [PATCH] Report dashboard statistics in scw info (#177) --- README.md | 1 + pkg/api/api.go | 36 +++++++++++++++++++++++- pkg/commands/info.go | 66 +++++++++++++++++++++++++++----------------- 3 files changed, 77 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index c58c1ad78a..22d2bf171d 100644 --- a/README.md +++ b/README.md @@ -1131,6 +1131,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address' ### master (unreleased) +* Report **dashboard** statistics in `scw info` ([#177](https://github.com/scaleway/scaleway-cli/issues/177)) * Support of `scw _userdata name VAR=@/path/to/file` ([#183](https://github.com/scaleway/scaleway-cli/issues/183)) * Support of `scw restart -w` ([#185](https://github.com/scaleway/scaleway-cli/issues/185)) * Restarting multiple servers in parallel ([#185](https://github.com/scaleway/scaleway-cli/issues/185)) diff --git a/pkg/api/api.go b/pkg/api/api.go index 77ce07be87..1f58521763 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -650,6 +650,21 @@ type ScalewayUserPatchSSHKeyDefinition struct { SSHPublicKeys []ScalewayKeyDefinition `json:"ssh_public_keys"` } +// ScalewayDashboardResp represents a dashboard received from the API +type ScalewayDashboardResp struct { + Dashboard ScalewayDashboard +} + +// ScalewayDashboard represents a dashboard +type ScalewayDashboard struct { + VolumesCount int `json:"volumes_count"` + RunningServersCount int `json:"running_servers_count"` + ImagesCount int `json:"images_count"` + SnapshotsCount int `json:"snapshots_count"` + ServersCount int `json:"servers_count"` + IPsCount int `json:"ips_count"` +} + // FuncMap used for json inspection var FuncMap = template.FuncMap{ "json": func(v interface{}) string { @@ -1659,7 +1674,26 @@ func (s *ScalewayAPI) GetUser() (*ScalewayUserDefinition, error) { return &user.User, nil } -// +// GetDashboard returns the dashboard +func (s *ScalewayAPI) GetDashboard() (*ScalewayDashboard, error) { + resp, err := s.GetResponse("dashboard") + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return nil, fmt.Errorf("[%d] no such dashboard", resp.StatusCode) + } + var dashboard ScalewayDashboardResp + + decoder := json.NewDecoder(resp.Body) + err = decoder.Decode(&dashboard) + if err != nil { + return nil, err + } + return &dashboard.Dashboard, nil +} // GetServerID returns exactly one server matching or dies func (s *ScalewayAPI) GetServerID(needle string) string { diff --git a/pkg/commands/info.go b/pkg/commands/info.go index ee19a39132..f7abacafca 100644 --- a/pkg/commands/info.go +++ b/pkg/commands/info.go @@ -22,43 +22,59 @@ type InfoArgs struct{} func RunInfo(ctx CommandContext, args InfoArgs) error { // FIXME: fmt.Fprintf(ctx.Stdout, "Servers: %s\n", "quantity") // FIXME: fmt.Fprintf(ctx.Stdout, "Images: %s\n", "quantity") - fmt.Fprintf(ctx.Stdout, "Debug mode (client): %v\n", ctx.Getenv("DEBUG") != "") + fmt.Fprintf(ctx.Stdout, "Debug mode (client):\t%v\n", ctx.Getenv("DEBUG") != "") - fmt.Fprintf(ctx.Stdout, "Organization: %s\n", ctx.API.Organization) + fmt.Fprintf(ctx.Stdout, "Organization:\t\t%s\n", ctx.API.Organization) // FIXME: add partially-masked token - fmt.Fprintf(ctx.Stdout, "API Endpoint: %s\n", ctx.Getenv("scaleway_api_endpoint")) + fmt.Fprintf(ctx.Stdout, "API Endpoint:\t\t%s\n", ctx.Getenv("scaleway_api_endpoint")) configPath, _ := config.GetConfigFilePath() - fmt.Fprintf(ctx.Stdout, "RC file: %s\n", configPath) - fmt.Fprintf(ctx.Stdout, "User: %s\n", ctx.Getenv("USER")) - fmt.Fprintf(ctx.Stdout, "CPUs: %d\n", runtime.NumCPU()) + fmt.Fprintf(ctx.Stdout, "RC file:\t\t%s\n", configPath) + fmt.Fprintf(ctx.Stdout, "User:\t\t\t%s\n", ctx.Getenv("USER")) + fmt.Fprintf(ctx.Stdout, "CPUs:\t\t\t%d\n", runtime.NumCPU()) hostname, _ := os.Hostname() - fmt.Fprintf(ctx.Stdout, "Hostname: %s\n", hostname) + fmt.Fprintf(ctx.Stdout, "Hostname:\t\t%s\n", hostname) cliPath, _ := osext.Executable() - fmt.Fprintf(ctx.Stdout, "CLI Path: %s\n", cliPath) + fmt.Fprintf(ctx.Stdout, "CLI Path:\t\t%s\n", cliPath) + + fmt.Fprintln(ctx.Stdout, "") + fmt.Fprintf(ctx.Stdout, "Cache:\t\t\t%s\n", ctx.API.Cache.Path) + fmt.Fprintf(ctx.Stdout, " Servers:\t\t%d\n", ctx.API.Cache.GetNbServers()) + fmt.Fprintf(ctx.Stdout, " Images:\t\t%d\n", ctx.API.Cache.GetNbImages()) + fmt.Fprintf(ctx.Stdout, " Snapshots:\t\t%d\n", ctx.API.Cache.GetNbSnapshots()) + fmt.Fprintf(ctx.Stdout, " Volumes:\t\t%d\n", ctx.API.Cache.GetNbVolumes()) + fmt.Fprintf(ctx.Stdout, " Bootscripts:\t\t%d\n", ctx.API.Cache.GetNbBootscripts()) - fmt.Fprintf(ctx.Stdout, "Cache: %s\n", ctx.API.Cache.Path) - fmt.Fprintf(ctx.Stdout, " Servers: %d\n", ctx.API.Cache.GetNbServers()) - fmt.Fprintf(ctx.Stdout, " Images: %d\n", ctx.API.Cache.GetNbImages()) - fmt.Fprintf(ctx.Stdout, " Snapshots: %d\n", ctx.API.Cache.GetNbSnapshots()) - fmt.Fprintf(ctx.Stdout, " Volumes: %d\n", ctx.API.Cache.GetNbVolumes()) - fmt.Fprintf(ctx.Stdout, " Bootscripts: %d\n", ctx.API.Cache.GetNbBootscripts()) user, err := ctx.API.GetUser() if err != nil { return fmt.Errorf("Unable to get your SSH Keys") + } + + if len(user.SSHPublicKeys) == 0 { + fmt.Fprintln(ctx.Stdout, "You have no ssh keys") } else { - if len(user.SSHPublicKeys) == 0 { - fmt.Fprintln(ctx.Stdout, "You have no ssh keys") - } else { - fmt.Fprintln(ctx.Stdout, "SSH Keys:") - for id, key := range user.SSHPublicKeys { - fingerprint, err := utils.SSHGetFingerprint(key.Key) - if err != nil { - return err - } else { - fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint) - } + fmt.Fprintln(ctx.Stdout, "") + fmt.Fprintln(ctx.Stdout, "SSH Keys:") + for id, key := range user.SSHPublicKeys { + fingerprint, err := utils.SSHGetFingerprint(key.Key) + if err != nil { + return err + } else { + fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint) } } } + + dashboard, err := ctx.API.GetDashboard() + if err != nil { + return fmt.Errorf("Unable to get your dashboard") + } + fmt.Fprintln(ctx.Stdout, "Dashboard:") + fmt.Fprintf(ctx.Stdout, " Volumes:\t\t%d\n", dashboard.VolumesCount) + fmt.Fprintf(ctx.Stdout, " Running servers:\t%d\n", dashboard.RunningServersCount) + fmt.Fprintf(ctx.Stdout, " Images:\t\t%d\n", dashboard.ImagesCount) + fmt.Fprintf(ctx.Stdout, " Snapshots:\t\t%d\n", dashboard.SnapshotsCount) + fmt.Fprintf(ctx.Stdout, " Servers:\t\t%d\n", dashboard.ServersCount) + fmt.Fprintf(ctx.Stdout, " Ips:\t\t\t%d\n", dashboard.IPsCount) + return nil }