Skip to content

Commit 132048d

Browse files
author
Quentin Perez
committed
Merge pull request #195 from QuentinPerez/list_permissions
Added permissions `scw info`
2 parents 1400a10 + 65f599a commit 132048d

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

README.md

+1
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 **permissions** in `scw info` ([#191](https://github.com/scaleway/scaleway-cli/issues/191))
11341135
* Report **dashboard** statistics in `scw info` ([#177](https://github.com/scaleway/scaleway-cli/issues/177))
11351136
* Support of `scw _userdata name VAR=@/path/to/file` ([#183](https://github.com/scaleway/scaleway-cli/issues/183))
11361137
* Support of `scw restart -w` ([#185](https://github.com/scaleway/scaleway-cli/issues/185))

pkg/api/api.go

+30
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,14 @@ type ScalewayDashboard struct {
665665
IPsCount int `json:"ips_count"`
666666
}
667667

668+
type ScalewayPermissions map[string]ScalewayPermCategory
669+
type ScalewayPermCategory map[string][]string
670+
671+
// ScalewayPermissionDefinition represents the permissions
672+
type ScalewayPermissionDefinition struct {
673+
Permissions ScalewayPermissions `json:"permissions"`
674+
}
675+
668676
// FuncMap used for json inspection
669677
var FuncMap = template.FuncMap{
670678
"json": func(v interface{}) string {
@@ -1674,6 +1682,28 @@ func (s *ScalewayAPI) GetUser() (*ScalewayUserDefinition, error) {
16741682
return &user.User, nil
16751683
}
16761684

1685+
func (s *ScalewayAPI) GetPermissions() (*ScalewayPermissionDefinition, error) {
1686+
s.EnableAccountAPI()
1687+
defer s.DisableAccountAPI()
1688+
resp, err := s.GetResponse(fmt.Sprintf("tokens/%s/permissions", s.Token))
1689+
if err != nil {
1690+
return nil, err
1691+
}
1692+
defer resp.Body.Close()
1693+
1694+
if resp.StatusCode != 200 {
1695+
return nil, fmt.Errorf("[%d] no such user", resp.StatusCode)
1696+
}
1697+
var permissions ScalewayPermissionDefinition
1698+
1699+
decoder := json.NewDecoder(resp.Body)
1700+
err = decoder.Decode(&permissions)
1701+
if err != nil {
1702+
return nil, err
1703+
}
1704+
return &permissions, nil
1705+
}
1706+
16771707
// GetDashboard returns the dashboard
16781708
func (s *ScalewayAPI) GetDashboard() (*ScalewayDashboard, error) {
16791709
resp, err := s.GetResponse("dashboard")

pkg/commands/info.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ func RunInfo(ctx CommandContext, args InfoArgs) error {
5959
if err != nil {
6060
return err
6161
} else {
62-
fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint)
62+
fmt.Fprintf(ctx.Stdout, " [%d] %s", id, fingerprint)
6363
}
6464
}
65+
fmt.Fprintf(ctx.Stdout, "\n")
6566
}
6667

6768
dashboard, err := ctx.API.GetDashboard()
@@ -76,5 +77,19 @@ func RunInfo(ctx CommandContext, args InfoArgs) error {
7677
fmt.Fprintf(ctx.Stdout, " Servers:\t\t%d\n", dashboard.ServersCount)
7778
fmt.Fprintf(ctx.Stdout, " Ips:\t\t\t%d\n", dashboard.IPsCount)
7879

80+
fmt.Fprintf(ctx.Stdout, "\n")
81+
permissions, err := ctx.API.GetPermissions()
82+
if err != nil {
83+
return fmt.Errorf("Unable to get your permisssions")
84+
}
85+
fmt.Fprintln(ctx.Stdout, "Permissions:")
86+
for _, service := range permissions.Permissions {
87+
for key, serviceName := range service {
88+
fmt.Fprintf(ctx.Stdout, " %s\n", key)
89+
for _, perm := range serviceName {
90+
fmt.Fprintf(ctx.Stdout, " %s\n", perm)
91+
}
92+
}
93+
}
7994
return nil
8095
}

0 commit comments

Comments
 (0)