Skip to content

Commit 7217d5c

Browse files
committed
Merge pull request #190 from moul/info-dashboard
Report dashboard statistics in scw info (#177)
2 parents 52d37fe + c16e306 commit 7217d5c

File tree

3 files changed

+77
-26
lines changed

3 files changed

+77
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11311131

11321132
### master (unreleased)
11331133

1134+
* Report **dashboard** statistics in `scw info` ([#177](https://github.com/scaleway/scaleway-cli/issues/177))
11341135
* Support of `scw _userdata name VAR=@/path/to/file` ([#183](https://github.com/scaleway/scaleway-cli/issues/183))
11351136
* Support of `scw restart -w` ([#185](https://github.com/scaleway/scaleway-cli/issues/185))
11361137
* Restarting multiple servers in parallel ([#185](https://github.com/scaleway/scaleway-cli/issues/185))

pkg/api/api.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,21 @@ type ScalewayUserPatchSSHKeyDefinition struct {
650650
SSHPublicKeys []ScalewayKeyDefinition `json:"ssh_public_keys"`
651651
}
652652

653+
// ScalewayDashboardResp represents a dashboard received from the API
654+
type ScalewayDashboardResp struct {
655+
Dashboard ScalewayDashboard
656+
}
657+
658+
// ScalewayDashboard represents a dashboard
659+
type ScalewayDashboard struct {
660+
VolumesCount int `json:"volumes_count"`
661+
RunningServersCount int `json:"running_servers_count"`
662+
ImagesCount int `json:"images_count"`
663+
SnapshotsCount int `json:"snapshots_count"`
664+
ServersCount int `json:"servers_count"`
665+
IPsCount int `json:"ips_count"`
666+
}
667+
653668
// FuncMap used for json inspection
654669
var FuncMap = template.FuncMap{
655670
"json": func(v interface{}) string {
@@ -1659,7 +1674,26 @@ func (s *ScalewayAPI) GetUser() (*ScalewayUserDefinition, error) {
16591674
return &user.User, nil
16601675
}
16611676

1662-
//
1677+
// GetDashboard returns the dashboard
1678+
func (s *ScalewayAPI) GetDashboard() (*ScalewayDashboard, error) {
1679+
resp, err := s.GetResponse("dashboard")
1680+
if err != nil {
1681+
return nil, err
1682+
}
1683+
defer resp.Body.Close()
1684+
1685+
if resp.StatusCode != 200 {
1686+
return nil, fmt.Errorf("[%d] no such dashboard", resp.StatusCode)
1687+
}
1688+
var dashboard ScalewayDashboardResp
1689+
1690+
decoder := json.NewDecoder(resp.Body)
1691+
err = decoder.Decode(&dashboard)
1692+
if err != nil {
1693+
return nil, err
1694+
}
1695+
return &dashboard.Dashboard, nil
1696+
}
16631697

16641698
// GetServerID returns exactly one server matching or dies
16651699
func (s *ScalewayAPI) GetServerID(needle string) string {

pkg/commands/info.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,59 @@ type InfoArgs struct{}
2222
func RunInfo(ctx CommandContext, args InfoArgs) error {
2323
// FIXME: fmt.Fprintf(ctx.Stdout, "Servers: %s\n", "quantity")
2424
// FIXME: fmt.Fprintf(ctx.Stdout, "Images: %s\n", "quantity")
25-
fmt.Fprintf(ctx.Stdout, "Debug mode (client): %v\n", ctx.Getenv("DEBUG") != "")
25+
fmt.Fprintf(ctx.Stdout, "Debug mode (client):\t%v\n", ctx.Getenv("DEBUG") != "")
2626

27-
fmt.Fprintf(ctx.Stdout, "Organization: %s\n", ctx.API.Organization)
27+
fmt.Fprintf(ctx.Stdout, "Organization:\t\t%s\n", ctx.API.Organization)
2828
// FIXME: add partially-masked token
29-
fmt.Fprintf(ctx.Stdout, "API Endpoint: %s\n", ctx.Getenv("scaleway_api_endpoint"))
29+
fmt.Fprintf(ctx.Stdout, "API Endpoint:\t\t%s\n", ctx.Getenv("scaleway_api_endpoint"))
3030
configPath, _ := config.GetConfigFilePath()
31-
fmt.Fprintf(ctx.Stdout, "RC file: %s\n", configPath)
32-
fmt.Fprintf(ctx.Stdout, "User: %s\n", ctx.Getenv("USER"))
33-
fmt.Fprintf(ctx.Stdout, "CPUs: %d\n", runtime.NumCPU())
31+
fmt.Fprintf(ctx.Stdout, "RC file:\t\t%s\n", configPath)
32+
fmt.Fprintf(ctx.Stdout, "User:\t\t\t%s\n", ctx.Getenv("USER"))
33+
fmt.Fprintf(ctx.Stdout, "CPUs:\t\t\t%d\n", runtime.NumCPU())
3434
hostname, _ := os.Hostname()
35-
fmt.Fprintf(ctx.Stdout, "Hostname: %s\n", hostname)
35+
fmt.Fprintf(ctx.Stdout, "Hostname:\t\t%s\n", hostname)
3636
cliPath, _ := osext.Executable()
37-
fmt.Fprintf(ctx.Stdout, "CLI Path: %s\n", cliPath)
37+
fmt.Fprintf(ctx.Stdout, "CLI Path:\t\t%s\n", cliPath)
38+
39+
fmt.Fprintln(ctx.Stdout, "")
40+
fmt.Fprintf(ctx.Stdout, "Cache:\t\t\t%s\n", ctx.API.Cache.Path)
41+
fmt.Fprintf(ctx.Stdout, " Servers:\t\t%d\n", ctx.API.Cache.GetNbServers())
42+
fmt.Fprintf(ctx.Stdout, " Images:\t\t%d\n", ctx.API.Cache.GetNbImages())
43+
fmt.Fprintf(ctx.Stdout, " Snapshots:\t\t%d\n", ctx.API.Cache.GetNbSnapshots())
44+
fmt.Fprintf(ctx.Stdout, " Volumes:\t\t%d\n", ctx.API.Cache.GetNbVolumes())
45+
fmt.Fprintf(ctx.Stdout, " Bootscripts:\t\t%d\n", ctx.API.Cache.GetNbBootscripts())
3846

39-
fmt.Fprintf(ctx.Stdout, "Cache: %s\n", ctx.API.Cache.Path)
40-
fmt.Fprintf(ctx.Stdout, " Servers: %d\n", ctx.API.Cache.GetNbServers())
41-
fmt.Fprintf(ctx.Stdout, " Images: %d\n", ctx.API.Cache.GetNbImages())
42-
fmt.Fprintf(ctx.Stdout, " Snapshots: %d\n", ctx.API.Cache.GetNbSnapshots())
43-
fmt.Fprintf(ctx.Stdout, " Volumes: %d\n", ctx.API.Cache.GetNbVolumes())
44-
fmt.Fprintf(ctx.Stdout, " Bootscripts: %d\n", ctx.API.Cache.GetNbBootscripts())
4547
user, err := ctx.API.GetUser()
4648
if err != nil {
4749
return fmt.Errorf("Unable to get your SSH Keys")
50+
}
51+
52+
if len(user.SSHPublicKeys) == 0 {
53+
fmt.Fprintln(ctx.Stdout, "You have no ssh keys")
4854
} else {
49-
if len(user.SSHPublicKeys) == 0 {
50-
fmt.Fprintln(ctx.Stdout, "You have no ssh keys")
51-
} else {
52-
fmt.Fprintln(ctx.Stdout, "SSH Keys:")
53-
for id, key := range user.SSHPublicKeys {
54-
fingerprint, err := utils.SSHGetFingerprint(key.Key)
55-
if err != nil {
56-
return err
57-
} else {
58-
fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint)
59-
}
55+
fmt.Fprintln(ctx.Stdout, "")
56+
fmt.Fprintln(ctx.Stdout, "SSH Keys:")
57+
for id, key := range user.SSHPublicKeys {
58+
fingerprint, err := utils.SSHGetFingerprint(key.Key)
59+
if err != nil {
60+
return err
61+
} else {
62+
fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint)
6063
}
6164
}
6265
}
66+
67+
dashboard, err := ctx.API.GetDashboard()
68+
if err != nil {
69+
return fmt.Errorf("Unable to get your dashboard")
70+
}
71+
fmt.Fprintln(ctx.Stdout, "Dashboard:")
72+
fmt.Fprintf(ctx.Stdout, " Volumes:\t\t%d\n", dashboard.VolumesCount)
73+
fmt.Fprintf(ctx.Stdout, " Running servers:\t%d\n", dashboard.RunningServersCount)
74+
fmt.Fprintf(ctx.Stdout, " Images:\t\t%d\n", dashboard.ImagesCount)
75+
fmt.Fprintf(ctx.Stdout, " Snapshots:\t\t%d\n", dashboard.SnapshotsCount)
76+
fmt.Fprintf(ctx.Stdout, " Servers:\t\t%d\n", dashboard.ServersCount)
77+
fmt.Fprintf(ctx.Stdout, " Ips:\t\t\t%d\n", dashboard.IPsCount)
78+
6379
return nil
6480
}

0 commit comments

Comments
 (0)