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 | |
| 127 | func 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 | |
| 134 | type CertMappingCreateOptsBuilder interface { |
| 135 | ToCertMappingCreateMap() (map[string]interface{}, error) |
| 136 | } |
| 137 | |
| 138 | type CertMappingCreateOpts struct { |
| 139 | HostName string |
| 140 | PrivateKey string |
| 141 | Certificate string |
| 142 | IntCertificate string |
| 143 | } |
| 144 | |
| 145 | func (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 | |
| 169 | func 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 | } |