blob: ead9fcd37eb5e08929b0a6eb896e2e5847063dff [file] [log] [blame]
Jamie Hannaford249bb622014-11-07 12:11:26 +01001package ssl
Jamie Hannaford276a0322014-11-06 14:26:12 +01002
3import (
4 "github.com/mitchellh/mapstructure"
Jamie Hannaford249bb622014-11-07 12:11:26 +01005
Jamie Hannaford276a0322014-11-06 14:26:12 +01006 "github.com/rackspace/gophercloud"
Jamie Hannaford249bb622014-11-07 12:11:26 +01007 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford276a0322014-11-06 14:26:12 +01008)
9
10// SSLTermConfig represents the SSL configuration for a particular load balancer.
11type 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.
43type DeleteResult struct {
44 gophercloud.ErrResult
45}
46
47// UpdateResult represents the result of an update operation.
48type UpdateResult struct {
49 gophercloud.ErrResult
50}
51
52// GetResult represents the result of a get operation.
53type GetResult struct {
54 gophercloud.Result
55}
56
57// Extract interprets a GetResult as a SSLTermConfig struct, if possible.
58func (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 Hannaford249bb622014-11-07 12:11:26 +010071
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010072// Certificate represents an SSL certificate associated with an SSL-terminated
73// HTTP load balancer.
Jamie Hannafordb65793f2014-11-07 13:45:06 +010074type Certificate struct {
Jamie Hannaford249bb622014-11-07 12:11:26 +010075 ID int
76 HostName string
77 Certificate string
78 IntCertificate string `mapstructure:"intermediateCertificate"`
79}
80
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010081// CertPage represents a page of certificates.
Jamie Hannafordb65793f2014-11-07 13:45:06 +010082type CertPage struct {
Jamie Hannaford249bb622014-11-07 12:11:26 +010083 pagination.LinkedPageBase
84}
85
86// IsEmpty checks whether a CertMappingPage struct is empty.
Jamie Hannafordb65793f2014-11-07 13:45:06 +010087func (p CertPage) IsEmpty() (bool, error) {
88 is, err := ExtractCerts(p)
Jamie Hannaford249bb622014-11-07 12:11:26 +010089 if err != nil {
90 return true, nil
91 }
92 return len(is) == 0, nil
93}
94
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010095// 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 Hannaford249bb622014-11-07 12:11:26 +010097// collection is mapped into a relevant slice.
Jamie Hannafordb65793f2014-11-07 13:45:06 +010098func ExtractCerts(page pagination.Page) ([]Certificate, error) {
Jamie Hannaford249bb622014-11-07 12:11:26 +010099 type NestedMap struct {
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100100 Cert Certificate `mapstructure:"certificateMapping" json:"certificateMapping"`
Jamie Hannaford249bb622014-11-07 12:11:26 +0100101 }
102 var resp struct {
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100103 Certs []NestedMap `mapstructure:"certificateMappings" json:"certificateMappings"`
Jamie Hannaford249bb622014-11-07 12:11:26 +0100104 }
105
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100106 err := mapstructure.Decode(page.(CertPage).Body, &resp)
Jamie Hannaford249bb622014-11-07 12:11:26 +0100107
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100108 slice := []Certificate{}
109 for _, cert := range resp.Certs {
110 slice = append(slice, cert.Cert)
Jamie Hannaford249bb622014-11-07 12:11:26 +0100111 }
112
113 return slice, err
114}
115
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100116type certResult struct {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100117 gophercloud.Result
118}
119
120// Extract interprets a result as a CertMapping struct, if possible.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100121func (r certResult) Extract() (*Certificate, error) {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100122 if r.Err != nil {
123 return nil, r.Err
124 }
125
126 var response struct {
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100127 Cert Certificate `mapstructure:"certificateMapping"`
Jamie Hannaford249bb622014-11-07 12:11:26 +0100128 }
129
130 err := mapstructure.Decode(r.Body, &response)
131
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100132 return &response.Cert, err
Jamie Hannaford249bb622014-11-07 12:11:26 +0100133}
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100134
Jamie Hannaford227d9592014-11-13 10:32:07 +0100135// CreateCertResult represents the result of an CreateCert operation.
136type CreateCertResult struct {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100137 certResult
138}
139
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100140// GetCertResult represents the result of a GetCert operation.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100141type GetCertResult struct {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100142 certResult
143}
144
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100145// UpdateCertResult represents the result of an UpdateCert operation.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100146type UpdateCertResult struct {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100147 certResult
148}