From 4af4852bba5fdbb804b936f5ab5055fefd2f46f4 Mon Sep 17 00:00:00 2001 From: Agustin Bettati Date: Fri, 18 Oct 2024 18:12:09 +0200 Subject: [PATCH] fix generation --- tools/codegen/config.yml | 4 +- tools/codegen/schema/schema_attribute.go | 68 ++++++------------- ...ttribute.go => schema_attribute_nested.go} | 0 ...ibute.go => schema_attribute_primitive.go} | 0 .../schema/schema_attribute_timeout.go | 33 +++++++++ .../schema/testdata/timeouts.golden.go | 8 ++- 6 files changed, 63 insertions(+), 50 deletions(-) rename tools/codegen/schema/{schema_nested_attribute.go => schema_attribute_nested.go} (100%) rename tools/codegen/schema/{schema_primitive_attribute.go => schema_attribute_primitive.go} (100%) create mode 100644 tools/codegen/schema/schema_attribute_timeout.go diff --git a/tools/codegen/config.yml b/tools/codegen/config.yml index 2f2e500cd3..1c8d6ccfa1 100644 --- a/tools/codegen/config.yml +++ b/tools/codegen/config.yml @@ -32,7 +32,7 @@ resources: optional: true computed: true - timeouts: ["create", "read", "delete"] + timeouts: ["create", "update", "delete"] search_deployment: read: @@ -45,4 +45,4 @@ resources: aliases: group_id: project_id ignores: ["links"] - timeouts: ["create", "read", "delete"] + timeouts: ["create", "update", "delete"] diff --git a/tools/codegen/schema/schema_attribute.go b/tools/codegen/schema/schema_attribute.go index c56847ef3e..767119a8d9 100644 --- a/tools/codegen/schema/schema_attribute.go +++ b/tools/codegen/schema/schema_attribute.go @@ -26,38 +26,17 @@ type attributeGenerator interface { AttributeCode() CodeStatement } -type TimeoutAttributeGenerator struct { - timeouts codespec.TimeoutsAttribute -} - -func (s *TimeoutAttributeGenerator) AttributeCode() CodeStatement { - var optionProperties string - for op := range s.timeouts.ConfigurableTimeouts { - switch op { - case int(codespec.Create): - optionProperties += "Create: true," - case int(codespec.Update): - optionProperties += "Update: true," - case int(codespec.Delete): - optionProperties += "Delete: true," - case int(codespec.Read): - optionProperties += "Read: true," - } - } - return CodeStatement{ - Code: fmt.Sprintf(`"timeouts": timeouts.Attributes(ctx, timeouts.Opts{ - %s - })`, optionProperties), - Imports: []string{"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"}, - } -} - -type ConventionalAttributeGenerator struct { +type conventionalAttributeGenerator struct { typeSpecificCode convetionalTypeSpecificCodeGenerator attribute codespec.Attribute } -func (s *ConventionalAttributeGenerator) AttributeCode() CodeStatement { +type convetionalTypeSpecificCodeGenerator interface { + TypeDefinition() string + TypeSpecificProperties() []CodeStatement +} + +func (s *conventionalAttributeGenerator) AttributeCode() CodeStatement { typeDefinition := s.typeSpecificCode.TypeDefinition() additionalPropertyStatements := s.typeSpecificCode.TypeSpecificProperties() @@ -99,86 +78,83 @@ func commonProperties(attr *codespec.Attribute) []string { return result } -type convetionalTypeSpecificCodeGenerator interface { - TypeDefinition() string - TypeSpecificProperties() []CodeStatement -} - func generator(attr *codespec.Attribute) attributeGenerator { if attr.Int64 != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &Int64AttrGenerator{model: *attr.Int64}, attribute: *attr, } } if attr.Float64 != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &Float64AttrGenerator{model: *attr.Float64}, attribute: *attr, } } if attr.String != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &StringAttrGenerator{model: *attr.String}, attribute: *attr, } } if attr.Bool != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &BoolAttrGenerator{model: *attr.Bool}, attribute: *attr, } } if attr.List != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &ListAttrGenerator{model: *attr.List}, attribute: *attr, } } if attr.ListNested != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &ListNestedAttrGenerator{model: *attr.ListNested}, attribute: *attr, } } if attr.Map != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &MapAttrGenerator{model: *attr.Map}, attribute: *attr, } } if attr.MapNested != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &MapNestedAttrGenerator{model: *attr.MapNested}, attribute: *attr, } } if attr.Number != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &NumberAttrGenerator{model: *attr.Number}, attribute: *attr, } } if attr.Set != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &SetAttrGenerator{model: *attr.Set}, attribute: *attr, } } if attr.SetNested != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &SetNestedGenerator{model: *attr.SetNested}, attribute: *attr, } } if attr.SingleNested != nil { - return &ConventionalAttributeGenerator{ + return &conventionalAttributeGenerator{ typeSpecificCode: &SingleNestedAttrGenerator{model: *attr.SingleNested}, attribute: *attr, } } if attr.Timeouts != nil { - return &TimeoutAttributeGenerator{} + return &timeoutAttributeGenerator{ + timeouts: *attr.Timeouts, + } } panic("Attribute with unknown type defined when generating schema attribute") } diff --git a/tools/codegen/schema/schema_nested_attribute.go b/tools/codegen/schema/schema_attribute_nested.go similarity index 100% rename from tools/codegen/schema/schema_nested_attribute.go rename to tools/codegen/schema/schema_attribute_nested.go diff --git a/tools/codegen/schema/schema_primitive_attribute.go b/tools/codegen/schema/schema_attribute_primitive.go similarity index 100% rename from tools/codegen/schema/schema_primitive_attribute.go rename to tools/codegen/schema/schema_attribute_primitive.go diff --git a/tools/codegen/schema/schema_attribute_timeout.go b/tools/codegen/schema/schema_attribute_timeout.go new file mode 100644 index 0000000000..1cd92e0cab --- /dev/null +++ b/tools/codegen/schema/schema_attribute_timeout.go @@ -0,0 +1,33 @@ +package schema + +import ( + "fmt" + + "github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/codespec" +) + +type timeoutAttributeGenerator struct { + timeouts codespec.TimeoutsAttribute +} + +func (s *timeoutAttributeGenerator) AttributeCode() CodeStatement { + var optionProperties string + for op := range s.timeouts.ConfigurableTimeouts { + switch op { + case int(codespec.Create): + optionProperties += "Create: true,\n" + case int(codespec.Update): + optionProperties += "Update: true,\n" + case int(codespec.Delete): + optionProperties += "Delete: true,\n" + case int(codespec.Read): + optionProperties += "Read: true,\n" + } + } + return CodeStatement{ + Code: fmt.Sprintf(`"timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + %s + })`, optionProperties), + Imports: []string{"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"}, + } +} diff --git a/tools/codegen/schema/testdata/timeouts.golden.go b/tools/codegen/schema/testdata/timeouts.golden.go index 77f041d40b..5efeab73a8 100644 --- a/tools/codegen/schema/testdata/timeouts.golden.go +++ b/tools/codegen/schema/testdata/timeouts.golden.go @@ -1,4 +1,4 @@ -package test_name +package testname import ( "context" @@ -10,7 +10,11 @@ import ( func ResourceSchema(ctx context.Context) schema.Schema { return schema.Schema{ Attributes: map[string]schema.Attribute{ - "timeouts": timeouts.Attributes(ctx, timeouts.Opts{}), + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + Create: true, + Update: true, + Read: true, + }), }, } }