-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathworkspace_service.go
149 lines (122 loc) · 3.36 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
package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
)
// WorkspaceService handles Workspaces in Kong.
type WorkspaceService service
// Create creates a Workspace in Kong.
func (s *WorkspaceService) Create(ctx context.Context,
workspace *Workspace) (*Workspace, error) {
if workspace == nil {
return nil, errors.New("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, errors.New("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, errors.New("cannot update a nil Workspace")
}
if isEmptyString(workspace.ID) {
return nil, errors.New("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 errors.New("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
}