github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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 APIKey string 19 IdentityEndpoint string 20 TenantID string 21 TenantName string 22 DomainID string 23 DomainName string 24 Insecure bool 25 EndpointType string 26 CACertFile string 27 28 osClient *gophercloud.ProviderClient 29 } 30 31 func (c *Config) loadAndValidate() error { 32 33 if c.EndpointType != "internal" && c.EndpointType != "internalURL" && 34 c.EndpointType != "admin" && c.EndpointType != "adminURL" && 35 c.EndpointType != "public" && c.EndpointType != "publicURL" && 36 c.EndpointType != "" { 37 return fmt.Errorf("Invalid endpoint type provided") 38 } 39 40 ao := gophercloud.AuthOptions{ 41 Username: c.Username, 42 UserID: c.UserID, 43 Password: c.Password, 44 APIKey: c.APIKey, 45 IdentityEndpoint: c.IdentityEndpoint, 46 TenantID: c.TenantID, 47 TenantName: c.TenantName, 48 DomainID: c.DomainID, 49 DomainName: c.DomainName, 50 } 51 52 client, err := openstack.NewClient(ao.IdentityEndpoint) 53 if err != nil { 54 return err 55 } 56 57 if c.CACertFile != "" { 58 59 caCert, err := ioutil.ReadFile(c.CACertFile) 60 if err != nil { 61 return err 62 } 63 64 caCertPool := x509.NewCertPool() 65 caCertPool.AppendCertsFromPEM(caCert) 66 67 config := &tls.Config{ 68 RootCAs: caCertPool, 69 } 70 71 transport := &http.Transport{TLSClientConfig: config} 72 client.HTTPClient.Transport = transport 73 } 74 75 if c.Insecure { 76 // Configure custom TLS settings. 77 config := &tls.Config{InsecureSkipVerify: true} 78 transport := &http.Transport{TLSClientConfig: config} 79 client.HTTPClient.Transport = transport 80 } 81 82 err = openstack.Authenticate(client, ao) 83 if err != nil { 84 return err 85 } 86 87 c.osClient = client 88 89 return nil 90 } 91 92 func (c *Config) blockStorageV1Client(region string) (*gophercloud.ServiceClient, error) { 93 return openstack.NewBlockStorageV1(c.osClient, gophercloud.EndpointOpts{ 94 Region: region, 95 Availability: c.getEndpointType(), 96 }) 97 } 98 99 func (c *Config) computeV2Client(region string) (*gophercloud.ServiceClient, error) { 100 return openstack.NewComputeV2(c.osClient, gophercloud.EndpointOpts{ 101 Region: region, 102 Availability: c.getEndpointType(), 103 }) 104 } 105 106 func (c *Config) networkingV2Client(region string) (*gophercloud.ServiceClient, error) { 107 return openstack.NewNetworkV2(c.osClient, gophercloud.EndpointOpts{ 108 Region: region, 109 Availability: c.getEndpointType(), 110 }) 111 } 112 113 func (c *Config) objectStorageV1Client(region string) (*gophercloud.ServiceClient, error) { 114 return openstack.NewObjectStorageV1(c.osClient, gophercloud.EndpointOpts{ 115 Region: region, 116 Availability: c.getEndpointType(), 117 }) 118 } 119 120 func (c *Config) getEndpointType() gophercloud.Availability { 121 if c.EndpointType == "internal" || c.EndpointType == "internalURL" { 122 return gophercloud.AvailabilityInternal 123 } 124 if c.EndpointType == "admin" || c.EndpointType == "adminURL" { 125 return gophercloud.AvailabilityAdmin 126 } 127 return gophercloud.AvailabilityPublic 128 }