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 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 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 | |
| 10 | // SSLTermConfig represents the SSL configuration for a particular load balancer. |
| 11 | type SSLTermConfig struct { |
| 12 | // The port on which the SSL termination load balancer listens for secure |
| 13 | // traffic. The value must be unique to the existing LB protocol/port |
| 14 | // combination |
| 15 | SecurePort int `mapstructure:"securePort"` |
| 16 | |
| 17 | // The private key for the SSL certificate which is validated and verified |
| 18 | // against the provided certificates. |
| 19 | PrivateKey string `mapstructure:"privatekey"` |
| 20 | |
| 21 | // The certificate used for SSL termination, which is validated and verified |
| 22 | // against the key and intermediate certificate if provided. |
| 23 | Certificate string |
| 24 | |
| 25 | // The intermediate certificate (for the user). The intermediate certificate |
| 26 | // is validated and verified against the key and certificate credentials |
| 27 | // provided. A user may only provide this value when accompanied by a |
| 28 | // Certificate, PrivateKey, and SecurePort. It may not be added or updated as |
| 29 | // a single attribute in a future operation. |
| 30 | IntCertificate string `mapstructure:"intermediatecertificate"` |
| 31 | |
| 32 | // Determines if the load balancer is enabled to terminate SSL traffic or not. |
| 33 | // If this is set to false, the load balancer retains its specified SSL |
| 34 | // attributes but does not terminate SSL traffic. |
| 35 | Enabled bool |
| 36 | |
| 37 | // Determines if the load balancer can only accept secure traffic. If set to |
| 38 | // true, the load balancer will not accept non-secure traffic. |
| 39 | SecureTrafficOnly bool |
| 40 | } |
| 41 | |
| 42 | // DeleteResult represents the result of a delete operation. |
| 43 | type DeleteResult struct { |
| 44 | gophercloud.ErrResult |
| 45 | } |
| 46 | |
| 47 | // UpdateResult represents the result of an update operation. |
| 48 | type UpdateResult struct { |
| 49 | gophercloud.ErrResult |
| 50 | } |
| 51 | |
| 52 | // GetResult represents the result of a get operation. |
| 53 | type GetResult struct { |
| 54 | gophercloud.Result |
| 55 | } |
| 56 | |
| 57 | // Extract interprets a GetResult as a SSLTermConfig struct, if possible. |
| 58 | func (r GetResult) Extract() (*SSLTermConfig, error) { |
| 59 | if r.Err != nil { |
| 60 | return nil, r.Err |
| 61 | } |
| 62 | |
| 63 | var response struct { |
| 64 | SSL SSLTermConfig `mapstructure:"sslTermination"` |
| 65 | } |
| 66 | |
| 67 | err := mapstructure.Decode(r.Body, &response) |
| 68 | |
| 69 | return &response.SSL, err |
| 70 | } |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 71 | |
| 72 | type CertificateMapping struct { |
| 73 | ID int |
| 74 | HostName string |
| 75 | Certificate string |
| 76 | IntCertificate string `mapstructure:"intermediateCertificate"` |
| 77 | } |
| 78 | |
| 79 | type CertMappingPage struct { |
| 80 | pagination.LinkedPageBase |
| 81 | } |
| 82 | |
| 83 | // IsEmpty checks whether a CertMappingPage struct is empty. |
| 84 | func (p CertMappingPage) IsEmpty() (bool, error) { |
| 85 | is, err := ExtractCertMappings(p) |
| 86 | if err != nil { |
| 87 | return true, nil |
| 88 | } |
| 89 | return len(is) == 0, nil |
| 90 | } |
| 91 | |
| 92 | // ExtractCertMappings accepts a Page struct, specifically a CertMappingPage struct, and extracts |
| 93 | // the elements into a slice of CertMapping structs. In other words, a generic |
| 94 | // collection is mapped into a relevant slice. |
| 95 | func ExtractCertMappings(page pagination.Page) ([]CertificateMapping, error) { |
| 96 | type NestedMap struct { |
| 97 | CertMap CertificateMapping `mapstructure:"certificateMapping" json:"certificateMapping"` |
| 98 | } |
| 99 | var resp struct { |
| 100 | CertMappings []NestedMap `mapstructure:"certificateMappings" json:"certificateMappings"` |
| 101 | } |
| 102 | |
| 103 | err := mapstructure.Decode(page.(CertMappingPage).Body, &resp) |
| 104 | |
| 105 | slice := []CertificateMapping{} |
| 106 | |
| 107 | for _, cert := range resp.CertMappings { |
| 108 | slice = append(slice, cert.CertMap) |
| 109 | } |
| 110 | |
| 111 | return slice, err |
| 112 | } |
| 113 | |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame^] | 114 | type certResult struct { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 115 | gophercloud.Result |
| 116 | } |
| 117 | |
| 118 | // Extract interprets a result as a CertMapping struct, if possible. |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame^] | 119 | func (r certResult) Extract() (*CertificateMapping, error) { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 120 | if r.Err != nil { |
| 121 | return nil, r.Err |
| 122 | } |
| 123 | |
| 124 | var response struct { |
| 125 | CertMapping CertificateMapping `mapstructure:"certificateMapping"` |
| 126 | } |
| 127 | |
| 128 | err := mapstructure.Decode(r.Body, &response) |
| 129 | |
| 130 | return &response.CertMapping, err |
| 131 | } |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame^] | 132 | |
| 133 | type CreateCertMappingResult struct { |
| 134 | certResult |
| 135 | } |
| 136 | |
| 137 | type GetCertMappingResult struct { |
| 138 | certResult |
| 139 | } |
| 140 | |
| 141 | type UpdateCertMappingResult struct { |
| 142 | certResult |
| 143 | } |