Skip to content

Commit 2247707

Browse files
committed
send full object when config is null
1 parent ffc40fa commit 2247707

File tree

9 files changed

+247
-80
lines changed

9 files changed

+247
-80
lines changed

internal/command/query_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ func queryFixtureProvider() *testing_provider.MockProvider {
285285
},
286286
},
287287
},
288-
Nesting: configschema.NestingSingle,
288+
Nesting: configschema.NestingSingle,
289+
MinItems: 1,
290+
MaxItems: 1,
289291
},
290292
},
291293
}
@@ -306,7 +308,9 @@ func queryFixtureProvider() *testing_provider.MockProvider {
306308
},
307309
},
308310
},
309-
Nesting: configschema.NestingSingle,
311+
Nesting: configschema.NestingSingle,
312+
MinItems: 1,
313+
MaxItems: 1,
310314
},
311315
},
312316
}

internal/plugin/convert/schema.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform/internal/configs/configschema"
1212
"github.com/hashicorp/terraform/internal/providers"
1313
proto "github.com/hashicorp/terraform/internal/tfplugin5"
14+
"github.com/zclconf/go-cty/cty"
1415
)
1516

1617
// ConfigSchemaToProto takes a *configschema.Block and converts it to a
@@ -111,6 +112,39 @@ func ProtoToActionSchema(s *proto.ActionSchema) providers.ActionSchema {
111112
}
112113
}
113114

115+
func ProtoToListSchema(s *proto.Schema) providers.Schema {
116+
listSchema := ProtoToProviderSchema(s, nil)
117+
itemCount := 0
118+
// check if the provider has set some attributes as required.
119+
// When yes, then we set minItem = 1, which
120+
// validates that the configuration contains a "config" block.
121+
for _, attrS := range listSchema.Body.Attributes {
122+
if attrS.Required {
123+
itemCount = 1
124+
break
125+
}
126+
}
127+
return providers.Schema{
128+
Version: listSchema.Version,
129+
Body: &configschema.Block{
130+
Attributes: map[string]*configschema.Attribute{
131+
"data": {
132+
Type: cty.DynamicPseudoType,
133+
Computed: true,
134+
},
135+
},
136+
BlockTypes: map[string]*configschema.NestedBlock{
137+
"config": {
138+
Block: *listSchema.Body,
139+
Nesting: configschema.NestingSingle,
140+
MinItems: itemCount,
141+
MaxItems: itemCount,
142+
},
143+
},
144+
},
145+
}
146+
}
147+
114148
// ProtoToConfigSchema takes the GetSchcema_Block from a grpc response and converts it
115149
// to a terraform *configschema.Block.
116150
func ProtoToConfigSchema(b *proto.Schema_Block) *configschema.Block {

internal/plugin/grpc_provider.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"google.golang.org/grpc/status"
2121

2222
"github.com/hashicorp/terraform/internal/addrs"
23-
"github.com/hashicorp/terraform/internal/configs/configschema"
2423
"github.com/hashicorp/terraform/internal/logging"
2524
"github.com/hashicorp/terraform/internal/plugin/convert"
2625
"github.com/hashicorp/terraform/internal/providers"
@@ -171,24 +170,7 @@ func (p *GRPCProvider) GetProviderSchema() providers.GetProviderSchemaResponse {
171170
}
172171

173172
for name, list := range protoResp.ListResourceSchemas {
174-
ret := convert.ProtoToProviderSchema(list, nil)
175-
resp.ListResourceTypes[name] = providers.Schema{
176-
Version: ret.Version,
177-
Body: &configschema.Block{
178-
Attributes: map[string]*configschema.Attribute{
179-
"data": {
180-
Type: cty.DynamicPseudoType,
181-
Computed: true,
182-
},
183-
},
184-
BlockTypes: map[string]*configschema.NestedBlock{
185-
"config": {
186-
Block: *ret.Body,
187-
Nesting: configschema.NestingSingle,
188-
},
189-
},
190-
},
191-
}
173+
resp.ListResourceTypes[name] = convert.ProtoToListSchema(list)
192174
}
193175

194176
for name, action := range protoResp.ActionSchemas {

internal/plugin6/convert/schema.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,39 @@ func ProtoToActionSchema(s *proto.ActionSchema) providers.ActionSchema {
118118
}
119119
}
120120

121+
func ProtoToListSchema(s *proto.Schema) providers.Schema {
122+
listSchema := ProtoToProviderSchema(s, nil)
123+
itemCount := 0
124+
// check if the provider has set some attributes as required.
125+
// When yes, then we set minItem = 1, which
126+
// validates that the configuration contains a "config" block.
127+
for _, attrS := range listSchema.Body.Attributes {
128+
if attrS.Required {
129+
itemCount = 1
130+
break
131+
}
132+
}
133+
return providers.Schema{
134+
Version: listSchema.Version,
135+
Body: &configschema.Block{
136+
Attributes: map[string]*configschema.Attribute{
137+
"data": {
138+
Type: cty.DynamicPseudoType,
139+
Computed: true,
140+
},
141+
},
142+
BlockTypes: map[string]*configschema.NestedBlock{
143+
"config": {
144+
Block: *listSchema.Body,
145+
Nesting: configschema.NestingSingle,
146+
MinItems: itemCount,
147+
MaxItems: itemCount,
148+
},
149+
},
150+
},
151+
}
152+
}
153+
121154
func ProtoToIdentitySchema(attributes []*proto.ResourceIdentitySchema_IdentityAttribute) *configschema.Object {
122155
obj := &configschema.Object{
123156
Attributes: make(map[string]*configschema.Attribute),

internal/plugin6/grpc_provider.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"google.golang.org/grpc/status"
2121

2222
"github.com/hashicorp/terraform/internal/addrs"
23-
"github.com/hashicorp/terraform/internal/configs/configschema"
2423
"github.com/hashicorp/terraform/internal/logging"
2524
"github.com/hashicorp/terraform/internal/plugin6/convert"
2625
"github.com/hashicorp/terraform/internal/providers"
@@ -172,24 +171,7 @@ func (p *GRPCProvider) GetProviderSchema() providers.GetProviderSchemaResponse {
172171
}
173172

174173
for name, list := range protoResp.ListResourceSchemas {
175-
ret := convert.ProtoToProviderSchema(list, nil)
176-
resp.ListResourceTypes[name] = providers.Schema{
177-
Version: ret.Version,
178-
Body: &configschema.Block{
179-
Attributes: map[string]*configschema.Attribute{
180-
"data": {
181-
Type: cty.DynamicPseudoType,
182-
Computed: true,
183-
},
184-
},
185-
BlockTypes: map[string]*configschema.NestedBlock{
186-
"config": {
187-
Block: *ret.Body,
188-
Nesting: configschema.NestingSingle,
189-
},
190-
},
191-
},
192-
}
174+
resp.ListResourceTypes[name] = convert.ProtoToListSchema(list)
193175
}
194176

195177
for name, store := range protoResp.StateStoreSchemas {

0 commit comments

Comments
 (0)