2019-02-25 22:32:47 +01:00
|
|
|
package jsonprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarshalProvider(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Input *terraform.ProviderSchema
|
2019-03-14 22:52:07 +01:00
|
|
|
Want *Provider
|
2019-02-25 22:32:47 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
nil,
|
2019-03-14 22:52:07 +01:00
|
|
|
&Provider{},
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testProvider(),
|
2019-03-14 22:52:07 +01:00
|
|
|
&Provider{
|
2019-02-25 22:32:47 +01:00
|
|
|
Provider: &schema{
|
|
|
|
Block: &block{
|
|
|
|
Attributes: map[string]*attribute{
|
|
|
|
"region": {
|
2020-03-06 01:53:24 +01:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Required: true,
|
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
},
|
2020-03-06 01:53:24 +01:00
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceSchemas: map[string]*schema{
|
|
|
|
"test_instance": {
|
|
|
|
Version: 42,
|
|
|
|
Block: &block{
|
|
|
|
Attributes: map[string]*attribute{
|
|
|
|
"id": {
|
2020-03-06 01:53:24 +01:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
"ami": {
|
2020-03-06 01:53:24 +01:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Optional: true,
|
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
2021-03-12 14:28:22 +01:00
|
|
|
"volumes": {
|
|
|
|
AttributeNestedType: &nestedType{
|
|
|
|
NestingMode: "list",
|
|
|
|
Attributes: map[string]*attribute{
|
|
|
|
"size": {
|
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Required: true,
|
|
|
|
DescriptionKind: "plain",
|
|
|
|
},
|
|
|
|
"mount_point": {
|
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Required: true,
|
|
|
|
DescriptionKind: "plain",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Optional: true,
|
|
|
|
DescriptionKind: "plain",
|
|
|
|
},
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
BlockTypes: map[string]*blockType{
|
|
|
|
"network_interface": {
|
|
|
|
Block: &block{
|
|
|
|
Attributes: map[string]*attribute{
|
|
|
|
"device_index": {
|
2020-03-06 01:53:24 +01:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Optional: true,
|
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
"description": {
|
2020-03-06 01:53:24 +01:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Optional: true,
|
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
},
|
2020-03-06 01:53:24 +01:00
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
NestingMode: "list",
|
|
|
|
},
|
|
|
|
},
|
2020-03-06 01:53:24 +01:00
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DataSourceSchemas: map[string]*schema{
|
|
|
|
"test_data_source": {
|
|
|
|
Version: 3,
|
|
|
|
Block: &block{
|
|
|
|
Attributes: map[string]*attribute{
|
|
|
|
"id": {
|
2020-03-06 01:53:24 +01:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
"ami": {
|
2020-03-06 01:53:24 +01:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Optional: true,
|
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
BlockTypes: map[string]*blockType{
|
|
|
|
"network_interface": {
|
|
|
|
Block: &block{
|
|
|
|
Attributes: map[string]*attribute{
|
|
|
|
"device_index": {
|
2020-03-06 01:53:24 +01:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Optional: true,
|
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
"description": {
|
2020-03-06 01:53:24 +01:00
|
|
|
AttributeType: json.RawMessage(`"string"`),
|
|
|
|
Optional: true,
|
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
},
|
2020-03-06 01:53:24 +01:00
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
NestingMode: "list",
|
|
|
|
},
|
|
|
|
},
|
2020-03-06 01:53:24 +01:00
|
|
|
DescriptionKind: "plain",
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
got := marshalProvider(test.Input)
|
|
|
|
if !cmp.Equal(got, test.Want) {
|
|
|
|
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, test.Want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testProvider() *terraform.ProviderSchema {
|
|
|
|
return &terraform.ProviderSchema{
|
|
|
|
Provider: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"region": {Type: cty.String, Required: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
2021-03-12 14:28:22 +01:00
|
|
|
"volumes": {
|
|
|
|
Optional: true,
|
|
|
|
NestedType: &configschema.Object{
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"size": {Type: cty.String, Required: true},
|
|
|
|
"mount_point": {Type: cty.String, Required: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-02-25 22:32:47 +01:00
|
|
|
},
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
"network_interface": {
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
Block: configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"device_index": {Type: cty.String, Optional: true},
|
|
|
|
"description": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DataSources: map[string]*configschema.Block{
|
|
|
|
"test_data_source": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
"network_interface": {
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
Block: configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"device_index": {Type: cty.String, Optional: true},
|
|
|
|
"description": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
ResourceTypeSchemaVersions: map[string]uint64{
|
|
|
|
"test_instance": 42,
|
|
|
|
"test_data_source": 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|