diff --git a/README.md b/README.md index 22d2bf171d..d0034cb28d 100644 --- a/README.md +++ b/README.md @@ -1131,6 +1131,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address' ### master (unreleased) +* Report **permissions** in `scw info` ([#191](https://github.com/scaleway/scaleway-cli/issues/191)) * 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)) diff --git a/pkg/api/api.go b/pkg/api/api.go index 1f58521763..de180c8ac4 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -665,6 +665,14 @@ type ScalewayDashboard struct { IPsCount int `json:"ips_count"` } +type ScalewayPermissions map[string]ScalewayPermCategory +type ScalewayPermCategory map[string][]string + +// ScalewayPermissionDefinition represents the permissions +type ScalewayPermissionDefinition struct { + Permissions ScalewayPermissions `json:"permissions"` +} + // FuncMap used for json inspection var FuncMap = template.FuncMap{ "json": func(v interface{}) string { @@ -1674,6 +1682,28 @@ func (s *ScalewayAPI) GetUser() (*ScalewayUserDefinition, error) { return &user.User, nil } +func (s *ScalewayAPI) GetPermissions() (*ScalewayPermissionDefinition, error) { + s.EnableAccountAPI() + defer s.DisableAccountAPI() + resp, err := s.GetResponse(fmt.Sprintf("tokens/%s/permissions", s.Token)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return nil, fmt.Errorf("[%d] no such user", resp.StatusCode) + } + var permissions ScalewayPermissionDefinition + + decoder := json.NewDecoder(resp.Body) + err = decoder.Decode(&permissions) + if err != nil { + return nil, err + } + return &permissions, nil +} + // GetDashboard returns the dashboard func (s *ScalewayAPI) GetDashboard() (*ScalewayDashboard, error) { resp, err := s.GetResponse("dashboard") diff --git a/pkg/commands/info.go b/pkg/commands/info.go index f7abacafca..28d666a026 100644 --- a/pkg/commands/info.go +++ b/pkg/commands/info.go @@ -59,9 +59,10 @@ func RunInfo(ctx CommandContext, args InfoArgs) error { if err != nil { return err } else { - fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint) + fmt.Fprintf(ctx.Stdout, " [%d] %s", id, fingerprint) } } + fmt.Fprintf(ctx.Stdout, "\n") } dashboard, err := ctx.API.GetDashboard() @@ -76,5 +77,19 @@ func RunInfo(ctx CommandContext, args InfoArgs) error { fmt.Fprintf(ctx.Stdout, " Servers:\t\t%d\n", dashboard.ServersCount) fmt.Fprintf(ctx.Stdout, " Ips:\t\t\t%d\n", dashboard.IPsCount) + fmt.Fprintf(ctx.Stdout, "\n") + permissions, err := ctx.API.GetPermissions() + if err != nil { + return fmt.Errorf("Unable to get your permisssions") + } + fmt.Fprintln(ctx.Stdout, "Permissions:") + for _, service := range permissions.Permissions { + for key, serviceName := range service { + fmt.Fprintf(ctx.Stdout, " %s\n", key) + for _, perm := range serviceName { + fmt.Fprintf(ctx.Stdout, " %s\n", perm) + } + } + } return nil }