builder/openstack: don't hardcode "DFW" and make region required

This commit is contained in:
Mark Peek 2013-09-01 13:22:22 -07:00
parent 12e33f6c87
commit 2ca48b4b6d
4 changed files with 29 additions and 6 deletions

View File

@ -9,9 +9,10 @@ import (
// AccessConfig is for common configuration related to openstack access // AccessConfig is for common configuration related to openstack access
type AccessConfig struct { type AccessConfig struct {
Username string `mapstructure:"username"` Username string `mapstructure:"username"`
Password string `mapstructure:"password"` Password string `mapstructure:"password"`
Provider string `mapstructure:"provider"` Provider string `mapstructure:"provider"`
RawRegion string `mapstructure:"region"`
} }
// Auth returns a valid Auth object for access to openstack services, or // Auth returns a valid Auth object for access to openstack services, or
@ -40,6 +41,10 @@ func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) {
return gophercloud.Authenticate(provider, authoptions) return gophercloud.Authenticate(provider, authoptions)
} }
func (c *AccessConfig) Region() string {
return c.RawRegion
}
func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error { func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error {
if t == nil { if t == nil {
var err error var err error
@ -65,6 +70,10 @@ func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error {
} }
} }
if c.RawRegion == "" {
errs = append(errs, fmt.Errorf("region must be specified"))
}
if len(errs) > 0 { if len(errs) > 0 {
return errs return errs
} }

View File

@ -8,9 +8,21 @@ func testAccessConfig() *AccessConfig {
return &AccessConfig{} return &AccessConfig{}
} }
func TestAccessConfigPrepare_Region(t *testing.T) { func TestAccessConfigPrepare_NoRegion(t *testing.T) {
c := testAccessConfig() c := testAccessConfig()
if err := c.Prepare(nil); err != nil { if err := c.Prepare(nil); err == nil {
t.Fatalf("shouldn't have err: %s", err) t.Fatalf("shouldn't have err: %s", err)
} }
} }
func TestAccessConfigPrepare_Region(t *testing.T) {
dfw := "DFW"
c := testAccessConfig()
c.RawRegion = dfw
if err := c.Prepare(nil); err != nil {
t.Fatalf("shouldn't have err: %s", err)
}
if dfw != c.Region() {
t.Fatalf("Regions do not match: %s %s", dfw, c.Region())
}
}

View File

@ -62,12 +62,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} }
api := &gophercloud.ApiCriteria{ api := &gophercloud.ApiCriteria{
Name: "cloudServersOpenStack", Name: "cloudServersOpenStack",
Region: "DFW", Region: b.config.AccessConfig.Region(),
VersionId: "2", VersionId: "2",
UrlChoice: gophercloud.PublicURL, UrlChoice: gophercloud.PublicURL,
} }
csp, err := gophercloud.ServersApi(auth, *api) csp, err := gophercloud.ServersApi(auth, *api)
if err != nil { if err != nil {
log.Printf("Region: %s", b.config.AccessConfig.Region())
return nil, err return nil, err
} }

View File

@ -10,6 +10,7 @@ func testConfig() map[string]interface{} {
"username": "foo", "username": "foo",
"password": "bar", "password": "bar",
"provider": "foo", "provider": "foo",
"region": "DFW",
"image_name": "foo", "image_name": "foo",
"source_image": "foo", "source_image": "foo",
"flavor": "foo", "flavor": "foo",