-
Notifications
You must be signed in to change notification settings - Fork 51
/
certificates.go
135 lines (102 loc) · 4.05 KB
/
certificates.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
package gokong
import (
"encoding/json"
"fmt"
)
type CertificateClient struct {
config *Config
}
type CertificateRequest struct {
Cert *string `json:"cert,omitempty" yaml:"cert,omitempty"`
Key *string `json:"key,omitempty" yaml:"key,omitempty"`
SNIs *[]string `json:"snis" yaml:"snis"`
}
type Certificate struct {
Id *string `json:"id,omitempty" yaml:"id,omitempty"`
Cert *string `json:"cert,omitempty" yaml:"cert,omitempty"`
Key *string `json:"key,omitempty" yaml:"key,omitempty"`
}
type Certificates struct {
Results []*Certificate `json:"data,omitempty" yaml:"data,omitempty"`
Total int `json:"total,omitempty" yaml:"total,omitempty"`
}
const CertificatesPath = "/certificates/"
func (certificateClient *CertificateClient) GetById(id string) (*Certificate, error) {
r, body, errs := newGet(certificateClient.config, certificateClient.config.HostAddress+CertificatesPath+id).End()
if errs != nil {
return nil, fmt.Errorf("could not get certificate, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
certificate := &Certificate{}
err := json.Unmarshal([]byte(body), certificate)
if err != nil {
return nil, fmt.Errorf("could not parse certificate get response, error: %v", err)
}
if certificate.Id == nil {
return nil, nil
}
return certificate, nil
}
func (certificateClient *CertificateClient) Create(certificateRequest *CertificateRequest) (*Certificate, error) {
r, body, errs := newPost(certificateClient.config, certificateClient.config.HostAddress+CertificatesPath).Send(certificateRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not create new certificate, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
createdCertificate := &Certificate{}
err := json.Unmarshal([]byte(body), createdCertificate)
if err != nil {
return nil, fmt.Errorf("could not parse certificate creation response, error: %v", err)
}
if createdCertificate.Id == nil {
return nil, fmt.Errorf("could not create certificate, error: %v", body)
}
return createdCertificate, nil
}
func (certificateClient *CertificateClient) DeleteById(id string) error {
r, body, errs := newDelete(certificateClient.config, certificateClient.config.HostAddress+CertificatesPath+id).End()
if errs != nil {
return fmt.Errorf("could not delete certificate, result: %v error: %v", r, errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return fmt.Errorf("not authorised, message from kong: %s", body)
}
return nil
}
func (certificateClient *CertificateClient) List() (*Certificates, error) {
r, body, errs := newGet(certificateClient.config, certificateClient.config.HostAddress+CertificatesPath).End()
if errs != nil {
return nil, fmt.Errorf("could not get certificates, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
certificates := &Certificates{}
err := json.Unmarshal([]byte(body), certificates)
if err != nil {
return nil, fmt.Errorf("could not parse certificates list response, error: %v", err)
}
return certificates, nil
}
func (certificateClient *CertificateClient) UpdateById(id string, certificateRequest *CertificateRequest) (*Certificate, error) {
r, body, errs := newPatch(certificateClient.config, certificateClient.config.HostAddress+CertificatesPath+id).Send(certificateRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not update certificate, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
updatedCertificate := &Certificate{}
err := json.Unmarshal([]byte(body), updatedCertificate)
if err != nil {
return nil, fmt.Errorf("could not parse certificate update response, error: %v", err)
}
if updatedCertificate.Id == nil {
return nil, fmt.Errorf("could not update certificate, error: %v", body)
}
return updatedCertificate, nil
}