github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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/gophercloud/gophercloud"
    11  	"github.com/gophercloud/gophercloud/openstack"
    12  )
    13  
    14  type Config struct {
    15  	Username         string
    16  	UserID           string
    17  	Password         string
    18  	Token            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  	ClientCertFile   string
    28  	ClientKeyFile    string
    29  
    30  	osClient *gophercloud.ProviderClient
    31  }
    32  
    33  func (c *Config) loadAndValidate() error {
    34  
    35  	if c.EndpointType != "internal" && c.EndpointType != "internalURL" &&
    36  		c.EndpointType != "admin" && c.EndpointType != "adminURL" &&
    37  		c.EndpointType != "public" && c.EndpointType != "publicURL" &&
    38  		c.EndpointType != "" {
    39  		return fmt.Errorf("Invalid endpoint type provided")
    40  	}
    41  
    42  	ao := gophercloud.AuthOptions{
    43  		Username:         c.Username,
    44  		UserID:           c.UserID,
    45  		Password:         c.Password,
    46  		TokenID:          c.Token,
    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  	config := &tls.Config{}
    60  	if c.CACertFile != "" {
    61  
    62  		caCert, err := ioutil.ReadFile(c.CACertFile)
    63  		if err != nil {
    64  			return err
    65  		}
    66  
    67  		caCertPool := x509.NewCertPool()
    68  		caCertPool.AppendCertsFromPEM(caCert)
    69  		config.RootCAs = caCertPool
    70  	}
    71  	if c.Insecure {
    72  		config.InsecureSkipVerify = true
    73  	}
    74  
    75  	if c.ClientCertFile != "" && c.ClientKeyFile != "" {
    76  		cert, err := tls.LoadX509KeyPair(c.ClientCertFile, c.ClientKeyFile)
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		config.Certificates = []tls.Certificate{cert}
    82  		config.BuildNameToCertificate()
    83  	}
    84  	transport := &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: config}
    85  	client.HTTPClient.Transport = transport
    86  
    87  	err = openstack.Authenticate(client, ao)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	c.osClient = client
    93  
    94  	return nil
    95  }
    96  
    97  func (c *Config) blockStorageV1Client(region string) (*gophercloud.ServiceClient, error) {
    98  	return openstack.NewBlockStorageV1(c.osClient, gophercloud.EndpointOpts{
    99  		Region:       region,
   100  		Availability: c.getEndpointType(),
   101  	})
   102  }
   103  
   104  func (c *Config) blockStorageV2Client(region string) (*gophercloud.ServiceClient, error) {
   105  	return openstack.NewBlockStorageV2(c.osClient, gophercloud.EndpointOpts{
   106  		Region:       region,
   107  		Availability: c.getEndpointType(),
   108  	})
   109  }
   110  
   111  func (c *Config) computeV2Client(region string) (*gophercloud.ServiceClient, error) {
   112  	return openstack.NewComputeV2(c.osClient, gophercloud.EndpointOpts{
   113  		Region:       region,
   114  		Availability: c.getEndpointType(),
   115  	})
   116  }
   117  
   118  func (c *Config) networkingV2Client(region string) (*gophercloud.ServiceClient, error) {
   119  	return openstack.NewNetworkV2(c.osClient, gophercloud.EndpointOpts{
   120  		Region:       region,
   121  		Availability: c.getEndpointType(),
   122  	})
   123  }
   124  
   125  func (c *Config) objectStorageV1Client(region string) (*gophercloud.ServiceClient, error) {
   126  	return openstack.NewObjectStorageV1(c.osClient, gophercloud.EndpointOpts{
   127  		Region:       region,
   128  		Availability: c.getEndpointType(),
   129  	})
   130  }
   131  
   132  func (c *Config) getEndpointType() gophercloud.Availability {
   133  	if c.EndpointType == "internal" || c.EndpointType == "internalURL" {
   134  		return gophercloud.AvailabilityInternal
   135  	}
   136  	if c.EndpointType == "admin" || c.EndpointType == "adminURL" {
   137  		return gophercloud.AvailabilityAdmin
   138  	}
   139  	return gophercloud.AvailabilityPublic
   140  }