Skip to content

Commit 6552346

Browse files
author
Joshua Anderson
committed
feat(client-go): add perms endpoint
1 parent 73246fc commit 6552346

6 files changed

Lines changed: 651 additions & 0 deletions

File tree

client-go/cmd/perms.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/deis/deis/client-go/controller/client"
7+
"github.com/deis/deis/client-go/controller/models/perms"
8+
)
9+
10+
// PermsList prints which users have permissions.
11+
func PermsList(appID string, admin bool) error {
12+
c, appID, err := permsLoad(appID, admin)
13+
14+
if err != nil {
15+
return err
16+
}
17+
18+
var users []string
19+
20+
if admin {
21+
users, err = perms.ListAdmins(c)
22+
} else {
23+
users, err = perms.List(c, appID)
24+
}
25+
26+
if err != nil {
27+
return err
28+
}
29+
30+
if admin {
31+
fmt.Println("=== Administrators")
32+
} else {
33+
fmt.Printf("=== %s's Users\n", appID)
34+
}
35+
36+
for _, user := range users {
37+
fmt.Println(user)
38+
}
39+
40+
return nil
41+
}
42+
43+
// PermCreate adds a user to an app or makes them an administrator.
44+
func PermCreate(appID string, username string, admin bool) error {
45+
46+
c, appID, err := permsLoad(appID, admin)
47+
48+
if err != nil {
49+
return err
50+
}
51+
52+
if admin {
53+
fmt.Printf("Adding %s to system administrators... ", username)
54+
perms.NewAdmin(c, username)
55+
} else {
56+
fmt.Printf("Adding %s to %s collaborators... ", username, appID)
57+
perms.New(c, appID, username)
58+
}
59+
60+
if err != nil {
61+
return err
62+
}
63+
64+
fmt.Println("done")
65+
66+
return nil
67+
}
68+
69+
// PermDelete removes a user from an app or revokes admin privilages.
70+
func PermDelete(appID string, username string, admin bool) error {
71+
72+
c, appID, err := permsLoad(appID, admin)
73+
74+
if err != nil {
75+
return err
76+
}
77+
78+
if admin {
79+
fmt.Printf("Removing %s from system administrators... ", username)
80+
perms.DeleteAdmin(c, username)
81+
} else {
82+
fmt.Printf("Removing %s from %s collaborators... ", username, appID)
83+
perms.Delete(c, appID, username)
84+
}
85+
86+
if err != nil {
87+
return err
88+
}
89+
90+
fmt.Println("done")
91+
92+
return nil
93+
}
94+
95+
func permsLoad(appID string, admin bool) (*client.Client, string, error) {
96+
c, err := client.New()
97+
98+
if err != nil {
99+
return nil, "", err
100+
}
101+
102+
if !admin && appID == "" {
103+
appID, err = c.DetectApp()
104+
105+
if err != nil {
106+
return nil, "", err
107+
}
108+
}
109+
110+
return c, appID, err
111+
}

client-go/controller/api/perms.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package api
2+
3+
// PermsAppResponse is the definition of GET /v1/apps/<app id>/perms/.
4+
type PermsAppResponse struct {
5+
Users []string `json:"users"`
6+
}
7+
8+
// PermsAdminResponse is the definition of GET /v1/admin/perms/.
9+
type PermsAdminResponse struct {
10+
Count int `json:"count"`
11+
Next int `json:"next"`
12+
Previous int `json:"previous"`
13+
Users []struct {
14+
Username string `json:"username"`
15+
} `json:"results"`
16+
}
17+
18+
// PermsRequest is the definition of a requst on /perms/.
19+
type PermsRequest struct {
20+
Username string `json:"username"`
21+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package perms
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/deis/deis/client-go/controller/api"
9+
"github.com/deis/deis/client-go/controller/client"
10+
)
11+
12+
// List users that can access an app.
13+
func List(c *client.Client, appID string) ([]string, error) {
14+
body, err := doList(c, fmt.Sprintf("/v1/apps/%s/perms/", appID))
15+
16+
if err != nil {
17+
return []string{}, err
18+
}
19+
20+
users := api.PermsAppResponse{}
21+
if err = json.Unmarshal([]byte(body), &users); err != nil {
22+
return []string{}, err
23+
}
24+
25+
return users.Users, nil
26+
}
27+
28+
// ListAdmins lists administrators.
29+
func ListAdmins(c *client.Client) ([]string, error) {
30+
body, err := doList(c, "/v1/admin/perms/")
31+
32+
if err != nil {
33+
return []string{}, err
34+
}
35+
36+
users := api.PermsAdminResponse{}
37+
if err = json.Unmarshal([]byte(body), &users); err != nil {
38+
return []string{}, err
39+
}
40+
41+
usersList := []string{}
42+
43+
for _, user := range users.Users {
44+
usersList = append(usersList, user.Username)
45+
}
46+
47+
return usersList, nil
48+
}
49+
50+
func doList(c *client.Client, u string) (string, error) {
51+
body, status, err := c.BasicRequest("GET", u, nil)
52+
53+
if err != nil {
54+
return "", err
55+
}
56+
57+
if status != 200 {
58+
return "", errors.New(body)
59+
}
60+
61+
return body, nil
62+
}
63+
64+
// New adds a user to an app.
65+
func New(c *client.Client, appID string, username string) error {
66+
return doNew(c, fmt.Sprintf("/v1/apps/%s/perms/", appID), username)
67+
}
68+
69+
// NewAdmin makes a user an administrator.
70+
func NewAdmin(c *client.Client, username string) error {
71+
return doNew(c, "/v1/admin/perms/", username)
72+
}
73+
74+
func doNew(c *client.Client, u string, username string) error {
75+
req := api.PermsRequest{Username: username}
76+
77+
reqBody, err := json.Marshal(req)
78+
79+
if err != nil {
80+
return err
81+
}
82+
83+
body, status, err := c.BasicRequest("POST", u, reqBody)
84+
85+
if err != nil {
86+
return err
87+
}
88+
89+
if status != 201 {
90+
return errors.New(body)
91+
}
92+
93+
return nil
94+
}
95+
96+
// Delete removes a user from an app.
97+
func Delete(c *client.Client, appID string, username string) error {
98+
return doDelete(c, fmt.Sprintf("/v1/apps/%s/perms/%s", appID, username))
99+
}
100+
101+
// DeleteAdmin removes administrative privilages from a user.
102+
func DeleteAdmin(c *client.Client, username string) error {
103+
return doDelete(c, fmt.Sprintf("/v1/admin/perms/%s", username))
104+
}
105+
106+
func doDelete(c *client.Client, u string) error {
107+
body, status, err := c.BasicRequest("DELETE", u, nil)
108+
109+
if err != nil {
110+
return err
111+
}
112+
113+
if status != 204 {
114+
return errors.New(body)
115+
}
116+
117+
return nil
118+
}

0 commit comments

Comments
 (0)