update jsonprovider to use new schema type

This commit is contained in:
James Bardin 2023-07-05 18:06:07 -04:00
parent ec3a38e5ed
commit d487ce20e1
4 changed files with 86 additions and 99 deletions

View File

@ -6,6 +6,7 @@ package jsonprovider
import (
"encoding/json"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/terraform"
)
@ -14,8 +15,8 @@ import (
// consuming parser.
const FormatVersion = "1.0"
// providers is the top-level object returned when exporting provider schemas
type providers struct {
// Providers is the top-level object returned when exporting provider schemas
type Providers struct {
FormatVersion string `json:"format_version"`
Schemas map[string]*Provider `json:"provider_schemas,omitempty"`
}
@ -26,9 +27,9 @@ type Provider struct {
DataSourceSchemas map[string]*Schema `json:"data_source_schemas,omitempty"`
}
func newProviders() *providers {
func newProviders() *Providers {
schemas := make(map[string]*Provider)
return &providers{
return &Providers{
FormatVersion: FormatVersion,
Schemas: schemas,
}
@ -53,29 +54,10 @@ func Marshal(s *terraform.Schemas) ([]byte, error) {
return ret, err
}
func marshalProvider(tps *terraform.ProviderSchema) *Provider {
if tps == nil {
return &Provider{}
}
var ps *Schema
var rs, ds map[string]*Schema
if tps.Provider != nil {
ps = marshalSchema(tps.Provider)
}
if tps.ResourceTypes != nil {
rs = marshalSchemas(tps.ResourceTypes, tps.ResourceTypeSchemaVersions)
}
if tps.DataSources != nil {
ds = marshalSchemas(tps.DataSources, tps.ResourceTypeSchemaVersions)
}
func marshalProvider(tps providers.Schemas) *Provider {
return &Provider{
Provider: ps,
ResourceSchemas: rs,
DataSourceSchemas: ds,
Provider: marshalSchema(tps.Provider),
ResourceSchemas: marshalSchemas(tps.ResourceTypes),
DataSourceSchemas: marshalSchemas(tps.DataSources),
}
}

View File

@ -5,23 +5,28 @@ package jsonprovider
import (
"encoding/json"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/terraform"
"github.com/hashicorp/terraform/internal/providers"
)
func TestMarshalProvider(t *testing.T) {
tests := []struct {
Input *terraform.ProviderSchema
Input providers.Schemas
Want *Provider
}{
{
nil,
&Provider{},
providers.Schemas{},
&Provider{
Provider: &Schema{},
ResourceSchemas: map[string]*Schema{},
DataSourceSchemas: map[string]*Schema{},
},
},
{
testProvider(),
@ -143,73 +148,78 @@ func TestMarshalProvider(t *testing.T) {
},
}
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))
}
for i, test := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
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},
func testProvider() providers.Schemas {
return providers.Schemas{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"region": {Type: cty.String, Required: true},
},
},
},
ResourceTypes: map[string]*configschema.Block{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
"volumes": {
Optional: true,
NestedType: &configschema.Object{
Version: 42,
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
"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},
},
},
},
},
BlockTypes: map[string]*configschema.NestedBlock{
"network_interface": {
Nesting: configschema.NestingList,
Attributes: map[string]*configschema.Attribute{
"size": {Type: cty.String, Required: true},
"mount_point": {Type: cty.String, Required: 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},
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{
DataSources: map[string]providers.Schema{
"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},
Version: 3,
Block: &configschema.Block{
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,
},
}
}

View File

@ -4,7 +4,7 @@
package jsonprovider
import (
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/providers"
)
type Schema struct {
@ -14,28 +14,25 @@ type Schema struct {
// marshalSchema is a convenience wrapper around mashalBlock. Schema version
// should be set by the caller.
func marshalSchema(block *configschema.Block) *Schema {
if block == nil {
func marshalSchema(schema providers.Schema) *Schema {
if schema.Block == nil {
return &Schema{}
}
var ret Schema
ret.Block = marshalBlock(block)
ret.Block = marshalBlock(schema.Block)
ret.Version = uint64(schema.Version)
return &ret
}
func marshalSchemas(blocks map[string]*configschema.Block, rVersions map[string]uint64) map[string]*Schema {
if blocks == nil {
func marshalSchemas(schemas map[string]providers.Schema) map[string]*Schema {
if schemas == nil {
return map[string]*Schema{}
}
ret := make(map[string]*Schema, len(blocks))
for k, v := range blocks {
ret := make(map[string]*Schema, len(schemas))
for k, v := range schemas {
ret[k] = marshalSchema(v)
version, ok := rVersions[k]
if ok {
ret[k].Version = version
}
}
return ret
}

View File

@ -8,24 +8,22 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/providers"
)
func TestMarshalSchemas(t *testing.T) {
tests := []struct {
Input map[string]*configschema.Block
Versions map[string]uint64
Want map[string]*Schema
Input map[string]providers.Schema
Want map[string]*Schema
}{
{
nil,
map[string]uint64{},
map[string]*Schema{},
},
}
for _, test := range tests {
got := marshalSchemas(test.Input, test.Versions)
got := marshalSchemas(test.Input)
if !cmp.Equal(got, test.Want) {
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, test.Want))
}
@ -34,11 +32,11 @@ func TestMarshalSchemas(t *testing.T) {
func TestMarshalSchema(t *testing.T) {
tests := map[string]struct {
Input *configschema.Block
Input providers.Schema
Want *Schema
}{
"nil_block": {
nil,
providers.Schema{},
&Schema{},
},
}