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 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 88 | _, res.Err = c.Put(rootURL(c, lbID), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 89 | OkCodes: []int{200}, |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 90 | }) |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 91 | return res |
| 92 | } |
| 93 | |
| 94 | // Get is the operation responsible for showing the details of the SSL |
| 95 | // Termination configuration for a load balancer. |
| 96 | func Get(c *gophercloud.ServiceClient, lbID int) GetResult { |
| 97 | var res GetResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 98 | _, res.Err = c.Get(rootURL(c, lbID), &res.Body, nil) |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 99 | return res |
| 100 | } |
| 101 | |
| 102 | // Delete is the operation responsible for deleting the SSL Termination |
| 103 | // configuration for a load balancer. |
| 104 | func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult { |
| 105 | var res DeleteResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 106 | _, res.Err = c.Delete(rootURL(c, lbID), &gophercloud.RequestOpts{ |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 107 | OkCodes: []int{200}, |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 108 | }) |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 109 | return res |
| 110 | } |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 111 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 112 | // ListCerts will list all of the certificate mappings associated with a |
| 113 | // SSL-terminated HTTP load balancer. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 114 | func ListCerts(c *gophercloud.ServiceClient, lbID int) pagination.Pager { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 115 | url := certURL(c, lbID) |
| 116 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 117 | return CertPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 118 | }) |
| 119 | } |
| 120 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 121 | // CreateCertOptsBuilder is the interface options structs have to satisfy in |
| 122 | // order to be used in the AddCert operation in this package. |
| 123 | type CreateCertOptsBuilder interface { |
| 124 | ToCertCreateMap() (map[string]interface{}, error) |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 127 | // CreateCertOpts represents the options used when adding a new certificate mapping. |
| 128 | type CreateCertOpts struct { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 129 | HostName string |
| 130 | PrivateKey string |
| 131 | Certificate string |
| 132 | IntCertificate string |
| 133 | } |
| 134 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 135 | // ToCertCreateMap will cast an CreateCertOpts struct to a map for JSON serialization. |
| 136 | func (opts CreateCertOpts) ToCertCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 137 | 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 Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 160 | // 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] | 161 | // 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 Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 164 | func CreateCert(c *gophercloud.ServiceClient, lbID int, opts CreateCertOptsBuilder) CreateCertResult { |
| 165 | var res CreateCertResult |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 166 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 167 | reqBody, err := opts.ToCertCreateMap() |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 168 | if err != nil { |
| 169 | res.Err = err |
| 170 | return res |
| 171 | } |
| 172 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 173 | _, res.Err = c.Post(certURL(c, lbID), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 174 | OkCodes: []int{200}, |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 175 | }) |
| 176 | |
| 177 | return res |
| 178 | } |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 179 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 180 | // GetCert will show the details of an existing SSL certificate. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 181 | func GetCert(c *gophercloud.ServiceClient, lbID, certID int) GetCertResult { |
| 182 | var res GetCertResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 183 | _, res.Err = c.Get(certResourceURL(c, lbID, certID), &res.Body, nil) |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 184 | return res |
| 185 | } |
| 186 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 187 | // UpdateCertOptsBuilder is the interface options structs have to satisfy in |
| 188 | // order to be used in the UpdateCert operation in this package. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 189 | type UpdateCertOptsBuilder interface { |
| 190 | ToCertUpdateMap() (map[string]interface{}, error) |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 193 | // UpdateCertOpts represents the options needed to update a SSL certificate. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 194 | type UpdateCertOpts struct { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 195 | HostName string |
| 196 | PrivateKey string |
| 197 | Certificate string |
| 198 | IntCertificate string |
| 199 | } |
| 200 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 201 | // ToCertUpdateMap will cast an UpdateCertOpts struct into a map for JSON |
| 202 | // seralization. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 203 | func (opts UpdateCertOpts) ToCertUpdateMap() (map[string]interface{}, error) { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 204 | 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 222 | // UpdateCert is the operation responsible for updating the details of an |
| 223 | // existing SSL certificate. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 224 | func UpdateCert(c *gophercloud.ServiceClient, lbID, certID int, opts UpdateCertOptsBuilder) UpdateCertResult { |
| 225 | var res UpdateCertResult |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 226 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 227 | reqBody, err := opts.ToCertUpdateMap() |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 228 | if err != nil { |
| 229 | res.Err = err |
| 230 | return res |
| 231 | } |
| 232 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 233 | _, res.Err = c.Put(certResourceURL(c, lbID, certID), reqBody, &res.Body, nil) |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 234 | return res |
| 235 | } |
| 236 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 237 | // DeleteCert is the operation responsible for permanently removing a SSL |
| 238 | // certificate. |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 239 | func DeleteCert(c *gophercloud.ServiceClient, lbID, certID int) DeleteResult { |
| 240 | var res DeleteResult |
| 241 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 242 | _, res.Err = c.Delete(certResourceURL(c, lbID, certID), &gophercloud.RequestOpts{ |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 243 | OkCodes: []int{200}, |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 244 | }) |
| 245 | |
| 246 | return res |
| 247 | } |