Skip to content

Commit 5a3df21

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 909314b commit 5a3df21

File tree

6 files changed

+127
-4
lines changed

6 files changed

+127
-4
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/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/string.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 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

0 commit comments

Comments
 (0)