github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/builtin/providers/openstack/config.go (about) 1 package openstack 2 3 import ( 4 "crypto/tls" 5 "crypto/x509" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 10 "github.com/rackspace/gophercloud" 11 "github.com/rackspace/gophercloud/openstack" 12 ) 13 14 type Config struct { 15 Username string 16 UserID string 17 Password string 18 Token string 19 APIKey string 20 IdentityEndpoint string 21 TenantID string 22 TenantName string 23 DomainID string 24 DomainName string 25 Insecure bool 26 EndpointType string 27 CACertFile string 28 ClientCertFile string 29 ClientKeyFile string 30 31 osClient *gophercloud.ProviderClient 32 } 33 34 func (c *Config) loadAndValidate() error { 35 36 if c.EndpointType != "internal" && c.EndpointType != "internalURL" && 37 c.EndpointType != "admin" && c.EndpointType != "adminURL" && 38 c.EndpointType != "public" && c.EndpointType != "publicURL" && 39 c.EndpointType != "" { 40 return fmt.Errorf("Invalid endpoint type provided") 41 } 42 43 ao := gophercloud.AuthOptions{ 44 Username: c.Username, 45 UserID: c.UserID, 46 Password: c.Password, 47 TokenID: c.Token, 48 APIKey: c.APIKey, 49 IdentityEndpoint: c.IdentityEndpoint, 50 TenantID: c.TenantID, 51 TenantName: c.TenantName, 52 DomainID: c.DomainID, 53 DomainName: c.DomainName, 54 } 55 56 client, err := openstack.NewClient(ao.IdentityEndpoint) 57 if err != nil { 58 return err 59 } 60 61 config := &tls.Config{} 62 if c.CACertFile != "" { 63 64 caCert, err := ioutil.ReadFile(c.CACertFile) 65 if err != nil { 66 return err 67 } 68 69 caCertPool := x509.NewCertPool() 70 caCertPool.AppendCertsFromPEM(caCert) 71 config.RootCAs = caCertPool 72 } 73 if c.Insecure { 74 config.InsecureSkipVerify = true 75 } 76 77 if c.ClientCertFile != "" && c.ClientKeyFile != "" { 78 cert, err := tls.LoadX509KeyPair(c.ClientCertFile, c.ClientKeyFile) 79 if err != nil { 80 return err 81 } 82 83 config.Certificates = []tls.Certificate{cert} 84 config.BuildNameToCertificate() 85 } 86 transport := &http.Transport{TLSClientConfig: config} 87 client.HTTPClient.Transport = transport 88 89 err = openstack.Authenticate(client, ao) 90 if err != nil { 91 return err 92 } 93 94 c.osClient = client 95 96 return nil 97 } 98 99 func (c *Config) blockStorageV1Client(region string) (*gophercloud.ServiceClient, error) { 100 return openstack.NewBlockStorageV1(c.osClient, gophercloud.EndpointOpts{ 101 Region: region, 102 Availability: c.getEndpointType(), 103 }) 104 } 105 106 func (c *Config) blockStorageV2Client(region string) (*gophercloud.ServiceClient, error) { 107 return openstack.NewBlockStorageV2(c.osClient, gophercloud.EndpointOpts{ 108 Region: region, 109 Availability: c.getEndpointType(), 110 }) 111 } 112 113 func (c *Config) computeV2Client(region string) (*gophercloud.ServiceClient, error) { 114 return openstack.NewComputeV2(c.osClient, gophercloud.EndpointOpts{ 115 Region: region, 116 Availability: c.getEndpointType(), 117 }) 118 } 119 120 func (c *Config) networkingV2Client(region string) (*gophercloud.ServiceClient, error) { 121 return openstack.NewNetworkV2(c.osClient, gophercloud.EndpointOpts{ 122 Region: region, 123 Availability: c.getEndpointType(), 124 }) 125 } 126 127 func (c *Config) objectStorageV1Client(region string) (*gophercloud.ServiceClient, error) { 128 return openstack.NewObjectStorageV1(c.osClient, gophercloud.EndpointOpts{ 129 Region: region, 130 Availability: c.getEndpointType(), 131 }) 132 } 133 134 func (c *Config) getEndpointType() gophercloud.Availability { 135 if c.EndpointType == "internal" || c.EndpointType == "internalURL" { 136 return gophercloud.AvailabilityInternal 137 } 138 if c.EndpointType == "admin" || c.EndpointType == "adminURL" { 139 return gophercloud.AvailabilityAdmin 140 } 141 return gophercloud.AvailabilityPublic 142 }