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