2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"github.com/rackspace/gophercloud"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AccessConfig is for common configuration related to openstack access
|
|
|
|
type AccessConfig struct {
|
2013-09-01 16:22:22 -04:00
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
Provider string `mapstructure:"provider"`
|
|
|
|
RawRegion string `mapstructure:"region"`
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Auth returns a valid Auth object for access to openstack services, or
|
|
|
|
// an error if the authentication couldn't be resolved.
|
|
|
|
func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) {
|
|
|
|
username := c.Username
|
|
|
|
password := c.Password
|
|
|
|
provider := c.Provider
|
|
|
|
|
|
|
|
if username == "" {
|
|
|
|
username = os.Getenv("SDK_USERNAME")
|
|
|
|
}
|
|
|
|
if password == "" {
|
|
|
|
password = os.Getenv("SDK_PASSWORD")
|
|
|
|
}
|
|
|
|
if provider == "" {
|
|
|
|
provider = os.Getenv("SDK_PROVIDER")
|
|
|
|
}
|
|
|
|
|
|
|
|
authoptions := gophercloud.AuthOptions{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
AllowReauth: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return gophercloud.Authenticate(provider, authoptions)
|
|
|
|
}
|
|
|
|
|
2013-09-01 16:22:22 -04:00
|
|
|
func (c *AccessConfig) Region() string {
|
|
|
|
return c.RawRegion
|
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error {
|
|
|
|
if t == nil {
|
|
|
|
var err error
|
|
|
|
t, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
templates := map[string]*string{
|
|
|
|
"username": &c.Username,
|
|
|
|
"password": &c.Password,
|
|
|
|
"provider": &c.Provider,
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := make([]error, 0)
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = t.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 16:22:22 -04:00
|
|
|
if c.RawRegion == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("region must be specified"))
|
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|