From 2ca48b4b6d88ee2d6b3d7bee933ca56a9b4a54b2 Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Sun, 1 Sep 2013 13:22:22 -0700 Subject: [PATCH] builder/openstack: don't hardcode "DFW" and make region required --- builder/openstack/access_config.go | 15 ++++++++++++--- builder/openstack/access_config_test.go | 16 ++++++++++++++-- builder/openstack/builder.go | 3 ++- builder/openstack/builder_test.go | 1 + 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/builder/openstack/access_config.go b/builder/openstack/access_config.go index 4602ecb75..489e43bea 100644 --- a/builder/openstack/access_config.go +++ b/builder/openstack/access_config.go @@ -9,9 +9,10 @@ import ( // AccessConfig is for common configuration related to openstack access type AccessConfig struct { - Username string `mapstructure:"username"` - Password string `mapstructure:"password"` - Provider string `mapstructure:"provider"` + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` + Provider string `mapstructure:"provider"` + RawRegion string `mapstructure:"region"` } // 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) } +func (c *AccessConfig) Region() string { + return c.RawRegion +} + func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error { if t == nil { 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 { return errs } diff --git a/builder/openstack/access_config_test.go b/builder/openstack/access_config_test.go index 3113edf52..7a9e5201e 100644 --- a/builder/openstack/access_config_test.go +++ b/builder/openstack/access_config_test.go @@ -8,9 +8,21 @@ func testAccessConfig() *AccessConfig { return &AccessConfig{} } -func TestAccessConfigPrepare_Region(t *testing.T) { +func TestAccessConfigPrepare_NoRegion(t *testing.T) { c := testAccessConfig() - if err := c.Prepare(nil); err != nil { + if err := c.Prepare(nil); err == nil { 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()) + } +} diff --git a/builder/openstack/builder.go b/builder/openstack/builder.go index 9882ea032..95204dab6 100644 --- a/builder/openstack/builder.go +++ b/builder/openstack/builder.go @@ -62,12 +62,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe } api := &gophercloud.ApiCriteria{ Name: "cloudServersOpenStack", - Region: "DFW", + Region: b.config.AccessConfig.Region(), VersionId: "2", UrlChoice: gophercloud.PublicURL, } csp, err := gophercloud.ServersApi(auth, *api) if err != nil { + log.Printf("Region: %s", b.config.AccessConfig.Region()) return nil, err } diff --git a/builder/openstack/builder_test.go b/builder/openstack/builder_test.go index 38bf4b5f6..cf7ca2927 100644 --- a/builder/openstack/builder_test.go +++ b/builder/openstack/builder_test.go @@ -10,6 +10,7 @@ func testConfig() map[string]interface{} { "username": "foo", "password": "bar", "provider": "foo", + "region": "DFW", "image_name": "foo", "source_image": "foo", "flavor": "foo",