Skip to content

Commit

Permalink
fix generation
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinBettati committed Oct 18, 2024
1 parent 052ed96 commit 4af4852
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 50 deletions.
4 changes: 2 additions & 2 deletions tools/codegen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ resources:
optional: true
computed: true

timeouts: ["create", "read", "delete"]
timeouts: ["create", "update", "delete"]

search_deployment:
read:
Expand All @@ -45,4 +45,4 @@ resources:
aliases:
group_id: project_id
ignores: ["links"]
timeouts: ["create", "read", "delete"]
timeouts: ["create", "update", "delete"]
68 changes: 22 additions & 46 deletions tools/codegen/schema/schema_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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")
}
33 changes: 33 additions & 0 deletions tools/codegen/schema/schema_attribute_timeout.go
Original file line number Diff line number Diff line change
@@ -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"},
}
}
8 changes: 6 additions & 2 deletions tools/codegen/schema/testdata/timeouts.golden.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test_name
package testname

import (
"context"
Expand All @@ -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,
}),
},
}
}
Expand Down

0 comments on commit 4af4852

Please sign in to comment.