diff --git a/tools/codegen/codespec/api_to_provider_spec_mapper_test.go b/tools/codegen/codespec/api_to_provider_spec_mapper_test.go index c9839763ef..53daf21217 100644 --- a/tools/codegen/codespec/api_to_provider_spec_mapper_test.go +++ b/tools/codegen/codespec/api_to_provider_spec_mapper_test.go @@ -337,6 +337,12 @@ func TestConvertToProviderSpec_nested_schemaOverrides(t *testing.T) { }, }, }, + { + Name: "timeouts", + Timeouts: &codespec.TimeoutsAttribute{ + ConfigurableTimeouts: []codespec.Operation{codespec.Create, codespec.Read, codespec.Update, codespec.Delete}, + }, + }, }, }, Name: "test_resource_with_nested_attr_overrides", diff --git a/tools/codegen/codespec/config.go b/tools/codegen/codespec/config.go index b63b789b45..f935472bad 100644 --- a/tools/codegen/codespec/config.go +++ b/tools/codegen/codespec/config.go @@ -1,6 +1,7 @@ package codespec import ( + "log" "strings" "github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/config" @@ -33,6 +34,10 @@ func applySchemaOptions(schemaOptions config.SchemaOptions, attributes *Attribut finalAttributes = append(finalAttributes, *attr) } + if timeoutAttr := applyTimeoutConfig(schemaOptions); parentName == "" && timeoutAttr != nil { // will not run for nested attributes + finalAttributes = append(finalAttributes, *timeoutAttr) + } + *attributes = finalAttributes } @@ -91,3 +96,28 @@ func processNestedAttributes(attr *Attribute, schemaOptions config.SchemaOptions applySchemaOptions(schemaOptions, &attr.MapNested.NestedObject.Attributes, attrPathName) } } + +func applyTimeoutConfig(options config.SchemaOptions) *Attribute { + var result []Operation + for _, op := range options.Timeouts { + switch op { + case "create": + result = append(result, Create) + case "read": + result = append(result, Read) + case "delete": + result = append(result, Delete) + case "update": + result = append(result, Update) + default: + log.Printf("[WARN] Unknown operation type defined in timeout configuration: %s", op) + } + } + if result != nil { + return &Attribute{ + Name: "timeouts", + Timeouts: &TimeoutsAttribute{ConfigurableTimeouts: result}, + } + } + return nil +} diff --git a/tools/codegen/codespec/model.go b/tools/codegen/codespec/model.go index ddeab098ac..ac86367ca1 100644 --- a/tools/codegen/codespec/model.go +++ b/tools/codegen/codespec/model.go @@ -44,6 +44,7 @@ type Attribute struct { Set *SetAttribute Int64 *Int64Attribute SingleNested *SingleNestedAttribute + Timeouts *TimeoutsAttribute Description *string Name SnakeCaseString @@ -99,6 +100,19 @@ type NestedAttributeObject struct { Attributes Attributes } +type TimeoutsAttribute struct { + ConfigurableTimeouts []Operation +} + +type Operation int + +const ( + Create Operation = iota + Update + Read + Delete +) + type CustomDefault struct { Definition string Imports []string diff --git a/tools/codegen/codespec/testdata/config-nested-schema-overrides.yml b/tools/codegen/codespec/testdata/config-nested-schema-overrides.yml index 39f9e9cfb5..95b2f7a9ee 100644 --- a/tools/codegen/codespec/testdata/config-nested-schema-overrides.yml +++ b/tools/codegen/codespec/testdata/config-nested-schema-overrides.yml @@ -24,4 +24,5 @@ resources: nested_list_array_attr.inner_num_attr_alias: description: "Overridden inner_num_attr_alias description" outer_object.nested_level1.level_field1_alias: - description: "Overridden level_field1_alias description" \ No newline at end of file + description: "Overridden level_field1_alias description" + timeouts: ["create", "read", "update", "delete"] diff --git a/tools/codegen/codespec/testdata/config-no-schema-opts.yml b/tools/codegen/codespec/testdata/config-no-schema-opts.yml index 700bb6eb4b..5a9921e8d5 100644 --- a/tools/codegen/codespec/testdata/config-no-schema-opts.yml +++ b/tools/codegen/codespec/testdata/config-no-schema-opts.yml @@ -11,4 +11,4 @@ resources: method: PATCH delete: path: /api/atlas/v2/groups/{groupId}/testResource - method: DELETE \ No newline at end of file + method: DELETE diff --git a/tools/codegen/codespec/testdata/config.yml b/tools/codegen/codespec/testdata/config.yml index 00a103f7ba..661116eb9a 100644 --- a/tools/codegen/codespec/testdata/config.yml +++ b/tools/codegen/codespec/testdata/config.yml @@ -33,4 +33,4 @@ resources: optional: true computed: true - timeouts: ["create", "read", "delete"] \ No newline at end of file + timeouts: ["create", "read", "delete"] diff --git a/tools/codegen/config.yml b/tools/codegen/config.yml index 2f2e500cd3..fcd9a73079 100644 --- a/tools/codegen/config.yml +++ b/tools/codegen/config.yml @@ -9,30 +9,27 @@ resources: schema: aliases: group_id: project_id - ignores: ["links"] + timeouts: ["create", "update", "delete"] - overrides: - project_id: - description: "Unique 24-hexadecimal digit string that identifies your project." - plan_modifiers: [{ - imports: [ "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" ], - definition: "stringplanmodifier.RequiresReplace()" - }] - validators: [{ - imports: [ - "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator", - "github.com/hashicorp/terraform-plugin-framework/path" - ], - definition: "stringvalidator.ConflictsWith(path.MatchRoot(\"name\"))" - }] - - prefix_path: - computability: - optional: true - computed: true + # overrides: + # project_id: + # plan_modifiers: [{ + # imports: [ "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" ], + # definition: "stringplanmodifier.RequiresReplace()" + # }] + # validators: [{ + # imports: [ + # "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator", + # "github.com/hashicorp/terraform-plugin-framework/path" + # ], + # definition: "stringvalidator.ConflictsWith(path.MatchRoot(\"name\"))" + # }] - timeouts: ["create", "read", "delete"] + # prefix_path: + # computability: + # optional: true + # computed: true search_deployment: read: @@ -45,4 +42,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 76c70aa71a..a99b7012a1 100644 --- a/tools/codegen/schema/schema_attribute.go +++ b/tools/codegen/schema/schema_attribute.go @@ -11,7 +11,7 @@ func GenerateSchemaAttributes(attrs codespec.Attributes) CodeStatement { attrsCode := []string{} imports := []string{} for i := range attrs { - result := generateAttribute(&attrs[i]) + result := generator(&attrs[i]).AttributeCode() attrsCode = append(attrsCode, result.Code) imports = append(imports, result.Imports...) } @@ -22,24 +22,102 @@ func GenerateSchemaAttributes(attrs codespec.Attributes) CodeStatement { } } -func generateAttribute(attr *codespec.Attribute) CodeStatement { - generator := typeGenerator(attr) +type attributeGenerator interface { + AttributeCode() CodeStatement +} - typeDefinition := generator.TypeDefinition() - additionalPropertyStatements := generator.TypeSpecificProperties() +func generator(attr *codespec.Attribute) attributeGenerator { + if attr.Int64 != nil { + return &Int64AttrGenerator{intModel: *attr.Int64, attr: *attr} + } + if attr.Float64 != nil { + return &Float64AttrGenerator{ + floatModel: *attr.Float64, + attr: *attr, + } + } + if attr.String != nil { + return &StringAttrGenerator{ + stringModel: *attr.String, + attr: *attr, + } + } + if attr.Bool != nil { + return &BoolAttrGenerator{ + boolModel: *attr.Bool, + attr: *attr, + } + } + if attr.List != nil { + return &ListAttrGenerator{ + listModel: *attr.List, + attr: *attr, + } + } + if attr.ListNested != nil { + return &ListNestedAttrGenerator{ + listNestedModel: *attr.ListNested, + attr: *attr, + } + } + if attr.Map != nil { + return &MapAttrGenerator{ + mapModel: *attr.Map, + attr: *attr, + } + } + if attr.MapNested != nil { + return &MapNestedAttrGenerator{ + mapNestedModel: *attr.MapNested, + attr: *attr, + } + } + if attr.Number != nil { + return &NumberAttrGenerator{ + numberModel: *attr.Number, + attr: *attr, + } + } + if attr.Set != nil { + return &SetAttrGenerator{ + setModel: *attr.Set, + attr: *attr, + } + } + if attr.SetNested != nil { + return &SetNestedGenerator{ + setNestedModel: *attr.SetNested, + attr: *attr, + } + } + if attr.SingleNested != nil { + return &SingleNestedAttrGenerator{ + singleNestedModel: *attr.SingleNested, + attr: *attr, + } + } + if attr.Timeouts != nil { + return &timeoutAttributeGenerator{ + timeouts: *attr.Timeouts, + } + } + panic("Attribute with unknown type defined when generating schema attribute") +} +// generation of conventional attribute types which have common properties like MarkdownDescription, Computed/Optional/Required, Sensitive +func commonAttrStructure(attr *codespec.Attribute, typeDef string, specificProperties []CodeStatement) CodeStatement { properties := commonProperties(attr) - imports := []string{"github.com/hashicorp/terraform-plugin-framework/resource/schema"} - for i := range additionalPropertyStatements { - properties = append(properties, additionalPropertyStatements[i].Code) - imports = append(imports, additionalPropertyStatements[i].Imports...) + imports := []string{} + for i := range specificProperties { + properties = append(properties, specificProperties[i].Code) + imports = append(imports, specificProperties[i].Imports...) } name := attr.Name propsResultString := strings.Join(properties, ",\n") + "," code := fmt.Sprintf(`"%s": %s{ %s - }`, name, typeDefinition, propsResultString) + }`, name, typeDef, propsResultString) return CodeStatement{ Code: code, Imports: imports, @@ -65,48 +143,3 @@ func commonProperties(attr *codespec.Attribute) []string { } return result } - -type schemaAttributeGenerator interface { - TypeDefinition() string - TypeSpecificProperties() []CodeStatement -} - -func typeGenerator(attr *codespec.Attribute) schemaAttributeGenerator { - if attr.Int64 != nil { - return &Int64AttrGenerator{model: *attr.Int64} - } - if attr.Float64 != nil { - return &Float64AttrGenerator{model: *attr.Float64} - } - if attr.String != nil { - return &StringAttrGenerator{model: *attr.String} - } - if attr.Bool != nil { - return &BoolAttrGenerator{model: *attr.Bool} - } - if attr.List != nil { - return &ListAttrGenerator{model: *attr.List} - } - if attr.ListNested != nil { - return &ListNestedAttrGenerator{model: *attr.ListNested} - } - if attr.Map != nil { - return &MapAttrGenerator{model: *attr.Map} - } - if attr.MapNested != nil { - return &MapNestedAttrGenerator{model: *attr.MapNested} - } - if attr.Number != nil { - return &NumberAttrGenerator{model: *attr.Number} - } - if attr.Set != nil { - return &SetAttrGenerator{model: *attr.Set} - } - if attr.SetNested != nil { - return &SetNestedGenerator{model: *attr.SetNested} - } - if attr.SingleNested != nil { - return &SingleNestedAttrGenerator{model: *attr.SingleNested} - } - panic("Attribute with unknown type defined") -} diff --git a/tools/codegen/schema/schema_attribute_nested.go b/tools/codegen/schema/schema_attribute_nested.go new file mode 100644 index 0000000000..90a283c260 --- /dev/null +++ b/tools/codegen/schema/schema_attribute_nested.go @@ -0,0 +1,65 @@ +package schema + +import ( + "fmt" + + "github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/codespec" +) + +type ListNestedAttrGenerator struct { + attr codespec.Attribute + listNestedModel codespec.ListNestedAttribute +} + +func (l *ListNestedAttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&l.attr, "schema.ListNestedAttribute", []CodeStatement{nestedObjectProperty(l.listNestedModel.NestedObject)}) +} + +type SetNestedGenerator struct { + attr codespec.Attribute + setNestedModel codespec.SetNestedAttribute +} + +func (l *SetNestedGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&l.attr, "schema.SetNestedAttribute", []CodeStatement{nestedObjectProperty(l.setNestedModel.NestedObject)}) +} + +type MapNestedAttrGenerator struct { + attr codespec.Attribute + mapNestedModel codespec.MapNestedAttribute +} + +func (m *MapNestedAttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&m.attr, "schema.MapNestedAttribute", []CodeStatement{nestedObjectProperty(m.mapNestedModel.NestedObject)}) +} + +type SingleNestedAttrGenerator struct { + attr codespec.Attribute + singleNestedModel codespec.SingleNestedAttribute +} + +func (l *SingleNestedAttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&l.attr, "schema.SingleNestedAttribute", []CodeStatement{attributesProperty(l.singleNestedModel.NestedObject)}) +} + +func attributesProperty(nested codespec.NestedAttributeObject) CodeStatement { + attrs := GenerateSchemaAttributes(nested.Attributes) + attributeProperty := fmt.Sprintf(`Attributes: map[string]schema.Attribute{ + %s + }`, attrs.Code) + return CodeStatement{ + Code: attributeProperty, + Imports: attrs.Imports, + } +} + +func nestedObjectProperty(nested codespec.NestedAttributeObject) CodeStatement { + result := attributesProperty(nested) + nestedObj := fmt.Sprintf(`NestedObject: schema.NestedAttributeObject{ + %s, + }`, result.Code) + return CodeStatement{ + Code: nestedObj, + Imports: result.Imports, + } +} diff --git a/tools/codegen/schema/schema_attribute_primitive.go b/tools/codegen/schema/schema_attribute_primitive.go new file mode 100644 index 0000000000..79e44975c0 --- /dev/null +++ b/tools/codegen/schema/schema_attribute_primitive.go @@ -0,0 +1,75 @@ +package schema + +import "github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/codespec" + +type Int64AttrGenerator struct { + intModel codespec.Int64Attribute + attr codespec.Attribute +} + +func (i *Int64AttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&i.attr, "schema.Int64Attribute", []CodeStatement{}) +} + +type Float64AttrGenerator struct { + floatModel codespec.Float64Attribute + attr codespec.Attribute +} + +func (f *Float64AttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&f.attr, "schema.Float64Attribute", []CodeStatement{}) +} + +type StringAttrGenerator struct { + stringModel codespec.StringAttribute + attr codespec.Attribute +} + +func (s *StringAttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&s.attr, "schema.StringAttribute", []CodeStatement{}) +} + +type BoolAttrGenerator struct { + boolModel codespec.BoolAttribute + attr codespec.Attribute +} + +func (s *BoolAttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&s.attr, "schema.BoolAttribute", []CodeStatement{}) +} + +type NumberAttrGenerator struct { + numberModel codespec.NumberAttribute + attr codespec.Attribute +} + +func (s *NumberAttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&s.attr, "schema.NumberAttribute", []CodeStatement{}) +} + +type ListAttrGenerator struct { + listModel codespec.ListAttribute + attr codespec.Attribute +} + +func (l *ListAttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&l.attr, "schema.ListAttribute", []CodeStatement{ElementTypeProperty(l.listModel.ElementType)}) +} + +type MapAttrGenerator struct { + mapModel codespec.MapAttribute + attr codespec.Attribute +} + +func (m *MapAttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&m.attr, "schema.MapAttribute", []CodeStatement{ElementTypeProperty(m.mapModel.ElementType)}) +} + +type SetAttrGenerator struct { + setModel codespec.SetAttribute + attr codespec.Attribute +} + +func (s *SetAttrGenerator) AttributeCode() CodeStatement { + return commonAttrStructure(&s.attr, "schema.SetAttribute", []CodeStatement{ElementTypeProperty(s.setModel.ElementType)}) +} diff --git a/tools/codegen/schema/schema_attribute_timeout.go b/tools/codegen/schema/schema_attribute_timeout.go new file mode 100644 index 0000000000..4dc1928870 --- /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 codespec.Create: + optionProperties += "Create: true,\n" + case codespec.Update: + optionProperties += "Update: true,\n" + case codespec.Delete: + optionProperties += "Delete: true,\n" + case 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/schema_file.go b/tools/codegen/schema/schema_file.go index 880e0e9014..d05fe4ee74 100644 --- a/tools/codegen/schema/schema_file.go +++ b/tools/codegen/schema/schema_file.go @@ -11,9 +11,13 @@ func GenerateGoCode(input codespec.Resource) string { schemaAttrs := GenerateSchemaAttributes(input.Schema.Attributes) models := GenerateTypedModels(input.Schema.Attributes) + imports := []string{"github.com/hashicorp/terraform-plugin-framework/resource/schema"} + imports = append(imports, schemaAttrs.Imports...) + imports = append(imports, models.Imports...) + tmplInputs := codetemplate.SchemaFileInputs{ PackageName: input.Name.LowerCaseNoUnderscore(), - Imports: append(schemaAttrs.Imports, models.Imports...), + Imports: imports, SchemaAttributes: schemaAttrs.Code, Models: models.Code, } diff --git a/tools/codegen/schema/schema_file_test.go b/tools/codegen/schema/schema_file_test.go index 3f4be9b053..ca38fe1278 100644 --- a/tools/codegen/schema/schema_file_test.go +++ b/tools/codegen/schema/schema_file_test.go @@ -144,6 +144,28 @@ func TestSchemaGenerationFromCodeSpec(t *testing.T) { }, goldenFileName: "nested-attributes", }, + "timeout attribute": { + inputModel: codespec.Resource{ + Name: "test_name", + Schema: &codespec.Schema{ + Attributes: []codespec.Attribute{ + { + Name: "string_attr", + String: &codespec.StringAttribute{}, + Description: admin.PtrString("string description"), + ComputedOptionalRequired: codespec.Required, + }, + { + Name: "timeouts", + Timeouts: &codespec.TimeoutsAttribute{ + ConfigurableTimeouts: []codespec.Operation{codespec.Create, codespec.Update, codespec.Delete}, + }, + }, + }, + }, + }, + goldenFileName: "timeouts", + }, } for testName, tc := range testCases { diff --git a/tools/codegen/schema/schema_nested_attribute.go b/tools/codegen/schema/schema_nested_attribute.go deleted file mode 100644 index 2014bcc510..0000000000 --- a/tools/codegen/schema/schema_nested_attribute.go +++ /dev/null @@ -1,77 +0,0 @@ -package schema - -import ( - "fmt" - - "github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/codespec" -) - -type ListNestedAttrGenerator struct { - model codespec.ListNestedAttribute -} - -func (l *ListNestedAttrGenerator) TypeDefinition() string { - return "schema.ListNestedAttribute" -} - -func (l *ListNestedAttrGenerator) TypeSpecificProperties() []CodeStatement { - return []CodeStatement{nestedObjectProperty(l.model.NestedObject)} -} - -type SetNestedGenerator struct { - model codespec.SetNestedAttribute -} - -func (s *SetNestedGenerator) TypeDefinition() string { - return "schema.SetNestedAttribute" -} - -func (s *SetNestedGenerator) TypeSpecificProperties() []CodeStatement { - return []CodeStatement{nestedObjectProperty(s.model.NestedObject)} -} - -type MapNestedAttrGenerator struct { - model codespec.MapNestedAttribute -} - -func (m *MapNestedAttrGenerator) TypeDefinition() string { - return "schema.MapNestedAttribute" -} - -func (m *MapNestedAttrGenerator) TypeSpecificProperties() []CodeStatement { - return []CodeStatement{nestedObjectProperty(m.model.NestedObject)} -} - -type SingleNestedAttrGenerator struct { - model codespec.SingleNestedAttribute -} - -func (s *SingleNestedAttrGenerator) TypeDefinition() string { - return "schema.SingleNestedAttribute" -} - -func (s *SingleNestedAttrGenerator) TypeSpecificProperties() []CodeStatement { - return []CodeStatement{attributesProperty(s.model.NestedObject)} -} - -func attributesProperty(nested codespec.NestedAttributeObject) CodeStatement { - attrs := GenerateSchemaAttributes(nested.Attributes) - attributeProperty := fmt.Sprintf(`Attributes: map[string]schema.Attribute{ - %s - }`, attrs.Code) - return CodeStatement{ - Code: attributeProperty, - Imports: attrs.Imports, - } -} - -func nestedObjectProperty(nested codespec.NestedAttributeObject) CodeStatement { - result := attributesProperty(nested) - nestedObj := fmt.Sprintf(`NestedObject: schema.NestedAttributeObject{ - %s, - }`, result.Code) - return CodeStatement{ - Code: nestedObj, - Imports: result.Imports, - } -} diff --git a/tools/codegen/schema/schema_primitive_attribute.go b/tools/codegen/schema/schema_primitive_attribute.go deleted file mode 100644 index 165cef170e..0000000000 --- a/tools/codegen/schema/schema_primitive_attribute.go +++ /dev/null @@ -1,99 +0,0 @@ -package schema - -import "github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/codespec" - -type Int64AttrGenerator struct { - model codespec.Int64Attribute -} - -func (i *Int64AttrGenerator) TypeDefinition() string { - return "schema.Int64Attribute" -} - -func (i *Int64AttrGenerator) TypeSpecificProperties() []CodeStatement { - return nil -} - -type Float64AttrGenerator struct { - model codespec.Float64Attribute -} - -func (f *Float64AttrGenerator) TypeDefinition() string { - return "schema.Float64Attribute" -} - -func (f *Float64AttrGenerator) TypeSpecificProperties() []CodeStatement { - return nil -} - -type StringAttrGenerator struct { - model codespec.StringAttribute -} - -func (s *StringAttrGenerator) TypeDefinition() string { - return "schema.StringAttribute" -} - -func (s *StringAttrGenerator) TypeSpecificProperties() []CodeStatement { - return nil -} - -type BoolAttrGenerator struct { - model codespec.BoolAttribute -} - -func (b *BoolAttrGenerator) TypeDefinition() string { - return "schema.BoolAttribute" -} - -func (b *BoolAttrGenerator) TypeSpecificProperties() []CodeStatement { - return nil -} - -type NumberAttrGenerator struct { - model codespec.NumberAttribute -} - -func (n *NumberAttrGenerator) TypeDefinition() string { - return "schema.NumberAttribute" -} - -func (n *NumberAttrGenerator) TypeSpecificProperties() []CodeStatement { - return nil -} - -type ListAttrGenerator struct { - model codespec.ListAttribute -} - -func (l *ListAttrGenerator) TypeDefinition() string { - return "schema.ListAttribute" -} - -func (l *ListAttrGenerator) TypeSpecificProperties() []CodeStatement { - return []CodeStatement{ElementTypeProperty(l.model.ElementType)} -} - -type MapAttrGenerator struct { - model codespec.MapAttribute -} - -func (m *MapAttrGenerator) TypeDefinition() string { - return "schema.MapAttribute" -} - -func (m *MapAttrGenerator) TypeSpecificProperties() []CodeStatement { - return []CodeStatement{ElementTypeProperty(m.model.ElementType)} -} - -type SetAttrGenerator struct { - model codespec.SetAttribute -} - -func (s *SetAttrGenerator) TypeDefinition() string { - return "schema.SetAttribute" -} - -func (s *SetAttrGenerator) TypeSpecificProperties() []CodeStatement { - return []CodeStatement{ElementTypeProperty(s.model.ElementType)} -} diff --git a/tools/codegen/schema/testdata/timeouts.golden.go b/tools/codegen/schema/testdata/timeouts.golden.go new file mode 100644 index 0000000000..ca5213bd1e --- /dev/null +++ b/tools/codegen/schema/testdata/timeouts.golden.go @@ -0,0 +1,30 @@ +package testname + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +func ResourceSchema(ctx context.Context) schema.Schema { + return schema.Schema{ + Attributes: map[string]schema.Attribute{ + "string_attr": schema.StringAttribute{ + Required: true, + MarkdownDescription: "string description", + }, + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + Create: true, + Update: true, + Delete: true, + }), + }, + } +} + +type TFModel struct { + StringAttr types.String `tfsdk:"string_attr"` + Timeouts timeouts.Value `tfsdk:"timeouts"` +} diff --git a/tools/codegen/schema/typed_model.go b/tools/codegen/schema/typed_model.go index dd7cee4649..5a324af296 100644 --- a/tools/codegen/schema/typed_model.go +++ b/tools/codegen/schema/typed_model.go @@ -97,6 +97,8 @@ func attrModelType(attr *codespec.Attribute) string { return "types.Number" case attr.Int64 != nil: return "types.Int64" + case attr.Timeouts != nil: + return "timeouts.Value" // For nested attributes the generic type is used, this is to ensure the model can store all possible values. e.g. nested computed only attributes need to be defined with TPF types to avoid error when getting the config. case attr.List != nil || attr.ListNested != nil: return "types.List" @@ -107,6 +109,6 @@ func attrModelType(attr *codespec.Attribute) string { case attr.SingleNested != nil: return "types.Object" default: - panic("Attribute with unknown type defined") + panic("Attribute with unknown type defined when generating typed model") } }