Skip to content

Commit a15e13b

Browse files
Allow overriding computed_optional_required
This can be useful, for example, when the person writing the Terraform provider does not control the contents of the OpenAPI spec. For example to set an attribute to "required": ``` attributes: overrides: name: description: The new description for name computed_optional_required: required ```
1 parent 5483b90 commit a15e13b

File tree

7 files changed

+167
-7
lines changed

7 files changed

+167
-7
lines changed

internal/config/parse.go

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ type AttributeOptions struct {
8282
type Override struct {
8383
// Description overrides the description that was mapped/merged from the OpenAPI specification.
8484
Description string `yaml:"description"`
85+
// ComputedOptionalRequired overrides the inferred value from the OpenAPI specification.
86+
ComputedOptionalRequired string `yaml:"computed_optional_required"`
8587
}
8688

8789
// ParseConfig takes in a byte array (of YAML), unmarshals into a Config struct, and validates the result

internal/explorer/config_explorer.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ func extractSchemaOptions(cfgSchemaOpts config.SchemaOptions) SchemaOptions {
211211
func extractOverrides(cfgOverrides map[string]config.Override) map[string]Override {
212212
overrides := make(map[string]Override, len(cfgOverrides))
213213
for key, cfgOverride := range cfgOverrides {
214-
overrides[key] = Override{Description: cfgOverride.Description}
214+
overrides[key] = Override{
215+
Description: cfgOverride.Description,
216+
ComputedOptionalRequired: cfgOverride.ComputedOptionalRequired,
217+
}
215218
}
216219

217220
return overrides

internal/explorer/config_explorer_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ func Test_ConfigExplorer_FindResources(t *testing.T) {
270270
},
271271
Overrides: map[string]config.Override{
272272
"test": {
273-
Description: "test description for override",
273+
Description: "test description for override",
274+
ComputedOptionalRequired: "computed_optional",
274275
},
275276
},
276277
},
@@ -310,7 +311,8 @@ func Test_ConfigExplorer_FindResources(t *testing.T) {
310311
},
311312
Overrides: map[string]explorer.Override{
312313
"test": {
313-
Description: "test description for override",
314+
Description: "test description for override",
315+
ComputedOptionalRequired: "computed_optional",
314316
},
315317
},
316318
},

internal/explorer/explorer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ type AttributeOptions struct {
5050
}
5151

5252
type Override struct {
53-
Description string
53+
Description string
54+
ComputedOptionalRequired string
5455
}

internal/mapper/attrmapper/data_source_attributes_test.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,16 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
243243
"matching overrides": {
244244
overrides: map[string]explorer.Override{
245245
"string_attribute": {
246-
Description: "new string description",
246+
Description: "new string description",
247+
ComputedOptionalRequired: "optional",
247248
},
248249
"float64_attribute": {
249-
Description: "new float64 description",
250+
Description: "new float64 description",
251+
ComputedOptionalRequired: "required",
252+
},
253+
"computed_optional_attribute": {
254+
Description: "new computed_optional",
255+
ComputedOptionalRequired: "computed_optional",
250256
},
251257
},
252258
attributes: attrmapper.DataSourceAttributes{
@@ -264,12 +270,19 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
264270
Description: pointer("old description"),
265271
},
266272
},
273+
&attrmapper.DataSourceStringAttribute{
274+
Name: "computed_optional_attribute",
275+
StringAttribute: datasource.StringAttribute{
276+
ComputedOptionalRequired: schema.Required,
277+
Description: pointer("old description"),
278+
},
279+
},
267280
},
268281
expectedAttributes: attrmapper.DataSourceAttributes{
269282
&attrmapper.DataSourceStringAttribute{
270283
Name: "string_attribute",
271284
StringAttribute: datasource.StringAttribute{
272-
ComputedOptionalRequired: schema.Required,
285+
ComputedOptionalRequired: schema.Optional,
273286
Description: pointer("new string description"),
274287
},
275288
},
@@ -280,6 +293,13 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
280293
Description: pointer("new float64 description"),
281294
},
282295
},
296+
&attrmapper.DataSourceStringAttribute{
297+
Name: "computed_optional_attribute",
298+
StringAttribute: datasource.StringAttribute{
299+
ComputedOptionalRequired: schema.ComputedOptional,
300+
Description: pointer("new computed_optional"),
301+
},
302+
},
283303
},
284304
},
285305
"matching nested overrides": {

internal/mapper/attrmapper/resource_attributes_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,102 @@ func TestResourceAttributes_ApplyOverrides(t *testing.T) {
282282
},
283283
},
284284
},
285+
"matching overrides computed": {
286+
overrides: map[string]explorer.Override{
287+
"string_attribute": {
288+
ComputedOptionalRequired: "computed",
289+
},
290+
},
291+
attributes: attrmapper.ResourceAttributes{
292+
&attrmapper.ResourceStringAttribute{
293+
Name: "string_attribute",
294+
StringAttribute: resource.StringAttribute{
295+
ComputedOptionalRequired: schema.Required,
296+
},
297+
},
298+
},
299+
expectedAttributes: attrmapper.ResourceAttributes{
300+
&attrmapper.ResourceStringAttribute{
301+
Name: "string_attribute",
302+
StringAttribute: resource.StringAttribute{
303+
ComputedOptionalRequired: schema.Computed,
304+
Description: pointer(""),
305+
},
306+
},
307+
},
308+
},
309+
"matching overrides optional": {
310+
overrides: map[string]explorer.Override{
311+
"string_attribute": {
312+
ComputedOptionalRequired: "optional",
313+
},
314+
},
315+
attributes: attrmapper.ResourceAttributes{
316+
&attrmapper.ResourceStringAttribute{
317+
Name: "string_attribute",
318+
StringAttribute: resource.StringAttribute{
319+
ComputedOptionalRequired: schema.Required,
320+
},
321+
},
322+
},
323+
expectedAttributes: attrmapper.ResourceAttributes{
324+
&attrmapper.ResourceStringAttribute{
325+
Name: "string_attribute",
326+
StringAttribute: resource.StringAttribute{
327+
ComputedOptionalRequired: schema.Optional,
328+
Description: pointer(""),
329+
},
330+
},
331+
},
332+
},
333+
"matching overrides required": {
334+
overrides: map[string]explorer.Override{
335+
"string_attribute": {
336+
ComputedOptionalRequired: "required",
337+
},
338+
},
339+
attributes: attrmapper.ResourceAttributes{
340+
&attrmapper.ResourceStringAttribute{
341+
Name: "string_attribute",
342+
StringAttribute: resource.StringAttribute{
343+
ComputedOptionalRequired: schema.Computed,
344+
},
345+
},
346+
},
347+
expectedAttributes: attrmapper.ResourceAttributes{
348+
&attrmapper.ResourceStringAttribute{
349+
Name: "string_attribute",
350+
StringAttribute: resource.StringAttribute{
351+
ComputedOptionalRequired: schema.Required,
352+
Description: pointer(""),
353+
},
354+
},
355+
},
356+
},
357+
"matching overrides computed_optional": {
358+
overrides: map[string]explorer.Override{
359+
"string_attribute": {
360+
ComputedOptionalRequired: "computed_optional",
361+
},
362+
},
363+
attributes: attrmapper.ResourceAttributes{
364+
&attrmapper.ResourceStringAttribute{
365+
Name: "string_attribute",
366+
StringAttribute: resource.StringAttribute{
367+
ComputedOptionalRequired: schema.Computed,
368+
},
369+
},
370+
},
371+
expectedAttributes: attrmapper.ResourceAttributes{
372+
&attrmapper.ResourceStringAttribute{
373+
Name: "string_attribute",
374+
StringAttribute: resource.StringAttribute{
375+
ComputedOptionalRequired: schema.ComputedOptional,
376+
Description: pointer(""),
377+
},
378+
},
379+
},
380+
},
285381
"matching nested overrides": {
286382
overrides: map[string]explorer.Override{
287383
"single_nested": {

internal/mapper/attrmapper/string.go

+36
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package attrmapper
55

66
import (
7+
"fmt"
78
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
89
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/util"
910
"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
1011
"github.com/hashicorp/terraform-plugin-codegen-spec/provider"
1112
"github.com/hashicorp/terraform-plugin-codegen-spec/resource"
13+
"github.com/hashicorp/terraform-plugin-codegen-spec/schema"
1214
)
1315

1416
type ResourceStringAttribute struct {
@@ -34,6 +36,23 @@ func (a *ResourceStringAttribute) Merge(mergeAttribute ResourceAttribute) (Resou
3436
func (a *ResourceStringAttribute) ApplyOverride(override explorer.Override) (ResourceAttribute, error) {
3537
a.Description = &override.Description
3638

39+
switch override.ComputedOptionalRequired {
40+
case "": // No override
41+
case "computed":
42+
a.ComputedOptionalRequired = schema.Computed
43+
case "optional":
44+
a.ComputedOptionalRequired = schema.Optional
45+
case "required":
46+
a.ComputedOptionalRequired = schema.Required
47+
case "computed_optional":
48+
a.ComputedOptionalRequired = schema.ComputedOptional
49+
default:
50+
return nil, fmt.Errorf(
51+
"invalid value for computed_optional_required: %s",
52+
override.ComputedOptionalRequired,
53+
)
54+
}
55+
3756
return a, nil
3857
}
3958

@@ -67,6 +86,23 @@ func (a *DataSourceStringAttribute) Merge(mergeAttribute DataSourceAttribute) (D
6786
func (a *DataSourceStringAttribute) ApplyOverride(override explorer.Override) (DataSourceAttribute, error) {
6887
a.Description = &override.Description
6988

89+
switch override.ComputedOptionalRequired {
90+
case "": // No override
91+
case "computed":
92+
a.ComputedOptionalRequired = schema.Computed
93+
case "optional":
94+
a.ComputedOptionalRequired = schema.Optional
95+
case "required":
96+
a.ComputedOptionalRequired = schema.Required
97+
case "computed_optional":
98+
a.ComputedOptionalRequired = schema.ComputedOptional
99+
default:
100+
return nil, fmt.Errorf(
101+
"invalid value for computed_optional_required: %s",
102+
override.ComputedOptionalRequired,
103+
)
104+
}
105+
70106
return a, nil
71107
}
72108

0 commit comments

Comments
 (0)