Skip to content

Added permissions scw info #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 28, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
30 changes: 30 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,14 @@ type ScalewayDashboard struct {
IPsCount int `json:"ips_count"`
}

type ScalewayPermissions map[string]ScalewayPermCategory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ScalewayPermissions should have comment or be unexported

type ScalewayPermCategory map[string][]string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ScalewayPermCategory should have comment or be unexported


// 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 {
Expand Down Expand Up @@ -1674,6 +1682,28 @@ func (s *ScalewayAPI) GetUser() (*ScalewayUserDefinition, error) {
return &user.User, nil
}

func (s *ScalewayAPI) GetPermissions() (*ScalewayPermissionDefinition, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method ScalewayAPI.GetPermissions should have comment or be unexported

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")
Expand Down
18 changes: 16 additions & 2 deletions pkg/commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you removed the \n ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because fingerprint has already a \n

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok LGTM

}
}
fmt.Fprintf(ctx.Stdout, "\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add a space between the two sections

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

dashboard, err := ctx.API.GetDashboard()
Expand All @@ -74,7 +75,20 @@ func RunInfo(ctx CommandContext, args InfoArgs) error {
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)
fmt.Fprintf(ctx.Stdout, " Ips:\t\t\t%d\n\n", dashboard.IPsCount)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the \n on a dedicated print to keep the code organized by sections ?


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
}