2014-08-15 04:55:47 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSchemaMap_Diff(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Schema map[string]*Schema
|
|
|
|
State *terraform.ResourceState
|
|
|
|
Config map[string]interface{}
|
|
|
|
Diff *terraform.ResourceDiff
|
|
|
|
Err bool
|
|
|
|
}{
|
2014-08-15 05:02:52 +02:00
|
|
|
/*
|
|
|
|
* String decode
|
|
|
|
*/
|
|
|
|
|
2014-08-15 04:55:47 +02:00
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"availability_zone": "foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"availability_zone": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "foo",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"availability_zone": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
NewComputed: true,
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
2014-08-22 21:18:08 +02:00
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
|
|
|
|
Diff: nil,
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
2014-08-22 17:45:54 +02:00
|
|
|
// String with StateFunc
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
StateFunc: func(a interface{}) string {
|
|
|
|
return a.(string) + "!"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"availability_zone": "foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"availability_zone": &terraform.ResourceAttrDiff{
|
2014-08-22 17:57:44 +02:00
|
|
|
Old: "",
|
|
|
|
New: "foo!",
|
|
|
|
NewExtra: "foo",
|
2014-08-22 17:45:54 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
2014-08-15 05:02:52 +02:00
|
|
|
/*
|
|
|
|
* Int decode
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"port": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"port": 27,
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"port": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "27",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bool decode
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"port": &Schema{
|
|
|
|
Type: TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"port": false,
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"port": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "0",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
2014-08-15 08:17:53 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* List decode
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ports": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Required: true,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"ports": []interface{}{1, 2, 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"ports.#": &terraform.ResourceAttrDiff{
|
2014-08-19 06:22:27 +02:00
|
|
|
Old: "0",
|
2014-08-15 08:17:53 +02:00
|
|
|
New: "3",
|
|
|
|
},
|
|
|
|
"ports.0": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "1",
|
|
|
|
},
|
|
|
|
"ports.1": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "2",
|
|
|
|
},
|
|
|
|
"ports.2": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ports": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Required: true,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"ports.#": "3",
|
|
|
|
"ports.0": "1",
|
|
|
|
"ports.1": "2",
|
|
|
|
"ports.2": "5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"ports": []interface{}{1, 2, 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: nil,
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ports": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Required: true,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"ports.#": "2",
|
|
|
|
"ports.0": "1",
|
|
|
|
"ports.1": "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"ports": []interface{}{1, 2, 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"ports.#": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "2",
|
|
|
|
New: "3",
|
|
|
|
},
|
|
|
|
"ports.2": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
2014-08-15 08:32:20 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ports": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Required: true,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"ports": []interface{}{1, 2, 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"ports.#": &terraform.ResourceAttrDiff{
|
2014-08-19 06:22:27 +02:00
|
|
|
Old: "0",
|
2014-08-15 08:32:20 +02:00
|
|
|
New: "3",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
"ports.0": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "1",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
"ports.1": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "2",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
"ports.2": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "5",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
2014-08-15 19:25:25 +02:00
|
|
|
|
2014-08-19 06:22:27 +02:00
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ports": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"ports.#": &terraform.ResourceAttrDiff{
|
2014-08-21 02:51:27 +02:00
|
|
|
Old: "",
|
2014-08-19 06:22:27 +02:00
|
|
|
NewComputed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
2014-08-15 19:25:25 +02:00
|
|
|
/*
|
2014-08-21 03:30:28 +02:00
|
|
|
* Set
|
|
|
|
*/
|
|
|
|
|
2014-08-15 19:25:25 +02:00
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
2014-08-21 00:45:34 +02:00
|
|
|
"ports": &Schema{
|
2014-08-21 02:51:27 +02:00
|
|
|
Type: TypeSet,
|
2014-08-15 19:25:25 +02:00
|
|
|
Required: true,
|
2014-08-21 00:45:34 +02:00
|
|
|
Elem: &Schema{Type: TypeInt},
|
2014-08-21 02:51:27 +02:00
|
|
|
Set: func(a interface{}) int {
|
|
|
|
return a.(int)
|
2014-08-15 19:25:25 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
2014-08-21 00:45:34 +02:00
|
|
|
"ports": []interface{}{5, 2, 1},
|
2014-08-15 19:25:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
2014-08-21 00:45:34 +02:00
|
|
|
"ports.#": &terraform.ResourceAttrDiff{
|
2014-08-19 06:22:27 +02:00
|
|
|
Old: "0",
|
2014-08-21 00:45:34 +02:00
|
|
|
New: "3",
|
|
|
|
},
|
|
|
|
"ports.0": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
2014-08-15 19:25:25 +02:00
|
|
|
New: "1",
|
|
|
|
},
|
2014-08-21 00:45:34 +02:00
|
|
|
"ports.1": &terraform.ResourceAttrDiff{
|
2014-08-15 19:25:25 +02:00
|
|
|
Old: "",
|
2014-08-21 00:45:34 +02:00
|
|
|
New: "2",
|
|
|
|
},
|
|
|
|
"ports.2": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "5",
|
2014-08-15 19:25:25 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
2014-08-15 19:39:40 +02:00
|
|
|
|
2014-08-21 06:02:42 +02:00
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ports": &Schema{
|
|
|
|
Type: TypeSet,
|
|
|
|
Required: true,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
Set: func(a interface{}) int {
|
|
|
|
return a.(int)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"ports.#": "2",
|
|
|
|
"ports.0": "2",
|
|
|
|
"ports.1": "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"ports": []interface{}{5, 2, 1},
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"ports.#": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "2",
|
|
|
|
New: "3",
|
|
|
|
},
|
|
|
|
"ports.2": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ports": &Schema{
|
|
|
|
Type: TypeSet,
|
|
|
|
Required: true,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
Set: func(a interface{}) int {
|
|
|
|
return a.(int)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"ports.#": "2",
|
|
|
|
"ports.0": "2",
|
|
|
|
"ports.1": "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"ports.#": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "2",
|
|
|
|
New: "0",
|
|
|
|
},
|
|
|
|
"ports.0": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "1",
|
|
|
|
NewRemoved: true,
|
|
|
|
},
|
|
|
|
"ports.1": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "2",
|
|
|
|
NewRemoved: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
2014-08-28 00:03:42 +02:00
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ports": &Schema{
|
|
|
|
Type: TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
Set: func(v interface{}) int { return v.(int) },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"availability_zone": "bar",
|
|
|
|
"ports.#": "1",
|
|
|
|
"ports.0": "80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
|
|
|
|
Diff: nil,
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
2014-08-21 00:45:34 +02:00
|
|
|
/*
|
|
|
|
* List of structure decode
|
|
|
|
*/
|
2014-08-15 19:39:40 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ingress": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Required: true,
|
|
|
|
Elem: &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"from": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: nil,
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
2014-08-21 00:45:34 +02:00
|
|
|
"ingress": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"from": 8080,
|
2014-08-15 19:39:40 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-21 00:45:34 +02:00
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"ingress.#": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "0",
|
|
|
|
New: "1",
|
|
|
|
},
|
|
|
|
"ingress.0.from": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "8080",
|
|
|
|
},
|
2014-08-15 19:39:40 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-21 00:45:34 +02:00
|
|
|
Err: false,
|
2014-08-15 19:39:40 +02:00
|
|
|
},
|
2014-08-16 18:49:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ComputedWhen
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Computed: true,
|
|
|
|
ComputedWhen: []string{"port"},
|
|
|
|
},
|
|
|
|
|
|
|
|
"port": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"availability_zone": "foo",
|
|
|
|
"port": "80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"port": 80,
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: nil,
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Computed: true,
|
|
|
|
ComputedWhen: []string{"port"},
|
|
|
|
},
|
|
|
|
|
|
|
|
"port": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"port": "80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"port": 80,
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"availability_zone": &terraform.ResourceAttrDiff{
|
|
|
|
NewComputed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
2014-08-16 22:32:21 +02:00
|
|
|
|
|
|
|
/* TODO
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Computed: true,
|
|
|
|
ComputedWhen: []string{"port"},
|
|
|
|
},
|
|
|
|
|
|
|
|
"port": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"availability_zone": "foo",
|
|
|
|
"port": "80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"port": 8080,
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"availability_zone": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
NewComputed: true,
|
|
|
|
},
|
|
|
|
"port": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "80",
|
|
|
|
New: "8080",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
*/
|
2014-08-19 00:07:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Maps
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"config_vars": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Elem: &Schema{Type: TypeMap},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"config_vars.#": "1",
|
|
|
|
"config_vars.0.foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"config_vars": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"config_vars.0.foo": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "bar",
|
|
|
|
NewRemoved: true,
|
|
|
|
},
|
|
|
|
"config_vars.0.bar": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "",
|
|
|
|
New: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
2014-08-19 01:54:30 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"config_vars": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Elem: &Schema{Type: TypeMap},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
State: &terraform.ResourceState{
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"config_vars.#": "1",
|
|
|
|
"config_vars.0.foo": "bar",
|
|
|
|
"config_vars.0.bar": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
|
|
|
|
Diff: &terraform.ResourceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"config_vars.#": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "1",
|
|
|
|
New: "0",
|
|
|
|
},
|
|
|
|
"config_vars.0.foo": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "bar",
|
|
|
|
NewRemoved: true,
|
|
|
|
},
|
|
|
|
"config_vars.0.bar": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "baz",
|
|
|
|
NewRemoved: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
c, err := config.NewRawConfig(tc.Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := schemaMap(tc.Schema).Diff(
|
|
|
|
tc.State, terraform.NewResourceConfig(c))
|
|
|
|
if (err != nil) != tc.Err {
|
2014-08-21 00:45:34 +02:00
|
|
|
t.Fatalf("#%d err: %s", i, err)
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tc.Diff, d) {
|
|
|
|
t.Fatalf("#%d: bad:\n\n%#v", i, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-16 07:00:16 +02:00
|
|
|
|
2014-08-17 23:50:44 +02:00
|
|
|
func TestSchemaMap_InternalValidate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
In map[string]*Schema
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// No optional and no required
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2014-08-25 01:52:51 +02:00
|
|
|
// No optional and no required
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2014-08-17 23:50:44 +02:00
|
|
|
// Missing Type
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Required but computed
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Required: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Looks good
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// List element not set
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// List element computed
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
2014-08-25 01:52:51 +02:00
|
|
|
Type: TypeList,
|
|
|
|
Optional: true,
|
2014-08-17 23:50:44 +02:00
|
|
|
Elem: &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2014-08-21 06:13:18 +02:00
|
|
|
// List element with Set set
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
2014-08-25 01:52:51 +02:00
|
|
|
Type: TypeList,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
Set: func(interface{}) int { return 0 },
|
|
|
|
Optional: true,
|
2014-08-21 06:13:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Set element with no Set set
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
2014-08-25 01:52:51 +02:00
|
|
|
Type: TypeSet,
|
|
|
|
Elem: &Schema{Type: TypeInt},
|
|
|
|
Optional: true,
|
2014-08-21 06:13:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2014-08-17 23:50:44 +02:00
|
|
|
// Required but computed
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ComputedWhen: []string{"foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Sub-resource invalid
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
2014-08-25 01:52:51 +02:00
|
|
|
Type: TypeList,
|
|
|
|
Optional: true,
|
2014-08-17 23:50:44 +02:00
|
|
|
Elem: &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": new(Schema),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Sub-resource valid
|
|
|
|
{
|
|
|
|
map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
2014-08-25 01:52:51 +02:00
|
|
|
Type: TypeList,
|
|
|
|
Optional: true,
|
2014-08-17 23:50:44 +02:00
|
|
|
Elem: &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{
|
2014-08-25 01:52:51 +02:00
|
|
|
Type: TypeInt,
|
|
|
|
Optional: true,
|
2014-08-17 23:50:44 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
err := schemaMap(tc.In).InternalValidate()
|
|
|
|
if (err != nil) != tc.Err {
|
2014-08-25 01:52:51 +02:00
|
|
|
t.Fatalf("%d: bad: %s\n\n%#v", i, err, tc.In)
|
2014-08-17 23:50:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
func TestSchemaMap_Validate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Schema map[string]*Schema
|
|
|
|
Config map[string]interface{}
|
|
|
|
Warn bool
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
// Good
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"availability_zone": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Required field not set
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
|
|
|
|
Err: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Invalid type
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"port": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"port": "I am invalid",
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Optional sub-resource
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ingress": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Elem: &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"from": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Not a list
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ingress": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Elem: &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"from": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"ingress": "foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Required sub-resource field
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ingress": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Elem: &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"from": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"ingress": []interface{}{
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Good sub-resource
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"ingress": &Schema{
|
2014-08-16 18:18:45 +02:00
|
|
|
Type: TypeList,
|
|
|
|
Optional: true,
|
2014-08-16 07:00:16 +02:00
|
|
|
Elem: &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"from": &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"ingress": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"from": 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: false,
|
|
|
|
},
|
2014-08-16 07:15:10 +02:00
|
|
|
|
|
|
|
// Invalid/unknown field
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: true,
|
|
|
|
},
|
2014-08-16 18:18:45 +02:00
|
|
|
|
|
|
|
// Computed field set
|
|
|
|
{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{
|
|
|
|
Type: TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"availability_zone": "bar",
|
|
|
|
},
|
|
|
|
|
|
|
|
Err: true,
|
|
|
|
},
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
c, err := config.NewRawConfig(tc.Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, es := schemaMap(tc.Schema).Validate(terraform.NewResourceConfig(c))
|
|
|
|
if (len(es) > 0) != tc.Err {
|
|
|
|
if len(es) == 0 {
|
|
|
|
t.Errorf("%d: no errors", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range es {
|
|
|
|
t.Errorf("%d: err: %s", i, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len(ws) > 0) != tc.Warn {
|
|
|
|
t.Fatalf("%d: ws: %#v", i, ws)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|