blob: d3f860b19d778bbc2644f882658f94ec2056c7bc [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 Hannafordb65793f2014-11-07 13:45:06 +010072type Certificate struct {
Jamie Hannaford249bb622014-11-07 12:11:26 +010073 ID int
74 HostName string
75 Certificate string
76 IntCertificate string `mapstructure:"intermediateCertificate"`
77}
78
Jamie Hannafordb65793f2014-11-07 13:45:06 +010079type CertPage struct {
Jamie Hannaford249bb622014-11-07 12:11:26 +010080 pagination.LinkedPageBase
81}
82
83// IsEmpty checks whether a CertMappingPage struct is empty.
Jamie Hannafordb65793f2014-11-07 13:45:06 +010084func (p CertPage) IsEmpty() (bool, error) {
85 is, err := ExtractCerts(p)
Jamie Hannaford249bb622014-11-07 12:11:26 +010086 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.
Jamie Hannafordb65793f2014-11-07 13:45:06 +010095func ExtractCerts(page pagination.Page) ([]Certificate, error) {
Jamie Hannaford249bb622014-11-07 12:11:26 +010096 type NestedMap struct {
Jamie Hannafordb65793f2014-11-07 13:45:06 +010097 Cert Certificate `mapstructure:"certificateMapping" json:"certificateMapping"`
Jamie Hannaford249bb622014-11-07 12:11:26 +010098 }
99 var resp struct {
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100100 Certs []NestedMap `mapstructure:"certificateMappings" json:"certificateMappings"`
Jamie Hannaford249bb622014-11-07 12:11:26 +0100101 }
102
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100103 err := mapstructure.Decode(page.(CertPage).Body, &resp)
Jamie Hannaford249bb622014-11-07 12:11:26 +0100104
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100105 slice := []Certificate{}
106 for _, cert := range resp.Certs {
107 slice = append(slice, cert.Cert)
Jamie Hannaford249bb622014-11-07 12:11:26 +0100108 }
109
110 return slice, err
111}
112
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100113type certResult struct {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100114 gophercloud.Result
115}
116
117// Extract interprets a result as a CertMapping struct, if possible.
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100118func (r certResult) Extract() (*Certificate, error) {
Jamie Hannaford249bb622014-11-07 12:11:26 +0100119 if r.Err != nil {
120 return nil, r.Err
121 }
122
123 var response struct {
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100124 Cert Certificate `mapstructure:"certificateMapping"`
Jamie Hannaford249bb622014-11-07 12:11:26 +0100125 }
126
127 err := mapstructure.Decode(r.Body, &response)
128
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100129 return &response.Cert, err
Jamie Hannaford249bb622014-11-07 12:11:26 +0100130}
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100131
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100132type AddCertResult struct {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100133 certResult
134}
135
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100136type GetCertResult struct {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100137 certResult
138}
139
Jamie Hannafordb65793f2014-11-07 13:45:06 +0100140type UpdateCertResult struct {
Jamie Hannafordcba541e2014-11-07 13:36:54 +0100141 certResult
142}