From e6b7a47841faaa2c55628ddc19a28e82e3e0b7ae Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Aug 2013 14:33:29 -0700 Subject: [PATCH] builder/amazon/common: RunConfig to template processing --- builder/amazon/common/run_config.go | 31 +++++++++++++++++++++++- builder/amazon/common/run_config_test.go | 16 ++++++------ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 805a34c4b..ed280ec6d 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -3,6 +3,7 @@ package common import ( "errors" "fmt" + "github.com/mitchellh/packer/common" "time" ) @@ -23,7 +24,15 @@ type RunConfig struct { sshTimeout time.Duration } -func (c *RunConfig) Prepare() []error { +func (c *RunConfig) Prepare(t *common.Template) []error { + if t == nil { + var err error + t, err = common.NewTemplate() + if err != nil { + return []error{err} + } + } + // Defaults if c.SSHPort == 0 { c.SSHPort = 22 @@ -48,6 +57,26 @@ func (c *RunConfig) Prepare() []error { errs = append(errs, errors.New("An ssh_username must be specified")) } + templates := map[string]*string{ + "iam_instance_profile": &c.IamInstanceProfile, + "instance_type": &c.InstanceType, + "ssh_timeout": &c.RawSSHTimeout, + "security_group_id": &c.SecurityGroupId, + "ssh_username": &c.SSHUsername, + "source_ami": &c.SourceAmi, + "subnet_id": &c.SubnetId, + "vpc_id": &c.VpcId, + } + + 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)) + } + } + c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout) if err != nil { errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err)) diff --git a/builder/amazon/common/run_config_test.go b/builder/amazon/common/run_config_test.go index b34b4c614..ddfe85069 100644 --- a/builder/amazon/common/run_config_test.go +++ b/builder/amazon/common/run_config_test.go @@ -24,7 +24,7 @@ func testConfig() *RunConfig { func TestRunConfigPrepare(t *testing.T) { c := testConfig() - err := c.Prepare() + err := c.Prepare(nil) if len(err) > 0 { t.Fatalf("err: %s", err) } @@ -33,7 +33,7 @@ func TestRunConfigPrepare(t *testing.T) { func TestRunConfigPrepare_InstanceType(t *testing.T) { c := testConfig() c.InstanceType = "" - if err := c.Prepare(); len(err) != 1 { + if err := c.Prepare(nil); len(err) != 1 { t.Fatalf("err: %s", err) } } @@ -41,7 +41,7 @@ func TestRunConfigPrepare_InstanceType(t *testing.T) { func TestRunConfigPrepare_SourceAmi(t *testing.T) { c := testConfig() c.SourceAmi = "" - if err := c.Prepare(); len(err) != 1 { + if err := c.Prepare(nil); len(err) != 1 { t.Fatalf("err: %s", err) } } @@ -49,7 +49,7 @@ func TestRunConfigPrepare_SourceAmi(t *testing.T) { func TestRunConfigPrepare_SSHPort(t *testing.T) { c := testConfig() c.SSHPort = 0 - if err := c.Prepare(); len(err) != 0 { + if err := c.Prepare(nil); len(err) != 0 { t.Fatalf("err: %s", err) } @@ -58,7 +58,7 @@ func TestRunConfigPrepare_SSHPort(t *testing.T) { } c.SSHPort = 44 - if err := c.Prepare(); len(err) != 0 { + if err := c.Prepare(nil); len(err) != 0 { t.Fatalf("err: %s", err) } @@ -70,12 +70,12 @@ func TestRunConfigPrepare_SSHPort(t *testing.T) { func TestRunConfigPrepare_SSHTimeout(t *testing.T) { c := testConfig() c.RawSSHTimeout = "" - if err := c.Prepare(); len(err) != 0 { + if err := c.Prepare(nil); len(err) != 0 { t.Fatalf("err: %s", err) } c.RawSSHTimeout = "bad" - if err := c.Prepare(); len(err) != 1 { + if err := c.Prepare(nil); len(err) != 1 { t.Fatalf("err: %s", err) } } @@ -83,7 +83,7 @@ func TestRunConfigPrepare_SSHTimeout(t *testing.T) { func TestRunConfigPrepare_SSHUsername(t *testing.T) { c := testConfig() c.SSHUsername = "" - if err := c.Prepare(); len(err) != 1 { + if err := c.Prepare(nil); len(err) != 1 { t.Fatalf("err: %s", err) } }