add support for attributes with nested types in providers schema (#28055)
This PR extends jsonprovider to support attributes with NestedTypes and extends test coverage in jsonprovider and the providers schemas tests. I've also cleaned up some comments and extracted the logic to parse the nesting mode so it can be used in both marshalling blocks and attributes.
This commit is contained in:
parent
42c6c5dd6c
commit
b26ae9cf48
|
@ -4,10 +4,12 @@ import (
|
|||
"encoding/json"
|
||||
|
||||
"github.com/hashicorp/terraform/configs/configschema"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
type attribute struct {
|
||||
AttributeType json.RawMessage `json:"type,omitempty"`
|
||||
AttributeNestedType *nestedType `json:"nested_type,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
DescriptionKind string `json:"description_kind,omitempty"`
|
||||
Deprecated bool `json:"deprecated,omitempty"`
|
||||
|
@ -17,6 +19,13 @@ type attribute struct {
|
|||
Sensitive bool `json:"sensitive,omitempty"`
|
||||
}
|
||||
|
||||
type nestedType struct {
|
||||
Attributes map[string]*attribute `json:"attributes,omitempty"`
|
||||
NestingMode string `json:"nesting_mode,omitempty"`
|
||||
MinItems uint64 `json:"min_items,omitempty"`
|
||||
MaxItems uint64 `json:"max_items,omitempty"`
|
||||
}
|
||||
|
||||
func marshalStringKind(sk configschema.StringKind) string {
|
||||
switch sk {
|
||||
default:
|
||||
|
@ -27,12 +36,7 @@ func marshalStringKind(sk configschema.StringKind) string {
|
|||
}
|
||||
|
||||
func marshalAttribute(attr *configschema.Attribute) *attribute {
|
||||
// we're not concerned about errors because at this point the schema has
|
||||
// already been checked and re-checked.
|
||||
attrTy, _ := attr.Type.MarshalJSON()
|
||||
|
||||
return &attribute{
|
||||
AttributeType: attrTy,
|
||||
ret := &attribute{
|
||||
Description: attr.Description,
|
||||
DescriptionKind: marshalStringKind(attr.DescriptionKind),
|
||||
Required: attr.Required,
|
||||
|
@ -41,4 +45,27 @@ func marshalAttribute(attr *configschema.Attribute) *attribute {
|
|||
Sensitive: attr.Sensitive,
|
||||
Deprecated: attr.Deprecated,
|
||||
}
|
||||
|
||||
// we're not concerned about errors because at this point the schema has
|
||||
// already been checked and re-checked.
|
||||
if attr.Type != cty.NilType {
|
||||
attrTy, _ := attr.Type.MarshalJSON()
|
||||
ret.AttributeType = attrTy
|
||||
}
|
||||
|
||||
if attr.NestedType != nil {
|
||||
nestedTy := nestedType{
|
||||
MinItems: uint64(attr.NestedType.MinItems),
|
||||
MaxItems: uint64(attr.NestedType.MaxItems),
|
||||
NestingMode: nestingModeString(attr.NestedType.Nesting),
|
||||
}
|
||||
attrs := make(map[string]*attribute, len(attr.NestedType.Attributes))
|
||||
for k, attr := range attr.NestedType.Attributes {
|
||||
attrs[k] = marshalAttribute(attr)
|
||||
}
|
||||
nestedTy.Attributes = attrs
|
||||
ret.AttributeNestedType = &nestedTy
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
|
|
@ -27,21 +27,7 @@ func marshalBlockTypes(nestedBlock *configschema.NestedBlock) *blockType {
|
|||
Block: marshalBlock(&nestedBlock.Block),
|
||||
MinItems: uint64(nestedBlock.MinItems),
|
||||
MaxItems: uint64(nestedBlock.MaxItems),
|
||||
}
|
||||
|
||||
switch nestedBlock.Nesting {
|
||||
case configschema.NestingSingle:
|
||||
ret.NestingMode = "single"
|
||||
case configschema.NestingGroup:
|
||||
ret.NestingMode = "group"
|
||||
case configschema.NestingList:
|
||||
ret.NestingMode = "list"
|
||||
case configschema.NestingSet:
|
||||
ret.NestingMode = "set"
|
||||
case configschema.NestingMap:
|
||||
ret.NestingMode = "map"
|
||||
default:
|
||||
ret.NestingMode = "invalid"
|
||||
NestingMode: nestingModeString(nestedBlock.Nesting),
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
@ -75,3 +61,20 @@ func marshalBlock(configBlock *configschema.Block) *block {
|
|||
|
||||
return &ret
|
||||
}
|
||||
|
||||
func nestingModeString(mode configschema.NestingMode) string {
|
||||
switch mode {
|
||||
case configschema.NestingSingle:
|
||||
return "single"
|
||||
case configschema.NestingGroup:
|
||||
return "group"
|
||||
case configschema.NestingList:
|
||||
return "list"
|
||||
case configschema.NestingSet:
|
||||
return "set"
|
||||
case configschema.NestingMap:
|
||||
return "map"
|
||||
default:
|
||||
return "invalid"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,6 +51,25 @@ func TestMarshalProvider(t *testing.T) {
|
|||
Optional: true,
|
||||
DescriptionKind: "plain",
|
||||
},
|
||||
"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",
|
||||
},
|
||||
},
|
||||
BlockTypes: map[string]*blockType{
|
||||
"network_interface": {
|
||||
|
@ -141,6 +160,16 @@ func testProvider() *terraform.ProviderSchema {
|
|||
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": {
|
||||
|
|
|
@ -9,7 +9,11 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform/configs/configschema"
|
||||
"github.com/hashicorp/terraform/providers"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
func TestProvidersSchema_error(t *testing.T) {
|
||||
|
@ -28,8 +32,6 @@ func TestProvidersSchema_error(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestProvidersSchema_output(t *testing.T) {
|
||||
// there's only one test at this time. This can be refactored to have
|
||||
// multiple test cases in individual directories as needed.
|
||||
fixtureDir := "testdata/providers-schema"
|
||||
testDirs, err := ioutil.ReadDir(fixtureDir)
|
||||
if err != nil {
|
||||
|
@ -48,11 +50,11 @@ func TestProvidersSchema_output(t *testing.T) {
|
|||
defer testChdir(t, td)()
|
||||
|
||||
providerSource, close := newMockProviderSource(t, map[string][]string{
|
||||
"test": []string{"1.2.3"},
|
||||
"test": {"1.2.3"},
|
||||
})
|
||||
defer close()
|
||||
|
||||
p := showFixtureProvider()
|
||||
p := providersSchemaFixtureProvider()
|
||||
ui := new(cli.MockUi)
|
||||
m := Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
|
@ -109,3 +111,45 @@ type providerSchema struct {
|
|||
ResourceSchemas map[string]interface{} `json:"resource_schemas,omitempty"`
|
||||
DataSourceSchemas map[string]interface{} `json:"data_source_schemas,omitempty"`
|
||||
}
|
||||
|
||||
// testProvider returns a mock provider that is configured for basic
|
||||
// operation with the configuration in testdata/providers-schema.
|
||||
func providersSchemaFixtureProvider() *terraform.MockProvider {
|
||||
p := testProvider()
|
||||
p.GetProviderSchemaResponse = providersSchemaFixtureSchema()
|
||||
return p
|
||||
}
|
||||
|
||||
// providersSchemaFixtureSchema returns a schema suitable for processing the
|
||||
// configuration in testdata/providers-schema.ß
|
||||
func providersSchemaFixtureSchema() *providers.GetProviderSchemaResponse {
|
||||
return &providers.GetProviderSchemaResponse{
|
||||
Provider: providers.Schema{
|
||||
Block: &configschema.Block{
|
||||
Attributes: map[string]*configschema.Attribute{
|
||||
"region": {Type: cty.String, Optional: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
ResourceTypes: map[string]providers.Schema{
|
||||
"test_instance": {
|
||||
Block: &configschema.Block{
|
||||
Attributes: map[string]*configschema.Attribute{
|
||||
"id": {Type: cty.String, Optional: true, Computed: true},
|
||||
"ami": {Type: cty.String, Optional: true},
|
||||
"volumes": {
|
||||
NestedType: &configschema.Object{
|
||||
Nesting: configschema.NestingList,
|
||||
Attributes: map[string]*configschema.Attribute{
|
||||
"size": {Type: cty.String, Required: true},
|
||||
"mount_point": {Type: cty.String, Required: true},
|
||||
},
|
||||
},
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,25 @@
|
|||
"optional": true,
|
||||
"computed": true,
|
||||
"description_kind": "plain"
|
||||
},
|
||||
"volumes": {
|
||||
"nested_type": {
|
||||
"nesting_mode": "list",
|
||||
"attributes": {
|
||||
"size": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"description_kind": "plain"
|
||||
},
|
||||
"mount_point": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"description_kind": "plain"
|
||||
}
|
||||
}
|
||||
},
|
||||
"description_kind": "plain",
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"description_kind": "plain"
|
||||
|
|
|
@ -30,6 +30,25 @@
|
|||
"optional": true,
|
||||
"computed": true,
|
||||
"description_kind": "plain"
|
||||
},
|
||||
"volumes": {
|
||||
"nested_type": {
|
||||
"nesting_mode": "list",
|
||||
"attributes": {
|
||||
"size": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"description_kind": "plain"
|
||||
},
|
||||
"mount_point": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"description_kind": "plain"
|
||||
}
|
||||
}
|
||||
},
|
||||
"description_kind": "plain",
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"description_kind": "plain"
|
||||
|
|
Loading…
Reference in New Issue