Commit 2efab467 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazonebs: Switch from json to mapstructure for decode

parent bee26600
...@@ -6,21 +6,21 @@ ...@@ -6,21 +6,21 @@
package amazonebs package amazonebs
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"time" "time"
) )
type config struct { type config struct {
AccessKey string `json:"access_key"` AccessKey string `mapstructure:"access_key"`
AMIName string `json:"ami_name"` AMIName string `mapstructure:"ami_name"`
Region string Region string
SecretKey string `json:"secret_key"` SecretKey string `mapstructure:"secret_key"`
SourceAmi string `json:"source_ami"` SourceAmi string `mapstructure:"source_ami"`
} }
type Builder struct { type Builder struct {
...@@ -28,16 +28,7 @@ type Builder struct { ...@@ -28,16 +28,7 @@ type Builder struct {
} }
func (b *Builder) Prepare(raw interface{}) (err error) { func (b *Builder) Prepare(raw interface{}) (err error) {
// Marshal and unmarshal the raw configuration as a way to get it err = mapstructure.Decode(raw, &b.config)
// into our "config" struct.
// TODO: Use the reflection package and provide this as an API for
// better error messages
jsonBytes, err := json.Marshal(raw)
if err != nil {
return
}
err = json.Unmarshal(jsonBytes, &b.config)
if err != nil { if err != nil {
return return
} }
......
...@@ -14,15 +14,15 @@ func TestBuilder_ImplementsBuilder(t *testing.T) { ...@@ -14,15 +14,15 @@ func TestBuilder_ImplementsBuilder(t *testing.T) {
} }
func TestBuilder_Prepare_BadType(t *testing.T) { func TestBuilder_Prepare_BadType(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
b := &Builder{} b := &Builder{}
c := map[string]interface{}{ c := map[string]interface{}{
"access_key": []string{}, "access_key": []string{},
} }
err := b.Prepare(c) err := b.Prepare(c)
assert.NotNil(err, "should have an error") if err == nil {
t.Fatalf("prepare should fail")
}
} }
func TestBuilder_Prepare_Good(t *testing.T) { func TestBuilder_Prepare_Good(t *testing.T) {
......
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