forked from kevholditch/gokong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnis.go
155 lines (117 loc) · 4.3 KB
/
snis.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
package gokong
import (
"encoding/json"
"fmt"
)
type SnisClient struct {
config *Config
}
type SnisRequest struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
CertificateId *Id `json:"certificate,omitempty" yaml:"certificate,omitempty"`
Tags []*string `json:"tags,omitempty" yaml:"tags,omitempty"`
}
type Sni struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
CertificateId *Id `json:"certificate,omitempty" yaml:"certificate,omitempty"`
Tags []*string `json:"tags,omitempty" yaml:"tags,omitempty"`
}
type Snis struct {
Results []*Sni `json:"data,omitempty" yaml:"data,omitempty"`
Total int `json:"total,omitempty" yaml:"total,omitempty"`
}
const SnisPath = "/snis/"
func (snisClient *SnisClient) Create(snisRequest *SnisRequest) (*Sni, error) {
r, body, errs := newPost(snisClient.config, snisClient.config.HostAddress+SnisPath).Send(snisRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not create new sni, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
if r.StatusCode >= 400 {
return nil, fmt.Errorf("unexpected response from kong. Status code %d, message: %s", r.StatusCode, body)
}
sni := &Sni{}
err := json.Unmarshal([]byte(body), sni)
if err != nil {
return nil, fmt.Errorf("could not parse sni creation response, error: %v", err)
}
if sni.CertificateId == nil {
return nil, fmt.Errorf("could not create sni, error: %v", body)
}
return sni, nil
}
func (snisClient *SnisClient) GetByName(name string) (*Sni, error) {
r, body, errs := newGet(snisClient.config, snisClient.config.HostAddress+SnisPath+name).End()
if errs != nil {
return nil, fmt.Errorf("could not get sni, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
if r.StatusCode >= 400 {
return nil, fmt.Errorf("unexpected response from kong. Status code %d, message: %s", r.StatusCode, body)
}
sni := &Sni{}
err := json.Unmarshal([]byte(body), sni)
if err != nil {
return nil, fmt.Errorf("could not parse sni get response, error: %v", err)
}
if sni.Name == "" {
return nil, nil
}
return sni, nil
}
func (snisClient *SnisClient) List() (*Snis, error) {
r, body, errs := newGet(snisClient.config, snisClient.config.HostAddress+SnisPath).End()
if errs != nil {
return nil, fmt.Errorf("could not get snis, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
if r.StatusCode >= 400 {
return nil, fmt.Errorf("unexpected response from kong. Status code %d, message: %s", r.StatusCode, body)
}
snis := &Snis{}
err := json.Unmarshal([]byte(body), snis)
if err != nil {
return nil, fmt.Errorf("could not parse snis list response, error: %v", err)
}
return snis, nil
}
func (snisClient *SnisClient) DeleteByName(name string) error {
r, body, errs := newDelete(snisClient.config, snisClient.config.HostAddress+SnisPath+name).End()
if errs != nil {
return fmt.Errorf("could not delete sni, result: %v error: %v", r, errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return fmt.Errorf("not authorised, message from kong: %s", body)
}
if r.StatusCode >= 400 {
return fmt.Errorf("unexpected response from kong. Status code %d, message: %s", r.StatusCode, body)
}
return nil
}
func (snisClient *SnisClient) UpdateByName(name string, snisRequest *SnisRequest) (*Sni, error) {
r, body, errs := newPatch(snisClient.config, snisClient.config.HostAddress+SnisPath+name).Send(snisRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not update sni, error: %v", errs)
}
if r.StatusCode == 401 || r.StatusCode == 403 {
return nil, fmt.Errorf("not authorised, message from kong: %s", body)
}
if r.StatusCode >= 400 {
return nil, fmt.Errorf("unexpected response from kong. Status code %d, message: %s", r.StatusCode, body)
}
updatedSni := &Sni{}
err := json.Unmarshal([]byte(body), updatedSni)
if err != nil {
return nil, fmt.Errorf("could not parse sni update response, error: %v", err)
}
if updatedSni.CertificateId == nil {
return nil, fmt.Errorf("could not update sni, error: %v", body)
}
return updatedSni, nil
}