Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 1 | package ssl |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 8 | ) |
| 9 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 10 | var ( |
| 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 Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 16 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 17 | // to be used in the main Update operation in this package. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 18 | type 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. |
| 24 | type UpdateOpts struct { |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 25 | // Required - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 26 | SecurePort int |
| 27 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 28 | // Required - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 29 | PrivateKey string |
| 30 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 31 | // Required - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 32 | Certificate string |
| 33 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 34 | // Required - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 35 | IntCertificate string |
| 36 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 37 | // Optional - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 38 | Enabled *bool |
| 39 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 40 | // Optional - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 41 | SecureTrafficOnly *bool |
| 42 | } |
| 43 | |
| 44 | // ToSSLUpdateMap casts a CreateOpts struct to a map. |
| 45 | func (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 Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 52 | return ssl, errPrivateKey |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 53 | } |
| 54 | if opts.Certificate == "" { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 55 | return ssl, errCertificate |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 56 | } |
| 57 | if opts.IntCertificate == "" { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 58 | return ssl, errIntCertificate |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ssl["securePort"] = opts.SecurePort |
| 62 | ssl["privateKey"] = opts.PrivateKey |
| 63 | ssl["certificate"] = opts.Certificate |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 64 | ssl["intermediateCertificate"] = opts.IntCertificate |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 65 | |
| 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. |
| 79 | func 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 Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 88 | _, res.Err = c.Request("PUT", rootURL(c, lbID), gophercloud.RequestOpts{ |
| 89 | JSONBody: &reqBody, |
| 90 | JSONResponse: &res.Body, |
| 91 | OkCodes: []int{200}, |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 92 | }) |
| 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. |
| 99 | func Get(c *gophercloud.ServiceClient, lbID int) GetResult { |
| 100 | var res GetResult |
| 101 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 102 | _, res.Err = c.Request("GET", rootURL(c, lbID), gophercloud.RequestOpts{ |
| 103 | JSONResponse: &res.Body, |
| 104 | OkCodes: []int{200}, |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 105 | }) |
| 106 | |
| 107 | return res |
| 108 | } |
| 109 | |
| 110 | // Delete is the operation responsible for deleting the SSL Termination |
| 111 | // configuration for a load balancer. |
| 112 | func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult { |
| 113 | var res DeleteResult |
| 114 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 115 | _, res.Err = c.Request("DELETE", rootURL(c, lbID), gophercloud.RequestOpts{ |
| 116 | OkCodes: []int{200}, |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 117 | }) |
| 118 | |
| 119 | return res |
| 120 | } |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 121 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 122 | // ListCerts will list all of the certificate mappings associated with a |
| 123 | // SSL-terminated HTTP load balancer. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 124 | func ListCerts(c *gophercloud.ServiceClient, lbID int) pagination.Pager { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 125 | url := certURL(c, lbID) |
| 126 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 127 | return CertPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 128 | }) |
| 129 | } |
| 130 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 131 | // CreateCertOptsBuilder is the interface options structs have to satisfy in |
| 132 | // order to be used in the AddCert operation in this package. |
| 133 | type CreateCertOptsBuilder interface { |
| 134 | ToCertCreateMap() (map[string]interface{}, error) |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 137 | // CreateCertOpts represents the options used when adding a new certificate mapping. |
| 138 | type CreateCertOpts struct { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 139 | HostName string |
| 140 | PrivateKey string |
| 141 | Certificate string |
| 142 | IntCertificate string |
| 143 | } |
| 144 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 145 | // ToCertCreateMap will cast an CreateCertOpts struct to a map for JSON serialization. |
| 146 | func (opts CreateCertOpts) ToCertCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 147 | 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 Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 170 | // CreateCert will add a new SSL certificate and allow an SSL-terminated HTTP |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 171 | // 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 Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 174 | func CreateCert(c *gophercloud.ServiceClient, lbID int, opts CreateCertOptsBuilder) CreateCertResult { |
| 175 | var res CreateCertResult |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 176 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 177 | reqBody, err := opts.ToCertCreateMap() |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 178 | if err != nil { |
| 179 | res.Err = err |
| 180 | return res |
| 181 | } |
| 182 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 183 | _, res.Err = c.Request("POST", certURL(c, lbID), gophercloud.RequestOpts{ |
| 184 | JSONBody: &reqBody, |
| 185 | JSONResponse: &res.Body, |
| 186 | OkCodes: []int{200}, |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 187 | }) |
| 188 | |
| 189 | return res |
| 190 | } |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 191 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 192 | // GetCert will show the details of an existing SSL certificate. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 193 | func GetCert(c *gophercloud.ServiceClient, lbID, certID int) GetCertResult { |
| 194 | var res GetCertResult |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 195 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 196 | _, res.Err = c.Request("GET", certResourceURL(c, lbID, certID), gophercloud.RequestOpts{ |
| 197 | JSONResponse: &res.Body, |
| 198 | OkCodes: []int{200}, |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 199 | }) |
| 200 | |
| 201 | return res |
| 202 | } |
| 203 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 204 | // UpdateCertOptsBuilder is the interface options structs have to satisfy in |
| 205 | // order to be used in the UpdateCert operation in this package. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 206 | type UpdateCertOptsBuilder interface { |
| 207 | ToCertUpdateMap() (map[string]interface{}, error) |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 210 | // UpdateCertOpts represents the options needed to update a SSL certificate. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 211 | type UpdateCertOpts struct { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 212 | HostName string |
| 213 | PrivateKey string |
| 214 | Certificate string |
| 215 | IntCertificate string |
| 216 | } |
| 217 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 218 | // ToCertUpdateMap will cast an UpdateCertOpts struct into a map for JSON |
| 219 | // seralization. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 220 | func (opts UpdateCertOpts) ToCertUpdateMap() (map[string]interface{}, error) { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 221 | 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 239 | // UpdateCert is the operation responsible for updating the details of an |
| 240 | // existing SSL certificate. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 241 | func UpdateCert(c *gophercloud.ServiceClient, lbID, certID int, opts UpdateCertOptsBuilder) UpdateCertResult { |
| 242 | var res UpdateCertResult |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 243 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 244 | reqBody, err := opts.ToCertUpdateMap() |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 245 | if err != nil { |
| 246 | res.Err = err |
| 247 | return res |
| 248 | } |
| 249 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 250 | _, res.Err = c.Request("PUT", certResourceURL(c, lbID, certID), gophercloud.RequestOpts{ |
| 251 | JSONBody: &reqBody, |
| 252 | JSONResponse: &res.Body, |
| 253 | OkCodes: []int{202}, |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 254 | }) |
| 255 | |
| 256 | return res |
| 257 | } |
| 258 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 259 | // DeleteCert is the operation responsible for permanently removing a SSL |
| 260 | // certificate. |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 261 | func DeleteCert(c *gophercloud.ServiceClient, lbID, certID int) DeleteResult { |
| 262 | var res DeleteResult |
| 263 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 264 | _, res.Err = c.Request("DELETE", certResourceURL(c, lbID, certID), gophercloud.RequestOpts{ |
| 265 | OkCodes: []int{200}, |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 266 | }) |
| 267 | |
| 268 | return res |
| 269 | } |