blob: bb53ef896021e5a0ed4fad061bbeeef854f187e1 [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
Jamie Hannaford5497f942015-03-25 11:55:51 +010088 _, res.Err = c.Put(rootURL(c, lbID), reqBody, &res.Body, &gophercloud.RequestOpts{
89 OkCodes: []int{200},
Jamie Hannaford276a0322014-11-06 14:26:12 +010090 })
Jamie Hannaford276a0322014-11-06 14:26:12 +010091 return res
92}
93
94// Get is the operation responsible for showing the details of the SSL
95// Termination configuration for a load balancer.
96func Get(c *gophercloud.ServiceClient, lbID int) GetResult {
97 var res GetResult
Jamie Hannaford5497f942015-03-25 11:55:51 +010098 _, res.Err = c.Get(rootURL(c, lbID), &res.Body, nil)
Jamie Hannaford276a0322014-11-06 14:26:12 +010099 return res
100}
101
102// Delete is the operation responsible for deleting the SSL Termination
103// configuration for a load balancer.
104func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult {
105 var res DeleteResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100106 _, res.Err = c.Delete(rootURL(c, lbID), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500107 OkCodes: []int{200},
Jamie Hannaford276a0322014-11-06 14:26:12 +0100108 })
Jamie Hannaford276a0322014-11-06 14:26:12 +0100109 return res
110}
Jamie Hannaford249bb622014-11-07 12:11:26 +0100111
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100112// ListCerts will list all of the certificate mappings associated with a
113// SSL-terminated HTTP load balancer.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100114func ListCerts(c *gophercloud.ServiceClient, lbID int) pagination.Pager {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100115 url := certURL(c, lbID)
116 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100117 return CertPage{pagination.LinkedPageBase{PageResult: r}}
Jamie Hannaford249bb622014-11-07 12:11:26 +0100118 })
119}
120
Jamie Hannaford227d9592014-11-13 10:32:07 +0100121// CreateCertOptsBuilder is the interface options structs have to satisfy in
122// order to be used in the AddCert operation in this package.
123type CreateCertOptsBuilder interface {
124 ToCertCreateMap() (map[string]interface{}, error)
Jamie Hannaford249bb622014-11-07 12:11:26 +0100125}
126
Jamie Hannaford227d9592014-11-13 10:32:07 +0100127// CreateCertOpts represents the options used when adding a new certificate mapping.
128type CreateCertOpts struct {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100129 HostName string
130 PrivateKey string
131 Certificate string
132 IntCertificate string
133}
134
Jamie Hannaford227d9592014-11-13 10:32:07 +0100135// ToCertCreateMap will cast an CreateCertOpts struct to a map for JSON serialization.
136func (opts CreateCertOpts) ToCertCreateMap() (map[string]interface{}, error) {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100137 cm := make(map[string]interface{})
138
139 if opts.HostName == "" {
140 return cm, errors.New("HostName is a required option")
141 }
142 if opts.PrivateKey == "" {
143 return cm, errPrivateKey
144 }
145 if opts.Certificate == "" {
146 return cm, errCertificate
147 }
148
149 cm["hostName"] = opts.HostName
150 cm["privateKey"] = opts.PrivateKey
151 cm["certificate"] = opts.Certificate
152
153 if opts.IntCertificate != "" {
154 cm["intermediateCertificate"] = opts.IntCertificate
155 }
156
157 return map[string]interface{}{"certificateMapping": cm}, nil
158}
159
Jamie Hannaford227d9592014-11-13 10:32:07 +0100160// CreateCert will add a new SSL certificate and allow an SSL-terminated HTTP
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100161// load balancer to use it. This feature is useful because it allows multiple
162// certificates to be used. The maximum number of certificates that can be
163// stored per LB is 20.
Jamie Hannaford227d9592014-11-13 10:32:07 +0100164func CreateCert(c *gophercloud.ServiceClient, lbID int, opts CreateCertOptsBuilder) CreateCertResult {
165 var res CreateCertResult
Jamie Hannaford249bb622014-11-07 12:11:26 +0100166
Jamie Hannaford227d9592014-11-13 10:32:07 +0100167 reqBody, err := opts.ToCertCreateMap()
Jamie Hannaford249bb622014-11-07 12:11:26 +0100168 if err != nil {
169 res.Err = err
170 return res
171 }
172
Jamie Hannaford5497f942015-03-25 11:55:51 +0100173 _, res.Err = c.Post(certURL(c, lbID), reqBody, &res.Body, &gophercloud.RequestOpts{
174 OkCodes: []int{200},
Jamie Hannaford249bb622014-11-07 12:11:26 +0100175 })
176
177 return res
178}
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100179
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100180// GetCert will show the details of an existing SSL certificate.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100181func GetCert(c *gophercloud.ServiceClient, lbID, certID int) GetCertResult {
182 var res GetCertResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100183 _, res.Err = c.Get(certResourceURL(c, lbID, certID), &res.Body, nil)
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100184 return res
185}
186
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100187// UpdateCertOptsBuilder is the interface options structs have to satisfy in
188// order to be used in the UpdateCert operation in this package.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100189type UpdateCertOptsBuilder interface {
190 ToCertUpdateMap() (map[string]interface{}, error)
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100191}
192
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100193// UpdateCertOpts represents the options needed to update a SSL certificate.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100194type UpdateCertOpts struct {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100195 HostName string
196 PrivateKey string
197 Certificate string
198 IntCertificate string
199}
200
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100201// ToCertUpdateMap will cast an UpdateCertOpts struct into a map for JSON
202// seralization.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100203func (opts UpdateCertOpts) ToCertUpdateMap() (map[string]interface{}, error) {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100204 cm := make(map[string]interface{})
205
206 if opts.HostName != "" {
207 cm["hostName"] = opts.HostName
208 }
209 if opts.PrivateKey != "" {
210 cm["privateKey"] = opts.PrivateKey
211 }
212 if opts.Certificate != "" {
213 cm["certificate"] = opts.Certificate
214 }
215 if opts.IntCertificate != "" {
216 cm["intermediateCertificate"] = opts.IntCertificate
217 }
218
219 return map[string]interface{}{"certificateMapping": cm}, nil
220}
221
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100222// UpdateCert is the operation responsible for updating the details of an
223// existing SSL certificate.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100224func UpdateCert(c *gophercloud.ServiceClient, lbID, certID int, opts UpdateCertOptsBuilder) UpdateCertResult {
225 var res UpdateCertResult
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100226
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100227 reqBody, err := opts.ToCertUpdateMap()
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100228 if err != nil {
229 res.Err = err
230 return res
231 }
232
Jamie Hannaford5497f942015-03-25 11:55:51 +0100233 _, res.Err = c.Put(certResourceURL(c, lbID, certID), reqBody, &res.Body, nil)
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100234 return res
235}
236
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100237// DeleteCert is the operation responsible for permanently removing a SSL
238// certificate.
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100239func DeleteCert(c *gophercloud.ServiceClient, lbID, certID int) DeleteResult {
240 var res DeleteResult
241
Jamie Hannaford5497f942015-03-25 11:55:51 +0100242 _, res.Err = c.Delete(certResourceURL(c, lbID, certID), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500243 OkCodes: []int{200},
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100244 })
245
246 return res
247}