forked from cloudfoundry/cli
/
organization.go
253 lines (209 loc) · 7.04 KB
/
organization.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package ccv2
import (
"bytes"
"encoding/json"
"net/url"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Organization represents a Cloud Controller Organization.
type Organization struct {
// GUID is the unique Organization identifier.
GUID string
// Name is the organization's name.
Name string
// QuotaDefinitionGUID is unique identifier of the quota assigned to this
// organization.
QuotaDefinitionGUID string
// DefaultIsolationSegmentGUID is the unique identifier of the isolation
// segment this organization is tagged with.
DefaultIsolationSegmentGUID string
}
// UnmarshalJSON helps unmarshal a Cloud Controller Organization response.
func (org *Organization) UnmarshalJSON(data []byte) error {
var ccOrg struct {
Metadata internal.Metadata `json:"metadata"`
Entity struct {
Name string `json:"name"`
QuotaDefinitionGUID string `json:"quota_definition_guid"`
DefaultIsolationSegmentGUID string `json:"default_isolation_segment_guid"`
} `json:"entity"`
}
err := cloudcontroller.DecodeJSON(data, &ccOrg)
if err != nil {
return err
}
org.GUID = ccOrg.Metadata.GUID
org.Name = ccOrg.Entity.Name
org.QuotaDefinitionGUID = ccOrg.Entity.QuotaDefinitionGUID
org.DefaultIsolationSegmentGUID = ccOrg.Entity.DefaultIsolationSegmentGUID
return nil
}
type createOrganizationRequestBody struct {
Name string `json:"name"`
QuotaDefinitionGUID string `json:"quota_definition_guid,omitempty"`
}
func (client *Client) CreateOrganization(orgName string, quotaGUID string) (Organization, Warnings, error) {
requestBody := createOrganizationRequestBody{
Name: orgName,
QuotaDefinitionGUID: quotaGUID,
}
bodyBytes, err := json.Marshal(requestBody)
if err != nil {
return Organization{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostOrganizationRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return Organization{}, nil, err
}
var org Organization
response := cloudcontroller.Response{
DecodeJSONResponseInto: &org,
}
err = client.connection.Make(request, &response)
return org, response.Warnings, err
}
// DeleteOrganization deletes the Organization associated with the provided
// GUID. It will return the Cloud Controller job that is assigned to the
// Organization deletion.
func (client *Client) DeleteOrganization(guid string) (Job, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteOrganizationRequest,
URIParams: Params{"organization_guid": guid},
Query: url.Values{
"recursive": {"true"},
"async": {"true"},
},
})
if err != nil {
return Job{}, nil, err
}
var job Job
response := cloudcontroller.Response{
DecodeJSONResponseInto: &job,
}
err = client.connection.Make(request, &response)
return job, response.Warnings, err
}
// GetOrganization returns an Organization associated with the provided GUID.
func (client *Client) GetOrganization(guid string) (Organization, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetOrganizationRequest,
URIParams: Params{"organization_guid": guid},
})
if err != nil {
return Organization{}, nil, err
}
var org Organization
response := cloudcontroller.Response{
DecodeJSONResponseInto: &org,
}
err = client.connection.Make(request, &response)
return org, response.Warnings, err
}
// GetOrganizations returns back a list of Organizations based off of the
// provided filters.
func (client *Client) GetOrganizations(filters ...Filter) ([]Organization, Warnings, error) {
allQueries := ConvertFilterParameters(filters)
allQueries.Add("order-by", "name")
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetOrganizationsRequest,
Query: allQueries,
})
if err != nil {
return nil, nil, err
}
var fullOrgsList []Organization
warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
if org, ok := item.(Organization); ok {
fullOrgsList = append(fullOrgsList, org)
} else {
return ccerror.UnknownObjectInListError{
Expected: Organization{},
Unexpected: item,
}
}
return nil
})
return fullOrgsList, warnings, err
}
type updateOrgManagerByUsernameRequestBody struct {
Username string `json:"username"`
}
// UpdateOrganizationManager assigns the org manager role to the UAA user or client with the provided ID.
func (client *Client) UpdateOrganizationManager(guid string, uaaID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutOrganizationManagerRequest,
URIParams: Params{"organization_guid": guid, "manager_guid": uaaID},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// UpdateOrganizationManagerByUsername assigns the org manager role to the user with the provided name.
func (client *Client) UpdateOrganizationManagerByUsername(guid string, username string) (Warnings, error) {
requestBody := updateOrgManagerByUsernameRequestBody{
Username: username,
}
body, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutOrganizationManagerByUsernameRequest,
Body: bytes.NewReader(body),
URIParams: Params{"organization_guid": guid},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// UpdateOrganizationUser makes the user or client with the given UAA ID a
// member of the org.
func (client *Client) UpdateOrganizationUser(guid string, uaaID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutOrganizationUserRequest,
URIParams: Params{"organization_guid": guid, "user_guid": uaaID},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
type updateOrgUserByUsernameRequestBody struct {
Username string `json:"username"`
}
// UpdateOrganizationUserByUsername makes the user with the given username a member of
// the org.
func (client Client) UpdateOrganizationUserByUsername(orgGUID string, username string) (Warnings, error) {
requestBody := updateOrgUserByUsernameRequestBody{
Username: username,
}
body, err := json.Marshal(requestBody)
if err != nil {
return Warnings{}, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutOrganizationUserByUsernameRequest,
Body: bytes.NewReader(body),
URIParams: Params{"organization_guid": orgGUID},
})
if err != nil {
return Warnings{}, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}