Skip to content

Commit

Permalink
Allow overriding computed_optional_required
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
stuart-mclaren-hpe committed Nov 26, 2024
1 parent 5483b90 commit a15e13b
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 7 deletions.
2 changes: 2 additions & 0 deletions internal/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type AttributeOptions struct {
type Override struct {
// Description overrides the description that was mapped/merged from the OpenAPI specification.
Description string `yaml:"description"`
// ComputedOptionalRequired overrides the inferred value from the OpenAPI specification.
ComputedOptionalRequired string `yaml:"computed_optional_required"`
}

// ParseConfig takes in a byte array (of YAML), unmarshals into a Config struct, and validates the result
Expand Down
5 changes: 4 additions & 1 deletion internal/explorer/config_explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ func extractSchemaOptions(cfgSchemaOpts config.SchemaOptions) SchemaOptions {
func extractOverrides(cfgOverrides map[string]config.Override) map[string]Override {
overrides := make(map[string]Override, len(cfgOverrides))
for key, cfgOverride := range cfgOverrides {
overrides[key] = Override{Description: cfgOverride.Description}
overrides[key] = Override{
Description: cfgOverride.Description,
ComputedOptionalRequired: cfgOverride.ComputedOptionalRequired,
}
}

return overrides
Expand Down
6 changes: 4 additions & 2 deletions internal/explorer/config_explorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ func Test_ConfigExplorer_FindResources(t *testing.T) {
},
Overrides: map[string]config.Override{
"test": {
Description: "test description for override",
Description: "test description for override",
ComputedOptionalRequired: "computed_optional",
},
},
},
Expand Down Expand Up @@ -310,7 +311,8 @@ func Test_ConfigExplorer_FindResources(t *testing.T) {
},
Overrides: map[string]explorer.Override{
"test": {
Description: "test description for override",
Description: "test description for override",
ComputedOptionalRequired: "computed_optional",
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion internal/explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ type AttributeOptions struct {
}

type Override struct {
Description string
Description string
ComputedOptionalRequired string
}
26 changes: 23 additions & 3 deletions internal/mapper/attrmapper/data_source_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,16 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
"matching overrides": {
overrides: map[string]explorer.Override{
"string_attribute": {
Description: "new string description",
Description: "new string description",
ComputedOptionalRequired: "optional",
},
"float64_attribute": {
Description: "new float64 description",
Description: "new float64 description",
ComputedOptionalRequired: "required",
},
"computed_optional_attribute": {
Description: "new computed_optional",
ComputedOptionalRequired: "computed_optional",
},
},
attributes: attrmapper.DataSourceAttributes{
Expand All @@ -264,12 +270,19 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
Description: pointer("old description"),
},
},
&attrmapper.DataSourceStringAttribute{
Name: "computed_optional_attribute",
StringAttribute: datasource.StringAttribute{
ComputedOptionalRequired: schema.Required,
Description: pointer("old description"),
},
},
},
expectedAttributes: attrmapper.DataSourceAttributes{
&attrmapper.DataSourceStringAttribute{
Name: "string_attribute",
StringAttribute: datasource.StringAttribute{
ComputedOptionalRequired: schema.Required,
ComputedOptionalRequired: schema.Optional,
Description: pointer("new string description"),
},
},
Expand All @@ -280,6 +293,13 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
Description: pointer("new float64 description"),
},
},
&attrmapper.DataSourceStringAttribute{
Name: "computed_optional_attribute",
StringAttribute: datasource.StringAttribute{
ComputedOptionalRequired: schema.ComputedOptional,
Description: pointer("new computed_optional"),
},
},
},
},
"matching nested overrides": {
Expand Down
96 changes: 96 additions & 0 deletions internal/mapper/attrmapper/resource_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,102 @@ func TestResourceAttributes_ApplyOverrides(t *testing.T) {
},
},
},
"matching overrides computed": {
overrides: map[string]explorer.Override{
"string_attribute": {
ComputedOptionalRequired: "computed",
},
},
attributes: attrmapper.ResourceAttributes{
&attrmapper.ResourceStringAttribute{
Name: "string_attribute",
StringAttribute: resource.StringAttribute{
ComputedOptionalRequired: schema.Required,
},
},
},
expectedAttributes: attrmapper.ResourceAttributes{
&attrmapper.ResourceStringAttribute{
Name: "string_attribute",
StringAttribute: resource.StringAttribute{
ComputedOptionalRequired: schema.Computed,
Description: pointer(""),
},
},
},
},
"matching overrides optional": {
overrides: map[string]explorer.Override{
"string_attribute": {
ComputedOptionalRequired: "optional",
},
},
attributes: attrmapper.ResourceAttributes{
&attrmapper.ResourceStringAttribute{
Name: "string_attribute",
StringAttribute: resource.StringAttribute{
ComputedOptionalRequired: schema.Required,
},
},
},
expectedAttributes: attrmapper.ResourceAttributes{
&attrmapper.ResourceStringAttribute{
Name: "string_attribute",
StringAttribute: resource.StringAttribute{
ComputedOptionalRequired: schema.Optional,
Description: pointer(""),
},
},
},
},
"matching overrides required": {
overrides: map[string]explorer.Override{
"string_attribute": {
ComputedOptionalRequired: "required",
},
},
attributes: attrmapper.ResourceAttributes{
&attrmapper.ResourceStringAttribute{
Name: "string_attribute",
StringAttribute: resource.StringAttribute{
ComputedOptionalRequired: schema.Computed,
},
},
},
expectedAttributes: attrmapper.ResourceAttributes{
&attrmapper.ResourceStringAttribute{
Name: "string_attribute",
StringAttribute: resource.StringAttribute{
ComputedOptionalRequired: schema.Required,
Description: pointer(""),
},
},
},
},
"matching overrides computed_optional": {
overrides: map[string]explorer.Override{
"string_attribute": {
ComputedOptionalRequired: "computed_optional",
},
},
attributes: attrmapper.ResourceAttributes{
&attrmapper.ResourceStringAttribute{
Name: "string_attribute",
StringAttribute: resource.StringAttribute{
ComputedOptionalRequired: schema.Computed,
},
},
},
expectedAttributes: attrmapper.ResourceAttributes{
&attrmapper.ResourceStringAttribute{
Name: "string_attribute",
StringAttribute: resource.StringAttribute{
ComputedOptionalRequired: schema.ComputedOptional,
Description: pointer(""),
},
},
},
},
"matching nested overrides": {
overrides: map[string]explorer.Override{
"single_nested": {
Expand Down
36 changes: 36 additions & 0 deletions internal/mapper/attrmapper/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package attrmapper

import (
"fmt"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/util"
"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
"github.com/hashicorp/terraform-plugin-codegen-spec/provider"
"github.com/hashicorp/terraform-plugin-codegen-spec/resource"
"github.com/hashicorp/terraform-plugin-codegen-spec/schema"
)

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

switch override.ComputedOptionalRequired {
case "": // No override
case "computed":
a.ComputedOptionalRequired = schema.Computed
case "optional":
a.ComputedOptionalRequired = schema.Optional
case "required":
a.ComputedOptionalRequired = schema.Required
case "computed_optional":
a.ComputedOptionalRequired = schema.ComputedOptional
default:
return nil, fmt.Errorf(
"invalid value for computed_optional_required: %s",
override.ComputedOptionalRequired,
)
}

return a, nil
}

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

switch override.ComputedOptionalRequired {
case "": // No override
case "computed":
a.ComputedOptionalRequired = schema.Computed
case "optional":
a.ComputedOptionalRequired = schema.Optional
case "required":
a.ComputedOptionalRequired = schema.Required
case "computed_optional":
a.ComputedOptionalRequired = schema.ComputedOptional
default:
return nil, fmt.Errorf(
"invalid value for computed_optional_required: %s",
override.ComputedOptionalRequired,
)
}

return a, nil
}

Expand Down

0 comments on commit a15e13b

Please sign in to comment.