Commit 6d0fa84e authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazonebs: Read config

parent 9600bf5b
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) {}
...@@ -12,3 +12,41 @@ func TestBuilder_ImplementsBuilder(t *testing.T) { ...@@ -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")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment