Skip to content

Commit

Permalink
rename DataResource to DataSource for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarfors committed Apr 15, 2024
1 parent d6e1fef commit d11c344
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 80 deletions.
4 changes: 2 additions & 2 deletions docs/terraform/localfile/out/local/data_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions docs/terraform/localfile/out/local/data_sensitive_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions docs/terraform/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/terraform/readme.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
14 changes: 7 additions & 7 deletions pkg/internal/hcl/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -37,7 +37,7 @@ type Provider struct {
Configuration interface{}
}

type DataResource struct {
type DataSource struct {
DataSource string
LocalName string
Configuration interface{}
Expand Down Expand Up @@ -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},
Expand Down
20 changes: 10 additions & 10 deletions pkg/internal/hcl/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ProviderBlock struct {
CommonBlockConfig `hcl:",remain"`
}

type DataResourceBlock struct {
type DataSourceBlock struct {
DataSource string `hcl:",label"`
LocalName string `hcl:",label"`

Expand All @@ -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
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestEncode(t *testing.T) {
CommonBlockConfig: cbcfg,
},
},
DataResources: []DataResourceBlock{
DataSources: []DataSourceBlock{
{
DataSource: "test_data_source",
LocalName: "test",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestEncodeRaw(t *testing.T) {
CommonBlockConfig: cbcfg,
},
},
DataResources: []DataResourceBlock{
DataSources: []DataSourceBlock{
{
DataSource: "some_data_source",
LocalName: "localname",
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/terrajen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
).
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/terrajen/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions pkg/internal/terrajen/qualifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

const (
idStructReference = "Reference"
idFuncReferenceResource = "ReferenceResource"
idFuncReferenceDataResource = "ReferenceDataResource"
idStructReference = "Reference"
idFuncReferenceResource = "ReferenceResource"
idFuncReferenceDataSource = "ReferenceDataSource"
)

var (
Expand All @@ -26,9 +26,9 @@ var (
pkgTerra,
idFuncReferenceResource,
).Clone
qualReferenceDataResource = jen.Qual(
qualReferenceDataSource = jen.Qual(
pkgTerra,
idFuncReferenceDataResource,
idFuncReferenceDataSource,
).Clone

qualReferenceAsString = jen.Qual(pkgTerra, "ReferenceAsString").Clone
Expand Down
16 changes: 8 additions & 8 deletions pkg/terra/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down
36 changes: 18 additions & 18 deletions pkg/terra/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/terra/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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(),
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/terra/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/terra/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading

0 comments on commit d11c344

Please sign in to comment.