Skip to content

Commit

Permalink
combine resource and data source into single pkg and clean up ready f…
Browse files Browse the repository at this point in the history
…or merge
  • Loading branch information
jlarfors committed Apr 12, 2024
1 parent 15e4c8e commit 3c4b3d8
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 218 deletions.
1 change: 1 addition & 0 deletions cmd/terragen/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func main() {
OutDir: outDir,
PkgPath: pkgPath,
Force: force,
Clean: clean,
},
schemas,
); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/internal/terrajen/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
suffixArgs = "Args"
suffixAttributes = "Attributes"
suffixState = "State"

prefixStructDataSource = "Data"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/terrajen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/dave/jennifer/jen"
)

// DataSourceFile generates a Go file for a Terraform data source configuration based on the given
// DataSourceFile generates a Go file for a Terraform data source configuration
// based on the given
// Schema
func DataSourceFile(s *Schema) *jen.File {
f := jen.NewFile(s.PackageName)
Expand Down
225 changes: 96 additions & 129 deletions pkg/internal/terrajen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,35 @@ type ProviderGenerator struct {
type SchemaType string

const (
SchemaTypeProvider SchemaType = "provider"
SchemaTypeResource SchemaType = "resource"
SchemaTypeData SchemaType = "data"
SchemaTypeProvider SchemaType = "provider"
SchemaTypeResource SchemaType = "resource"
SchemaTypeDataSource SchemaType = "data"
)

// SchemaProvider creates a schema for the provider config block for the
// provider
// represented by ProviderGenerator
func (a *ProviderGenerator) SchemaProvider(sb *tfjson.SchemaBlock) *Schema {
return &Schema{
SchemaType: SchemaTypeProvider,
GoProviderPkgPath: a.GoProviderPkgPath, // github.com/golingon/lingon/gen/aws
GeneratedPackageLocation: a.GeneratedPackageLocation, // gen/aws
ProviderName: a.ProviderName, // aws
ProviderSource: a.ProviderSource, // registry.terraform.io/hashicorp/aws
ProviderVersion: a.ProviderVersion, // 4.49.0
PackageName: a.ProviderName, // aws
ShortName: "provider",
Type: "provider",
StructName: "Provider",
ArgumentStructName: "Provider", // Args struct *is* the provider struct
Receiver: structReceiverFromName("provider"),

NewFuncName: "NewProvider",
SubPackageName: a.ProviderName,
SchemaType: SchemaTypeProvider,
GoProviderPkgPath: a.GoProviderPkgPath, // github.com/golingon/lingon/gen/aws
GeneratedPkgLocation: a.GeneratedPackageLocation, // gen/aws
ProviderName: a.ProviderName, // aws
ProviderSource: a.ProviderSource, // registry.terraform.io/hashicorp/aws
ProviderVersion: a.ProviderVersion, // 4.49.0
PackageName: a.ProviderName, // aws
Type: "provider",
StructName: "Provider",
ArgumentStructName: "Provider", // Edge case for provider: args struct *is* the provider struct.
StateStructName: "n/a", // Providers do not have a state.
Receiver: structReceiverFromName("provider"),

NewFuncName: "n/a", // Not used.
SubPkgName: a.ProviderName,
SubPkgPath: filepath.Join(
a.GeneratedPackageLocation,
"provider_types"+fileExtension,
),
FilePath: filepath.Join(
a.GeneratedPackageLocation,
"provider"+fileExtension,
Expand All @@ -87,55 +91,41 @@ func (a *ProviderGenerator) SchemaResource(
name string,
sb *tfjson.SchemaBlock,
) *Schema {
shortName := name
// shortName := providerShortName(name)
// split := strings.SplitN(shortName, "_", 2)
pkgName := shortName
subPkgName := shortName
fileName := shortName
fp := filepath.Join(
a.GeneratedPackageLocation,
pkgName,
fileName+fileExtension,
)
// If the pkgName is a keyword prefix it with the provider name.
// E.g. aws_default_vpc => awsdefault
// if token.IsKeyword(pkgName) {
// pkgName = a.ProviderName + pkgName
// }
// if token.IsKeyword(subPkgName) {
// subPkgName = a.ProviderName + subPkgName
// }

rs := &Schema{
SchemaType: SchemaTypeResource,
GoProviderPkgPath: a.GoProviderPkgPath, // github.com/golingon/lingon/gen/aws
GeneratedPackageLocation: a.GeneratedPackageLocation, // gen/aws
ProviderName: a.ProviderName, // aws
ProviderSource: a.ProviderSource, // hashicorp/aws
ProviderVersion: a.ProviderVersion, // 4.49.0
ShortName: shortName, // aws_iam_role => iam_role
PackageName: pkgName, // aws
Type: name, // aws

StructName: strcase.Pascal(
shortName,
), // iam_role => IamRole
ArgumentStructName: suffixArgs, // iam_role => IamRoleArgs
SchemaType: SchemaTypeResource,
GoProviderPkgPath: a.GoProviderPkgPath, // github.com/golingon/lingon/gen/aws
GeneratedPkgLocation: a.GeneratedPackageLocation, // gen/aws
ProviderName: a.ProviderName, // aws
ProviderSource: a.ProviderSource, // hashicorp/aws
ProviderVersion: a.ProviderVersion, // 4.49.0
PackageName: name, // aws_iam_role
Type: name, // aws_iam_role

StructName: "Resource",
ArgumentStructName: suffixArgs, // Args
AttributesStructName: strcase.Camel(
shortName,
) + suffixAttributes, // iam_role => iamRoleAttributes
name,
) + suffixAttributes, // iam_role => awsIamRoleAttributes
StateStructName: strcase.Camel(
shortName,
) + suffixState, // iam_role => IamRoleOut
name,
) + suffixState, // aws_iam_role => awsIamRoleState
Receiver: structReceiverFromName(
shortName,
name,
), // iam_role => ir

NewFuncName: "New",
SubPackageName: subPkgName, // iam_role => iam
FilePath: fp,
graph: newGraph(sb),
NewFuncName: "New",
SubPkgName: name, // aws_iam_role => aws_iam_role
SubPkgPath: filepath.Join(
a.GeneratedPackageLocation,
name,
name+"_types"+fileExtension,
),
FilePath: filepath.Join(
a.GeneratedPackageLocation,
name,
name+fileExtension,
),
graph: newGraph(sb),
}
return rs
}
Expand All @@ -146,62 +136,45 @@ func (a *ProviderGenerator) SchemaData(
name string,
sb *tfjson.SchemaBlock,
) *Schema {
shortName := name
// shortName := providerShortName(name)
pkgName := "data_" + shortName
subPkgName := "data_" + shortName
// spn := strings.ReplaceAll(shortName, "_", "")
dataName := shortName
// dataName := "data_" + shortName
fp := filepath.Join(
a.GeneratedPackageLocation,
pkgName,
dataName+fileExtension,
)
dataName := "data_" + name
ds := &Schema{
SchemaType: SchemaTypeData,
GoProviderPkgPath: a.GoProviderPkgPath, // github.com/golingon/lingon/gen/aws
GeneratedPackageLocation: a.GeneratedPackageLocation, // gen/aws
ProviderName: a.ProviderName, // aws
ProviderSource: a.ProviderSource, // hashicorp/aws
ProviderVersion: a.ProviderVersion, // 4.49.0
ShortName: shortName, // aws_iam_role => iam_role
PackageName: pkgName, // aws
Type: name, // aws_iam_role

StructName: strcase.Pascal(
shortName,
), // iam_role => IamRole
ArgumentStructName: suffixArgs, // iam_role => Args
SchemaType: SchemaTypeDataSource,
GoProviderPkgPath: a.GoProviderPkgPath, // github.com/golingon/lingon/gen/aws
GeneratedPkgLocation: a.GeneratedPackageLocation, // gen/aws
ProviderName: a.ProviderName, // aws
ProviderSource: a.ProviderSource, // hashicorp/aws
ProviderVersion: a.ProviderVersion, // 4.49.0
PackageName: name, // aws_iam_role
Type: name, // aws_iam_role

StructName: "DataSource",
ArgumentStructName: prefixStructDataSource + suffixArgs, // aws_iam_role => DataArgs
AttributesStructName: strcase.Camel(
shortName,
) + suffixAttributes, // iam_role => iamRoleAttributes
StateStructName: strcase.Camel(
shortName,
) + suffixState, // iam_role => IamRoleOut
dataName,
) + suffixAttributes, // iam_role => dataAwsIamRoleAttributes
StateStructName: "n/a", // Data sources do not have a state.
Receiver: structReceiverFromName(
shortName,
name,
), // iam_role => ir

NewFuncName: "New", // iam_role => New
SubPackageName: subPkgName, // iam_role => iamrole
FilePath: fp,
graph: newGraph(sb),
NewFuncName: "Data",
SubPkgName: name, // aws_iam_role => aws_iam_role
SubPkgPath: filepath.Join(
a.GeneratedPackageLocation,
name,
dataName+"_types"+fileExtension,
), // gen/aws/aws_iam_role/data_aws_iam_role_types.go
FilePath: filepath.Join(
a.GeneratedPackageLocation,
name,
dataName+fileExtension,
), // gen/aws/aws_iam_role/data_aws_iam_role.go
graph: newGraph(sb),
}

return ds
}

// providerShortName takes a name like "aws_iam_role" and returns the name
// without the leading provider prefix, i.e. it returns "iam_role"
func providerShortName(name string) string {
underscoreIndex := strings.Index(name, "_")
if underscoreIndex == -1 {
return name
}
return name[underscoreIndex+1:]
}

// structReceiverFromName calculates a suitable receiver from the name of the
// object. It gets the first character of each word separated by underscores,
// e.g. iam_role => ir
Expand All @@ -224,15 +197,14 @@ func structReceiverFromName(name string) string {
// A schema can represent a resource, a data object or the provider
// configuration.
type Schema struct {
SchemaType SchemaType // resource / provider / data
GoProviderPkgPath string // github.com/golingon/lingon/gen/providers
GeneratedPackageLocation string // gen/providers/aws
ProviderName string // aws
ProviderSource string // registry.terraform.io/hashicorp/aws
ProviderVersion string // 4.49.0
ShortName string // aws_iam_role => iam_role
PackageName string // aws
Type string // aws_iam_role
SchemaType SchemaType // resource / provider / data
GoProviderPkgPath string // github.com/golingon/lingon/gen/providers
GeneratedPkgLocation string // gen/providers/aws
ProviderName string // aws
ProviderSource string // registry.terraform.io/hashicorp/aws
ProviderVersion string // 4.49.0
PackageName string // aws
Type string // aws_iam_role

// Structs
StructName string // iam_role => IamRole
Expand All @@ -242,20 +214,15 @@ type Schema struct {

Receiver string // iam_role => ir

NewFuncName string // iam_role => NewIamRole
SubPackageName string // iam_role => iamrole
FilePath string // gen/providers/aws/ xxx
graph *graph
NewFuncName string // iam_role => NewIamRole
SubPkgName string // iam_role => iamrole
// SubPkgPath is the filepath for the schema entities types (args,
// attributes, state).
SubPkgPath string
FilePath string // gen/providers/aws/ xxx
graph *graph
}

func (s *Schema) SubPkgQualPath() string {
return s.GoProviderPkgPath + "/" + s.SubPackageName
}

func (s *Schema) SubPkgPath() string {
return filepath.Join(
s.GeneratedPackageLocation,
s.SubPackageName,
"sub_"+s.ShortName+fileExtension,
)
return s.GoProviderPkgPath + "/" + s.SubPkgName
}
16 changes: 0 additions & 16 deletions pkg/internal/terrajen/generator_test.go

This file was deleted.

Loading

0 comments on commit 3c4b3d8

Please sign in to comment.