forked from skippbox/kmachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
247 lines (202 loc) · 6.94 KB
/
requests.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package ssl
import (
"errors"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
var (
errPrivateKey = errors.New("PrivateKey is a required field")
errCertificate = errors.New("Certificate is a required field")
errIntCertificate = errors.New("IntCertificate is a required field")
)
// UpdateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Update operation in this package.
type UpdateOptsBuilder interface {
ToSSLUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts is the common options struct used in this package's Update
// operation.
type UpdateOpts struct {
// Required - consult the SSLTermConfig struct for more info.
SecurePort int
// Required - consult the SSLTermConfig struct for more info.
PrivateKey string
// Required - consult the SSLTermConfig struct for more info.
Certificate string
// Required - consult the SSLTermConfig struct for more info.
IntCertificate string
// Optional - consult the SSLTermConfig struct for more info.
Enabled *bool
// Optional - consult the SSLTermConfig struct for more info.
SecureTrafficOnly *bool
}
// ToSSLUpdateMap casts a CreateOpts struct to a map.
func (opts UpdateOpts) ToSSLUpdateMap() (map[string]interface{}, error) {
ssl := make(map[string]interface{})
if opts.SecurePort == 0 {
return ssl, errors.New("SecurePort needs to be an integer greater than 0")
}
if opts.PrivateKey == "" {
return ssl, errPrivateKey
}
if opts.Certificate == "" {
return ssl, errCertificate
}
if opts.IntCertificate == "" {
return ssl, errIntCertificate
}
ssl["securePort"] = opts.SecurePort
ssl["privateKey"] = opts.PrivateKey
ssl["certificate"] = opts.Certificate
ssl["intermediateCertificate"] = opts.IntCertificate
if opts.Enabled != nil {
ssl["enabled"] = &opts.Enabled
}
if opts.SecureTrafficOnly != nil {
ssl["secureTrafficOnly"] = &opts.SecureTrafficOnly
}
return map[string]interface{}{"sslTermination": ssl}, nil
}
// Update is the operation responsible for updating the SSL Termination
// configuration for a load balancer.
func Update(c *gophercloud.ServiceClient, lbID int, opts UpdateOptsBuilder) UpdateResult {
var res UpdateResult
reqBody, err := opts.ToSSLUpdateMap()
if err != nil {
res.Err = err
return res
}
_, res.Err = c.Put(rootURL(c, lbID), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return res
}
// Get is the operation responsible for showing the details of the SSL
// Termination configuration for a load balancer.
func Get(c *gophercloud.ServiceClient, lbID int) GetResult {
var res GetResult
_, res.Err = c.Get(rootURL(c, lbID), &res.Body, nil)
return res
}
// Delete is the operation responsible for deleting the SSL Termination
// configuration for a load balancer.
func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult {
var res DeleteResult
_, res.Err = c.Delete(rootURL(c, lbID), &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return res
}
// ListCerts will list all of the certificate mappings associated with a
// SSL-terminated HTTP load balancer.
func ListCerts(c *gophercloud.ServiceClient, lbID int) pagination.Pager {
url := certURL(c, lbID)
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
return CertPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// CreateCertOptsBuilder is the interface options structs have to satisfy in
// order to be used in the AddCert operation in this package.
type CreateCertOptsBuilder interface {
ToCertCreateMap() (map[string]interface{}, error)
}
// CreateCertOpts represents the options used when adding a new certificate mapping.
type CreateCertOpts struct {
HostName string
PrivateKey string
Certificate string
IntCertificate string
}
// ToCertCreateMap will cast an CreateCertOpts struct to a map for JSON serialization.
func (opts CreateCertOpts) ToCertCreateMap() (map[string]interface{}, error) {
cm := make(map[string]interface{})
if opts.HostName == "" {
return cm, errors.New("HostName is a required option")
}
if opts.PrivateKey == "" {
return cm, errPrivateKey
}
if opts.Certificate == "" {
return cm, errCertificate
}
cm["hostName"] = opts.HostName
cm["privateKey"] = opts.PrivateKey
cm["certificate"] = opts.Certificate
if opts.IntCertificate != "" {
cm["intermediateCertificate"] = opts.IntCertificate
}
return map[string]interface{}{"certificateMapping": cm}, nil
}
// CreateCert will add a new SSL certificate and allow an SSL-terminated HTTP
// load balancer to use it. This feature is useful because it allows multiple
// certificates to be used. The maximum number of certificates that can be
// stored per LB is 20.
func CreateCert(c *gophercloud.ServiceClient, lbID int, opts CreateCertOptsBuilder) CreateCertResult {
var res CreateCertResult
reqBody, err := opts.ToCertCreateMap()
if err != nil {
res.Err = err
return res
}
_, res.Err = c.Post(certURL(c, lbID), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return res
}
// GetCert will show the details of an existing SSL certificate.
func GetCert(c *gophercloud.ServiceClient, lbID, certID int) GetCertResult {
var res GetCertResult
_, res.Err = c.Get(certResourceURL(c, lbID, certID), &res.Body, nil)
return res
}
// UpdateCertOptsBuilder is the interface options structs have to satisfy in
// order to be used in the UpdateCert operation in this package.
type UpdateCertOptsBuilder interface {
ToCertUpdateMap() (map[string]interface{}, error)
}
// UpdateCertOpts represents the options needed to update a SSL certificate.
type UpdateCertOpts struct {
HostName string
PrivateKey string
Certificate string
IntCertificate string
}
// ToCertUpdateMap will cast an UpdateCertOpts struct into a map for JSON
// seralization.
func (opts UpdateCertOpts) ToCertUpdateMap() (map[string]interface{}, error) {
cm := make(map[string]interface{})
if opts.HostName != "" {
cm["hostName"] = opts.HostName
}
if opts.PrivateKey != "" {
cm["privateKey"] = opts.PrivateKey
}
if opts.Certificate != "" {
cm["certificate"] = opts.Certificate
}
if opts.IntCertificate != "" {
cm["intermediateCertificate"] = opts.IntCertificate
}
return map[string]interface{}{"certificateMapping": cm}, nil
}
// UpdateCert is the operation responsible for updating the details of an
// existing SSL certificate.
func UpdateCert(c *gophercloud.ServiceClient, lbID, certID int, opts UpdateCertOptsBuilder) UpdateCertResult {
var res UpdateCertResult
reqBody, err := opts.ToCertUpdateMap()
if err != nil {
res.Err = err
return res
}
_, res.Err = c.Put(certResourceURL(c, lbID, certID), reqBody, &res.Body, nil)
return res
}
// DeleteCert is the operation responsible for permanently removing a SSL
// certificate.
func DeleteCert(c *gophercloud.ServiceClient, lbID, certID int) DeleteResult {
var res DeleteResult
_, res.Err = c.Delete(certResourceURL(c, lbID, certID), &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return res
}