Skip to content
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

Generate metadata to allow documentation tooling to include data not included in the Terraform schema. #2024

Closed
wants to merge 7 commits into from
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
30 changes: 28 additions & 2 deletions internal/provider/generators/plural-data-source/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package main
import (
"bytes"
_ "embed"
"encoding/json"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -49,10 +50,11 @@ func main() {

schemaFilename := args[0]
acctestsFilename := args[1]
metadataFilename := args[2]

g := NewGenerator()

if err := g.Generate(destinationPackage, schemaFilename, acctestsFilename); err != nil {
if err := g.Generate(destinationPackage, schemaFilename, acctestsFilename, metadataFilename); err != nil {
g.Fatalf("error generating Terraform %s data source: %s", *tfDataSourceType, err)
}
}
Expand All @@ -72,7 +74,7 @@ func NewGenerator() *Generator {
}

// Generate generates the plural data source type's factory into the specified file.
func (g *Generator) Generate(packageName, schemaFilename, acctestsFilename string) error {
func (g *Generator) Generate(packageName, schemaFilename, acctestsFilename, metadataFilename string) error {
g.Infof("generating Terraform data source code for %[1]q into %[2]q and %[3]q", g.tfDataSourceType, schemaFilename, acctestsFilename)

org, svc, res, err := naming.ParseCloudFormationTypeName(g.cfType)
Expand Down Expand Up @@ -127,6 +129,30 @@ func (g *Generator) Generate(packageName, schemaFilename, acctestsFilename strin
return err
}

d = g.NewGoFileDestination(metadataFilename)

if err := d.CreateDirectories(); err != nil {
return err
}

metadata := make(map[string]string)
metadata["cloudformationName"] = g.cfType
metadata["service"] = svc

jsonData, err := json.Marshal(metadata)

if err != nil {
return err
}

if err := d.WriteBytes(jsonData); err != nil {
return err
}

if err := d.Write(); err != nil {
return err
}

return nil
}

Expand Down
49 changes: 47 additions & 2 deletions internal/provider/generators/resource/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ package main

import (
_ "embed"
"encoding/json"
"flag"
"fmt"
"os"

"github.com/hashicorp/terraform-provider-awscc/internal/naming"
"github.com/hashicorp/terraform-provider-awscc/internal/provider/generators/common"
"github.com/hashicorp/terraform-provider-awscc/internal/provider/generators/shared"
)
Expand Down Expand Up @@ -47,10 +49,11 @@ func main() {

schemaFilename := args[0]
acctestsFilename := args[1]
metadataFilename := args[2]

g := NewGenerator()

if err := g.Generate(destinationPackage, schemaFilename, acctestsFilename); err != nil {
if err := g.Generate(destinationPackage, schemaFilename, acctestsFilename, metadataFilename); err != nil {
g.Fatalf("error generating Terraform %s resource: %s", *tfResourceType, err)
}
}
Expand All @@ -70,7 +73,7 @@ func NewGenerator() *Generator {
}

// Generate generates the resource's type factory into the specified file.
func (g *Generator) Generate(packageName, schemaFilename, acctestsFilename string) error {
func (g *Generator) Generate(packageName, schemaFilename, acctestsFilename, metadataFilename string) error {
g.Infof("generating Terraform resource code for %[1]q from %[2]q into %[3]q and %[4]q", g.tfResourceType, g.cfTypeSchemaFile, schemaFilename, acctestsFilename)

templateData, err := shared.GenerateTemplateData(g.UI(), g.cfTypeSchemaFile, shared.ResourceType, g.tfResourceType, packageName)
Expand Down Expand Up @@ -107,6 +110,48 @@ func (g *Generator) Generate(packageName, schemaFilename, acctestsFilename strin
return err
}

d = g.NewGoFileDestination(metadataFilename)

if err := d.CreateDirectories(); err != nil {
return err
}

schemaJson, err := os.ReadFile(*cfTypeSchemaFile)
if err != nil {
return err
}

t := struct {
TypeName string `json:"typeName"`
}{}
err = json.Unmarshal(schemaJson, &t)
if err != nil {
return err
}

_, service, _, err := naming.ParseCloudFormationTypeName(t.TypeName)

if err != nil {
return err
}

metadata := make(map[string]string)
metadata["cloudformationName"] = t.TypeName
metadata["service"] = service

jsonData, err := json.Marshal(metadata)

if err != nil {
return err
}

if err := d.WriteBytes(jsonData); err != nil {
return err
}

if err := d.Write(); err != nil {
return err
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/generators/schema/datasource.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Code generated by generators/schema/main.go; DO NOT EDIT.

{{- range .DataSources }}
{{ if .CloudFormationType }}//go:generate go run generators/plural-data-source/main.go -data-source {{ .TerraformResourceType }} -cftype {{ .CloudFormationType }} -package {{ .GeneratedCodePackageName }} {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedCodeFileName }}.go {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedAccTestsFileName }}.go{{ else }}//go:generate go run generators/singular-data-source/main.go -data-source {{ .TerraformResourceType }} -cfschema {{ .CloudFormationTypeSchemaFile }} -package {{ .GeneratedCodePackageName }} {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedCodeFileName }}.go {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedAccTestsFileName }}.go{{- end }}
{{ if .CloudFormationType }}//go:generate go run generators/plural-data-source/main.go -data-source {{ .TerraformResourceType }} -cftype {{ .CloudFormationType }} -package {{ .GeneratedCodePackageName }} {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedCodeFileName }}.go {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedAccTestsFileName }}.go {{ $.MetadataPathRoot }}/{{ .GeneratedTemplateMetadataFileName }}.json{{ else }}//go:generate go run generators/singular-data-source/main.go -data-source {{ .TerraformResourceType }} -cfschema {{ .CloudFormationTypeSchemaFile }} -package {{ .GeneratedCodePackageName }} {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedCodeFileName }}.go {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedAccTestsFileName }}.go {{ $.MetadataPathRoot }}/{{ .GeneratedCodePackageName}}_{{ .GeneratedTemplateMetadataFileName }}.json{{- end }}
{{- end }}

package {{ .PackageName }}
Expand Down
87 changes: 49 additions & 38 deletions internal/provider/generators/schema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ResourceSchema struct {
var (
configFile = flag.String("config", "", "configuration file; required")
generatedCodeRoot = flag.String("generated-code-root", "", "directory root for generated resource code")
templatesRoot = flag.String("templates-root", "", "directory root for generated template content;required")
importPathRoot = flag.String("import-path-root", "", "import path root for generated resource code; required")
packageName = flag.String("package", "", "override package name for generated code")
)
Expand Down Expand Up @@ -92,10 +93,10 @@ func main() {
generatedCodeRootDirectoryName = *generatedCodeRoot
}

os.Exit(run(destinationPackage, generatedCodeRootDirectoryName, resourcesFilename, singularDatasourcesFilename, pluralDatasourcesFilename, importExamplesFilename))
os.Exit(run(destinationPackage, generatedCodeRootDirectoryName, resourcesFilename, singularDatasourcesFilename, pluralDatasourcesFilename, importExamplesFilename, *templatesRoot))
}

func run(destinationPackage, generatedCodeRootDirectoryName, resourcesFilename, singularDatasourcesFilename, pluralDatasourcesFilename, importExamplesFilename string) int {
func run(destinationPackage, generatedCodeRootDirectoryName, resourcesFilename, singularDatasourcesFilename, pluralDatasourcesFilename, importExamplesFilename, templatesRoot string) int {
g := NewGenerator()
ctx := context.TODO()
cfg, err := config.LoadDefaultConfig(ctx)
Expand Down Expand Up @@ -140,17 +141,17 @@ func run(destinationPackage, generatedCodeRootDirectoryName, resourcesFilename,
return 1
}

if err := g.GenerateResources(destinationPackage, resourcesFilename, generatedCodeRootDirectoryName, *importPathRoot, resources); err != nil {
if err := g.GenerateResources(destinationPackage, resourcesFilename, generatedCodeRootDirectoryName, *importPathRoot, resources, templatesRoot); err != nil {
g.Errorf("error generating Terraform resource generation instructions: %s", err)
return 1
}

if err := g.GenerateDataSources(destinationPackage, singularDatasourcesFilename, generatedCodeRootDirectoryName, *importPathRoot, dataSources.Singular); err != nil {
if err := g.GenerateDataSources(destinationPackage, singularDatasourcesFilename, generatedCodeRootDirectoryName, *importPathRoot, dataSources.Singular, templatesRoot); err != nil {
g.Errorf("error generating Terraform singular data-source generation instructions: %s", err)
return 1
}

if err := g.GenerateDataSources(destinationPackage, pluralDatasourcesFilename, generatedCodeRootDirectoryName, *importPathRoot, dataSources.Plural); err != nil {
if err := g.GenerateDataSources(destinationPackage, pluralDatasourcesFilename, generatedCodeRootDirectoryName, *importPathRoot, dataSources.Plural, templatesRoot); err != nil {
g.Errorf("error generating Terraform plural data-source generation instructions: %s", err)
return 1
}
Expand Down Expand Up @@ -262,12 +263,13 @@ func (d *Downloader) Schemas() ([]*ResourceData, *DataSources, error) {
d.ui.Info(fmt.Sprintf("generation of a Terraform singular data source schema for %s has been suppressed", tfResourceTypeName))
} else {
singularDataSources = append(singularDataSources, &DataSourceData{
CloudFormationTypeSchemaFile: cfResourceSchemaFilename,
GeneratedAccTestsFileName: res + "_singular_data_source_gen_test",
GeneratedCodeFileName: res + "_singular_data_source_gen",
GeneratedCodePackageName: svc,
GeneratedCodePathSuffix: fmt.Sprintf("%s/%s", org, svc),
TerraformResourceType: tfResourceTypeName,
CloudFormationTypeSchemaFile: cfResourceSchemaFilename,
GeneratedAccTestsFileName: res + "_singular_data_source_gen_test",
GeneratedCodeFileName: res + "_singular_data_source_gen",
GeneratedTemplateMetadataFileName: res + "_gen",
GeneratedCodePackageName: svc,
GeneratedCodePathSuffix: fmt.Sprintf("%s/%s", org, svc),
TerraformResourceType: tfResourceTypeName,
})
}

Expand All @@ -276,13 +278,16 @@ func (d *Downloader) Schemas() ([]*ResourceData, *DataSources, error) {
} else {
pluralTfResourceTypeName := naming.Pluralize(tfResourceTypeName)

trimmedName := strings.TrimPrefix(pluralTfResourceTypeName, "awscc_")

pluralDataSources = append(pluralDataSources, &DataSourceData{
CloudFormationType: cfResourceTypeName,
GeneratedAccTestsFileName: res + "_plural_data_source_gen_test",
GeneratedCodeFileName: res + "_plural_data_source_gen",
GeneratedCodePackageName: svc,
GeneratedCodePathSuffix: fmt.Sprintf("%s/%s", org, svc),
TerraformResourceType: pluralTfResourceTypeName,
CloudFormationType: cfResourceTypeName,
GeneratedAccTestsFileName: res + "_plural_data_source_gen_test",
GeneratedCodeFileName: res + "_plural_data_source_gen",
GeneratedTemplateMetadataFileName: trimmedName + "_gen",
GeneratedCodePackageName: svc,
GeneratedCodePathSuffix: fmt.Sprintf("%s/%s", org, svc),
TerraformResourceType: pluralTfResourceTypeName,
})
}

Expand All @@ -292,12 +297,13 @@ func (d *Downloader) Schemas() ([]*ResourceData, *DataSources, error) {
}

resources = append(resources, &ResourceData{
CloudFormationTypeSchemaFile: cfResourceSchemaFilename,
GeneratedAccTestsFileName: res + "_resource_gen_test", // e.g. "log_group_resource_gen_test"
GeneratedCodeFileName: res + "_resource_gen", // e.g. "log_group_resource_gen"
GeneratedCodePackageName: svc, // e.g. "logs"
GeneratedCodePathSuffix: fmt.Sprintf("%s/%s", org, svc), // e.g. "aws/logs"
TerraformResourceType: tfResourceTypeName,
CloudFormationTypeSchemaFile: cfResourceSchemaFilename,
GeneratedAccTestsFileName: res + "_resource_gen_test", // e.g. "log_group_resource_gen_test"
GeneratedCodeFileName: res + "_resource_gen", // e.g. "log_group_resource_gen"
GeneratedTemplateMetadataFileName: res + "_gen",
GeneratedCodePackageName: svc, // e.g. "logs"
GeneratedCodePathSuffix: fmt.Sprintf("%s/%s", org, svc), // e.g. "aws/logs"
TerraformResourceType: tfResourceTypeName,
})
}

Expand Down Expand Up @@ -386,22 +392,24 @@ func (d *Downloader) infof(format string, a ...interface{}) {
}

type ResourceData struct {
CloudFormationTypeSchemaFile string
GeneratedAccTestsFileName string
GeneratedCodeFileName string
GeneratedCodePackageName string
GeneratedCodePathSuffix string
TerraformResourceType string
CloudFormationTypeSchemaFile string
GeneratedAccTestsFileName string
GeneratedTemplateMetadataFileName string
GeneratedCodeFileName string
GeneratedCodePackageName string
GeneratedCodePathSuffix string
TerraformResourceType string
}

type DataSourceData struct {
CloudFormationType string
CloudFormationTypeSchemaFile string
GeneratedAccTestsFileName string
GeneratedCodeFileName string
GeneratedCodePackageName string
GeneratedCodePathSuffix string
TerraformResourceType string
CloudFormationType string
CloudFormationTypeSchemaFile string
GeneratedAccTestsFileName string
GeneratedTemplateMetadataFileName string
GeneratedCodeFileName string
GeneratedCodePackageName string
GeneratedCodePathSuffix string
TerraformResourceType string
}

type ResourceImportData struct {
Expand All @@ -424,7 +432,7 @@ func NewGenerator() *Generator {
}
}

func (g *Generator) GenerateResources(packageName, filename, generatedCodeRootDirectoryName, importPathRoot string, resources []*ResourceData) error {
func (g *Generator) GenerateResources(packageName, filename, generatedCodeRootDirectoryName, importPathRoot string, resources []*ResourceData, templatesRoot string) error {
g.Infof("generating Terraform resource generation instructions into %q", filename)

importPaths := make(map[string]struct{}) // Set of strings.
Expand All @@ -449,6 +457,7 @@ func (g *Generator) GenerateResources(packageName, filename, generatedCodeRootDi
ImportPathSuffixes: importPathSuffixes,
PackageName: packageName,
Resources: resources,
MetadataPathRoot: templatesRoot + "/resources/metadata",
}

d := g.NewGoFileDestination(filename)
Expand All @@ -468,7 +477,7 @@ func (g *Generator) GenerateResources(packageName, filename, generatedCodeRootDi
return nil
}

func (g *Generator) GenerateDataSources(packageName, filename, generatedCodeRootDirectoryName, importPathRoot string, dataSources []*DataSourceData) error {
func (g *Generator) GenerateDataSources(packageName, filename, generatedCodeRootDirectoryName, importPathRoot string, dataSources []*DataSourceData, templatesRoot string) error {
g.Infof("generating Terraform data-source generation instructions into %q", filename)

importPaths := make(map[string]struct{}) // Set of strings.
Expand All @@ -493,6 +502,7 @@ func (g *Generator) GenerateDataSources(packageName, filename, generatedCodeRoot
ImportPathRoot: importPathRoot,
ImportPathSuffixes: importPathSuffixes,
PackageName: packageName,
MetadataPathRoot: templatesRoot + "/data-sources/metadata",
}

d := g.NewGoFileDestination(filename)
Expand Down Expand Up @@ -570,6 +580,7 @@ type TemplateData struct {
ImportPathSuffixes []string
PackageName string
Resources []*ResourceData
MetadataPathRoot string
}

type ImportTemplateData struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/generators/schema/resource.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Code generated by generators/schema/main.go; DO NOT EDIT.

{{- range .Resources }}
//go:generate go run generators/resource/main.go -resource {{ .TerraformResourceType }} -cfschema {{ .CloudFormationTypeSchemaFile }} -package {{ .GeneratedCodePackageName }} -- {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedCodeFileName }}.go {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedAccTestsFileName }}.go
//go:generate go run generators/resource/main.go -resource {{ .TerraformResourceType }} -cfschema {{ .CloudFormationTypeSchemaFile }} -package {{ .GeneratedCodePackageName }} -- {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedCodeFileName }}.go {{ $.GeneratedCodeRootDirectoryName }}/{{ .GeneratedCodePathSuffix }}/{{ .GeneratedAccTestsFileName }}.go {{ $.MetadataPathRoot }}/{{ .GeneratedCodePackageName}}_{{ .GeneratedTemplateMetadataFileName }}.json
{{- end }}

package {{ .PackageName }}
Expand Down
Loading