mirror of https://github.com/hashicorp/packer
parent
c3bf014b5f
commit
be0959004a
@ -0,0 +1,54 @@
|
|||||||
|
package fix
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FixerScalewayAccessKey changes the "access_key" of a template
|
||||||
|
// to "organization_id".
|
||||||
|
type FixerScalewayAccessKey struct{}
|
||||||
|
|
||||||
|
func (FixerScalewayAccessKey) Fix(input map[string]interface{}) (map[string]interface{}, error) {
|
||||||
|
// The type we'll decode into; we only care about builders
|
||||||
|
type template struct {
|
||||||
|
Builders []map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode the input into our structure, if we can
|
||||||
|
var tpl template
|
||||||
|
if err := mapstructure.Decode(input, &tpl); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, builder := range tpl.Builders {
|
||||||
|
if builder["type"] != "scaleway" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
keyRaw, ok := builder["access_key"]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
accessKey, ok := keyRaw.(string)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// only assign to organization_id if it doesn't
|
||||||
|
// already exist; otherwise we'll just ignore access_key
|
||||||
|
_, organizationIdIncluded := builder["organization_id"]
|
||||||
|
if !organizationIdIncluded {
|
||||||
|
builder["organization_id"] = accessKey
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(builder, "access_key")
|
||||||
|
}
|
||||||
|
|
||||||
|
input["builders"] = tpl.Builders
|
||||||
|
return input, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (FixerScalewayAccessKey) Synopsis() string {
|
||||||
|
return `Updates builders using "access_key" to use "organization_id"`
|
||||||
|
}
|
||||||
@ -0,0 +1,89 @@
|
|||||||
|
package fix
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFixerScalewayAccessKey_Fix_Impl(t *testing.T) {
|
||||||
|
var _ Fixer = new(FixerScalewayAccessKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFixerScalewayAccessKey_Fix(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Input map[string]interface{}
|
||||||
|
Expected map[string]interface{}
|
||||||
|
}{
|
||||||
|
// No key_path field
|
||||||
|
{
|
||||||
|
Input: map[string]interface{}{
|
||||||
|
"type": "scaleway",
|
||||||
|
},
|
||||||
|
|
||||||
|
Expected: map[string]interface{}{
|
||||||
|
"type": "scaleway",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// organization_id without access_key
|
||||||
|
{
|
||||||
|
Input: map[string]interface{}{
|
||||||
|
"type": "scaleway",
|
||||||
|
"organization_id": "0000",
|
||||||
|
},
|
||||||
|
|
||||||
|
Expected: map[string]interface{}{
|
||||||
|
"type": "scaleway",
|
||||||
|
"organization_id": "0000",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// access_key without organization_id
|
||||||
|
{
|
||||||
|
Input: map[string]interface{}{
|
||||||
|
"type": "scaleway",
|
||||||
|
"access_key": "1111",
|
||||||
|
},
|
||||||
|
|
||||||
|
Expected: map[string]interface{}{
|
||||||
|
"type": "scaleway",
|
||||||
|
"organization_id": "1111",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// access_key and organization_id
|
||||||
|
{
|
||||||
|
Input: map[string]interface{}{
|
||||||
|
"type": "scaleway",
|
||||||
|
"access_key": "2222",
|
||||||
|
"organization_id": "3333",
|
||||||
|
},
|
||||||
|
|
||||||
|
Expected: map[string]interface{}{
|
||||||
|
"type": "scaleway",
|
||||||
|
"organization_id": "3333",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
var f FixerScalewayAccessKey
|
||||||
|
|
||||||
|
input := map[string]interface{}{
|
||||||
|
"builders": []map[string]interface{}{tc.Input},
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := map[string]interface{}{
|
||||||
|
"builders": []map[string]interface{}{tc.Expected},
|
||||||
|
}
|
||||||
|
|
||||||
|
output, err := f.Fix(input)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(output, expected) {
|
||||||
|
t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue