Skip to content

Commit 6732f84

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 65718c9 commit 6732f84

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
@@ -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

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
@@ -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/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
@@ -144,11 +144,12 @@ func TestResourceInt64Attribute_ApplyOverride(t *testing.T) {
144144
},
145145
override: explorer.Override{
146146
Description: "new description",
147+
ComputedOptionalRequired: string(schema.Computed),
147148
},
148149
expectedAttribute: &attrmapper.ResourceInt64Attribute{
149150
Name: "test_attribute",
150151
Int64Attribute: resource.Int64Attribute{
151-
ComputedOptionalRequired: schema.Required,
152+
ComputedOptionalRequired: schema.Computed,
152153
Description: pointer("new description"),
153154
},
154155
},

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
@@ -526,7 +526,8 @@ func TestResourceListNestedAttribute_ApplyNestedOverride(t *testing.T) {
526526
},
527527
overridePath: []string{"nested_attribute", "double_nested_attribute"},
528528
override: explorer.Override{
529-
Description: "new description",
529+
Description: "new description",
530+
ComputedOptionalRequired: string(schema.Optional),
530531
},
531532
expectedAttribute: &attrmapper.ResourceListNestedAttribute{
532533
Name: "attribute",
@@ -539,7 +540,7 @@ func TestResourceListNestedAttribute_ApplyNestedOverride(t *testing.T) {
539540
&attrmapper.ResourceStringAttribute{
540541
Name: "double_nested_attribute",
541542
StringAttribute: resource.StringAttribute{
542-
ComputedOptionalRequired: schema.Required,
543+
ComputedOptionalRequired: schema.Optional,
543544
Description: pointer("new description"),
544545
},
545546
},

internal/mapper/attrmapper/resource_attributes_test.go

Lines changed: 96 additions & 0 deletions
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/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
@@ -352,7 +352,8 @@ func TestResourceSingleNestedAttribute_ApplyOverride(t *testing.T) {
352352
},
353353
},
354354
override: explorer.Override{
355-
Description: "new description",
355+
ComputedOptionalRequired: "computed",
356+
Description: "new description",
356357
},
357358
expectedAttribute: &attrmapper.ResourceSingleNestedAttribute{
358359
Name: "test_attribute",
@@ -365,7 +366,7 @@ func TestResourceSingleNestedAttribute_ApplyOverride(t *testing.T) {
365366
},
366367
},
367368
SingleNestedAttribute: resource.SingleNestedAttribute{
368-
ComputedOptionalRequired: schema.Required,
369+
ComputedOptionalRequired: schema.Computed,
369370
Description: pointer("new description"),
370371
},
371372
},

0 commit comments

Comments
 (0)