builder/amazon/common: RunConfig to template processing

This commit is contained in:
Mitchell Hashimoto 2013-08-08 14:33:29 -07:00
parent 93ac76e9c7
commit 8e7aa41bdf
2 changed files with 38 additions and 9 deletions

View File

@ -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))

View File

@ -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)
}
}