diff --git a/docs/terraform/localfile/out/local/data_file.go b/docs/terraform/localfile/out/local/data_file.go index 5c4076b..fd70fcf 100644 --- a/docs/terraform/localfile/out/local/data_file.go +++ b/docs/terraform/localfile/out/local/data_file.go @@ -12,7 +12,7 @@ func NewDataFile(name string, args DataFileArgs) *DataFile { } } -var _ terra.DataResource = (*DataFile)(nil) +var _ terra.DataSource = (*DataFile)(nil) // DataFile represents the Terraform data resource local_file. type DataFile struct { @@ -37,7 +37,7 @@ func (f *DataFile) Configuration() interface{} { // Attributes returns the attributes for [DataFile]. func (f *DataFile) Attributes() dataFileAttributes { - return dataFileAttributes{ref: terra.ReferenceDataResource(f)} + return dataFileAttributes{ref: terra.ReferenceDataSource(f)} } // DataFileArgs contains the configurations for local_file. diff --git a/docs/terraform/localfile/out/local/data_sensitive_file.go b/docs/terraform/localfile/out/local/data_sensitive_file.go index c4d7bbd..c9ec1ae 100644 --- a/docs/terraform/localfile/out/local/data_sensitive_file.go +++ b/docs/terraform/localfile/out/local/data_sensitive_file.go @@ -12,7 +12,7 @@ func NewDataSensitiveFile(name string, args DataSensitiveFileArgs) *DataSensitiv } } -var _ terra.DataResource = (*DataSensitiveFile)(nil) +var _ terra.DataSource = (*DataSensitiveFile)(nil) // DataSensitiveFile represents the Terraform data resource local_sensitive_file. type DataSensitiveFile struct { @@ -37,7 +37,7 @@ func (sf *DataSensitiveFile) Configuration() interface{} { // Attributes returns the attributes for [DataSensitiveFile]. func (sf *DataSensitiveFile) Attributes() dataSensitiveFileAttributes { - return dataSensitiveFileAttributes{ref: terra.ReferenceDataResource(sf)} + return dataSensitiveFileAttributes{ref: terra.ReferenceDataSource(sf)} } // DataSensitiveFileArgs contains the configurations for local_sensitive_file. diff --git a/docs/terraform/readme.md b/docs/terraform/readme.md index 7894609..c4f85c6 100644 --- a/docs/terraform/readme.md +++ b/docs/terraform/readme.md @@ -42,7 +42,7 @@ Additionally, you need to provide an `out` location and the path to the `pkg` fo We recommend creating a Go file with a `go:generate` directive to invoke the `terragen` command. E.g. ```go -//go:generate go run -mod=readonly github.com/golingon/lingon/cmd/terragen -out ./gen/aws -pkg mypkg/gen/aws -provider local=hashicorp/aws:4.60.0 -force +//go:generate go run -mod=readonly github.com/golingon/lingon/cmd/terragen -out ./gen/aws -pkg mypkg/gen/aws -provider local=hashicorp/aws:4.60.0 -force ``` ## Creating and Exporting Terraform Stacks @@ -88,7 +88,7 @@ func Example_minimalStack() { Lingon uses Go reflection on the struct to identify all fields of a stack struct. All fields need to be one of: -1. An exported (public) field implementing one of the Terraform object interfaces (such as `terra.Backend`, `terra.Provider`, `terra.Resource` or `terra.DataResource`) +1. An exported (public) field implementing one of the Terraform object interfaces (such as `terra.Backend`, `terra.Provider`, `terra.Resource` or `terra.DataSource`) 2. A field with a struct tag `lingon:"-"` telling the encoder to ignore the field 3. An embedded struct, whose fields follow these same rules diff --git a/docs/terraform/readme.md.tpl b/docs/terraform/readme.md.tpl index c073514..ad46d8d 100644 --- a/docs/terraform/readme.md.tpl +++ b/docs/terraform/readme.md.tpl @@ -42,7 +42,7 @@ Additionally, you need to provide an `out` location and the path to the `pkg` fo We recommend creating a Go file with a `go:generate` directive to invoke the `terragen` command. E.g. ```go -//go:generate go run -mod=readonly github.com/golingon/lingon/cmd/terragen -out ./gen/aws -pkg mypkg/gen/aws -provider local=hashicorp/aws:4.60.0 -force +//go:generate go run -mod=readonly github.com/golingon/lingon/cmd/terragen -out ./gen/aws -pkg mypkg/gen/aws -provider local=hashicorp/aws:4.60.0 -force ``` ## Creating and Exporting Terraform Stacks @@ -58,7 +58,7 @@ Here is a minimal stack that we export to HCL: Lingon uses Go reflection on the struct to identify all fields of a stack struct. All fields need to be one of: -1. An exported (public) field implementing one of the Terraform object interfaces (such as `terra.Backend`, `terra.Provider`, `terra.Resource` or `terra.DataResource`) +1. An exported (public) field implementing one of the Terraform object interfaces (such as `terra.Backend`, `terra.Provider`, `terra.Resource` or `terra.DataSource`) 2. A field with a struct tag `lingon:"-"` telling the encoder to ignore the field 3. An embedded struct, whose fields follow these same rules diff --git a/pkg/internal/hcl/encode.go b/pkg/internal/hcl/encode.go index a4d04a2..51b92fb 100644 --- a/pkg/internal/hcl/encode.go +++ b/pkg/internal/hcl/encode.go @@ -19,10 +19,10 @@ const ( ) type EncodeArgs struct { - Backend *Backend - Providers []Provider - DataResources []DataResource - Resources []Resource + Backend *Backend + Providers []Provider + DataSources []DataSource + Resources []Resource } type Backend struct { @@ -37,7 +37,7 @@ type Provider struct { Configuration interface{} } -type DataResource struct { +type DataSource struct { DataSource string LocalName string Configuration interface{} @@ -99,11 +99,11 @@ func Encode(wr io.Writer, args EncodeArgs) error { fileBody.AppendNewline() } // Encode data blocks - if len(args.DataResources) > 0 { + if len(args.DataSources) > 0 { fileBody.AppendUnstructuredTokens(hclwrite.TokensForIdentifier("// Data blocks")) fileBody.AppendNewline() } - for _, data := range args.DataResources { + for _, data := range args.DataSources { dataBlock := fileBody.AppendNewBlock( "data", []string{data.DataSource, data.LocalName}, diff --git a/pkg/internal/hcl/encode_test.go b/pkg/internal/hcl/encode_test.go index 35829fc..bff897c 100644 --- a/pkg/internal/hcl/encode_test.go +++ b/pkg/internal/hcl/encode_test.go @@ -37,7 +37,7 @@ type ProviderBlock struct { CommonBlockConfig `hcl:",remain"` } -type DataResourceBlock struct { +type DataSourceBlock struct { DataSource string `hcl:",label"` LocalName string `hcl:",label"` @@ -60,10 +60,10 @@ type CommonBlockConfig struct { // the resulting HCL // into type HCLFile struct { - Terraform TerraformBlock `hcl:"terraform,block"` - Providers []ProviderBlock `hcl:"provider,block"` - DataResources []DataResourceBlock `hcl:"data,block"` - Resources []ResourceBlock `hcl:"resource,block"` + Terraform TerraformBlock `hcl:"terraform,block"` + Providers []ProviderBlock `hcl:"provider,block"` + DataSources []DataSourceBlock `hcl:"data,block"` + Resources []ResourceBlock `hcl:"resource,block"` } // Create a common config which we reuse. We might want to make this a bit more @@ -109,7 +109,7 @@ func TestEncode(t *testing.T) { CommonBlockConfig: cbcfg, }, }, - DataResources: []DataResourceBlock{ + DataSources: []DataSourceBlock{ { DataSource: "test_data_source", LocalName: "test", @@ -142,9 +142,9 @@ func TestEncode(t *testing.T) { }, ) } - for _, data := range expectedHCL.DataResources { - args.DataResources = append( - args.DataResources, DataResource{ + for _, data := range expectedHCL.DataSources { + args.DataSources = append( + args.DataSources, DataSource{ DataSource: data.DataSource, LocalName: data.LocalName, Configuration: data.CommonBlockConfig, @@ -201,7 +201,7 @@ func TestEncodeRaw(t *testing.T) { CommonBlockConfig: cbcfg, }, }, - DataResources: []DataResourceBlock{ + DataSources: []DataSourceBlock{ { DataSource: "some_data_source", LocalName: "localname", diff --git a/pkg/internal/terrajen/data.go b/pkg/internal/terrajen/data.go index 89057fe..ca804ce 100644 --- a/pkg/internal/terrajen/data.go +++ b/pkg/internal/terrajen/data.go @@ -54,7 +54,7 @@ func dataNewFunc(s *Schema) *jen.Statement { } func dataStructCompileCheck(s *Schema) *jen.Statement { - return jen.Var().Op("_").Qual(pkgTerra, "DataResource").Op("="). + return jen.Var().Op("_").Qual(pkgTerra, "DataSource").Op("="). Params( jen.Op("*").Id(s.StructName), ). diff --git a/pkg/internal/terrajen/funcs.go b/pkg/internal/terrajen/funcs.go index 2955773..9fb331e 100644 --- a/pkg/internal/terrajen/funcs.go +++ b/pkg/internal/terrajen/funcs.go @@ -158,7 +158,7 @@ func funcAttributes(s *Schema) *jen.Statement { if s.SchemaType == SchemaTypeResource { createRefFunc = qualReferenceResource() } else { - createRefFunc = qualReferenceDataResource() + createRefFunc = qualReferenceDataSource() } return jen.Comment( fmt.Sprintf( diff --git a/pkg/internal/terrajen/qualifiers.go b/pkg/internal/terrajen/qualifiers.go index 8411ecd..810fb16 100644 --- a/pkg/internal/terrajen/qualifiers.go +++ b/pkg/internal/terrajen/qualifiers.go @@ -8,9 +8,9 @@ import ( ) const ( - idStructReference = "Reference" - idFuncReferenceResource = "ReferenceResource" - idFuncReferenceDataResource = "ReferenceDataResource" + idStructReference = "Reference" + idFuncReferenceResource = "ReferenceResource" + idFuncReferenceDataSource = "ReferenceDataSource" ) var ( @@ -26,9 +26,9 @@ var ( pkgTerra, idFuncReferenceResource, ).Clone - qualReferenceDataResource = jen.Qual( + qualReferenceDataSource = jen.Qual( pkgTerra, - idFuncReferenceDataResource, + idFuncReferenceDataSource, ).Clone qualReferenceAsString = jen.Qual(pkgTerra, "ReferenceAsString").Clone diff --git a/pkg/terra/blocks.go b/pkg/terra/blocks.go index a48ac12..8c06deb 100644 --- a/pkg/terra/blocks.go +++ b/pkg/terra/blocks.go @@ -34,9 +34,9 @@ type Resource interface { ImportState(attributes io.Reader) error } -// DataResource represents a Terraform DataResource. +// DataSource represents a Terraform DataSource. // The generated Go structs from a Terraform provider data resource will implement this interface -type DataResource interface { +type DataSource interface { DataSource() string LocalName() string Configuration() interface{} @@ -59,10 +59,10 @@ type Backend interface { // stackObjects contains all the blocks that are extracted from a user-defined stack type stackObjects struct { - Backend Backend - Providers []Provider - Resources []Resource - DataResources []DataResource + Backend Backend + Providers []Provider + Resources []Resource + DataSources []DataSource } const ( @@ -196,8 +196,8 @@ func parseStackStructFields(rv reflect.Value, sb *stackObjects) error { switch v := obj.(type) { case Resource: sb.Resources = append(sb.Resources, v) - case DataResource: - sb.DataResources = append(sb.DataResources, v) + case DataSource: + sb.DataSources = append(sb.DataSources, v) case Provider: sb.Providers = append(sb.Providers, v) case Backend: diff --git a/pkg/terra/blocks_test.go b/pkg/terra/blocks_test.go index 13567d6..21fe5a3 100644 --- a/pkg/terra/blocks_test.go +++ b/pkg/terra/blocks_test.go @@ -13,11 +13,11 @@ import ( func TestExtractBlocks_Simple(t *testing.T) { type simpleStack struct { DummyStack - DummyRes *dummyResource `validate:"required"` - DummyData *dummyDataResource `validate:"required"` + DummyRes *dummyResource `validate:"required"` + DummyData *dummyDataSource `validate:"required"` } dr := &dummyResource{} - ddr := &dummyDataResource{} + ddr := &dummyDataSource{} st := simpleStack{ DummyStack: newDummyBaseStack(), DummyRes: dr, @@ -27,24 +27,24 @@ func TestExtractBlocks_Simple(t *testing.T) { tu.AssertNoError(t, err) tu.IsEqual(t, len(sb.Resources), 1) tu.IsEqual[Resource](t, dr, sb.Resources[0]) - tu.IsEqual(t, len(sb.DataResources), 1) - tu.IsEqual[DataResource](t, ddr, sb.DataResources[0]) + tu.IsEqual(t, len(sb.DataSources), 1) + tu.IsEqual[DataSource](t, ddr, sb.DataSources[0]) } func TestExtractBlocks_Complex(t *testing.T) { type DummyModule struct { - Resource *dummyResource `validate:"required"` - Data *dummyDataResource `validate:"required"` + Resource *dummyResource `validate:"required"` + Data *dummyDataSource `validate:"required"` } type complexStack struct { DummyStack DummyModule - SliceRes []*dummyResource `validate:"required,dive,required"` - OneRes [1]*dummyResource `validate:"required,dive,required"` - OneData [1]*dummyDataResource `validate:"required,dive,required"` + SliceRes []*dummyResource `validate:"required,dive,required"` + OneRes [1]*dummyResource `validate:"required,dive,required"` + OneData [1]*dummyDataSource `validate:"required,dive,required"` } dr := &dummyResource{} - ddr := &dummyDataResource{} + ddr := &dummyDataSource{} st := complexStack{ DummyStack: newDummyBaseStack(), DummyModule: DummyModule{ @@ -53,12 +53,12 @@ func TestExtractBlocks_Complex(t *testing.T) { }, SliceRes: []*dummyResource{dr, dr}, OneRes: [1]*dummyResource{dr}, - OneData: [1]*dummyDataResource{ddr}, + OneData: [1]*dummyDataSource{ddr}, } sb, err := objectsFromStack(&st) tu.AssertNoError(t, err) tu.IsEqual(t, len(sb.Resources), 4) - tu.IsEqual(t, len(sb.DataResources), 2) + tu.IsEqual(t, len(sb.DataSources), 2) } func TestExtractBlocks_UnknownField(t *testing.T) { @@ -182,19 +182,19 @@ func (r *dummyResource) LifecycleManagement() *Lifecycle { // Dummy Data Resources // -var _ DataResource = (*dummyDataResource)(nil) +var _ DataSource = (*dummyDataSource)(nil) -type dummyDataResource struct{} +type dummyDataSource struct{} -func (d *dummyDataResource) DataSource() string { +func (d *dummyDataSource) DataSource() string { return "dummy" } -func (d *dummyDataResource) LocalName() string { +func (d *dummyDataSource) LocalName() string { return "dummy" } -func (d *dummyDataResource) Configuration() interface{} { +func (d *dummyDataSource) Configuration() interface{} { return dummyConfig } diff --git a/pkg/terra/export.go b/pkg/terra/export.go index 346a74a..0a1cc46 100644 --- a/pkg/terra/export.go +++ b/pkg/terra/export.go @@ -98,9 +98,9 @@ func encodeStack(stack Exporter, w io.Writer) error { } args := hcl.EncodeArgs{ - Providers: make([]hcl.Provider, len(blocks.Providers)), - DataResources: make([]hcl.DataResource, len(blocks.DataResources)), - Resources: make([]hcl.Resource, len(blocks.Resources)), + Providers: make([]hcl.Provider, len(blocks.Providers)), + DataSources: make([]hcl.DataSource, len(blocks.DataSources)), + Resources: make([]hcl.Resource, len(blocks.Resources)), } if blocks.Backend != nil { args.Backend = &hcl.Backend{ @@ -116,8 +116,8 @@ func encodeStack(stack Exporter, w io.Writer) error { Configuration: prov.Configuration(), } } - for i, data := range blocks.DataResources { - args.DataResources[i] = hcl.DataResource{ + for i, data := range blocks.DataSources { + args.DataSources[i] = hcl.DataSource{ DataSource: data.DataSource(), LocalName: data.LocalName(), Configuration: data.Configuration(), @@ -147,7 +147,7 @@ func encodeStack(stack Exporter, w io.Writer) error { // Future things to check for (TODO): // 1. Each resource/data block's specific provider exists func validateStack(sb *stackObjects) error { - if (len(sb.Resources)+len(sb.DataResources)) > 0 && len(sb.Providers) == 0 { + if (len(sb.Resources)+len(sb.DataSources)) > 0 && len(sb.Providers) == 0 { return ErrNoProviderBlock } return nil diff --git a/pkg/terra/export_test.go b/pkg/terra/export_test.go index a349852..4067ac4 100644 --- a/pkg/terra/export_test.go +++ b/pkg/terra/export_test.go @@ -13,11 +13,11 @@ import ( func TestExport(t *testing.T) { type simpleStack struct { DummyStack - DummyRes *dummyResource `validate:"required"` - DummyData *dummyDataResource `validate:"required"` + DummyRes *dummyResource `validate:"required"` + DummyData *dummyDataSource `validate:"required"` } dr := &dummyResource{} - ddr := &dummyDataResource{} + ddr := &dummyDataSource{} st := simpleStack{ DummyStack: newDummyBaseStack(), DummyRes: dr, diff --git a/pkg/terra/list_test.go b/pkg/terra/list_test.go index f1fe06c..d288b5f 100644 --- a/pkg/terra/list_test.go +++ b/pkg/terra/list_test.go @@ -42,7 +42,7 @@ func ExampleList_bool() { func ExampleList_ref() { // Create some dummy references refA := ReferenceAsString(ReferenceResource(&dummyResource{})) - refB := ReferenceAsString(ReferenceDataResource(&dummyDataResource{})) + refB := ReferenceAsString(ReferenceDataSource(&dummyDataSource{})) s := List(refA, refB) fmt.Println(exampleTokensOrError(s)) diff --git a/pkg/terra/reference.go b/pkg/terra/reference.go index 52f85b3..bf3acb2 100644 --- a/pkg/terra/reference.go +++ b/pkg/terra/reference.go @@ -51,11 +51,11 @@ func ReferenceResource(res Resource) Reference { } } -// ReferenceDataResource takes a data resource and returns a Reference which +// ReferenceDataSource takes a data resource and returns a Reference which // is the address to that data resource in the Terraform configuration. -func ReferenceDataResource(data DataResource) Reference { +func ReferenceDataSource(data DataSource) Reference { return Reference{ - underlyingType: referenceDataResource, + underlyingType: referenceDataSource, data: data, } } @@ -71,8 +71,8 @@ func ReferenceAsSingle[T Value[T]](ref Reference) T { type referenceUnderlyingType int const ( - referenceResource referenceUnderlyingType = 1 - referenceDataResource referenceUnderlyingType = 2 + referenceResource referenceUnderlyingType = 1 + referenceDataSource referenceUnderlyingType = 2 ) var _ tkihcl.Tokenizer = (*Reference)(nil) @@ -82,12 +82,12 @@ var _ tkihcl.Tokenizer = (*Reference)(nil) // nested objects or even the splat operator (i.e. [*]). // // A reference can be created by passing a [Resource] to -// [ReferenceResource] or passing a [DataResource] to -// [ReferenceDataResource]. +// [ReferenceResource] or passing a [DataSource] to +// [ReferenceDataSource]. type Reference struct { underlyingType referenceUnderlyingType res Resource - data DataResource + data DataSource steps []referenceStep } @@ -128,7 +128,7 @@ func (r Reference) InternalTokens() (hclwrite.Tokens, error) { attribute: r.res.LocalName(), }, } - case referenceDataResource: + case referenceDataSource: fullSteps = []referenceStep{ { stepType: referenceStepAttribute, diff --git a/pkg/terra/set_test.go b/pkg/terra/set_test.go index b85473c..d65b9d9 100644 --- a/pkg/terra/set_test.go +++ b/pkg/terra/set_test.go @@ -40,7 +40,7 @@ func ExampleSet_bool() { func ExampleSet_ref() { s := Set( ReferenceAsString(ReferenceResource(&dummyResource{})), - ReferenceAsString(ReferenceDataResource(&dummyDataResource{})), + ReferenceAsString(ReferenceDataSource(&dummyDataSource{})), ) fmt.Println(exampleTokensOrError(s)) diff --git a/pkg/terragen/testdata/golden/aws_iam_role/provider.txtar b/pkg/terragen/testdata/golden/aws_iam_role/provider.txtar index 5cc3f6e..3cbbc54 100644 --- a/pkg/terragen/testdata/golden/aws_iam_role/provider.txtar +++ b/pkg/terragen/testdata/golden/aws_iam_role/provider.txtar @@ -2573,7 +2573,7 @@ func NewDataIamRole(name string, args DataIamRoleArgs) *DataIamRole { } } -var _ terra.DataResource = (*DataIamRole)(nil) +var _ terra.DataSource = (*DataIamRole)(nil) // DataIamRole represents the Terraform data resource aws_iam_role. type DataIamRole struct { @@ -2598,7 +2598,7 @@ func (ir *DataIamRole) Configuration() interface{} { // Attributes returns the attributes for [DataIamRole]. func (ir *DataIamRole) Attributes() dataIamRoleAttributes { - return dataIamRoleAttributes{ref: terra.ReferenceDataResource(ir)} + return dataIamRoleAttributes{ref: terra.ReferenceDataSource(ir)} } // DataIamRoleArgs contains the configurations for aws_iam_role.