Commit bef63846 authored by Devin Carlen's avatar Devin Carlen

Add support for standard OpenStack environment variables

parent 1a57e389
...@@ -2,6 +2,7 @@ package openstack ...@@ -2,6 +2,7 @@ package openstack
import ( import (
"fmt" "fmt"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/rackspace/gophercloud" "github.com/rackspace/gophercloud"
"net/http" "net/http"
...@@ -22,39 +23,26 @@ type AccessConfig struct { ...@@ -22,39 +23,26 @@ type AccessConfig struct {
// Auth returns a valid Auth object for access to openstack services, or // Auth returns a valid Auth object for access to openstack services, or
// an error if the authentication couldn't be resolved. // an error if the authentication couldn't be resolved.
func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) { func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) {
username := c.Username c.Username = common.CoalesceVals(c.Username, os.Getenv("SDK_USERNAME"), os.Getenv("OS_USERNAME"))
password := c.Password c.Password = common.CoalesceVals(c.Password, os.Getenv("SDK_PASSWORD"), os.Getenv("OS_PASSWORD"))
project := c.Project c.Project = common.CoalesceVals(c.Project, os.Getenv("SDK_PROJECT"), os.Getenv("OS_TENANT_NAME"))
provider := c.Provider c.Provider = common.CoalesceVals(c.Provider, os.Getenv("SDK_PROVIDER"), os.Getenv("OS_AUTH_URL"))
proxy := c.ProxyUrl c.RawRegion = common.CoalesceVals(c.RawRegion, os.Getenv("SDK_REGION"), os.Getenv("OS_REGION_NAME"))
if username == "" {
username = os.Getenv("SDK_USERNAME")
}
if password == "" {
password = os.Getenv("SDK_PASSWORD")
}
if project == "" {
project = os.Getenv("SDK_PROJECT")
}
if provider == "" {
provider = os.Getenv("SDK_PROVIDER")
}
authoptions := gophercloud.AuthOptions{ authoptions := gophercloud.AuthOptions{
Username: username, Username: c.Username,
Password: password, Password: c.Password,
AllowReauth: true, AllowReauth: true,
} }
if project != "" { if c.Project != "" {
authoptions.TenantName = project authoptions.TenantName = c.Project
} }
// For corporate networks it may be the case where we want our API calls // For corporate networks it may be the case where we want our API calls
// to be sent through a separate HTTP proxy than external traffic. // to be sent through a separate HTTP proxy than external traffic.
if proxy != "" { if c.ProxyUrl != "" {
url, err := url.Parse(proxy) url, err := url.Parse(c.ProxyUrl)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -64,11 +52,11 @@ func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) { ...@@ -64,11 +52,11 @@ func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) {
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(url)} http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(url)}
} }
return gophercloud.Authenticate(provider, authoptions) return gophercloud.Authenticate(c.Provider, authoptions)
} }
func (c *AccessConfig) Region() string { func (c *AccessConfig) Region() string {
return c.RawRegion return common.CoalesceVals(c.RawRegion, os.Getenv("SDK_REGION"), os.Getenv("OS_REGION_NAME"))
} }
func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error { func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error {
......
...@@ -194,3 +194,12 @@ func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) { ...@@ -194,3 +194,12 @@ func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) {
return v, nil return v, nil
}, nil }, nil
} }
func CoalesceVals(vals ...string) string {
for _, el := range vals {
if el != "" {
return el
}
}
return ""
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment