Skip to content

Commit facb175

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 6900187 commit facb175

13 files changed

+232
-12
lines changed

internal/config/parse.go

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ func Test_ConfigExplorer_FindResources(t *testing.T) {
271271
},
272272
Overrides: map[string]config.Override{
273273
"test": {
274-
Description: "test description for override",
274+
Description: "test description for override",
275+
ComputedOptionalRequired: "computed_optional",
275276
},
276277
},
277278
},
@@ -311,7 +312,8 @@ func Test_ConfigExplorer_FindResources(t *testing.T) {
311312
},
312313
Overrides: map[string]explorer.Override{
313314
"test": {
314-
Description: "test description for override",
315+
Description: "test description for override",
316+
ComputedOptionalRequired: "computed_optional",
315317
},
316318
},
317319
},

internal/explorer/explorer.go

Lines changed: 2 additions & 1 deletion
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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,16 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
244244
"matching overrides": {
245245
overrides: map[string]explorer.Override{
246246
"string_attribute": {
247-
Description: "new string description",
247+
Description: "new string description",
248+
ComputedOptionalRequired: "optional",
248249
},
249250
"float64_attribute": {
250-
Description: "new float64 description",
251+
Description: "new float64 description",
252+
ComputedOptionalRequired: "required",
253+
},
254+
"computed_optional_attribute": {
255+
Description: "new computed_optional",
256+
ComputedOptionalRequired: "computed_optional",
251257
},
252258
},
253259
attributes: attrmapper.DataSourceAttributes{
@@ -265,12 +271,19 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
265271
Description: pointer("old description"),
266272
},
267273
},
274+
&attrmapper.DataSourceStringAttribute{
275+
Name: "computed_optional_attribute",
276+
StringAttribute: datasource.StringAttribute{
277+
ComputedOptionalRequired: schema.Required,
278+
Description: pointer("old description"),
279+
},
280+
},
268281
},
269282
expectedAttributes: attrmapper.DataSourceAttributes{
270283
&attrmapper.DataSourceStringAttribute{
271284
Name: "string_attribute",
272285
StringAttribute: datasource.StringAttribute{
273-
ComputedOptionalRequired: schema.Required,
286+
ComputedOptionalRequired: schema.Optional,
274287
Description: pointer("new string description"),
275288
},
276289
},
@@ -281,6 +294,13 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
281294
Description: pointer("new float64 description"),
282295
},
283296
},
297+
&attrmapper.DataSourceStringAttribute{
298+
Name: "computed_optional_attribute",
299+
StringAttribute: datasource.StringAttribute{
300+
ComputedOptionalRequired: schema.ComputedOptional,
301+
Description: pointer("new computed_optional"),
302+
},
303+
},
284304
},
285305
},
286306
"matching nested overrides": {

internal/mapper/attrmapper/int64.go

Lines changed: 19 additions & 0 deletions
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 ResourceInt64Attribute struct {
@@ -34,6 +36,23 @@ func (a *ResourceInt64Attribute) Merge(mergeAttribute ResourceAttribute) (Resour
3436
func (a *ResourceInt64Attribute) 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

internal/mapper/attrmapper/int64_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ func TestResourceInt64Attribute_ApplyOverride(t *testing.T) {
145145
},
146146
override: explorer.Override{
147147
Description: "new description",
148+
ComputedOptionalRequired: string(schema.Computed),
148149
},
149150
expectedAttribute: &attrmapper.ResourceInt64Attribute{
150151
Name: "test_attribute",
151152
Int64Attribute: resource.Int64Attribute{
152-
ComputedOptionalRequired: schema.Required,
153+
ComputedOptionalRequired: schema.Computed,
153154
Description: pointer("new description"),
154155
},
155156
},

internal/mapper/attrmapper/list_nested.go

Lines changed: 19 additions & 0 deletions
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 ResourceListNestedAttribute struct {
@@ -40,6 +42,23 @@ func (a *ResourceListNestedAttribute) Merge(mergeAttribute ResourceAttribute) (R
4042
func (a *ResourceListNestedAttribute) ApplyOverride(override explorer.Override) (ResourceAttribute, error) {
4143
a.Description = &override.Description
4244

45+
switch override.ComputedOptionalRequired {
46+
case "": // No override
47+
case "computed":
48+
a.ComputedOptionalRequired = schema.Computed
49+
case "optional":
50+
a.ComputedOptionalRequired = schema.Optional
51+
case "required":
52+
a.ComputedOptionalRequired = schema.Required
53+
case "computed_optional":
54+
a.ComputedOptionalRequired = schema.ComputedOptional
55+
default:
56+
return nil, fmt.Errorf(
57+
"invalid value for computed_optional_required: %s",
58+
override.ComputedOptionalRequired,
59+
)
60+
}
61+
4362
return a, nil
4463
}
4564

internal/mapper/attrmapper/list_nested_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ func TestResourceListNestedAttribute_ApplyNestedOverride(t *testing.T) {
527527
},
528528
overridePath: []string{"nested_attribute", "double_nested_attribute"},
529529
override: explorer.Override{
530-
Description: "new description",
530+
Description: "new description",
531+
ComputedOptionalRequired: string(schema.Optional),
531532
},
532533
expectedAttribute: &attrmapper.ResourceListNestedAttribute{
533534
Name: "attribute",
@@ -540,7 +541,7 @@ func TestResourceListNestedAttribute_ApplyNestedOverride(t *testing.T) {
540541
&attrmapper.ResourceStringAttribute{
541542
Name: "double_nested_attribute",
542543
StringAttribute: resource.StringAttribute{
543-
ComputedOptionalRequired: schema.Required,
544+
ComputedOptionalRequired: schema.Optional,
544545
Description: pointer("new description"),
545546
},
546547
},

internal/mapper/attrmapper/resource_attributes_test.go

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

internal/mapper/attrmapper/single_nested.go

Lines changed: 19 additions & 0 deletions
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 ResourceSingleNestedAttribute struct {
@@ -40,6 +42,23 @@ func (a *ResourceSingleNestedAttribute) Merge(mergeAttribute ResourceAttribute)
4042
func (a *ResourceSingleNestedAttribute) ApplyOverride(override explorer.Override) (ResourceAttribute, error) {
4143
a.Description = &override.Description
4244

45+
switch override.ComputedOptionalRequired {
46+
case "": // No override
47+
case "computed":
48+
a.ComputedOptionalRequired = schema.Computed
49+
case "optional":
50+
a.ComputedOptionalRequired = schema.Optional
51+
case "required":
52+
a.ComputedOptionalRequired = schema.Required
53+
case "computed_optional":
54+
a.ComputedOptionalRequired = schema.ComputedOptional
55+
default:
56+
return nil, fmt.Errorf(
57+
"invalid value for computed_optional_required: %s",
58+
override.ComputedOptionalRequired,
59+
)
60+
}
61+
4362
return a, nil
4463
}
4564

internal/mapper/attrmapper/single_nested_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ func TestResourceSingleNestedAttribute_ApplyOverride(t *testing.T) {
353353
},
354354
},
355355
override: explorer.Override{
356-
Description: "new description",
356+
ComputedOptionalRequired: "computed",
357+
Description: "new description",
357358
},
358359
expectedAttribute: &attrmapper.ResourceSingleNestedAttribute{
359360
Name: "test_attribute",
@@ -366,7 +367,7 @@ func TestResourceSingleNestedAttribute_ApplyOverride(t *testing.T) {
366367
},
367368
},
368369
SingleNestedAttribute: resource.SingleNestedAttribute{
369-
ComputedOptionalRequired: schema.Required,
370+
ComputedOptionalRequired: schema.Computed,
370371
Description: pointer("new description"),
371372
},
372373
},

0 commit comments

Comments
 (0)