/
v3organizations.go
178 lines (147 loc) · 5.13 KB
/
v3organizations.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package cfclient
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type V3Organization struct {
Name string `json:"name,omitempty"`
GUID string `json:"guid,omitempty"`
Suspended *bool `json:"suspended,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
Relationships map[string]V3ToOneRelationship `json:"relationships,omitempty"`
Links map[string]Link `json:"links,omitempty"`
Metadata V3Metadata `json:"metadata,omitempty"`
}
type CreateV3OrganizationRequest struct {
Name string
Suspended *bool `json:"suspended,omitempty"`
Metadata *V3Metadata
}
type UpdateV3OrganizationRequest struct {
Name string
Suspended *bool `json:"suspended,omitempty"`
Metadata *V3Metadata
}
func (c *Client) CreateV3Organization(r CreateV3OrganizationRequest) (*V3Organization, error) {
req := c.NewRequest("POST", "/v3/organizations")
params := map[string]interface{}{
"name": r.Name,
}
if r.Suspended != nil {
params["suspended"] = r.Suspended
}
if r.Metadata != nil {
params["metadata"] = r.Metadata
}
req.obj = params
resp, err := c.DoRequest(req)
if err != nil {
return nil, errors.Wrap(err, "Error while creating v3 organization")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("Error creating v3 organization %s, response code: %d", r.Name, resp.StatusCode)
}
var organization V3Organization
if err := json.NewDecoder(resp.Body).Decode(&organization); err != nil {
return nil, errors.Wrap(err, "Error reading v3 organization JSON")
}
return &organization, nil
}
func (c *Client) GetV3OrganizationByGUID(organizationGUID string) (*V3Organization, error) {
req := c.NewRequest("GET", "/v3/organizations/"+organizationGUID)
resp, err := c.DoRequest(req)
if err != nil {
return nil, errors.Wrap(err, "Error while getting v3 organization")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Error getting v3 organization with GUID [%s], response code: %d", organizationGUID, resp.StatusCode)
}
var organization V3Organization
if err := json.NewDecoder(resp.Body).Decode(&organization); err != nil {
return nil, errors.Wrap(err, "Error reading v3 organization JSON")
}
return &organization, nil
}
func (c *Client) DeleteV3Organization(organizationGUID string) error {
req := c.NewRequest("DELETE", "/v3/organizations/"+organizationGUID)
resp, err := c.DoRequest(req)
if err != nil {
return errors.Wrap(err, "Error while deleting v3 organization")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
return fmt.Errorf("Error deleting v3 organization with GUID [%s], response code: %d", organizationGUID, resp.StatusCode)
}
return nil
}
func (c *Client) UpdateV3Organization(organizationGUID string, r UpdateV3OrganizationRequest) (*V3Organization, error) {
req := c.NewRequest("PATCH", "/v3/organizations/"+organizationGUID)
params := make(map[string]interface{})
if r.Name != "" {
params["name"] = r.Name
}
if r.Suspended != nil {
params["suspended"] = r.Suspended
}
if r.Metadata != nil {
params["metadata"] = r.Metadata
}
if len(params) > 0 {
req.obj = params
}
resp, err := c.DoRequest(req)
if err != nil {
return nil, errors.Wrap(err, "Error while updating v3 organization")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Error updating v3 organization %s, response code: %d", organizationGUID, resp.StatusCode)
}
var organization V3Organization
if err := json.NewDecoder(resp.Body).Decode(&organization); err != nil {
return nil, errors.Wrap(err, "Error reading v3 organization JSON")
}
return &organization, nil
}
type listV3OrganizationsResponse struct {
Pagination Pagination `json:"pagination,omitempty"`
Resources []V3Organization `json:"resources,omitempty"`
}
func (c *Client) ListV3OrganizationsByQuery(query url.Values) ([]V3Organization, error) {
var organizations []V3Organization
requestURL := "/v3/organizations"
if e := query.Encode(); len(e) > 0 {
requestURL += "?" + e
}
for {
r := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting v3 organizations")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Error listing v3 organizations, response code: %d", resp.StatusCode)
}
var data listV3OrganizationsResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, errors.Wrap(err, "Error parsing JSON from list v3 organizations")
}
organizations = append(organizations, data.Resources...)
requestURL = data.Pagination.Next.Href
if requestURL == "" || query.Get("page") != "" {
break
}
requestURL, err = extractPathFromURL(requestURL)
if err != nil {
return nil, errors.Wrap(err, "Error parsing the next page request url for v3 organizations")
}
}
return organizations, nil
}