/
workspace_service.go
295 lines (258 loc) · 8.19 KB
/
workspace_service.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package kong
import (
"context"
"encoding/json"
"fmt"
)
// AbstractWorkspaceService handles Workspaces in Kong.
type AbstractWorkspaceService interface {
// Exists checks the exitence of a Workspace in Kong.
Exists(ctx context.Context, nameOrID *string) (bool, error)
// ExistsByName checks the exitence of a Workspace using name only in Kong.
ExistsByName(ctx context.Context, name *string) (bool, error)
// Create creates a Workspace in Kong.
Create(ctx context.Context, workspace *Workspace) (*Workspace, error)
// Get fetches a Workspace in Kong.
Get(ctx context.Context, nameOrID *string) (*Workspace, error)
// Update updates a Workspace in Kong.
Update(ctx context.Context, workspace *Workspace) (*Workspace, error)
// Delete deletes a Workspace in Kong
Delete(ctx context.Context, WorkspaceOrID *string) error
// List fetches a list of all Workspaces in Kong.
List(ctx context.Context, opt *ListOpt) ([]*Workspace, *ListOpt, error)
// ListAll fetches all workspaces in Kong.
ListAll(ctx context.Context) ([]*Workspace, error)
// AddEntities adds entity ids given as a a comma delimited string
// to a given workspace in Kong. The response is a representation
// of the entity that was added to the workspace.
//
// Deprecated: Kong 2.x removed this endpoint.
AddEntities(ctx context.Context, workspaceNameOrID *string, entityIds *string) (*[]map[string]interface{}, error)
// DeleteEntities deletes entity ids given as a a comma delimited string
// to a given workspace in Kong.
//
// Deprecated: Kong 2.x removed this endpoint.
DeleteEntities(ctx context.Context, workspaceNameOrID *string, entityIds *string) error
// ListEntities fetches a list of all workspace entities in Kong.
//
// Deprecated: Kong 2.x removed this endpoint.
ListEntities(ctx context.Context, workspaceNameOrID *string) ([]*WorkspaceEntity, error)
}
// WorkspaceService handles Workspaces in Kong.
type WorkspaceService service
// Exists checks the exitence of the Workspace in Kong.
func (s *WorkspaceService) Exists(ctx context.Context,
nameOrID *string,
) (bool, error) {
if isEmptyString(nameOrID) {
return false, fmt.Errorf("nameOrID cannot be nil for Get operation")
}
endpoint := fmt.Sprintf("/workspaces/%v", *nameOrID)
return s.client.exists(ctx, endpoint)
}
// ExistsByName checks the exitence of the Workspace using its name in Kong.
func (s *WorkspaceService) ExistsByName(ctx context.Context,
name *string,
) (bool, error) {
if isEmptyString(name) {
return false, fmt.Errorf("nameOrID cannot be nil for Get operation")
}
endpoint := fmt.Sprintf("/%v/workspaces/%v", *name, *name)
return s.client.exists(ctx, endpoint)
}
// Create creates a Workspace in Kong.
func (s *WorkspaceService) Create(ctx context.Context,
workspace *Workspace,
) (*Workspace, error) {
if workspace == nil {
return nil, fmt.Errorf("cannot create a nil workspace")
}
endpoint := "/workspaces"
method := "POST"
if workspace.ID != nil {
endpoint = endpoint + "/" + *workspace.ID
method = "PUT"
}
req, err := s.client.NewRequest(method, endpoint, nil, workspace)
if err != nil {
return nil, err
}
var createdWorkspace Workspace
_, err = s.client.Do(ctx, req, &createdWorkspace)
if err != nil {
return nil, err
}
return &createdWorkspace, nil
}
// Get fetches a Workspace in Kong.
func (s *WorkspaceService) Get(ctx context.Context,
nameOrID *string,
) (*Workspace, error) {
if isEmptyString(nameOrID) {
return nil, fmt.Errorf("nameOrID cannot be nil for Get operation")
}
endpoint := fmt.Sprintf("/workspaces/%v", *nameOrID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var Workspace Workspace
_, err = s.client.Do(ctx, req, &Workspace)
if err != nil {
return nil, err
}
return &Workspace, nil
}
// Update updates a Workspace in Kong. Only updates to the
// `comment` field are supported. To rename a workspace use Create.
func (s *WorkspaceService) Update(ctx context.Context,
workspace *Workspace,
) (*Workspace, error) {
if workspace == nil {
return nil, fmt.Errorf("cannot update a nil Workspace")
}
if isEmptyString(workspace.ID) {
return nil, fmt.Errorf("ID cannot be nil for Update operation")
}
endpoint := fmt.Sprintf("/workspaces/%v", *workspace.ID)
req, err := s.client.NewRequest("PATCH", endpoint, nil, workspace)
if err != nil {
return nil, err
}
var updatedWorkspace Workspace
_, err = s.client.Do(ctx, req, &updatedWorkspace)
if err != nil {
return nil, err
}
return &updatedWorkspace, nil
}
// Delete deletes a Workspace in Kong
func (s *WorkspaceService) Delete(ctx context.Context,
WorkspaceOrID *string,
) error {
if isEmptyString(WorkspaceOrID) {
return fmt.Errorf("WorkspaceOrID cannot be nil for Delete operation")
}
endpoint := fmt.Sprintf("/workspaces/%v", *WorkspaceOrID)
req, err := s.client.NewRequest("DELETE", endpoint, nil, nil)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
return err
}
// List fetches a list of all Workspaces in Kong.
func (s *WorkspaceService) List(ctx context.Context,
opt *ListOpt,
) ([]*Workspace, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/workspaces/", opt)
if err != nil {
return nil, nil, err
}
var workspaces []*Workspace
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var workspace Workspace
err = json.Unmarshal(b, &workspace)
if err != nil {
return nil, nil, err
}
workspaces = append(workspaces, &workspace)
}
return workspaces, next, nil
}
// ListAll fetches all workspaces in Kong.
func (s *WorkspaceService) ListAll(ctx context.Context) ([]*Workspace, error) {
var workspaces, data []*Workspace
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, opt)
if err != nil {
return nil, err
}
workspaces = append(workspaces, data...)
}
return workspaces, nil
}
// AddEntities adds entity ids given as a a comma delimited string
// to a given workspace in Kong. The response is a representation
// of the entity that was added to the workspace.
//
// Deprecated: Kong 2.x removed this endpoint.
func (s *WorkspaceService) AddEntities(ctx context.Context,
workspaceNameOrID *string, entityIds *string,
) (*[]map[string]interface{}, error) {
if entityIds == nil {
return nil, fmt.Errorf("entityIds cannot be nil")
}
endpoint := fmt.Sprintf("/workspaces/%v/entities", *workspaceNameOrID)
var entities struct {
Entities *string `json:"entities,omitempty"`
}
entities.Entities = entityIds
req, err := s.client.NewRequest("POST", endpoint, nil, entities)
if err != nil {
return nil, err
}
var createdWorkspaceEntities []map[string]interface{}
_, err = s.client.Do(ctx, req, &createdWorkspaceEntities)
if err != nil {
return nil, err
}
return &createdWorkspaceEntities, nil
}
// DeleteEntities deletes entity ids given as a a comma delimited string
// to a given workspace in Kong.
//
// Deprecated: Kong 2.x removed this endpoint.
func (s *WorkspaceService) DeleteEntities(ctx context.Context,
workspaceNameOrID *string, entityIds *string,
) error {
if entityIds == nil {
return fmt.Errorf("entityIds cannot be nil")
}
endpoint := fmt.Sprintf("/workspaces/%v/entities", *workspaceNameOrID)
var entities struct {
Entities *string `json:"entities,omitempty"`
}
entities.Entities = entityIds
req, err := s.client.NewRequest("DELETE", endpoint, nil, entities)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}
// ListEntities fetches a list of all workspace entities in Kong.
//
// Deprecated: Kong 2.x removed this endpoint.
func (s *WorkspaceService) ListEntities(ctx context.Context,
workspaceNameOrID *string,
) ([]*WorkspaceEntity, error) {
endpoint := fmt.Sprintf("/workspaces/%v/entities", *workspaceNameOrID)
data, _, err := s.client.list(ctx, endpoint, nil)
if err != nil {
return nil, err
}
var workspaceEntities []*WorkspaceEntity
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, err
}
var workspaceEntity WorkspaceEntity
err = json.Unmarshal(b, &workspaceEntity)
if err != nil {
return nil, err
}
workspaceEntities = append(workspaceEntities, &workspaceEntity)
}
return workspaceEntities, nil
}