builder/amazonebs: Read config

This commit is contained in:
Mitchell Hashimoto 2013-05-09 13:19:38 -07:00
parent 9600bf5b4b
commit 6d0fa84e2c
2 changed files with 61 additions and 5 deletions

View File

@ -1,22 +1,40 @@
package amazonebs package amazonebs
import ( import (
"encoding/json"
"errors"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
) )
type config struct { type config struct {
AccessKey string AccessKey string `json:"access_key"`
Region string Region string
SecretKey string SecretKey string `json:"secret_key"`
SourceAmi string SourceAmi string `json:"source_ami"`
} }
type Builder struct { type Builder struct {
config config config config
} }
func (*Builder) Prepare(interface{}) error { func (b *Builder) Prepare(raw interface{}) (err error) {
return nil _, ok := raw.(map[string]interface{})
if !ok {
err = errors.New("configuration isn't a valid map")
return
}
jsonBytes, err := json.Marshal(raw)
if err != nil {
return
}
err = json.Unmarshal(jsonBytes, &b.config)
if err != nil {
return
}
return
} }
func (*Builder) Run(packer.Build, packer.Ui) {} func (*Builder) Run(packer.Build, packer.Ui) {}

View File

@ -12,3 +12,41 @@ func TestBuilder_ImplementsBuilder(t *testing.T) {
var actual packer.Builder var actual packer.Builder
assert.Implementor(&Builder{}, &actual, "should be a Builder") assert.Implementor(&Builder{}, &actual, "should be a Builder")
} }
func TestBuilder_Prepare_NotMap(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
b := &Builder{}
err := b.Prepare(42)
assert.NotNil(err, "should have an error")
assert.Equal(err.Error(), "configuration isn't a valid map", "config is not a map")
}
func TestBuilder_Prepare_BadType(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
b := &Builder{}
c := map[string]interface{} {
"access_key": []string{},
}
err := b.Prepare(c)
assert.NotNil(err, "should have an error")
}
func TestBuilder_Prepare_Good(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
b := &Builder{}
c := map[string]interface{} {
"access_key": "foo",
"secret_key": "bar",
"source_ami": "123456",
}
err := b.Prepare(c)
assert.Nil(err, "should not have an error")
assert.Equal(b.config.AccessKey, "foo", "should be valid access key")
assert.Equal(b.config.SecretKey, "bar", "should be valid secret key")
assert.Equal(b.config.SourceAmi, "123456", "should have source AMI")
}