github.com/alouche/packer@v0.3.7/builder/openstack/access_config.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/packer/packer" 6 "github.com/rackspace/gophercloud" 7 "os" 8 ) 9 10 // AccessConfig is for common configuration related to openstack access 11 type AccessConfig struct { 12 Username string `mapstructure:"username"` 13 Password string `mapstructure:"password"` 14 Project string `mapstructure:"project"` 15 Provider string `mapstructure:"provider"` 16 RawRegion string `mapstructure:"region"` 17 } 18 19 // Auth returns a valid Auth object for access to openstack services, or 20 // an error if the authentication couldn't be resolved. 21 func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) { 22 username := c.Username 23 password := c.Password 24 project := c.Project 25 provider := c.Provider 26 27 if username == "" { 28 username = os.Getenv("SDK_USERNAME") 29 } 30 if password == "" { 31 password = os.Getenv("SDK_PASSWORD") 32 } 33 if project == "" { 34 project = os.Getenv("SDK_PROJECT") 35 } 36 if provider == "" { 37 provider = os.Getenv("SDK_PROVIDER") 38 } 39 40 authoptions := gophercloud.AuthOptions{ 41 Username: username, 42 Password: password, 43 AllowReauth: true, 44 } 45 46 if project != "" { 47 authoptions.TenantName = project 48 } 49 50 return gophercloud.Authenticate(provider, authoptions) 51 } 52 53 func (c *AccessConfig) Region() string { 54 return c.RawRegion 55 } 56 57 func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error { 58 if t == nil { 59 var err error 60 t, err = packer.NewConfigTemplate() 61 if err != nil { 62 return []error{err} 63 } 64 } 65 66 templates := map[string]*string{ 67 "username": &c.Username, 68 "password": &c.Password, 69 "provider": &c.Provider, 70 } 71 72 errs := make([]error, 0) 73 for n, ptr := range templates { 74 var err error 75 *ptr, err = t.Process(*ptr, nil) 76 if err != nil { 77 errs = append( 78 errs, fmt.Errorf("Error processing %s: %s", n, err)) 79 } 80 } 81 82 if c.RawRegion == "" { 83 errs = append(errs, fmt.Errorf("region must be specified")) 84 } 85 86 if len(errs) > 0 { 87 return errs 88 } 89 90 return nil 91 }