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