| 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 | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 72 | type Certificate struct { | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 73 | 	ID             int | 
 | 74 | 	HostName       string | 
 | 75 | 	Certificate    string | 
 | 76 | 	IntCertificate string `mapstructure:"intermediateCertificate"` | 
 | 77 | } | 
 | 78 |  | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 79 | type CertPage struct { | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 80 | 	pagination.LinkedPageBase | 
 | 81 | } | 
 | 82 |  | 
 | 83 | // IsEmpty checks whether a CertMappingPage struct is empty. | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 84 | func (p CertPage) IsEmpty() (bool, error) { | 
 | 85 | 	is, err := ExtractCerts(p) | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 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. | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 95 | func ExtractCerts(page pagination.Page) ([]Certificate, error) { | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 96 | 	type NestedMap struct { | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 97 | 		Cert Certificate `mapstructure:"certificateMapping" json:"certificateMapping"` | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 98 | 	} | 
 | 99 | 	var resp struct { | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 100 | 		Certs []NestedMap `mapstructure:"certificateMappings" json:"certificateMappings"` | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 101 | 	} | 
 | 102 |  | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 103 | 	err := mapstructure.Decode(page.(CertPage).Body, &resp) | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 104 |  | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 105 | 	slice := []Certificate{} | 
 | 106 | 	for _, cert := range resp.Certs { | 
 | 107 | 		slice = append(slice, cert.Cert) | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 108 | 	} | 
 | 109 |  | 
 | 110 | 	return slice, err | 
 | 111 | } | 
 | 112 |  | 
| Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 113 | type certResult struct { | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 114 | 	gophercloud.Result | 
 | 115 | } | 
 | 116 |  | 
 | 117 | // Extract interprets a result as a CertMapping struct, if possible. | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 118 | func (r certResult) Extract() (*Certificate, error) { | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 119 | 	if r.Err != nil { | 
 | 120 | 		return nil, r.Err | 
 | 121 | 	} | 
 | 122 |  | 
 | 123 | 	var response struct { | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 124 | 		Cert Certificate `mapstructure:"certificateMapping"` | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 125 | 	} | 
 | 126 |  | 
 | 127 | 	err := mapstructure.Decode(r.Body, &response) | 
 | 128 |  | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 129 | 	return &response.Cert, err | 
| Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 130 | } | 
| Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 131 |  | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 132 | type AddCertResult struct { | 
| Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 133 | 	certResult | 
 | 134 | } | 
 | 135 |  | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 136 | type GetCertResult struct { | 
| Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 137 | 	certResult | 
 | 138 | } | 
 | 139 |  | 
| Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 140 | type UpdateCertResult struct { | 
| Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 141 | 	certResult | 
 | 142 | } |