forked from cloudfoundry/go-cfclient
/
isolationsegments.go
263 lines (237 loc) · 7.79 KB
/
isolationsegments.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
254
255
256
257
258
259
260
261
262
263
package cfclient
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/pkg/errors"
)
type IsolationSegment struct {
GUID string `json:"guid"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
c *Client
}
type IsolationSegementResponse struct {
GUID string `json:"guid"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Spaces struct {
Href string `json:"href"`
} `json:"spaces"`
Organizations struct {
Href string `json:"href"`
} `json:"organizations"`
} `json:"links"`
}
type ListIsolationSegmentsResponse struct {
Pagination Pagination `json:"pagination"`
Resources []IsolationSegementResponse `json:"resources"`
}
func (c *Client) CreateIsolationSegment(name string) (*IsolationSegment, error) {
req := c.NewRequest("POST", "/v3/isolation_segments")
req.obj = map[string]interface{}{
"name": name,
}
resp, err := c.DoRequest(req)
if err != nil {
return nil, errors.Wrap(err, "Error while creating isolation segment")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("Error creating isolation segment %s, response code: %d", name, resp.StatusCode)
}
return respBodyToIsolationSegment(resp.Body, c)
}
func respBodyToIsolationSegment(body io.ReadCloser, c *Client) (*IsolationSegment, error) {
bodyRaw, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
}
isr := IsolationSegementResponse{}
err = json.Unmarshal(bodyRaw, &isr)
if err != nil {
return nil, err
}
return &IsolationSegment{
GUID: isr.GUID,
Name: isr.Name,
CreatedAt: isr.CreatedAt,
UpdatedAt: isr.UpdatedAt,
c: c,
}, nil
}
func (c *Client) GetIsolationSegmentByGUID(guid string) (*IsolationSegment, error) {
var isr IsolationSegementResponse
r := c.NewRequest("GET", "/v3/isolation_segments/"+guid)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting isolation segment by GUID")
}
defer resp.Body.Close()
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading isolation segment response body")
}
err = json.Unmarshal(resBody, &isr)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshalling isolation segment response")
}
return &IsolationSegment{Name: isr.Name, GUID: isr.GUID, CreatedAt: isr.CreatedAt, UpdatedAt: isr.UpdatedAt, c: c}, nil
}
func (c *Client) ListIsolationSegmentsByQuery(query url.Values) ([]IsolationSegment, error) {
var iss []IsolationSegment
requestURL := "/v3/isolation_segments"
if e := query.Encode(); len(e) > 0 {
requestURL += "?" + e
}
for {
var isr ListIsolationSegmentsResponse
r := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting isolation segments")
}
defer resp.Body.Close()
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading isolation segment request")
}
err = json.Unmarshal(resBody, &isr)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshalling isolation segment")
}
for _, is := range isr.Resources {
iss = append(iss, IsolationSegment{
Name: is.Name,
GUID: is.GUID,
CreatedAt: is.CreatedAt,
UpdatedAt: is.UpdatedAt,
c: c,
})
}
requestURL = isr.Pagination.Next.Href
if requestURL == "" {
break
}
requestURL, err = extractPathFromURL(requestURL)
if err != nil {
return nil, err
}
}
return iss, nil
}
func (c *Client) ListIsolationSegments() ([]IsolationSegment, error) {
return c.ListIsolationSegmentsByQuery(nil)
}
// TODO listOrgsForIsolationSegments
// TODO listSpacesForIsolationSegments
// TODO setDefaultIsolationSegmentForOrg
func (c *Client) DeleteIsolationSegmentByGUID(guid string) error {
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v3/isolation_segments/%s", guid)))
if err != nil {
return errors.Wrap(err, "Error during sending DELETE request for isolation segments")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Error deleting isolation segment %s, response code: %d", guid, resp.StatusCode)
}
return nil
}
func (i *IsolationSegment) Delete() error {
return i.c.DeleteIsolationSegmentByGUID(i.GUID)
}
func (c *Client) AddIsolationSegmentToOrg(isolationSegmentGUID, orgGUID string) error {
isoSegment := IsolationSegment{GUID: isolationSegmentGUID, c: c}
return isoSegment.AddOrg(orgGUID)
}
func (c *Client) RemoveIsolationSegmentFromOrg(isolationSegmentGUID, orgGUID string) error {
isoSegment := IsolationSegment{GUID: isolationSegmentGUID, c: c}
return isoSegment.RemoveOrg(orgGUID)
}
func (c *Client) AddIsolationSegmentToSpace(isolationSegmentGUID, spaceGUID string) error {
isoSegment := IsolationSegment{GUID: isolationSegmentGUID, c: c}
return isoSegment.AddSpace(spaceGUID)
}
func (c *Client) RemoveIsolationSegmentFromSpace(isolationSegmentGUID, spaceGUID string) error {
isoSegment := IsolationSegment{GUID: isolationSegmentGUID, c: c}
return isoSegment.RemoveSpace(spaceGUID)
}
func (i *IsolationSegment) AddOrg(orgGuid string) error {
if i == nil || i.c == nil {
return errors.New("No communication handle.")
}
req := i.c.NewRequest("POST", fmt.Sprintf("/v3/isolation_segments/%s/relationships/organizations", i.GUID))
type Entry struct {
GUID string `json:"guid"`
}
req.obj = map[string]interface{}{
"data": []Entry{{GUID: orgGuid}},
}
resp, err := i.c.DoRequest(req)
if err != nil {
return errors.Wrap(err, "Error during adding org to isolation segment")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error adding org %s to isolation segment %s, response code: %d", orgGuid, i.Name, resp.StatusCode)
}
return nil
}
func (i *IsolationSegment) RemoveOrg(orgGuid string) error {
if i == nil || i.c == nil {
return errors.New("No communication handle.")
}
req := i.c.NewRequest("DELETE", fmt.Sprintf("/v3/isolation_segments/%s/relationships/organizations/%s", i.GUID, orgGuid))
resp, err := i.c.DoRequest(req)
if err != nil {
return errors.Wrapf(err, "Error during removing org %s in isolation segment %s", orgGuid, i.Name)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Error deleting org %s in isolation segment %s, response code: %d", orgGuid, i.Name, resp.StatusCode)
}
return nil
}
func (i *IsolationSegment) AddSpace(spaceGuid string) error {
if i == nil || i.c == nil {
return errors.New("No communication handle.")
}
req := i.c.NewRequest("PUT", fmt.Sprintf("/v2/spaces/%s", spaceGuid))
req.obj = map[string]interface{}{
"isolation_segment_guid": i.GUID,
}
resp, err := i.c.DoRequest(req)
if err != nil {
return errors.Wrapf(err, "Error during adding space %s to isolation segment %s", spaceGuid, i.Name)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("Error adding space to isolation segment %s, response code: %d", i.Name, resp.StatusCode)
}
return nil
}
func (i *IsolationSegment) RemoveSpace(spaceGuid string) error {
if i == nil || i.c == nil {
return errors.New("No communication handle.")
}
req := i.c.NewRequest("DELETE", fmt.Sprintf("/v2/spaces/%s/isolation_segment", spaceGuid))
resp, err := i.c.DoRequest(req)
if err != nil {
return errors.Wrapf(err, "Error during deleting space %s in isolation segment %s", spaceGuid, i.Name)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Error deleting space %s from isolation segment %s, response code: %d", spaceGuid, i.Name, resp.StatusCode)
}
return nil
}