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