builder/amazon/common: RunConfig to template processing

This commit is contained in:
Mitchell Hashimoto 2013-08-08 14:33:29 -07:00
parent 96cc8a4aee
commit e6b7a47841
2 changed files with 38 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package common
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/packer/common"
"time" "time"
) )
@ -23,7 +24,15 @@ type RunConfig struct {
sshTimeout time.Duration 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 // Defaults
if c.SSHPort == 0 { if c.SSHPort == 0 {
c.SSHPort = 22 c.SSHPort = 22
@ -48,6 +57,26 @@ func (c *RunConfig) Prepare() []error {
errs = append(errs, errors.New("An ssh_username must be specified")) 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) c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
if err != nil { if err != nil {
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err)) errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))

View File

@ -24,7 +24,7 @@ func testConfig() *RunConfig {
func TestRunConfigPrepare(t *testing.T) { func TestRunConfigPrepare(t *testing.T) {
c := testConfig() c := testConfig()
err := c.Prepare() err := c.Prepare(nil)
if len(err) > 0 { if len(err) > 0 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -33,7 +33,7 @@ func TestRunConfigPrepare(t *testing.T) {
func TestRunConfigPrepare_InstanceType(t *testing.T) { func TestRunConfigPrepare_InstanceType(t *testing.T) {
c := testConfig() c := testConfig()
c.InstanceType = "" c.InstanceType = ""
if err := c.Prepare(); len(err) != 1 { if err := c.Prepare(nil); len(err) != 1 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
} }
@ -41,7 +41,7 @@ func TestRunConfigPrepare_InstanceType(t *testing.T) {
func TestRunConfigPrepare_SourceAmi(t *testing.T) { func TestRunConfigPrepare_SourceAmi(t *testing.T) {
c := testConfig() c := testConfig()
c.SourceAmi = "" c.SourceAmi = ""
if err := c.Prepare(); len(err) != 1 { if err := c.Prepare(nil); len(err) != 1 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
} }
@ -49,7 +49,7 @@ func TestRunConfigPrepare_SourceAmi(t *testing.T) {
func TestRunConfigPrepare_SSHPort(t *testing.T) { func TestRunConfigPrepare_SSHPort(t *testing.T) {
c := testConfig() c := testConfig()
c.SSHPort = 0 c.SSHPort = 0
if err := c.Prepare(); len(err) != 0 { if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -58,7 +58,7 @@ func TestRunConfigPrepare_SSHPort(t *testing.T) {
} }
c.SSHPort = 44 c.SSHPort = 44
if err := c.Prepare(); len(err) != 0 { if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -70,12 +70,12 @@ func TestRunConfigPrepare_SSHPort(t *testing.T) {
func TestRunConfigPrepare_SSHTimeout(t *testing.T) { func TestRunConfigPrepare_SSHTimeout(t *testing.T) {
c := testConfig() c := testConfig()
c.RawSSHTimeout = "" c.RawSSHTimeout = ""
if err := c.Prepare(); len(err) != 0 { if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
c.RawSSHTimeout = "bad" c.RawSSHTimeout = "bad"
if err := c.Prepare(); len(err) != 1 { if err := c.Prepare(nil); len(err) != 1 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
} }
@ -83,7 +83,7 @@ func TestRunConfigPrepare_SSHTimeout(t *testing.T) {
func TestRunConfigPrepare_SSHUsername(t *testing.T) { func TestRunConfigPrepare_SSHUsername(t *testing.T) {
c := testConfig() c := testConfig()
c.SSHUsername = "" c.SSHUsername = ""
if err := c.Prepare(); len(err) != 1 { if err := c.Prepare(nil); len(err) != 1 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
} }