Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame^] | 1 | package throttle |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "github.com/racker/perigee" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | ) |
| 10 | |
| 11 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 12 | // to be used in the main Create operation in this package. |
| 13 | type UpdateOptsBuilder interface { |
| 14 | ToSSLUpdateMap() (map[string]interface{}, error) |
| 15 | } |
| 16 | |
| 17 | // UpdateOpts is the common options struct used in this package's Update |
| 18 | // operation. |
| 19 | type UpdateOpts struct { |
| 20 | // Required |
| 21 | SecurePort int |
| 22 | |
| 23 | // Required |
| 24 | PrivateKey string |
| 25 | |
| 26 | // Required |
| 27 | Certificate string |
| 28 | |
| 29 | // Required |
| 30 | IntCertificate string |
| 31 | |
| 32 | // Optional |
| 33 | Enabled *bool |
| 34 | |
| 35 | // Optional |
| 36 | SecureTrafficOnly *bool |
| 37 | } |
| 38 | |
| 39 | // ToSSLUpdateMap casts a CreateOpts struct to a map. |
| 40 | func (opts UpdateOpts) ToSSLUpdateMap() (map[string]interface{}, error) { |
| 41 | ssl := make(map[string]interface{}) |
| 42 | |
| 43 | if opts.SecurePort == 0 { |
| 44 | return ssl, errors.New("SecurePort needs to be an integer greater than 0") |
| 45 | } |
| 46 | if opts.PrivateKey == "" { |
| 47 | return ssl, errors.New("PrivateKey is a required field") |
| 48 | } |
| 49 | if opts.Certificate == "" { |
| 50 | return ssl, errors.New("Certificate is a required field") |
| 51 | } |
| 52 | if opts.IntCertificate == "" { |
| 53 | return ssl, errors.New("IntCertificate is a required field") |
| 54 | } |
| 55 | |
| 56 | ssl["securePort"] = opts.SecurePort |
| 57 | ssl["privateKey"] = opts.PrivateKey |
| 58 | ssl["certificate"] = opts.Certificate |
| 59 | ssl["intermediatecertificate"] = opts.IntCertificate |
| 60 | |
| 61 | if opts.Enabled != nil { |
| 62 | ssl["enabled"] = &opts.Enabled |
| 63 | } |
| 64 | |
| 65 | if opts.SecureTrafficOnly != nil { |
| 66 | ssl["secureTrafficOnly"] = &opts.SecureTrafficOnly |
| 67 | } |
| 68 | |
| 69 | return map[string]interface{}{"sslTermination": ssl}, nil |
| 70 | } |
| 71 | |
| 72 | // Update is the operation responsible for updating the SSL Termination |
| 73 | // configuration for a load balancer. |
| 74 | func Update(c *gophercloud.ServiceClient, lbID int, opts UpdateOptsBuilder) UpdateResult { |
| 75 | var res UpdateResult |
| 76 | |
| 77 | reqBody, err := opts.ToSSLUpdateMap() |
| 78 | if err != nil { |
| 79 | res.Err = err |
| 80 | return res |
| 81 | } |
| 82 | |
| 83 | _, res.Err = perigee.Request("PUT", rootURL(c, lbID), perigee.Options{ |
| 84 | MoreHeaders: c.AuthenticatedHeaders(), |
| 85 | ReqBody: &reqBody, |
| 86 | Results: &res.Body, |
| 87 | OkCodes: []int{200}, |
| 88 | }) |
| 89 | |
| 90 | return res |
| 91 | } |
| 92 | |
| 93 | // Get is the operation responsible for showing the details of the SSL |
| 94 | // Termination configuration for a load balancer. |
| 95 | func Get(c *gophercloud.ServiceClient, lbID int) GetResult { |
| 96 | var res GetResult |
| 97 | |
| 98 | _, res.Err = perigee.Request("GET", rootURL(c, lbID), perigee.Options{ |
| 99 | MoreHeaders: c.AuthenticatedHeaders(), |
| 100 | Results: &res.Body, |
| 101 | OkCodes: []int{200}, |
| 102 | }) |
| 103 | |
| 104 | return res |
| 105 | } |
| 106 | |
| 107 | // Delete is the operation responsible for deleting the SSL Termination |
| 108 | // configuration for a load balancer. |
| 109 | func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult { |
| 110 | var res DeleteResult |
| 111 | |
| 112 | _, res.Err = perigee.Request("DELETE", rootURL(c, lbID), perigee.Options{ |
| 113 | MoreHeaders: c.AuthenticatedHeaders(), |
| 114 | OkCodes: []int{200}, |
| 115 | }) |
| 116 | |
| 117 | return res |
| 118 | } |