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 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 72 | // Certificate represents an SSL certificate associated with an SSL-terminated |
| 73 | // HTTP load balancer. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 74 | type Certificate struct { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 75 | ID int |
| 76 | HostName string |
| 77 | Certificate string |
| 78 | IntCertificate string `mapstructure:"intermediateCertificate"` |
| 79 | } |
| 80 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 81 | // CertPage represents a page of certificates. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 82 | type CertPage struct { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 83 | pagination.LinkedPageBase |
| 84 | } |
| 85 | |
| 86 | // IsEmpty checks whether a CertMappingPage struct is empty. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 87 | func (p CertPage) IsEmpty() (bool, error) { |
| 88 | is, err := ExtractCerts(p) |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 89 | if err != nil { |
| 90 | return true, nil |
| 91 | } |
| 92 | return len(is) == 0, nil |
| 93 | } |
| 94 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 95 | // ExtractCerts accepts a Page struct, specifically a CertPage struct, and |
| 96 | // extracts the elements into a slice of Cert structs. In other words, a generic |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 97 | // collection is mapped into a relevant slice. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 98 | func ExtractCerts(page pagination.Page) ([]Certificate, error) { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 99 | type NestedMap struct { |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 100 | Cert Certificate `mapstructure:"certificateMapping" json:"certificateMapping"` |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 101 | } |
| 102 | var resp struct { |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 103 | Certs []NestedMap `mapstructure:"certificateMappings" json:"certificateMappings"` |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 106 | err := mapstructure.Decode(page.(CertPage).Body, &resp) |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 107 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 108 | slice := []Certificate{} |
| 109 | for _, cert := range resp.Certs { |
| 110 | slice = append(slice, cert.Cert) |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return slice, err |
| 114 | } |
| 115 | |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 116 | type certResult struct { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 117 | gophercloud.Result |
| 118 | } |
| 119 | |
| 120 | // Extract interprets a result as a CertMapping struct, if possible. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 121 | func (r certResult) Extract() (*Certificate, error) { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 122 | if r.Err != nil { |
| 123 | return nil, r.Err |
| 124 | } |
| 125 | |
| 126 | var response struct { |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 127 | Cert Certificate `mapstructure:"certificateMapping"` |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | err := mapstructure.Decode(r.Body, &response) |
| 131 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 132 | return &response.Cert, err |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 133 | } |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 134 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 135 | // CreateCertResult represents the result of an CreateCert operation. |
| 136 | type CreateCertResult struct { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 137 | certResult |
| 138 | } |
| 139 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 140 | // GetCertResult represents the result of a GetCert operation. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 141 | type GetCertResult struct { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 142 | certResult |
| 143 | } |
| 144 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 145 | // UpdateCertResult represents the result of an UpdateCert operation. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 146 | type UpdateCertResult struct { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 147 | certResult |
| 148 | } |