forked from cloud-gov/uaa-credentials-broker
/
cf.go
92 lines (72 loc) · 2.3 KB
/
cf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"net/http"
"code.cloudfoundry.org/lager"
)
type PAASClient interface {
CreateUser(userID string) error
DeleteUser(userID string) error
AddUserToOrg(userID, orgID string) error
AddUserToSpace(userID, spaceID string) error
}
type CFClient struct {
logger lager.Logger
client *http.Client
endpoint string
}
func (c *CFClient) CreateUser(userID string) error {
c.logger.Info("cf-create-user", lager.Data{"userID": userID})
body, _ := encodeBody(map[string]string{"guid": userID})
req, _ := http.NewRequest("POST", fmt.Sprintf("%s/v2/users", c.endpoint), body)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 201 {
return fmt.Errorf("Expected status 201; got: %d", resp.StatusCode)
}
return nil
}
func (c *CFClient) DeleteUser(userID string) error {
c.logger.Info("cf-delete-user", lager.Data{"userID": userID})
req, _ := http.NewRequest("DELETE", fmt.Sprintf("%s/v2/users/%s", c.endpoint, userID), nil)
resp, err := c.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 204 {
return fmt.Errorf("Expected status 204; got: %d", resp.StatusCode)
}
return nil
}
func (c *CFClient) AddUserToOrg(userID, orgID string) error {
c.logger.Info("cf-add-org-user", lager.Data{"userID": userID, "orgID": orgID})
req, _ := http.NewRequest("PUT", fmt.Sprintf("%s/v2/organizations/%s/users/%s", c.endpoint, orgID, userID), nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 201 {
return fmt.Errorf("Expected status 201; got: %d", resp.StatusCode)
}
return nil
}
func (c *CFClient) AddUserToSpace(userID, spaceID string) error {
c.logger.Info("cf-add-org-user", lager.Data{"userID": userID, "spaceID": spaceID})
req, _ := http.NewRequest("PUT", fmt.Sprintf("%s/v2/spaces/%s/developers/%s", c.endpoint, spaceID, userID), nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 201 {
return fmt.Errorf("Expected status 201; got: %d", resp.StatusCode)
}
return nil
}