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