Skip to content

Commit

Permalink
Split-out Uuid resource generation into separate functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kklimonda-cl committed Aug 8, 2024
1 parent e84eeeb commit 9cd495e
Show file tree
Hide file tree
Showing 10 changed files with 1,147 additions and 415 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ linters:
#- err113 # disabled because of too many dynamic errors that don't wrap anything

linters-settings:
exhaustive:
default-signifies-exhaustive: true
gci:
sections:
- standard
Expand Down
36 changes: 28 additions & 8 deletions pkg/commands/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,36 @@ func (c *Command) Execute() error {

if c.commandType == properties.CommandTypeTerraform {

newProviderObject := properties.NewTerraformProviderFile(spec.Name)
terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
err = terraformGenerator.RenderTerraformProviderFile(newProviderObject, spec)
if err != nil {
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
_, uuid := spec.Spec.Params["uuid"]
if !uuid {
terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, properties.ResourceEntry)
if err != nil {
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
}

resourceList = append(resourceList, resources...)
dataSourceList = append(dataSourceList, dataSources...)
} else {
terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, properties.ResourceUuid)
if err != nil {
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
}

resourceList = append(resourceList, resources...)
dataSourceList = append(dataSourceList, dataSources...)

terraformGenerator = generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
dataSources, resources, err = terraformGenerator.RenderTerraformProviderFile(spec, properties.ResourceUuidPlural)
if err != nil {
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
}

resourceList = append(resourceList, resources...)
dataSourceList = append(dataSourceList, dataSources...)
}

resourceList = append(resourceList, newProviderObject.Resources...)
dataSourceList = append(dataSourceList, newProviderObject.DataSources...)

} else if c.commandType == properties.CommandTypeSDK {
generator := generate.NewCreator(config.Output.GoSdk, c.templatePath, spec)
if err = generator.RenderTemplate(); err != nil {
Expand Down
39 changes: 29 additions & 10 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,46 @@ func (c *Creator) RenderTemplate() error {
}

// RenderTerraformProviderFile generates a Go file for a Terraform provider based on the provided TerraformProviderFile and Normalization arguments.
func (c *Creator) RenderTerraformProviderFile(terraformProvider *properties.TerraformProviderFile, spec *properties.Normalization) error {
func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, typ properties.ResourceType) ([]string, []string, error) {
var name string
if typ == properties.ResourceUuidPlural {
name = fmt.Sprintf("%s_%s", spec.TerraformProviderConfig.Suffix, spec.TerraformProviderConfig.PluralName)
} else {
name = spec.Name
}

terraformProvider := properties.NewTerraformProviderFile(name)
tfp := terraform_provider.GenerateTerraformProvider{}

if err := tfp.GenerateTerraformDataSource(spec, terraformProvider); err != nil {
return err
if err := tfp.GenerateTerraformDataSource(typ, spec, terraformProvider); err != nil {
return nil, nil, err
}

if err := tfp.GenerateTerraformResource(spec, terraformProvider); err != nil {
return err
if err := tfp.GenerateTerraformResource(typ, spec, terraformProvider); err != nil {
return nil, nil, err
}

if err := tfp.GenerateCommonCode(spec, terraformProvider); err != nil {
return err
if err := tfp.GenerateCommonCode(typ, spec, terraformProvider); err != nil {
return nil, nil, err
}

if err := tfp.GenerateTerraformProviderFile(spec, terraformProvider); err != nil {
return err
return nil, nil, err
}
filePath := c.createTerraformProviderFilePath(spec.TerraformProviderConfig.Suffix)

return c.writeFormattedContentToFile(filePath, terraformProvider.Code.String())
var filePath string
if typ == properties.ResourceUuidPlural {
name = fmt.Sprintf("%s_%s", spec.TerraformProviderConfig.Suffix, spec.TerraformProviderConfig.PluralName)
filePath = c.createTerraformProviderFilePath(name)
} else {
filePath = c.createTerraformProviderFilePath(spec.TerraformProviderConfig.Suffix)
}

if err := c.writeFormattedContentToFile(filePath, terraformProvider.Code.String()); err != nil {
return nil, nil, err
}

return terraformProvider.DataSources, terraformProvider.Resources, nil
}

// RenderTerraformProvider generates and writes a Terraform provider file.
Expand Down
32 changes: 19 additions & 13 deletions pkg/properties/normalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type TerraformProviderConfig struct {
SkipDatasource bool `json:"skip_datasource" yaml:"skip_datasource"`
SkipDatasourceListing bool `json:"skip_datasource_listing" yaml:"skip_datasource_listing"`
Suffix string `json:"suffix" yaml:"suffix"`
PluralName string `json:"plural_name" yaml:"plural_name"`
}

type NameVariant struct {
Expand Down Expand Up @@ -106,19 +107,24 @@ type ConstValue struct {
}

type SpecParam struct {
Name *NameVariant
Description string `json:"description" yaml:"description"`
Type string `json:"type" yaml:"type"`
Default string `json:"default" yaml:"default"`
Required bool `json:"required" yaml:"required"`
Sensitive bool `json:"sensitive" yaml:"sensitive"`
Length *SpecParamLength `json:"length" yaml:"length,omitempty"`
Count *SpecParamCount `json:"count" yaml:"count,omitempty"`
Hashing *SpecParamHashing `json:"hashing" yaml:"hashing,omitempty"`
Items *SpecParamItems `json:"items" yaml:"items,omitempty"`
Regex string `json:"regex" yaml:"regex,omitempty"`
Profiles []*SpecParamProfile `json:"profiles" yaml:"profiles"`
Spec *Spec `json:"spec" yaml:"spec"`
Name *NameVariant
Description string `json:"description" yaml:"description"`
Type string `json:"type" yaml:"type"`
Default string `json:"default" yaml:"default"`
Required bool `json:"required" yaml:"required"`
Sensitive bool `json:"sensitive" yaml:"sensitive"`
TerraformConfig SpecParamTerraformProviderConfig `json:"terraform_provider_config" yaml:"terraform_provider_config"`
Length *SpecParamLength `json:"length" yaml:"length,omitempty"`
Count *SpecParamCount `json:"count" yaml:"count,omitempty"`
Hashing *SpecParamHashing `json:"hashing" yaml:"hashing,omitempty"`
Items *SpecParamItems `json:"items" yaml:"items,omitempty"`
Regex string `json:"regex" yaml:"regex,omitempty"`
Profiles []*SpecParamProfile `json:"profiles" yaml:"profiles"`
Spec *Spec `json:"spec" yaml:"spec"`
}

type SpecParamTerraformProviderConfig struct {
Computed bool `json:"computed" yaml:"computed"`
}

type SpecParamLength struct {
Expand Down
9 changes: 9 additions & 0 deletions pkg/properties/resourcetype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package properties

type ResourceType int

const (
ResourceEntry ResourceType = iota
ResourceUuid ResourceType = iota
ResourceUuidPlural ResourceType = iota
)
Loading

0 comments on commit 9cd495e

Please sign in to comment.