Skip to content

Commit

Permalink
Embed templates for resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Aug 18, 2022
1 parent 0fa9643 commit 747a39f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 139 deletions.
144 changes: 5 additions & 139 deletions internal/provider/generators/resource/main.go
Expand Up @@ -4,6 +4,7 @@
package main

import (
_ "embed"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -107,144 +108,9 @@ func (r *ResourceGenerator) Generate(packageName, schemaFilename, acctestsFilena
}

// Terraform resource schema definition.
var resourceSchemaTemplateBody = `
// Code generated by generators/resource/main.go; DO NOT EDIT.
package {{ .PackageName }}
import (
"context"
{{if .ImportRegexp }}"regexp"{{- end}}
{{if .ImportFrameworkAttr }}"github.com/hashicorp/terraform-plugin-framework/attr"{{- end}}
"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"
{{ if .ImportValidate }}"github.com/hashicorp/terraform-provider-awscc/internal/validate"{{- end }}
)
func init() {
registry.AddResourceTypeFactory("{{ .TerraformTypeName }}", {{ .FactoryFunctionName }})
}
// {{ .FactoryFunctionName }} returns the Terraform {{ .TerraformTypeName }} resource type.
// This Terraform resource type corresponds to the CloudFormation {{ .CloudFormationTypeName }} resource type.
func {{ .FactoryFunctionName }}(ctx context.Context) (tfsdk.ResourceType, error) {
attributes := {{ .RootPropertiesSchema }}
{{ if .SyntheticIDAttribute }}
attributes["id"] = tfsdk.Attribute{
Description: "Uniquely identifies the resource.",
Type: types.StringType,
Computed: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
tfsdk.UseStateForUnknown(),
},
}
{{- end }}
schema := tfsdk.Schema{
Description: {{ .SchemaDescription | printf "%q" }},
Version: {{ .SchemaVersion }},
Attributes: attributes,
}
var opts ResourceTypeOptions
opts = opts.WithCloudFormationTypeName("{{ .CloudFormationTypeName }}").WithTerraformTypeName("{{ .TerraformTypeName }}")
opts = opts.WithTerraformSchema(schema)
opts = opts.WithSyntheticIDAttribute({{ .SyntheticIDAttribute }})
opts = opts.WithAttributeNameMap(map[string]string{
{{- range $key, $value := .AttributeNameMap }}
"{{ $key }}": "{{ $value }}",
{{- end }}
})
{{ if not .HasUpdateMethod }}
opts = opts.IsImmutableType(true)
{{- end }}
{{ if .WriteOnlyPropertyPaths }}
opts = opts.WithWriteOnlyPropertyPaths([]string{
{{- range .WriteOnlyPropertyPaths }}
"{{ . }}",
{{- end }}
})
{{- end }}
opts = opts.WithCreateTimeoutInMinutes({{ .CreateTimeoutInMinutes }}).WithDeleteTimeoutInMinutes({{ .DeleteTimeoutInMinutes }})
{{ if .HasUpdateMethod }}
opts = opts.WithUpdateTimeoutInMinutes({{ .UpdateTimeoutInMinutes }})
{{- end }}
{{ if .RequiredAttributesValidator }}
opts = opts.WithRequiredAttributesValidators({{ .RequiredAttributesValidator }})
{{- end }}
resourceType, err := NewResourceType(ctx, opts...)
if err != nil {
return nil, err
}
return resourceType, nil
}
`
//go:embed schema.tmpl
var resourceSchemaTemplateBody string

// Terraform acceptance tests.
var acceptanceTestsTemplateBody = `
// Code generated by generators/resource/main.go; DO NOT EDIT.
package {{ .PackageName }}_test
import (
{{ if .HasRequiredAttribute }}"regexp"{{- end }}
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-awscc/internal/acctest"
)
{{ if .HasRequiredAttribute }}
func {{ .AcceptanceTestFunctionPrefix }}_basic(t *testing.T) {
td := acctest.NewTestData(t, "{{ .CloudFormationTypeName }}", "{{ .TerraformTypeName }}", "test")
td.ResourceTest(t, []resource.TestStep{
{
Config: td.EmptyConfig(),
ExpectError: regexp.MustCompile("Missing required argument"),
},
})
}
{{- else }}
func {{ .AcceptanceTestFunctionPrefix }}_basic(t *testing.T) {
td := acctest.NewTestData(t, "{{ .CloudFormationTypeName }}", "{{ .TerraformTypeName }}", "test")
td.ResourceTest(t, []resource.TestStep{
{
Config: td.EmptyConfig(),
Check: resource.ComposeTestCheckFunc(
td.CheckExistsInAWS(),
),
},
{
ResourceName: td.ResourceName,
ImportState: true,
ImportStateVerify: true,
},
})
}
func {{ .AcceptanceTestFunctionPrefix }}_disappears(t *testing.T) {
td := acctest.NewTestData(t, "{{ .CloudFormationTypeName }}", "{{ .TerraformTypeName }}", "test")
td.ResourceTest(t, []resource.TestStep{
{
Config: td.EmptyConfig(),
Check: resource.ComposeTestCheckFunc(
td.CheckExistsInAWS(),
td.DeleteResource(),
),
ExpectNonEmptyPlan: true,
},
})
}
{{- end }}
`
//go:embed tests.tmpl
var acceptanceTestsTemplateBody string
78 changes: 78 additions & 0 deletions internal/provider/generators/resource/schema.tmpl
@@ -0,0 +1,78 @@
// Code generated by generators/resource/main.go; DO NOT EDIT.

package {{ .PackageName }}

import (
"context"
{{if .ImportRegexp }}"regexp"{{- end}}

{{if .ImportFrameworkAttr }}"github.com/hashicorp/terraform-plugin-framework/attr"{{- end}}
"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"
{{ if .ImportValidate }}"github.com/hashicorp/terraform-provider-awscc/internal/validate"{{- end }}
)

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

// {{ .FactoryFunctionName }} returns the Terraform {{ .TerraformTypeName }} resource type.
// This Terraform resource type corresponds to the CloudFormation {{ .CloudFormationTypeName }} resource type.
func {{ .FactoryFunctionName }}(ctx context.Context) (tfsdk.ResourceType, error) {
attributes := {{ .RootPropertiesSchema }}

{{ if .SyntheticIDAttribute }}
attributes["id"] = tfsdk.Attribute{
Description: "Uniquely identifies the resource.",
Type: types.StringType,
Computed: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
tfsdk.UseStateForUnknown(),
},
}
{{- end }}

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

var opts ResourceTypeOptions

opts = opts.WithCloudFormationTypeName("{{ .CloudFormationTypeName }}").WithTerraformTypeName("{{ .TerraformTypeName }}")
opts = opts.WithTerraformSchema(schema)
opts = opts.WithSyntheticIDAttribute({{ .SyntheticIDAttribute }})
opts = opts.WithAttributeNameMap(map[string]string{
{{- range $key, $value := .AttributeNameMap }}
"{{ $key }}": "{{ $value }}",
{{- end }}
})
{{ if not .HasUpdateMethod }}
opts = opts.IsImmutableType(true)
{{- end }}
{{ if .WriteOnlyPropertyPaths }}
opts = opts.WithWriteOnlyPropertyPaths([]string{
{{- range .WriteOnlyPropertyPaths }}
"{{ . }}",
{{- end }}
})
{{- end }}
opts = opts.WithCreateTimeoutInMinutes({{ .CreateTimeoutInMinutes }}).WithDeleteTimeoutInMinutes({{ .DeleteTimeoutInMinutes }})
{{ if .HasUpdateMethod }}
opts = opts.WithUpdateTimeoutInMinutes({{ .UpdateTimeoutInMinutes }})
{{- end }}
{{ if .RequiredAttributesValidator }}
opts = opts.WithRequiredAttributesValidators({{ .RequiredAttributesValidator }})
{{- end }}

resourceType, err := NewResourceType(ctx, opts...)

if err != nil {
return nil, err
}

return resourceType, nil
}
57 changes: 57 additions & 0 deletions internal/provider/generators/resource/tests.tmpl
@@ -0,0 +1,57 @@
// Code generated by generators/resource/main.go; DO NOT EDIT.

package {{ .PackageName }}_test

import (
{{ if .HasRequiredAttribute }}"regexp"{{- end }}
"testing"

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

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

td.ResourceTest(t, []resource.TestStep{
{
Config: td.EmptyConfig(),
ExpectError: regexp.MustCompile("Missing required argument"),
},
})
}
{{- else }}
func {{ .AcceptanceTestFunctionPrefix }}_basic(t *testing.T) {
td := acctest.NewTestData(t, "{{ .CloudFormationTypeName }}", "{{ .TerraformTypeName }}", "test")

td.ResourceTest(t, []resource.TestStep{
{
Config: td.EmptyConfig(),
Check: resource.ComposeTestCheckFunc(
td.CheckExistsInAWS(),
),
},
{
ResourceName: td.ResourceName,
ImportState: true,
ImportStateVerify: true,
},
})
}

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

td.ResourceTest(t, []resource.TestStep{
{
Config: td.EmptyConfig(),
Check: resource.ComposeTestCheckFunc(
td.CheckExistsInAWS(),
td.DeleteResource(),
),
ExpectNonEmptyPlan: true,
},
})
}
{{- end }}

0 comments on commit 747a39f

Please sign in to comment.