blob: e9c65142862c2bbbb228c676b97de81b6ae74516 [file] [log] [blame]
Jamie Hannaford249bb622014-11-07 12:11:26 +01001package ssl
Jamie Hannaford276a0322014-11-06 14:26:12 +01002
3import (
4 "errors"
5
Jamie Hannaford276a0322014-11-06 14:26:12 +01006 "github.com/rackspace/gophercloud"
Jamie Hannaford249bb622014-11-07 12:11:26 +01007 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford276a0322014-11-06 14:26:12 +01008)
9
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010010var (
11 errPrivateKey = errors.New("PrivateKey is a required field")
12 errCertificate = errors.New("Certificate is a required field")
13 errIntCertificate = errors.New("IntCertificate is a required field")
14)
15
Jamie Hannaford276a0322014-11-06 14:26:12 +010016// UpdateOptsBuilder is the interface options structs have to satisfy in order
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010017// to be used in the main Update operation in this package.
Jamie Hannaford276a0322014-11-06 14:26:12 +010018type UpdateOptsBuilder interface {
19 ToSSLUpdateMap() (map[string]interface{}, error)
20}
21
22// UpdateOpts is the common options struct used in this package's Update
23// operation.
24type UpdateOpts struct {
Jamie Hannaford227d9592014-11-13 10:32:07 +010025 // Required - consult the SSLTermConfig struct for more info.
Jamie Hannaford276a0322014-11-06 14:26:12 +010026 SecurePort int
27
Jamie Hannaford227d9592014-11-13 10:32:07 +010028 // Required - consult the SSLTermConfig struct for more info.
Jamie Hannaford276a0322014-11-06 14:26:12 +010029 PrivateKey string
30
Jamie Hannaford227d9592014-11-13 10:32:07 +010031 // Required - consult the SSLTermConfig struct for more info.
Jamie Hannaford276a0322014-11-06 14:26:12 +010032 Certificate string
33
Jamie Hannaford227d9592014-11-13 10:32:07 +010034 // Required - consult the SSLTermConfig struct for more info.
Jamie Hannaford276a0322014-11-06 14:26:12 +010035 IntCertificate string
36
Jamie Hannaford227d9592014-11-13 10:32:07 +010037 // Optional - consult the SSLTermConfig struct for more info.
Jamie Hannaford276a0322014-11-06 14:26:12 +010038 Enabled *bool
39
Jamie Hannaford227d9592014-11-13 10:32:07 +010040 // Optional - consult the SSLTermConfig struct for more info.
Jamie Hannaford276a0322014-11-06 14:26:12 +010041 SecureTrafficOnly *bool
42}
43
44// ToSSLUpdateMap casts a CreateOpts struct to a map.
45func (opts UpdateOpts) ToSSLUpdateMap() (map[string]interface{}, error) {
46 ssl := make(map[string]interface{})
47
48 if opts.SecurePort == 0 {
49 return ssl, errors.New("SecurePort needs to be an integer greater than 0")
50 }
51 if opts.PrivateKey == "" {
Jamie Hannaford249bb622014-11-07 12:11:26 +010052 return ssl, errPrivateKey
Jamie Hannaford276a0322014-11-06 14:26:12 +010053 }
54 if opts.Certificate == "" {
Jamie Hannaford249bb622014-11-07 12:11:26 +010055 return ssl, errCertificate
Jamie Hannaford276a0322014-11-06 14:26:12 +010056 }
57 if opts.IntCertificate == "" {
Jamie Hannaford249bb622014-11-07 12:11:26 +010058 return ssl, errIntCertificate
Jamie Hannaford276a0322014-11-06 14:26:12 +010059 }
60
61 ssl["securePort"] = opts.SecurePort
62 ssl["privateKey"] = opts.PrivateKey
63 ssl["certificate"] = opts.Certificate
Jamie Hannaford249bb622014-11-07 12:11:26 +010064 ssl["intermediateCertificate"] = opts.IntCertificate
Jamie Hannaford276a0322014-11-06 14:26:12 +010065
66 if opts.Enabled != nil {
67 ssl["enabled"] = &opts.Enabled
68 }
69
70 if opts.SecureTrafficOnly != nil {
71 ssl["secureTrafficOnly"] = &opts.SecureTrafficOnly
72 }
73
74 return map[string]interface{}{"sslTermination": ssl}, nil
75}
76
77// Update is the operation responsible for updating the SSL Termination
78// configuration for a load balancer.
79func Update(c *gophercloud.ServiceClient, lbID int, opts UpdateOptsBuilder) UpdateResult {
80 var res UpdateResult
81
82 reqBody, err := opts.ToSSLUpdateMap()
83 if err != nil {
84 res.Err = err
85 return res
86 }
87
Ash Wilson59fb6c42015-02-12 16:21:13 -050088 _, res.Err = c.Request("PUT", rootURL(c, lbID), gophercloud.RequestOpts{
89 JSONBody: &reqBody,
90 JSONResponse: &res.Body,
91 OkCodes: []int{200},
Jamie Hannaford276a0322014-11-06 14:26:12 +010092 })
93
94 return res
95}
96
97// Get is the operation responsible for showing the details of the SSL
98// Termination configuration for a load balancer.
99func Get(c *gophercloud.ServiceClient, lbID int) GetResult {
100 var res GetResult
101
Ash Wilson59fb6c42015-02-12 16:21:13 -0500102 _, res.Err = c.Request("GET", rootURL(c, lbID), gophercloud.RequestOpts{
103 JSONResponse: &res.Body,
104 OkCodes: []int{200},
Jamie Hannaford276a0322014-11-06 14:26:12 +0100105 })
106
107 return res
108}
109
110// Delete is the operation responsible for deleting the SSL Termination
111// configuration for a load balancer.
112func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult {
113 var res DeleteResult
114
Ash Wilson59fb6c42015-02-12 16:21:13 -0500115 _, res.Err = c.Request("DELETE", rootURL(c, lbID), gophercloud.RequestOpts{
116 OkCodes: []int{200},
Jamie Hannaford276a0322014-11-06 14:26:12 +0100117 })
118
119 return res
120}
Jamie Hannaford249bb622014-11-07 12:11:26 +0100121
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100122// ListCerts will list all of the certificate mappings associated with a
123// SSL-terminated HTTP load balancer.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100124func ListCerts(c *gophercloud.ServiceClient, lbID int) pagination.Pager {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100125 url := certURL(c, lbID)
126 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100127 return CertPage{pagination.LinkedPageBase{PageResult: r}}
Jamie Hannaford249bb622014-11-07 12:11:26 +0100128 })
129}
130
Jamie Hannaford227d9592014-11-13 10:32:07 +0100131// CreateCertOptsBuilder is the interface options structs have to satisfy in
132// order to be used in the AddCert operation in this package.
133type CreateCertOptsBuilder interface {
134 ToCertCreateMap() (map[string]interface{}, error)
Jamie Hannaford249bb622014-11-07 12:11:26 +0100135}
136
Jamie Hannaford227d9592014-11-13 10:32:07 +0100137// CreateCertOpts represents the options used when adding a new certificate mapping.
138type CreateCertOpts struct {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100139 HostName string
140 PrivateKey string
141 Certificate string
142 IntCertificate string
143}
144
Jamie Hannaford227d9592014-11-13 10:32:07 +0100145// ToCertCreateMap will cast an CreateCertOpts struct to a map for JSON serialization.
146func (opts CreateCertOpts) ToCertCreateMap() (map[string]interface{}, error) {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100147 cm := make(map[string]interface{})
148
149 if opts.HostName == "" {
150 return cm, errors.New("HostName is a required option")
151 }
152 if opts.PrivateKey == "" {
153 return cm, errPrivateKey
154 }
155 if opts.Certificate == "" {
156 return cm, errCertificate
157 }
158
159 cm["hostName"] = opts.HostName
160 cm["privateKey"] = opts.PrivateKey
161 cm["certificate"] = opts.Certificate
162
163 if opts.IntCertificate != "" {
164 cm["intermediateCertificate"] = opts.IntCertificate
165 }
166
167 return map[string]interface{}{"certificateMapping": cm}, nil
168}
169
Jamie Hannaford227d9592014-11-13 10:32:07 +0100170// CreateCert will add a new SSL certificate and allow an SSL-terminated HTTP
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100171// load balancer to use it. This feature is useful because it allows multiple
172// certificates to be used. The maximum number of certificates that can be
173// stored per LB is 20.
Jamie Hannaford227d9592014-11-13 10:32:07 +0100174func CreateCert(c *gophercloud.ServiceClient, lbID int, opts CreateCertOptsBuilder) CreateCertResult {
175 var res CreateCertResult
Jamie Hannaford249bb622014-11-07 12:11:26 +0100176
Jamie Hannaford227d9592014-11-13 10:32:07 +0100177 reqBody, err := opts.ToCertCreateMap()
Jamie Hannaford249bb622014-11-07 12:11:26 +0100178 if err != nil {
179 res.Err = err
180 return res
181 }
182
Ash Wilson59fb6c42015-02-12 16:21:13 -0500183 _, res.Err = c.Request("POST", certURL(c, lbID), gophercloud.RequestOpts{
184 JSONBody: &reqBody,
185 JSONResponse: &res.Body,
186 OkCodes: []int{200},
Jamie Hannaford249bb622014-11-07 12:11:26 +0100187 })
188
189 return res
190}
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100191
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100192// GetCert will show the details of an existing SSL certificate.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100193func GetCert(c *gophercloud.ServiceClient, lbID, certID int) GetCertResult {
194 var res GetCertResult
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100195
Ash Wilson59fb6c42015-02-12 16:21:13 -0500196 _, res.Err = c.Request("GET", certResourceURL(c, lbID, certID), gophercloud.RequestOpts{
197 JSONResponse: &res.Body,
198 OkCodes: []int{200},
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100199 })
200
201 return res
202}
203
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100204// UpdateCertOptsBuilder is the interface options structs have to satisfy in
205// order to be used in the UpdateCert operation in this package.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100206type UpdateCertOptsBuilder interface {
207 ToCertUpdateMap() (map[string]interface{}, error)
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100208}
209
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100210// UpdateCertOpts represents the options needed to update a SSL certificate.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100211type UpdateCertOpts struct {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100212 HostName string
213 PrivateKey string
214 Certificate string
215 IntCertificate string
216}
217
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100218// ToCertUpdateMap will cast an UpdateCertOpts struct into a map for JSON
219// seralization.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100220func (opts UpdateCertOpts) ToCertUpdateMap() (map[string]interface{}, error) {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100221 cm := make(map[string]interface{})
222
223 if opts.HostName != "" {
224 cm["hostName"] = opts.HostName
225 }
226 if opts.PrivateKey != "" {
227 cm["privateKey"] = opts.PrivateKey
228 }
229 if opts.Certificate != "" {
230 cm["certificate"] = opts.Certificate
231 }
232 if opts.IntCertificate != "" {
233 cm["intermediateCertificate"] = opts.IntCertificate
234 }
235
236 return map[string]interface{}{"certificateMapping": cm}, nil
237}
238
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100239// UpdateCert is the operation responsible for updating the details of an
240// existing SSL certificate.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100241func UpdateCert(c *gophercloud.ServiceClient, lbID, certID int, opts UpdateCertOptsBuilder) UpdateCertResult {
242 var res UpdateCertResult
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100243
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100244 reqBody, err := opts.ToCertUpdateMap()
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100245 if err != nil {
246 res.Err = err
247 return res
248 }
249
Ash Wilson59fb6c42015-02-12 16:21:13 -0500250 _, res.Err = c.Request("PUT", certResourceURL(c, lbID, certID), gophercloud.RequestOpts{
251 JSONBody: &reqBody,
252 JSONResponse: &res.Body,
253 OkCodes: []int{202},
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100254 })
255
256 return res
257}
258
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100259// DeleteCert is the operation responsible for permanently removing a SSL
260// certificate.
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100261func DeleteCert(c *gophercloud.ServiceClient, lbID, certID int) DeleteResult {
262 var res DeleteResult
263
Ash Wilson59fb6c42015-02-12 16:21:13 -0500264 _, res.Err = c.Request("DELETE", certResourceURL(c, lbID, certID), gophercloud.RequestOpts{
265 OkCodes: []int{200},
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100266 })
267
268 return res
269}