-
Notifications
You must be signed in to change notification settings - Fork 41
/
license_service.go
167 lines (146 loc) · 3.94 KB
/
license_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
package kong
import (
"context"
"encoding/json"
"fmt"
)
// AbstractLicenseService handles Licenses in Kong.
type AbstractLicenseService interface {
// Create creates a License in Kong.
Create(ctx context.Context, license *License) (*License, error)
// Get fetches a License in Kong.
Get(ctx context.Context, ID *string) (*License, error)
// Update updates a License in Kong
Update(ctx context.Context, license *License) (*License, error)
// Delete deletes a License in Kong
Delete(ctx context.Context, ID *string) error
// List fetches a list of Licenses in Kong.
List(ctx context.Context, opt *ListOpt) ([]*License, *ListOpt, error)
// ListAll fetches all Licenses in Kong.
ListAll(ctx context.Context) ([]*License, error)
}
// LicenseService handles Licenses in Kong.
type LicenseService service
// Create creates a License in Kong.
// If an ID is specified, it will be used to
// create a license in Kong, otherwise an ID
// is auto-generated.
func (s *LicenseService) Create(ctx context.Context,
license *License,
) (*License, error) {
if license == nil {
return nil, fmt.Errorf("cannot create a nil license")
}
queryPath := "/licenses"
method := "POST"
if license.ID != nil {
queryPath = queryPath + "/" + *license.ID
method = "PUT"
}
req, err := s.client.NewRequest(method, queryPath, nil, license)
if err != nil {
return nil, err
}
var createdLicense License
_, err = s.client.Do(ctx, req, &createdLicense)
if err != nil {
return nil, err
}
return &createdLicense, nil
}
// Get fetches a License in Kong.
func (s *LicenseService) Get(ctx context.Context,
ID *string,
) (*License, error) {
if isEmptyString(ID) {
return nil, fmt.Errorf("ID cannot be nil for Get operation")
}
endpoint := fmt.Sprintf("/licenses/%v", *ID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var license License
_, err = s.client.Do(ctx, req, &license)
if err != nil {
return nil, err
}
return &license, nil
}
// Update updates a License in Kong
func (s *LicenseService) Update(ctx context.Context,
license *License,
) (*License, error) {
if license == nil {
return nil, fmt.Errorf("cannot update a nil license")
}
if isEmptyString(license.ID) {
return nil, fmt.Errorf("ID cannot be nil for Update operation")
}
endpoint := fmt.Sprintf("/licenses/%v", *license.ID)
req, err := s.client.NewRequest("PATCH", endpoint, nil, license)
if err != nil {
return nil, err
}
var updatedAPI License
_, err = s.client.Do(ctx, req, &updatedAPI)
if err != nil {
return nil, err
}
return &updatedAPI, nil
}
// Delete deletes a License in Kong
func (s *LicenseService) Delete(ctx context.Context,
ID *string,
) error {
if isEmptyString(ID) {
return fmt.Errorf("ID cannot be nil for Delete operation")
}
endpoint := fmt.Sprintf("/licenses/%v", *ID)
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 Licenses in Kong.
// opt can be used to control pagination.
func (s *LicenseService) List(ctx context.Context,
opt *ListOpt,
) ([]*License, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/licenses", opt)
if err != nil {
return nil, nil, err
}
var licenses []*License
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var license License
err = json.Unmarshal(b, &license)
if err != nil {
return nil, nil, err
}
licenses = append(licenses, &license)
}
return licenses, next, nil
}
// ListAll fetches all Licenses in Kong.
// This method can take a while if there
// a lot of Licenses present.
func (s *LicenseService) ListAll(ctx context.Context) ([]*License, error) {
var licenses, data []*License
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, opt)
if err != nil {
return nil, err
}
licenses = append(licenses, data...)
}
return licenses, nil
}