github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/config.go (about)

     1  package openstack
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/rackspace/gophercloud"
     9  	"github.com/rackspace/gophercloud/openstack"
    10  )
    11  
    12  type Config struct {
    13  	Username         string
    14  	UserID           string
    15  	Password         string
    16  	APIKey           string
    17  	IdentityEndpoint string
    18  	TenantID         string
    19  	TenantName       string
    20  	DomainID         string
    21  	DomainName       string
    22  	Insecure         bool
    23  	EndpointType     string
    24  
    25  	osClient *gophercloud.ProviderClient
    26  }
    27  
    28  func (c *Config) loadAndValidate() error {
    29  
    30  	if c.EndpointType != "internal" && c.EndpointType != "internalURL" &&
    31  		c.EndpointType != "admin" && c.EndpointType != "adminURL" &&
    32  		c.EndpointType != "public" && c.EndpointType != "publicURL" &&
    33  		c.EndpointType != "" {
    34  		return fmt.Errorf("Invalid endpoint type provided")
    35  	}
    36  
    37  	ao := gophercloud.AuthOptions{
    38  		Username:         c.Username,
    39  		UserID:           c.UserID,
    40  		Password:         c.Password,
    41  		APIKey:           c.APIKey,
    42  		IdentityEndpoint: c.IdentityEndpoint,
    43  		TenantID:         c.TenantID,
    44  		TenantName:       c.TenantName,
    45  		DomainID:         c.DomainID,
    46  		DomainName:       c.DomainName,
    47  	}
    48  
    49  	client, err := openstack.NewClient(ao.IdentityEndpoint)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	if c.Insecure {
    55  		// Configure custom TLS settings.
    56  		config := &tls.Config{InsecureSkipVerify: true}
    57  		transport := &http.Transport{TLSClientConfig: config}
    58  		client.HTTPClient.Transport = transport
    59  	}
    60  
    61  	err = openstack.Authenticate(client, ao)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	c.osClient = client
    67  
    68  	return nil
    69  }
    70  
    71  func (c *Config) blockStorageV1Client(region string) (*gophercloud.ServiceClient, error) {
    72  	return openstack.NewBlockStorageV1(c.osClient, gophercloud.EndpointOpts{
    73  		Region:       region,
    74  		Availability: c.getEndpointType(),
    75  	})
    76  }
    77  
    78  func (c *Config) computeV2Client(region string) (*gophercloud.ServiceClient, error) {
    79  	return openstack.NewComputeV2(c.osClient, gophercloud.EndpointOpts{
    80  		Region:       region,
    81  		Availability: c.getEndpointType(),
    82  	})
    83  }
    84  
    85  func (c *Config) networkingV2Client(region string) (*gophercloud.ServiceClient, error) {
    86  	return openstack.NewNetworkV2(c.osClient, gophercloud.EndpointOpts{
    87  		Region:       region,
    88  		Availability: c.getEndpointType(),
    89  	})
    90  }
    91  
    92  func (c *Config) objectStorageV1Client(region string) (*gophercloud.ServiceClient, error) {
    93  	return openstack.NewObjectStorageV1(c.osClient, gophercloud.EndpointOpts{
    94  		Region:       region,
    95  		Availability: c.getEndpointType(),
    96  	})
    97  }
    98  
    99  func (c *Config) getEndpointType() gophercloud.Availability {
   100  	if c.EndpointType == "internal" || c.EndpointType == "internalURL" {
   101  		return gophercloud.AvailabilityInternal
   102  	}
   103  	if c.EndpointType == "admin" || c.EndpointType == "adminURL" {
   104  		return gophercloud.AvailabilityAdmin
   105  	}
   106  	return gophercloud.AvailabilityPublic
   107  }