This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
api.go
131 lines (117 loc) · 3.2 KB
/
api.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
package kong
import (
"encoding/json"
"reflect"
"net/url"
"regexp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)
// APIGetter has a method to return an ApiInterface
// A group's client should implement this interface.
type APIGetter interface {
API() APIInterface
}
// APIInterface has methods to work with Kong Endpoints
// ref: https://getkong.org/docs/0.9.x/admin-api/#api-object
type APIInterface interface {
List(params url.Values) (*APIList, error)
ListByRegexp(params url.Values, pattern string) (*APIList, error)
Get(name string) (*API, *APIResponse)
UpdateOrCreate(data *API) (*API, *APIResponse)
Delete(nameOrID string) error
}
type apiKong struct {
client rest.Interface
resource *metav1.APIResource
}
// Get gets the resource with the specified name.
func (a *apiKong) Get(name string) (*API, *APIResponse) {
api := &API{}
resp := a.client.Get().
Resource(a.resource.Name).
Name(name).
Do()
statusCode := reflect.ValueOf(resp).FieldByName("statusCode").Int()
raw, err := resp.Raw()
response := &APIResponse{StatusCode: int(statusCode), err: err}
if err != nil {
response.Raw = raw
return nil, response
}
response.err = json.Unmarshal(raw, api)
return api, response
}
// List returns a list of objects for this resource.
func (a *apiKong) List(params url.Values) (*APIList, error) {
apiList := &APIList{}
request := a.client.Get().Resource(a.resource.Name)
for k, vals := range params {
for _, v := range vals {
request.Param(k, v)
}
}
data, err := request.DoRaw()
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, apiList); err != nil {
return nil, err
}
// recurse to retrieve all apis
if len(apiList.NextPage) > 0 {
params.Add("offset", apiList.Offset)
result, err := a.List(params)
if err != nil {
return nil, err
}
apiList.Items = append(apiList.Items, result.Items...)
}
return apiList, err
}
// ListByRegexp returns a list of object for this resource based on a regexp pattern
// which is evaluated by regexp.MustCompile
func (a *apiKong) ListByRegexp(params url.Values, pattern string) (*APIList, error) {
apiList, err := a.List(params)
if err != nil {
return nil, err
}
apiListResult := &APIList{}
re := regexp.MustCompile(pattern)
for _, api := range apiList.Items {
if re.MatchString(api.Name) {
apiListResult.Items = append(apiListResult.Items, api)
}
}
return apiListResult, nil
}
// Delete deletes the resource with the specified name.
func (a *apiKong) Delete(nameOrID string) error {
return a.client.Delete().
Resource(a.resource.Name).
Name(nameOrID).
Do().
Error()
}
// Update updates the provided resource.
func (a *apiKong) UpdateOrCreate(data *API) (*API, *APIResponse) {
rawData, err := json.Marshal(data)
if err != nil {
return nil, &APIResponse{err: err}
}
resp := a.client.Put().
Resource(a.resource.Name).
Body(rawData).
SetHeader("Content-Type", "application/json").
Do()
statusCode := reflect.ValueOf(resp).FieldByName("statusCode").Int()
raw, err := resp.Raw()
response := &APIResponse{StatusCode: int(statusCode), err: err}
if err != nil {
response.Raw = raw
return nil, response
}
api := &API{}
response.err = json.Unmarshal(raw, api)
return api, response
}