-
Notifications
You must be signed in to change notification settings - Fork 41
/
degraphql_route_service.go
220 lines (189 loc) · 6.13 KB
/
degraphql_route_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
package kong
import (
"context"
"encoding/json"
"fmt"
)
type AbstractDegraphqlRouteService interface {
// Creates a DeGraphQL route in kong.
Create(ctx context.Context, route *DegraphqlRoute) (*DegraphqlRoute, error)
// Fetches a DeGraphQL route from kong.
Get(ctx context.Context, serviceNameOrID *string, ID *string) (*DegraphqlRoute, error)
// Updates a DeGraphQL route in kong.
Update(ctx context.Context, route *DegraphqlRoute) (*DegraphqlRoute, error)
// Deletes a DeGraphQL route in kong.
Delete(ctx context.Context, serviceNameOrID *string, id *string) error
// Retrieves a page of DeGraphQL routes in kong.
List(ctx context.Context, serviceNameOrID *string, listopt *ListOpt) ([]*DegraphqlRoute, *ListOpt, error)
// Retrieves all DeGraphQL routes in kong.
ListAll(ctx context.Context, serviceNameOrID *string) ([]*DegraphqlRoute, error)
}
type DegraphqlRouteService service
func getServiceNameOrID(service *Service) *string {
if service == nil {
return nil
}
if service.Name != nil {
return service.Name
}
return service.ID
}
func (s *DegraphqlRouteService) fetchService(ctx context.Context, service *Service) (*Service, error) {
if service == nil || service.ID == nil || *service.ID == "" {
return nil, fmt.Errorf("invalid service in fetched DeGraphQL route")
}
serviceService := Svcservice{client: s.client}
return serviceService.Get(ctx, service.ID)
}
// Create creates a DeGraphQL route in Kong.
// Note the Service must be specified.
func (s *DegraphqlRouteService) Create(ctx context.Context, route *DegraphqlRoute) (*DegraphqlRoute, error) {
if route == nil {
return nil, fmt.Errorf("cannot create a nil route")
}
serviceNameOrID := getServiceNameOrID(route.Service)
if serviceNameOrID == nil {
return nil, fmt.Errorf("cannot create a DeGraphQL route for a service without name or ID")
}
endpoint := fmt.Sprintf("/services/%s/degraphql/routes", *serviceNameOrID)
req, err := s.client.NewRequest("POST", endpoint, nil, route)
if err != nil {
return nil, err
}
var createdRoute DegraphqlRoute
err = ErrorOrResponseError(s.client.Do(ctx, req, &createdRoute))
if err != nil {
return nil, err
}
createdRoute.Service, err = s.fetchService(ctx, createdRoute.Service)
if err != nil {
return nil, err
}
return &createdRoute, nil
}
// Get returns an existing DeGraphQL route from Kong, given a Service and Route ID.
func (s *DegraphqlRouteService) Get(
ctx context.Context,
serviceNameOrID *string,
ID *string,
) (*DegraphqlRoute, error) {
if ID == nil {
return nil, fmt.Errorf("ID cannot be nil for a Get operation")
}
if serviceNameOrID == nil {
return nil, fmt.Errorf("serviceID cannot be nil for fetching a DeGraphQL route")
}
endpoint := fmt.Sprintf("/services/%s/degraphql/routes/%s", *serviceNameOrID, *ID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var route DegraphqlRoute
err = ErrorOrResponseError(s.client.Do(ctx, req, &route))
if err != nil {
return nil, err
}
route.Service, err = s.fetchService(ctx, route.Service)
if err != nil {
return nil, err
}
return &route, nil
}
// Update modifies an existing Degraphql route in Kong.
// Note that the Service must be specified.
func (s *DegraphqlRouteService) Update(ctx context.Context, route *DegraphqlRoute) (*DegraphqlRoute, error) {
if route == nil {
return nil, fmt.Errorf("cannot update a nil route")
}
if isEmptyString(route.ID) {
return nil, fmt.Errorf("ID cannot be nil or empty for Update operation")
}
serviceNameOrID := getServiceNameOrID(route.Service)
if serviceNameOrID == nil {
return nil, fmt.Errorf("cannot update a DeGraphQL route without a valid service")
}
endpoint := fmt.Sprintf("/services/%s/degraphql/routes/%s", *serviceNameOrID, *route.ID)
req, err := s.client.NewRequest("PATCH", endpoint, nil, route)
if err != nil {
return nil, err
}
var updatedRoute DegraphqlRoute
err = ErrorOrResponseError(s.client.Do(ctx, req, &updatedRoute))
if err != nil {
return nil, err
}
updatedRoute.Service, err = s.fetchService(ctx, updatedRoute.Service)
if err != nil {
return nil, err
}
return &updatedRoute, nil
}
// Delete removes an existing DeGraphQL route from Kong given a Service and Route ID.
func (s *DegraphqlRouteService) Delete(
ctx context.Context,
serviceNameOrID *string,
ID *string,
) error {
if isEmptyString(ID) {
return fmt.Errorf("id cannot be nil for Delete operation")
}
if isEmptyString(serviceNameOrID) {
return fmt.Errorf("serviceID cannot be nil for fetching a DeGraphQL route")
}
endpoint := fmt.Sprintf("/services/%s/degraphql/routes/%s", *serviceNameOrID, *ID)
req, err := s.client.NewRequest("DELETE", endpoint, nil, nil)
if err != nil {
return err
}
err = ErrorOrResponseError(s.client.Do(ctx, req, nil))
return err
}
// List returns a page of DeGraphQL routes from Kong,
// all associated to the specified Service.
func (s *DegraphqlRouteService) List(
ctx context.Context,
serviceNameOrID *string,
opt *ListOpt,
) ([]*DegraphqlRoute, *ListOpt, error) {
if isEmptyString(serviceNameOrID) {
return nil, nil, fmt.Errorf("serviceID cannot be nil for fetching a DeGraphQL route")
}
endpoint := fmt.Sprintf("/services/%s/degraphql/routes", *serviceNameOrID)
data, next, err := s.client.list(ctx, endpoint, opt)
if err != nil {
return nil, nil, err
}
routes := make([]*DegraphqlRoute, 0, len(data))
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var route DegraphqlRoute
err = json.Unmarshal(b, &route)
if err != nil {
return nil, nil, err
}
routes = append(routes, &route)
}
return routes, next, nil
}
// ListAll fetches all DeGraphQL routes associated with the given Service present in Kong.
// This method can take a while to pull all pages of content
// if there are many items present
func (s *DegraphqlRouteService) ListAll(
ctx context.Context,
serviceNameOrID *string,
) ([]*DegraphqlRoute, error) {
var routes, data []*DegraphqlRoute
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, serviceNameOrID, opt)
if err != nil {
return nil, err
}
routes = append(routes, data...)
}
return routes, nil
}