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 | |
| 6 | "github.com/racker/perigee" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 10 | ) |
| 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. |
| 14 | type 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. |
| 20 | type 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 Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 40 | var ( |
| 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 Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 46 | // ToSSLUpdateMap casts a CreateOpts struct to a map. |
| 47 | func (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 Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 54 | return ssl, errPrivateKey |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 55 | } |
| 56 | if opts.Certificate == "" { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 57 | return ssl, errCertificate |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 58 | } |
| 59 | if opts.IntCertificate == "" { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 60 | return ssl, errIntCertificate |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | ssl["securePort"] = opts.SecurePort |
| 64 | ssl["privateKey"] = opts.PrivateKey |
| 65 | ssl["certificate"] = opts.Certificate |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 66 | ssl["intermediateCertificate"] = opts.IntCertificate |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 67 | |
| 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. |
| 81 | func 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. |
| 102 | func 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. |
| 116 | func 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 Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 126 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 127 | func ListCerts(c *gophercloud.ServiceClient, lbID int) pagination.Pager { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 128 | url := certURL(c, lbID) |
| 129 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 130 | return CertPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 131 | }) |
| 132 | } |
| 133 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 134 | type AddCertOptsBuilder interface { |
| 135 | ToCertAddMap() (map[string]interface{}, error) |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 138 | type AddCertOpts 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 | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 145 | func (opts AddCertOpts) ToCertAddMap() (map[string]interface{}, error) { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 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 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 169 | func AddCert(c *gophercloud.ServiceClient, lbID int, opts AddCertOptsBuilder) AddCertResult { |
| 170 | var res AddCertResult |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 171 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 172 | reqBody, err := opts.ToCertAddMap() |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 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 Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 187 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 188 | func GetCert(c *gophercloud.ServiceClient, lbID, certID int) GetCertResult { |
| 189 | var res GetCertResult |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 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 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 200 | type UpdateCertOptsBuilder interface { |
| 201 | ToCertUpdateMap() (map[string]interface{}, error) |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 204 | type UpdateCertOpts struct { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 205 | HostName string |
| 206 | PrivateKey string |
| 207 | Certificate string |
| 208 | IntCertificate string |
| 209 | } |
| 210 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 211 | func (opts UpdateCertOpts) ToCertUpdateMap() (map[string]interface{}, error) { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 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 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 230 | func UpdateCert(c *gophercloud.ServiceClient, lbID, certID int, opts UpdateCertOptsBuilder) UpdateCertResult { |
| 231 | var res UpdateCertResult |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 232 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 233 | reqBody, err := opts.ToCertUpdateMap() |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 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 | |
| 249 | func 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 | } |