Skip to content

Feat/schema overrides #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type AttributeOptions struct {
type Override struct {
// Description overrides the description that was mapped/merged from the OpenAPI specification.
Description string `yaml:"description"`
// Schema overrides the schema that was mapped/merged from the OpenAPI specification.
Schema SchemaOptions `yaml:"schema"`
// A power tool to perform any arbitrary override of or insertion - not validated against OpenAPI spec.
CustomOverrides map[string]string `yaml:"custom_overrides"`
}

// ParseConfig takes in a byte array (of YAML), unmarshals into a Config struct, and validates the result
Expand Down
14 changes: 8 additions & 6 deletions internal/explorer/config_explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,22 @@ func extractSchemaProxy(document high.Document, componentRef string) (*highbase.
return highbase.CreateSchemaProxy(highSchema), nil
}

func extractSchemaOptions(cfgSchemaOpts config.SchemaOptions) SchemaOptions {
return SchemaOptions{
func extractSchemaOptions(cfgSchemaOpts config.SchemaOptions) config.SchemaOptions {
return config.SchemaOptions{
Ignores: cfgSchemaOpts.Ignores,
AttributeOptions: AttributeOptions{
AttributeOptions: config.AttributeOptions{
Aliases: cfgSchemaOpts.AttributeOptions.Aliases,
Overrides: extractOverrides(cfgSchemaOpts.AttributeOptions.Overrides),
},
}
}

func extractOverrides(cfgOverrides map[string]config.Override) map[string]Override {
overrides := make(map[string]Override, len(cfgOverrides))
func extractOverrides(cfgOverrides map[string]config.Override) map[string]config.Override {
overrides := make(map[string]config.Override, len(cfgOverrides))
for key, cfgOverride := range cfgOverrides {
overrides[key] = Override{Description: cfgOverride.Description}
overrides[key] = config.Override{Description: cfgOverride.Description}
overrides[key] = config.Override{CustomOverrides: cfgOverride.CustomOverrides}
overrides[key] = config.Override{Schema: cfgOverride.Schema}
}

return overrides
Expand Down
36 changes: 18 additions & 18 deletions internal/explorer/config_explorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func Test_ConfigExplorer_FindResources(t *testing.T) {
Description: "delete op here",
OperationId: "delete_resource",
},
SchemaOptions: explorer.SchemaOptions{
AttributeOptions: explorer.AttributeOptions{
Overrides: map[string]explorer.Override{},
SchemaOptions: config.SchemaOptions{
AttributeOptions: config.AttributeOptions{
Overrides: map[string]config.Override{},
},
},
},
Expand Down Expand Up @@ -165,9 +165,9 @@ func Test_ConfigExplorer_FindResources(t *testing.T) {
Description: "delete op here",
OperationId: "delete_resource",
},
SchemaOptions: explorer.SchemaOptions{
AttributeOptions: explorer.AttributeOptions{
Overrides: map[string]explorer.Override{},
SchemaOptions: config.SchemaOptions{
AttributeOptions: config.AttributeOptions{
Overrides: map[string]config.Override{},
},
},
},
Expand Down Expand Up @@ -302,13 +302,13 @@ func Test_ConfigExplorer_FindResources(t *testing.T) {
Description: "read op here",
OperationId: "read_resource",
},
SchemaOptions: explorer.SchemaOptions{
SchemaOptions: config.SchemaOptions{
Ignores: []string{"ignore1.abc", "ignore2.def"},
AttributeOptions: explorer.AttributeOptions{
AttributeOptions: config.AttributeOptions{
Aliases: map[string]string{
"otherId": "id",
},
Overrides: map[string]explorer.Override{
Overrides: map[string]config.Override{
"test": {
Description: "test description for override",
},
Expand Down Expand Up @@ -382,9 +382,9 @@ func Test_ConfigExplorer_FindDataSources(t *testing.T) {
Description: "read op here",
OperationId: "read_resource",
},
SchemaOptions: explorer.SchemaOptions{
AttributeOptions: explorer.AttributeOptions{
Overrides: map[string]explorer.Override{},
SchemaOptions: config.SchemaOptions{
AttributeOptions: config.AttributeOptions{
Overrides: map[string]config.Override{},
},
},
},
Expand Down Expand Up @@ -415,9 +415,9 @@ func Test_ConfigExplorer_FindDataSources(t *testing.T) {
Description: "read op here",
OperationId: "read_resource",
},
SchemaOptions: explorer.SchemaOptions{
AttributeOptions: explorer.AttributeOptions{
Overrides: map[string]explorer.Override{},
SchemaOptions: config.SchemaOptions{
AttributeOptions: config.AttributeOptions{
Overrides: map[string]config.Override{},
},
},
},
Expand Down Expand Up @@ -496,13 +496,13 @@ func Test_ConfigExplorer_FindDataSources(t *testing.T) {
Description: "read op here",
OperationId: "read_resource",
},
SchemaOptions: explorer.SchemaOptions{
SchemaOptions: config.SchemaOptions{
Ignores: []string{"ignore1.abc", "ignore2.def"},
AttributeOptions: explorer.AttributeOptions{
AttributeOptions: config.AttributeOptions{
Aliases: map[string]string{
"otherId": "id",
},
Overrides: map[string]explorer.Override{
Overrides: map[string]config.Override{
"test": {
Description: "test description for override",
},
Expand Down
20 changes: 4 additions & 16 deletions internal/explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package explorer

import (
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
"github.com/pb33f/libopenapi/datamodel/high/base"

high "github.com/pb33f/libopenapi/datamodel/high/v3"
)

Expand All @@ -22,14 +24,14 @@ type Resource struct {
UpdateOp *high.Operation
DeleteOp *high.Operation
CommonParameters []*high.Parameter
SchemaOptions SchemaOptions
SchemaOptions config.SchemaOptions
}

// DataSource contains a Read operation and schema options for configuration.
type DataSource struct {
ReadOp *high.Operation
CommonParameters []*high.Parameter
SchemaOptions SchemaOptions
SchemaOptions config.SchemaOptions
}

// Provider contains a name and a schema.
Expand All @@ -38,17 +40,3 @@ type Provider struct {
SchemaProxy *base.SchemaProxy
Ignores []string
}

type SchemaOptions struct {
Ignores []string
AttributeOptions AttributeOptions
}

type AttributeOptions struct {
Aliases map[string]string
Overrides map[string]Override
}

type Override struct {
Description string
}
6 changes: 3 additions & 3 deletions internal/mapper/attrmapper/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package attrmapper

import (
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
"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"
Expand All @@ -31,7 +31,7 @@ func (a *ResourceBoolAttribute) Merge(mergeAttribute ResourceAttribute) (Resourc
return a, nil
}

func (a *ResourceBoolAttribute) ApplyOverride(override explorer.Override) (ResourceAttribute, error) {
func (a *ResourceBoolAttribute) ApplyOverride(override config.Override) (ResourceAttribute, error) {
a.Description = &override.Description

return a, nil
Expand Down Expand Up @@ -64,7 +64,7 @@ func (a *DataSourceBoolAttribute) Merge(mergeAttribute DataSourceAttribute) (Dat
return a, nil
}

func (a *DataSourceBoolAttribute) ApplyOverride(override explorer.Override) (DataSourceAttribute, error) {
func (a *DataSourceBoolAttribute) ApplyOverride(override config.Override) (DataSourceAttribute, error) {
a.Description = &override.Description

return a, nil
Expand Down
10 changes: 5 additions & 5 deletions internal/mapper/attrmapper/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/attrmapper"
"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
"github.com/hashicorp/terraform-plugin-codegen-spec/resource"
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestResourceBoolAttribute_ApplyOverride(t *testing.T) {

testCases := map[string]struct {
attribute attrmapper.ResourceBoolAttribute
override explorer.Override
override config.Override
expectedAttribute attrmapper.ResourceAttribute
}{
"override description": {
Expand All @@ -142,7 +142,7 @@ func TestResourceBoolAttribute_ApplyOverride(t *testing.T) {
Description: pointer("old description"),
},
},
override: explorer.Override{
override: config.Override{
Description: "new description",
},
expectedAttribute: &attrmapper.ResourceBoolAttribute{
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestDataSourceBoolAttribute_ApplyOverride(t *testing.T) {

testCases := map[string]struct {
attribute attrmapper.DataSourceBoolAttribute
override explorer.Override
override config.Override
expectedAttribute attrmapper.DataSourceAttribute
}{
"override description": {
Expand All @@ -296,7 +296,7 @@ func TestDataSourceBoolAttribute_ApplyOverride(t *testing.T) {
Description: pointer("old description"),
},
},
override: explorer.Override{
override: config.Override{
Description: "new description",
},
expectedAttribute: &attrmapper.DataSourceBoolAttribute{
Expand Down
10 changes: 5 additions & 5 deletions internal/mapper/attrmapper/data_source_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"errors"
"strings"

"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
)

type DataSourceAttribute interface {
GetName() string
Merge(DataSourceAttribute) (DataSourceAttribute, error)
ApplyOverride(explorer.Override) (DataSourceAttribute, error)
ApplyOverride(config.Override) (DataSourceAttribute, error)
ToSpec() datasource.Attribute
}

type DataSourceNestedAttribute interface {
ApplyNestedOverride([]string, explorer.Override) (DataSourceAttribute, error)
ApplyNestedOverride([]string, config.Override) (DataSourceAttribute, error)
}

type DataSourceAttributes []DataSourceAttribute
Expand Down Expand Up @@ -67,7 +67,7 @@ func (attributes DataSourceAttributes) ToSpec() []datasource.Attribute {
return specAttributes
}

func (attributes DataSourceAttributes) ApplyOverrides(overrideMap map[string]explorer.Override) (DataSourceAttributes, error) {
func (attributes DataSourceAttributes) ApplyOverrides(overrideMap map[string]config.Override) (DataSourceAttributes, error) {
var errResult error
for key, override := range overrideMap {
var err error
Expand All @@ -78,7 +78,7 @@ func (attributes DataSourceAttributes) ApplyOverrides(overrideMap map[string]exp
return attributes, errResult
}

func (attributes DataSourceAttributes) ApplyOverride(path []string, override explorer.Override) (DataSourceAttributes, error) {
func (attributes DataSourceAttributes) ApplyOverride(path []string, override config.Override) (DataSourceAttributes, error) {
var errResult error
if len(path) == 0 {
return attributes, errResult
Expand Down
10 changes: 5 additions & 5 deletions internal/mapper/attrmapper/data_source_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/attrmapper"
"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
"github.com/hashicorp/terraform-plugin-codegen-spec/schema"
Expand Down Expand Up @@ -204,13 +204,13 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
overrides map[string]explorer.Override
overrides map[string]config.Override
attributes attrmapper.DataSourceAttributes
expectedAttributes attrmapper.DataSourceAttributes
}{
// TODO: this may eventually return an error, but for now just returns without modification
"no matching overrides": {
overrides: map[string]explorer.Override{
overrides: map[string]config.Override{
"": {
Description: "new description",
},
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
},
},
"matching overrides": {
overrides: map[string]explorer.Override{
overrides: map[string]config.Override{
"string_attribute": {
Description: "new string description",
},
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestDataSourceAttributes_ApplyOverrides(t *testing.T) {
},
},
"matching nested overrides": {
overrides: map[string]explorer.Override{
overrides: map[string]config.Override{
"single_nested": {
Description: "new description",
},
Expand Down
6 changes: 3 additions & 3 deletions internal/mapper/attrmapper/float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package attrmapper

import (
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
"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"
Expand All @@ -31,7 +31,7 @@ func (a *ResourceFloat64Attribute) Merge(mergeAttribute ResourceAttribute) (Reso
return a, nil
}

func (a *ResourceFloat64Attribute) ApplyOverride(override explorer.Override) (ResourceAttribute, error) {
func (a *ResourceFloat64Attribute) ApplyOverride(override config.Override) (ResourceAttribute, error) {
a.Description = &override.Description

return a, nil
Expand Down Expand Up @@ -64,7 +64,7 @@ func (a *DataSourceFloat64Attribute) Merge(mergeAttribute DataSourceAttribute) (
return a, nil
}

func (a *DataSourceFloat64Attribute) ApplyOverride(override explorer.Override) (DataSourceAttribute, error) {
func (a *DataSourceFloat64Attribute) ApplyOverride(override config.Override) (DataSourceAttribute, error) {
a.Description = &override.Description

return a, nil
Expand Down
10 changes: 5 additions & 5 deletions internal/mapper/attrmapper/float64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/attrmapper"
"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
"github.com/hashicorp/terraform-plugin-codegen-spec/resource"
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestResourceFloat64Attribute_ApplyOverride(t *testing.T) {

testCases := map[string]struct {
attribute attrmapper.ResourceFloat64Attribute
override explorer.Override
override config.Override
expectedAttribute attrmapper.ResourceAttribute
}{
"override description": {
Expand All @@ -142,7 +142,7 @@ func TestResourceFloat64Attribute_ApplyOverride(t *testing.T) {
Description: pointer("old description"),
},
},
override: explorer.Override{
override: config.Override{
Description: "new description",
},
expectedAttribute: &attrmapper.ResourceFloat64Attribute{
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestDataSourceFloat64Attribute_ApplyOverride(t *testing.T) {

testCases := map[string]struct {
attribute attrmapper.DataSourceFloat64Attribute
override explorer.Override
override config.Override
expectedAttribute attrmapper.DataSourceAttribute
}{
"override description": {
Expand All @@ -296,7 +296,7 @@ func TestDataSourceFloat64Attribute_ApplyOverride(t *testing.T) {
Description: pointer("old description"),
},
},
override: explorer.Override{
override: config.Override{
Description: "new description",
},
expectedAttribute: &attrmapper.DataSourceFloat64Attribute{
Expand Down
Loading