forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorgs.go
More file actions
77 lines (67 loc) · 1.74 KB
/
Copy pathorgs.go
File metadata and controls
77 lines (67 loc) · 1.74 KB
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
package cfclient
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
)
type OrgResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
Resources []OrgResource `json:"resources"`
}
type OrgResource struct {
Meta Meta `json:"metadata"`
Entity Org `json:"entity"`
}
type Org struct {
Guid string `json:"guid"`
Name string `json:"name"`
c *Client
}
func (c *Client) ListOrgs() ([]Org, error) {
var orgs []Org
var orgResp OrgResponse
r := c.NewRequest("GET", "/v2/organizations")
resp, err := c.DoRequest(r)
if err != nil {
return nil, fmt.Errorf("Error requesting organizations %v", err)
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading organization request %v", resBody)
}
err = json.Unmarshal(resBody, &orgResp)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling organization %v", err)
}
for _, org := range orgResp.Resources {
org.Entity.Guid = org.Meta.Guid
org.Entity.c = c
orgs = append(orgs, org.Entity)
}
return orgs, nil
}
func (c *Client) OrgSpaces(guid string) ([]Space, error) {
var spaces []Space
var spaceResp SpaceResponse
path := fmt.Sprintf("/v2/organizations/%s/spaces", guid)
r := c.NewRequest("GET", path)
resp, err := c.DoRequest(r)
if err != nil {
return nil, fmt.Errorf("Error requesting space %v", err)
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading space request %v", resBody)
}
err = json.Unmarshal(resBody, &spaceResp)
if err != nil {
return nil, fmt.Errorf("Error space organization %v", err)
}
for _, space := range spaceResp.Resources {
space.Entity.Guid = space.Meta.Guid
spaces = append(spaces, space.Entity)
}
return spaces, nil
}