-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathspace.go
211 lines (185 loc) · 7.05 KB
/
space.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
package client
import (
"context"
"net/url"
"github.com/cloudfoundry/go-cfclient/v3/internal/path"
"github.com/cloudfoundry/go-cfclient/v3/resource"
)
type SpaceClient commonClient
// SpaceListOptions list filters
type SpaceListOptions struct {
*ListOptions
GUIDs Filter `qs:"guids"` // list of space guids to filter by
Names Filter `qs:"names"` // list of space names to filter by
OrganizationGUIDs Filter `qs:"organization_guids"` // list of organization guids to filter by
Include resource.SpaceIncludeType `qs:"include"` // include parent objects if any
}
// NewSpaceListOptions creates new options to pass to list
func NewSpaceListOptions() *SpaceListOptions {
return &SpaceListOptions{
ListOptions: NewListOptions(),
}
}
func (o SpaceListOptions) ToQueryString() (url.Values, error) {
return o.ListOptions.ToQueryString(o)
}
// AssignIsolationSegment assigns an isolation segment to the space
//
// Apps will not run in the isolation segment until they are restarted
// An empty isolationSegmentGUID will un-assign the isolation segment
func (c *SpaceClient) AssignIsolationSegment(ctx context.Context, guid, isolationSegmentGUID string) error {
r := &resource.NullableToOneRelationship{
Data: &resource.NullableRelationship{
GUID: &isolationSegmentGUID,
},
}
if isolationSegmentGUID == "" {
r.Data.GUID = nil // set data to null to remove the relationship
}
_, err := c.client.patch(ctx, path.Format("/v3/spaces/%s/relationships/isolation_segment", guid), r, nil)
return err
}
// Create a new space
func (c *SpaceClient) Create(ctx context.Context, r *resource.SpaceCreate) (*resource.Space, error) {
var space resource.Space
_, err := c.client.post(ctx, "/v3/spaces", r, &space)
if err != nil {
return nil, err
}
return &space, nil
}
// Delete the specified space asynchronously and return a jobGUID
func (c *SpaceClient) Delete(ctx context.Context, guid string) (string, error) {
return c.client.delete(ctx, path.Format("/v3/spaces/%s", guid))
}
// First returns the first space matching the options or an error when less than 1 match
func (c *SpaceClient) First(ctx context.Context, opts *SpaceListOptions) (*resource.Space, error) {
return First[*SpaceListOptions, *resource.Space](opts, func(opts *SpaceListOptions) ([]*resource.Space, *Pager, error) {
return c.List(ctx, opts)
})
}
// Get the specified space
func (c *SpaceClient) Get(ctx context.Context, guid string) (*resource.Space, error) {
var space resource.Space
err := c.client.get(ctx, path.Format("/v3/spaces/%s", guid), &space)
if err != nil {
return nil, err
}
return &space, nil
}
// GetAssignedIsolationSegment gets the space's assigned isolation segment, if any
func (c *SpaceClient) GetAssignedIsolationSegment(ctx context.Context, guid string) (string, error) {
var relation resource.ToOneRelationship
err := c.client.get(ctx, path.Format("/v3/spaces/%s/relationships/isolation_segment", guid), &relation)
if err != nil {
return "", err
}
if relation.Data == nil {
return "", nil
}
return relation.Data.GUID, nil
}
// GetIncludeOrganization allows callers to fetch a space and include the parent organization
func (c *SpaceClient) GetIncludeOrganization(ctx context.Context, guid string) (*resource.Space, *resource.Organization, error) {
var space resource.SpaceWithIncluded
err := c.client.get(ctx, path.Format("/v3/spaces/%s?include=%s", guid, resource.SpaceIncludeOrganization), &space)
if err != nil {
return nil, nil, err
}
return &space.Space, space.Included.Organizations[0], nil
}
// List pages all spaces the user has access to
func (c *SpaceClient) List(ctx context.Context, opts *SpaceListOptions) ([]*resource.Space, *Pager, error) {
if opts == nil {
opts = NewSpaceListOptions()
}
opts.Include = resource.SpaceIncludeNone
var res resource.SpaceList
err := c.client.list(ctx, "/v3/spaces", opts.ToQueryString, &res)
if err != nil {
return nil, nil, err
}
pager := NewPager(res.Pagination)
return res.Resources, pager, nil
}
// ListAll retrieves all spaces the user has access to
func (c *SpaceClient) ListAll(ctx context.Context, opts *SpaceListOptions) ([]*resource.Space, error) {
if opts == nil {
opts = NewSpaceListOptions()
}
return AutoPage[*SpaceListOptions, *resource.Space](opts, func(opts *SpaceListOptions) ([]*resource.Space, *Pager, error) {
return c.List(ctx, opts)
})
}
// ListIncludeOrganizations page all spaces the user has access to and include the parent organizations
func (c *SpaceClient) ListIncludeOrganizations(ctx context.Context, opts *SpaceListOptions) ([]*resource.Space, []*resource.Organization, *Pager, error) {
if opts == nil {
opts = NewSpaceListOptions()
}
opts.Include = resource.SpaceIncludeOrganization
var res resource.SpaceList
err := c.client.list(ctx, "/v3/spaces", opts.ToQueryString, &res)
if err != nil {
return nil, nil, nil, err
}
pager := NewPager(res.Pagination)
return res.Resources, res.Included.Organizations, pager, nil
}
// ListIncludeOrganizationsAll retrieves all spaces the user has access to and include the parent organizations
func (c *SpaceClient) ListIncludeOrganizationsAll(ctx context.Context, opts *SpaceListOptions) ([]*resource.Space, []*resource.Organization, error) {
if opts == nil {
opts = NewSpaceListOptions()
}
var all []*resource.Space
var allOrgs []*resource.Organization
for {
page, orgs, pager, err := c.ListIncludeOrganizations(ctx, opts)
if err != nil {
return nil, nil, err
}
all = append(all, page...)
allOrgs = append(allOrgs, orgs...)
if !pager.HasNextPage() {
break
}
pager.NextPage(opts)
}
return all, allOrgs, nil
}
// ListUsers pages users by space GUID
func (c *SpaceClient) ListUsers(ctx context.Context, spaceGUID string, opts *UserListOptions) ([]*resource.User, *Pager, error) {
if opts == nil {
opts = NewUserListOptions()
}
var res resource.UserList
err := c.client.list(ctx, "/v3/spaces/"+spaceGUID+"/users", opts.ToQueryString, &res)
if err != nil {
return nil, nil, err
}
pager := NewPager(res.Pagination)
return res.Resources, pager, nil
}
// ListUsersAll retrieves all users by space GUID
func (c *SpaceClient) ListUsersAll(ctx context.Context, spaceGUID string, opts *UserListOptions) ([]*resource.User, error) {
if opts == nil {
opts = NewUserListOptions()
}
return AutoPage[*UserListOptions, *resource.User](opts, func(opts *UserListOptions) ([]*resource.User, *Pager, error) {
return c.ListUsers(ctx, spaceGUID, opts)
})
}
// Single returns a single space matching the options or an error if not exactly 1 match
func (c *SpaceClient) Single(ctx context.Context, opts *SpaceListOptions) (*resource.Space, error) {
return Single[*SpaceListOptions, *resource.Space](opts, func(opts *SpaceListOptions) ([]*resource.Space, *Pager, error) {
return c.List(ctx, opts)
})
}
// Update the specified attributes of a space
func (c *SpaceClient) Update(ctx context.Context, guid string, r *resource.SpaceUpdate) (*resource.Space, error) {
var space resource.Space
_, err := c.client.patch(ctx, path.Format("/v3/spaces/%s", guid), r, &space)
if err != nil {
return nil, err
}
return &space, nil
}