Skip to content

Commit

Permalink
Embed templates for plural data sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Aug 18, 2022
1 parent d178bed commit db4f4c4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 80 deletions.
86 changes: 6 additions & 80 deletions internal/provider/generators/plural-data-source/main.go
@@ -1,9 +1,11 @@
//go:build ignore
// +build ignore

package main

import (
"bytes"
_ "embed"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -126,85 +128,9 @@ func (p *PluralDataSourceGenerator) Generate(packageName, schemaFilename, acctes
}

// Terraform resource schema definition.
var dataSourceSchemaTemplateBody = `
// Code generated by generators/plural-data-source/main.go; DO NOT EDIT.
package {{ .PackageName }}
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
. "github.com/hashicorp/terraform-provider-awscc/internal/generic"
"github.com/hashicorp/terraform-provider-awscc/internal/registry"
)
func init() {
registry.AddDataSourceTypeFactory("{{ .TerraformTypeName }}", {{ .FactoryFunctionName }})
}
// {{ .FactoryFunctionName }} returns the Terraform {{ .TerraformTypeName }} data source type.
// This Terraform data source type corresponds to the CloudFormation {{ .CloudFormationTypeName }} resource type.
func {{ .FactoryFunctionName }}(ctx context.Context) (tfsdk.DataSourceType, error) {
attributes := map[string]tfsdk.Attribute {
"id": {
Description: "Uniquely identifies the data source.",
Type: types.StringType,
Computed: true,
},
"ids": {
Description: "Set of Resource Identifiers.",
Type: types.SetType{ElemType:types.StringType},
Computed: true,
},
}
schema := tfsdk.Schema{
Description: "{{ .SchemaDescription }}",
Version: {{ .SchemaVersion }},
Attributes: attributes,
}
var opts DataSourceTypeOptions
opts = opts.WithCloudFormationTypeName("{{ .CloudFormationTypeName }}").WithTerraformTypeName("{{ .TerraformTypeName }}")
opts = opts.WithTerraformSchema(schema)
pluralDataSourceType, err := NewPluralDataSourceType(ctx, opts...)
if err != nil {
return nil, err
}
return pluralDataSourceType, nil
}
`
//go:embed schema.tmpl
var dataSourceSchemaTemplateBody string

// Terraform acceptance tests.
var acceptanceTestsTemplateBody = `
// Code generated by generators/plural-data-source/main.go; DO NOT EDIT.
package {{ .PackageName }}_test
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-awscc/internal/acctest"
)
func {{ .AcceptanceTestFunctionPrefix }}DataSource_basic(t *testing.T) {
td := acctest.NewTestData(t, "{{ .CloudFormationTypeName }}", "{{ .TerraformTypeName }}", "test")
td.DataSourceTest(t, []resource.TestStep{
{
Config: td.EmptyDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(fmt.Sprintf("data.%s", td.ResourceName), "ids.#"),
),
},
})
}
`
//go:embed tests.tmpl
var acceptanceTestsTemplateBody string
52 changes: 52 additions & 0 deletions internal/provider/generators/plural-data-source/schema.tmpl
@@ -0,0 +1,52 @@
// Code generated by generators/plural-data-source/main.go; DO NOT EDIT.

package {{ .PackageName }}

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
. "github.com/hashicorp/terraform-provider-awscc/internal/generic"
"github.com/hashicorp/terraform-provider-awscc/internal/registry"
)

func init() {
registry.AddDataSourceTypeFactory("{{ .TerraformTypeName }}", {{ .FactoryFunctionName }})
}

// {{ .FactoryFunctionName }} returns the Terraform {{ .TerraformTypeName }} data source type.
// This Terraform data source type corresponds to the CloudFormation {{ .CloudFormationTypeName }} resource type.
func {{ .FactoryFunctionName }}(ctx context.Context) (tfsdk.DataSourceType, error) {
attributes := map[string]tfsdk.Attribute {
"id": {
Description: "Uniquely identifies the data source.",
Type: types.StringType,
Computed: true,
},
"ids": {
Description: "Set of Resource Identifiers.",
Type: types.SetType{ElemType:types.StringType},
Computed: true,
},
}

schema := tfsdk.Schema{
Description: "{{ .SchemaDescription }}",
Version: {{ .SchemaVersion }},
Attributes: attributes,
}

var opts DataSourceTypeOptions

opts = opts.WithCloudFormationTypeName("{{ .CloudFormationTypeName }}").WithTerraformTypeName("{{ .TerraformTypeName }}")
opts = opts.WithTerraformSchema(schema)

pluralDataSourceType, err := NewPluralDataSourceType(ctx, opts...)

if err != nil {
return nil, err
}

return pluralDataSourceType, nil
}
24 changes: 24 additions & 0 deletions internal/provider/generators/plural-data-source/tests.tmpl
@@ -0,0 +1,24 @@
// Code generated by generators/plural-data-source/main.go; DO NOT EDIT.

package {{ .PackageName }}_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-awscc/internal/acctest"
)

func {{ .AcceptanceTestFunctionPrefix }}DataSource_basic(t *testing.T) {
td := acctest.NewTestData(t, "{{ .CloudFormationTypeName }}", "{{ .TerraformTypeName }}", "test")

td.DataSourceTest(t, []resource.TestStep{
{
Config: td.EmptyDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(fmt.Sprintf("data.%s", td.ResourceName), "ids.#"),
),
},
})
}

0 comments on commit db4f4c4

Please sign in to comment.