2017-06-12 16:30:22 +02:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2017-06-16 22:31:31 +02:00
|
|
|
"log"
|
2017-06-12 16:30:22 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/swauth"
|
|
|
|
"github.com/hashicorp/terraform/helper/pathorcontents"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
CACertFile string
|
|
|
|
ClientCertFile string
|
|
|
|
ClientKeyFile string
|
|
|
|
DomainID string
|
|
|
|
DomainName string
|
|
|
|
EndpointType string
|
|
|
|
IdentityEndpoint string
|
|
|
|
Insecure bool
|
|
|
|
Password string
|
2017-06-16 22:31:31 +02:00
|
|
|
Region string
|
2017-06-12 16:30:22 +02:00
|
|
|
Swauth bool
|
|
|
|
TenantID string
|
|
|
|
TenantName string
|
|
|
|
Token string
|
|
|
|
Username string
|
|
|
|
UserID string
|
|
|
|
|
2017-06-16 22:31:31 +02:00
|
|
|
OsClient *gophercloud.ProviderClient
|
2017-06-12 16:30:22 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 22:31:31 +02:00
|
|
|
func (c *Config) LoadAndValidate() error {
|
2017-06-12 16:30:22 +02:00
|
|
|
validEndpoint := false
|
|
|
|
validEndpoints := []string{
|
|
|
|
"internal", "internalURL",
|
|
|
|
"admin", "adminURL",
|
|
|
|
"public", "publicURL",
|
|
|
|
"",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, endpoint := range validEndpoints {
|
|
|
|
if c.EndpointType == endpoint {
|
|
|
|
validEndpoint = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validEndpoint {
|
|
|
|
return fmt.Errorf("Invalid endpoint type provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
ao := gophercloud.AuthOptions{
|
|
|
|
DomainID: c.DomainID,
|
|
|
|
DomainName: c.DomainName,
|
|
|
|
IdentityEndpoint: c.IdentityEndpoint,
|
|
|
|
Password: c.Password,
|
|
|
|
TenantID: c.TenantID,
|
|
|
|
TenantName: c.TenantName,
|
|
|
|
TokenID: c.Token,
|
|
|
|
Username: c.Username,
|
|
|
|
UserID: c.UserID,
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := openstack.NewClient(ao.IdentityEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set UserAgent
|
|
|
|
client.UserAgent.Prepend(terraform.UserAgentString())
|
|
|
|
|
|
|
|
config := &tls.Config{}
|
|
|
|
if c.CACertFile != "" {
|
|
|
|
caCert, _, err := pathorcontents.Read(c.CACertFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading CA Cert: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM([]byte(caCert))
|
|
|
|
config.RootCAs = caCertPool
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Insecure {
|
|
|
|
config.InsecureSkipVerify = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ClientCertFile != "" && c.ClientKeyFile != "" {
|
|
|
|
clientCert, _, err := pathorcontents.Read(c.ClientCertFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading Client Cert: %s", err)
|
|
|
|
}
|
|
|
|
clientKey, _, err := pathorcontents.Read(c.ClientKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading Client Key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Certificates = []tls.Certificate{cert}
|
|
|
|
config.BuildNameToCertificate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// if OS_DEBUG is set, log the requests and responses
|
|
|
|
var osDebug bool
|
|
|
|
if os.Getenv("OS_DEBUG") != "" {
|
|
|
|
osDebug = true
|
|
|
|
}
|
|
|
|
|
|
|
|
transport := &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: config}
|
|
|
|
client.HTTPClient = http.Client{
|
|
|
|
Transport: &LogRoundTripper{
|
|
|
|
Rt: transport,
|
|
|
|
OsDebug: osDebug,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// If using Swift Authentication, there's no need to validate authentication normally.
|
|
|
|
if !c.Swauth {
|
|
|
|
err = openstack.Authenticate(client, ao)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 22:31:31 +02:00
|
|
|
c.OsClient = client
|
2017-06-12 16:30:22 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-16 22:31:31 +02:00
|
|
|
func (c *Config) determineRegion(region string) string {
|
|
|
|
// If a resource-level region was not specified, and a provider-level region was set,
|
|
|
|
// use the provider-level region.
|
|
|
|
if region == "" && c.Region != "" {
|
|
|
|
region = c.Region
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] OpenStack Region is: %s", region)
|
|
|
|
return region
|
|
|
|
}
|
|
|
|
|
2017-06-12 16:30:22 +02:00
|
|
|
func (c *Config) blockStorageV1Client(region string) (*gophercloud.ServiceClient, error) {
|
2017-06-16 22:31:31 +02:00
|
|
|
return openstack.NewBlockStorageV1(c.OsClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.determineRegion(region),
|
2017-06-12 16:30:22 +02:00
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) blockStorageV2Client(region string) (*gophercloud.ServiceClient, error) {
|
2017-06-16 22:31:31 +02:00
|
|
|
return openstack.NewBlockStorageV2(c.OsClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.determineRegion(region),
|
2017-06-12 16:30:22 +02:00
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) computeV2Client(region string) (*gophercloud.ServiceClient, error) {
|
2017-06-16 22:31:31 +02:00
|
|
|
return openstack.NewComputeV2(c.OsClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.determineRegion(region),
|
2017-06-12 16:30:22 +02:00
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) dnsV2Client(region string) (*gophercloud.ServiceClient, error) {
|
2017-06-16 22:31:31 +02:00
|
|
|
return openstack.NewDNSV2(c.OsClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.determineRegion(region),
|
2017-06-12 16:30:22 +02:00
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) imageV2Client(region string) (*gophercloud.ServiceClient, error) {
|
2017-06-16 22:31:31 +02:00
|
|
|
return openstack.NewImageServiceV2(c.OsClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.determineRegion(region),
|
2017-06-12 16:30:22 +02:00
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) networkingV2Client(region string) (*gophercloud.ServiceClient, error) {
|
2017-06-16 22:31:31 +02:00
|
|
|
return openstack.NewNetworkV2(c.OsClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.determineRegion(region),
|
2017-06-12 16:30:22 +02:00
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) objectStorageV1Client(region string) (*gophercloud.ServiceClient, error) {
|
|
|
|
// If Swift Authentication is being used, return a swauth client.
|
|
|
|
if c.Swauth {
|
2017-06-16 22:31:31 +02:00
|
|
|
return swauth.NewObjectStorageV1(c.OsClient, swauth.AuthOpts{
|
2017-06-12 16:30:22 +02:00
|
|
|
User: c.Username,
|
|
|
|
Key: c.Password,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-16 22:31:31 +02:00
|
|
|
return openstack.NewObjectStorageV1(c.OsClient, gophercloud.EndpointOpts{
|
|
|
|
Region: c.determineRegion(region),
|
2017-06-12 16:30:22 +02:00
|
|
|
Availability: c.getEndpointType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) getEndpointType() gophercloud.Availability {
|
|
|
|
if c.EndpointType == "internal" || c.EndpointType == "internalURL" {
|
|
|
|
return gophercloud.AvailabilityInternal
|
|
|
|
}
|
|
|
|
if c.EndpointType == "admin" || c.EndpointType == "adminURL" {
|
|
|
|
return gophercloud.AvailabilityAdmin
|
|
|
|
}
|
|
|
|
return gophercloud.AvailabilityPublic
|
|
|
|
}
|