blob: bae9ca61b3ff1db8d218e9b49a49db2b64ed1149 [file] [log] [blame]
Jamie Hannaford249bb622014-11-07 12:11:26 +01001package ssl
Jamie Hannaford276a0322014-11-06 14:26:12 +01002
3import (
4 "errors"
5
6 "github.com/racker/perigee"
7
8 "github.com/rackspace/gophercloud"
Jamie Hannaford249bb622014-11-07 12:11:26 +01009 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford276a0322014-11-06 14:26:12 +010010)
11
12// UpdateOptsBuilder is the interface options structs have to satisfy in order
13// to be used in the main Create operation in this package.
14type UpdateOptsBuilder interface {
15 ToSSLUpdateMap() (map[string]interface{}, error)
16}
17
18// UpdateOpts is the common options struct used in this package's Update
19// operation.
20type UpdateOpts struct {
21 // Required
22 SecurePort int
23
24 // Required
25 PrivateKey string
26
27 // Required
28 Certificate string
29
30 // Required
31 IntCertificate string
32
33 // Optional
34 Enabled *bool
35
36 // Optional
37 SecureTrafficOnly *bool
38}
39
Jamie Hannaford249bb622014-11-07 12:11:26 +010040var (
41 errPrivateKey = errors.New("PrivateKey is a required field")
42 errCertificate = errors.New("Certificate is a required field")
43 errIntCertificate = errors.New("IntCertificate is a required field")
44)
45
Jamie Hannaford276a0322014-11-06 14:26:12 +010046// ToSSLUpdateMap casts a CreateOpts struct to a map.
47func (opts UpdateOpts) ToSSLUpdateMap() (map[string]interface{}, error) {
48 ssl := make(map[string]interface{})
49
50 if opts.SecurePort == 0 {
51 return ssl, errors.New("SecurePort needs to be an integer greater than 0")
52 }
53 if opts.PrivateKey == "" {
Jamie Hannaford249bb622014-11-07 12:11:26 +010054 return ssl, errPrivateKey
Jamie Hannaford276a0322014-11-06 14:26:12 +010055 }
56 if opts.Certificate == "" {
Jamie Hannaford249bb622014-11-07 12:11:26 +010057 return ssl, errCertificate
Jamie Hannaford276a0322014-11-06 14:26:12 +010058 }
59 if opts.IntCertificate == "" {
Jamie Hannaford249bb622014-11-07 12:11:26 +010060 return ssl, errIntCertificate
Jamie Hannaford276a0322014-11-06 14:26:12 +010061 }
62
63 ssl["securePort"] = opts.SecurePort
64 ssl["privateKey"] = opts.PrivateKey
65 ssl["certificate"] = opts.Certificate
Jamie Hannaford249bb622014-11-07 12:11:26 +010066 ssl["intermediateCertificate"] = opts.IntCertificate
Jamie Hannaford276a0322014-11-06 14:26:12 +010067
68 if opts.Enabled != nil {
69 ssl["enabled"] = &opts.Enabled
70 }
71
72 if opts.SecureTrafficOnly != nil {
73 ssl["secureTrafficOnly"] = &opts.SecureTrafficOnly
74 }
75
76 return map[string]interface{}{"sslTermination": ssl}, nil
77}
78
79// Update is the operation responsible for updating the SSL Termination
80// configuration for a load balancer.
81func Update(c *gophercloud.ServiceClient, lbID int, opts UpdateOptsBuilder) UpdateResult {
82 var res UpdateResult
83
84 reqBody, err := opts.ToSSLUpdateMap()
85 if err != nil {
86 res.Err = err
87 return res
88 }
89
90 _, res.Err = perigee.Request("PUT", rootURL(c, lbID), perigee.Options{
91 MoreHeaders: c.AuthenticatedHeaders(),
92 ReqBody: &reqBody,
93 Results: &res.Body,
94 OkCodes: []int{200},
95 })
96
97 return res
98}
99
100// Get is the operation responsible for showing the details of the SSL
101// Termination configuration for a load balancer.
102func Get(c *gophercloud.ServiceClient, lbID int) GetResult {
103 var res GetResult
104
105 _, res.Err = perigee.Request("GET", rootURL(c, lbID), perigee.Options{
106 MoreHeaders: c.AuthenticatedHeaders(),
107 Results: &res.Body,
108 OkCodes: []int{200},
109 })
110
111 return res
112}
113
114// Delete is the operation responsible for deleting the SSL Termination
115// configuration for a load balancer.
116func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult {
117 var res DeleteResult
118
119 _, res.Err = perigee.Request("DELETE", rootURL(c, lbID), perigee.Options{
120 MoreHeaders: c.AuthenticatedHeaders(),
121 OkCodes: []int{200},
122 })
123
124 return res
125}
Jamie Hannaford249bb622014-11-07 12:11:26 +0100126
127func ListCertMappings(c *gophercloud.ServiceClient, lbID int) pagination.Pager {
128 url := certURL(c, lbID)
129 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
130 return CertMappingPage{pagination.LinkedPageBase{PageResult: r}}
131 })
132}
133
134type CertMappingCreateOptsBuilder interface {
135 ToCertMappingCreateMap() (map[string]interface{}, error)
136}
137
138type CertMappingCreateOpts struct {
139 HostName string
140 PrivateKey string
141 Certificate string
142 IntCertificate string
143}
144
145func (opts CertMappingCreateOpts) ToCertMappingCreateMap() (map[string]interface{}, error) {
146 cm := make(map[string]interface{})
147
148 if opts.HostName == "" {
149 return cm, errors.New("HostName is a required option")
150 }
151 if opts.PrivateKey == "" {
152 return cm, errPrivateKey
153 }
154 if opts.Certificate == "" {
155 return cm, errCertificate
156 }
157
158 cm["hostName"] = opts.HostName
159 cm["privateKey"] = opts.PrivateKey
160 cm["certificate"] = opts.Certificate
161
162 if opts.IntCertificate != "" {
163 cm["intermediateCertificate"] = opts.IntCertificate
164 }
165
166 return map[string]interface{}{"certificateMapping": cm}, nil
167}
168
169func AddCertMapping(c *gophercloud.ServiceClient, lbID int, opts CertMappingCreateOptsBuilder) CreateCertMappingResult {
170 var res CreateCertMappingResult
171
172 reqBody, err := opts.ToCertMappingCreateMap()
173 if err != nil {
174 res.Err = err
175 return res
176 }
177
178 _, res.Err = perigee.Request("POST", certURL(c, lbID), perigee.Options{
179 MoreHeaders: c.AuthenticatedHeaders(),
180 ReqBody: &reqBody,
181 Results: &res.Body,
182 OkCodes: []int{200},
183 })
184
185 return res
186}
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100187
188func GetCertMapping(c *gophercloud.ServiceClient, lbID, certID int) GetCertMappingResult {
189 var res GetCertMappingResult
190
191 _, res.Err = perigee.Request("GET", certResourceURL(c, lbID, certID), perigee.Options{
192 MoreHeaders: c.AuthenticatedHeaders(),
193 Results: &res.Body,
194 OkCodes: []int{200},
195 })
196
197 return res
198}
199
200type CertMappingUpdateOptsBuilder interface {
201 ToCertMappingUpdateMap() (map[string]interface{}, error)
202}
203
204type CertMappingUpdateOpts struct {
205 HostName string
206 PrivateKey string
207 Certificate string
208 IntCertificate string
209}
210
211func (opts CertMappingUpdateOpts) ToCertMappingUpdateMap() (map[string]interface{}, error) {
212 cm := make(map[string]interface{})
213
214 if opts.HostName != "" {
215 cm["hostName"] = opts.HostName
216 }
217 if opts.PrivateKey != "" {
218 cm["privateKey"] = opts.PrivateKey
219 }
220 if opts.Certificate != "" {
221 cm["certificate"] = opts.Certificate
222 }
223 if opts.IntCertificate != "" {
224 cm["intermediateCertificate"] = opts.IntCertificate
225 }
226
227 return map[string]interface{}{"certificateMapping": cm}, nil
228}
229
230func UpdateCertMapping(c *gophercloud.ServiceClient, lbID, certID int, opts CertMappingUpdateOptsBuilder) UpdateCertMappingResult {
231 var res UpdateCertMappingResult
232
233 reqBody, err := opts.ToCertMappingUpdateMap()
234 if err != nil {
235 res.Err = err
236 return res
237 }
238
239 _, res.Err = perigee.Request("PUT", certResourceURL(c, lbID, certID), perigee.Options{
240 MoreHeaders: c.AuthenticatedHeaders(),
241 ReqBody: &reqBody,
242 Results: &res.Body,
243 OkCodes: []int{202},
244 })
245
246 return res
247}
248
249func DeleteCert(c *gophercloud.ServiceClient, lbID, certID int) DeleteResult {
250 var res DeleteResult
251
252 _, res.Err = perigee.Request("DELETE", certResourceURL(c, lbID, certID), perigee.Options{
253 MoreHeaders: c.AuthenticatedHeaders(),
254 OkCodes: []int{200},
255 })
256
257 return res
258}