-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsni_service.go
168 lines (144 loc) · 3.62 KB
/
sni_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
package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
)
// SNIService handles SNIs in Kong.
type SNIService service
// Create creates a SNI in Kong.
// If an ID is specified, it will be used to
// create a sni in Kong, otherwise an ID
// is auto-generated.
func (s *SNIService) Create(ctx context.Context, sni *SNI) (*SNI, error) {
queryPath := "/snis"
method := "POST"
if sni.ID != nil {
queryPath = queryPath + "/" + *sni.ID
method = "PUT"
}
req, err := s.client.NewRequest(method, queryPath, nil, sni)
if err != nil {
return nil, err
}
var createdSNI SNI
_, err = s.client.Do(ctx, req, &createdSNI)
if err != nil {
return nil, err
}
return &createdSNI, nil
}
// Get fetches a SNI in Kong.
func (s *SNIService) Get(ctx context.Context,
usernameOrID *string) (*SNI, error) {
if isEmptyString(usernameOrID) {
return nil, errors.New(
"usernameOrID cannot be nil for Get operation")
}
endpoint := fmt.Sprintf("/snis/%v", *usernameOrID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var sni SNI
_, err = s.client.Do(ctx, req, &sni)
if err != nil {
return nil, err
}
return &sni, nil
}
// Update updates a SNI in Kong
func (s *SNIService) Update(ctx context.Context, sni *SNI) (*SNI, error) {
if isEmptyString(sni.ID) {
return nil, errors.New("ID cannot be nil for Update operation")
}
endpoint := fmt.Sprintf("/snis/%v", *sni.ID)
req, err := s.client.NewRequest("PATCH", endpoint, nil, sni)
if err != nil {
return nil, err
}
var updatedAPI SNI
_, err = s.client.Do(ctx, req, &updatedAPI)
if err != nil {
return nil, err
}
return &updatedAPI, nil
}
// Delete deletes a SNI in Kong
func (s *SNIService) Delete(ctx context.Context, usernameOrID *string) error {
if isEmptyString(usernameOrID) {
return errors.New("usernameOrID cannot be nil for Delete operation")
}
endpoint := fmt.Sprintf("/snis/%v", *usernameOrID)
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 SNIs in Kong.
// opt can be used to control pagination.
func (s *SNIService) List(ctx context.Context,
opt *ListOpt) ([]*SNI, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/snis", opt)
if err != nil {
return nil, nil, err
}
var snis []*SNI
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var sni SNI
err = json.Unmarshal(b, &sni)
if err != nil {
return nil, nil, err
}
snis = append(snis, &sni)
}
return snis, next, nil
}
// ListForCertificate fetches a list of SNIs
// in Kong associated with certificateID.
// opt can be used to control pagination.
func (s *SNIService) ListForCertificate(ctx context.Context,
certificateID *string, opt *ListOpt) ([]*SNI, *ListOpt, error) {
data, next, err := s.client.list(ctx,
"/certificates/"+*certificateID+"/snis", opt)
if err != nil {
return nil, nil, err
}
var snis []*SNI
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var sni SNI
err = json.Unmarshal(b, &sni)
if err != nil {
return nil, nil, err
}
snis = append(snis, &sni)
}
return snis, next, nil
}
// ListAll fetches all SNIs in Kong.
// This method can take a while if there
// a lot of SNIs present.
func (s *SNIService) ListAll(ctx context.Context) ([]*SNI, error) {
var snis, data []*SNI
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, opt)
if err != nil {
return nil, err
}
snis = append(snis, data...)
}
return snis, nil
}