-
Notifications
You must be signed in to change notification settings - Fork 928
/
organization.go
194 lines (162 loc) · 5.37 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
package ccv3
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// Organization represents a Cloud Controller V3 Organization.
type Organization struct {
// GUID is the unique organization identifier.
GUID string `json:"guid,omitempty"`
// Name is the name of the organization.
Name string `json:"name"`
// Metadata is used for custom tagging of API resources
Metadata *Metadata `json:"metadata,omitempty"`
}
// CreateOrganization creates an organization with the given name.
func (client *Client) CreateOrganization(orgName string) (Organization, Warnings, error) {
org := Organization{Name: orgName}
orgBytes, err := json.Marshal(org)
if err != nil {
return Organization{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostOrganizationRequest,
Body: bytes.NewReader(orgBytes),
})
if err != nil {
return Organization{}, nil, err
}
var responseOrg Organization
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseOrg,
}
err = client.connection.Make(request, &response)
if err != nil {
return Organization{}, response.Warnings, err
}
return responseOrg, response.Warnings, err
}
// DeleteOrganization deletes the organization with the given GUID.
func (client *Client) DeleteOrganization(orgGUID string) (JobURL, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteOrganizationRequest,
URIParams: map[string]string{"organization_guid": orgGUID},
})
if err != nil {
return "", nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return JobURL(response.ResourceLocationURL), response.Warnings, err
}
// GetDefaultDomain gets the default domain for the organization with the given GUID.
func (client *Client) GetDefaultDomain(orgGUID string) (Domain, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetDefaultDomainRequest,
URIParams: map[string]string{"organization_guid": orgGUID},
})
if err != nil {
return Domain{}, nil, err
}
var defaultDomain Domain
response := cloudcontroller.Response{
DecodeJSONResponseInto: &defaultDomain,
}
err = client.connection.Make(request, &response)
return defaultDomain, response.Warnings, err
}
// GetIsolationSegmentOrganizations lists organizations
// entitled to an isolation segment.
func (client *Client) GetIsolationSegmentOrganizations(isolationSegmentGUID string) ([]Organization, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetIsolationSegmentOrganizationsRequest,
URIParams: map[string]string{"isolation_segment_guid": isolationSegmentGUID},
})
if err != nil {
return nil, nil, err
}
var fullOrgsList []Organization
warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
if app, ok := item.(Organization); ok {
fullOrgsList = append(fullOrgsList, app)
} else {
return ccerror.UnknownObjectInListError{
Expected: Organization{},
Unexpected: item,
}
}
return nil
})
return fullOrgsList, warnings, err
}
// GetOrganization gets an organization by the given guid.
func (client *Client) GetOrganization(orgGUID string) (Organization, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetOrganizationRequest,
URIParams: map[string]string{"organization_guid": orgGUID},
})
if err != nil {
return Organization{}, nil, err
}
var responseOrg Organization
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseOrg,
}
err = client.connection.Make(request, &response)
if err != nil {
return Organization{}, response.Warnings, err
}
return responseOrg, response.Warnings, nil
}
// GetOrganizations lists organizations with optional filters.
func (client *Client) GetOrganizations(query ...Query) ([]Organization, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetOrganizationsRequest,
Query: query,
})
if err != nil {
return nil, nil, err
}
var fullOrgsList []Organization
warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
if app, ok := item.(Organization); ok {
fullOrgsList = append(fullOrgsList, app)
} else {
return ccerror.UnknownObjectInListError{
Expected: Organization{},
Unexpected: item,
}
}
return nil
})
return fullOrgsList, warnings, err
}
// UpdateOrganization updates an organization with the given properties.
func (client *Client) UpdateOrganization(org Organization) (Organization, Warnings, error) {
orgGUID := org.GUID
org.GUID = ""
orgBytes, err := json.Marshal(org)
if err != nil {
return Organization{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PatchOrganizationRequest,
Body: bytes.NewReader(orgBytes),
URIParams: map[string]string{"organization_guid": orgGUID},
})
if err != nil {
return Organization{}, nil, err
}
var responseOrg Organization
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseOrg,
}
err = client.connection.Make(request, &response)
if err != nil {
return Organization{}, nil, err
}
return responseOrg, nil, err
}