Skip to content

Commit

Permalink
Merge pull request #38386 from hashicorp/td-update-tfsdk2fw
Browse files Browse the repository at this point in the history
Updates to `tfsdk2fw`
  • Loading branch information
gdavison authored Jul 23, 2024
2 parents b0425a8 + ea9f38b commit c082eaf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import (

// @FrameworkDataSource
func newDataSource{{ .Name }}(context.Context) (datasource.DataSourceWithConfigure, error) {
d := &dataSource{{ .Name }}{}
d.SetMigratedFromPluginSDK(true)

return d, nil
return &dataSource{{ .Name }}{}, nil
}

type dataSource{{ .Name }} struct {
Expand Down
55 changes: 39 additions & 16 deletions tools/tfsdk2fw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ func (m *migrator) generateTemplateData() (*templateData, error) {
templateData.FrameworkValidatorsPackages = append(templateData.FrameworkValidatorsPackages, v)
}
}
for _, v := range emitter.ProviderPlanModifierPackages {
if !slices.Contains(templateData.ProviderPlanModifierPackages, v) {
templateData.ProviderPlanModifierPackages = append(templateData.ProviderPlanModifierPackages, v)
for _, v := range emitter.GoImports {
if !slices.Contains(templateData.GoImports, v) {
templateData.GoImports = append(templateData.GoImports, v)
}
}

Expand All @@ -194,13 +194,13 @@ type emitter struct {
Generator *common.Generator
FrameworkPlanModifierPackages []string // Package names for any terraform-plugin-framework plan modifiers. May contain duplicates.
FrameworkValidatorsPackages []string // Package names for any terraform-plugin-framework-validators validators. May contain duplicates.
GoImports []goImport
HasTimeouts bool
HasTopLevelTagsAllMap bool
HasTopLevelTagsMap bool
ImportFrameworkAttr bool
ImportProviderFrameworkTypes bool
IsDataSource bool
ProviderPlanModifierPackages []string // Package names for any provider plan modifiers. May contain duplicates.
SchemaWriter io.Writer
StructWriter io.Writer
}
Expand Down Expand Up @@ -344,7 +344,8 @@ func (e *emitter) emitAttributeProperty(path []string, property *schema.Schema)
isComputedOnly := property.Computed && !property.Optional
isTopLevelAttribute := len(path) == 1
var planModifiers []string
var fwPlanModifierPackage, fwPlanModifierType, fwValidatorsPackage, fwValidatorType, providerPlanModifierPackage string
var defaultSpec string
var fwPlanModifierPackage, fwPlanModifierType, fwValidatorsPackage, fwValidatorType string

// At this point we are emitting code for the values of a schema.Schema's Attributes (map[string]schema.Attribute).
switch v := property.Type; v {
Expand Down Expand Up @@ -519,6 +520,10 @@ func (e *emitter) emitAttributeProperty(path []string, property *schema.Schema)
fprintf(e.SchemaWriter, "Optional:true,\n")
}

if def := property.Default; def != nil {
property.Computed = true
}

if property.Computed {
fprintf(e.SchemaWriter, "Computed:true,\n")
}
Expand Down Expand Up @@ -561,18 +566,27 @@ func (e *emitter) emitAttributeProperty(path []string, property *schema.Schema)
if def := property.Default; def != nil {
switch v := def.(type) {
case bool:
fprintf(e.SchemaWriter, "// TODO Default:%#v,\n", def)
e.GoImports = append(e.GoImports, goImport{
Path: "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault",
})
defaultSpec = fmt.Sprintf("booldefault.StaticBool(%t)", v)
case int:
fprintf(e.SchemaWriter, "// TODO Default:%#v,\n", def)
e.GoImports = append(e.GoImports, goImport{
Path: "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default",
})
defaultSpec = fmt.Sprintf("int64default.StaticInt64(%d)", v)
case float64:
fprintf(e.SchemaWriter, "// TODO Default:%#v,\n", def)
e.GoImports = append(e.GoImports, goImport{
Path: "github.com/hashicorp/terraform-plugin-framework/resource/schema/float64default",
})
defaultSpec = fmt.Sprintf("float64default.StaticFloat64(%f)", v)
case string:
providerPlanModifierPackage = "stringplanmodifier"
// Alias the provider plan modifier package name with an "fw" prefix. See also resource.tmpl.
planModifiers = append(planModifiers, fmt.Sprintf("fw%s.DefaultValue(%q)", providerPlanModifierPackage, v))
e.ProviderPlanModifierPackages = append(e.ProviderPlanModifierPackages, providerPlanModifierPackage)
e.GoImports = append(e.GoImports, goImport{
Path: "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault",
})
defaultSpec = fmt.Sprintf("stringdefault.StaticString(%q)", v)
default:
fprintf(e.SchemaWriter, "// TODO Default:%#v,\n", def)
fprintf(e.SchemaWriter, "// TODO Default: %#[1]v (%[1]T),\n", def)
}
}

Expand All @@ -584,6 +598,10 @@ func (e *emitter) emitAttributeProperty(path []string, property *schema.Schema)
fprintf(e.SchemaWriter, "},\n")
}

if defaultSpec != "" {
fprintf(e.SchemaWriter, "Default: %s,\n", defaultSpec)
}

// Features that we can't (yet) migrate:

if property.ValidateFunc != nil || property.ValidateDiagFunc != nil {
Expand Down Expand Up @@ -892,19 +910,24 @@ type templateData struct {
EmitResourceUpdateSkeleton bool
FrameworkPlanModifierPackages []string
FrameworkValidatorsPackages []string
GoImports []goImport
HasTimeouts bool
ImportFrameworkAttr bool
ImportProviderFrameworkTypes bool
Name string // e.g. Instance
PackageName string // e.g. ec2
ProviderPlanModifierPackages []string
Schema string
Struct string
TFTypeName string // e.g. aws_instance
}

//go:embed datasource.tmpl
//go:embed datasource.gtpl
var datasourceImpl string

//go:embed resource.tmpl
//go:embed resource.gtpl
var resourceImpl string

type goImport struct {
Path string
Alias string
}
9 changes: 4 additions & 5 deletions tools/tfsdk2fw/resource.tmpl → tools/tfsdk2fw/resource.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
{{if .EmitResourceImportState }}"github.com/hashicorp/terraform-plugin-framework/path"{{- end}}
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
{{if or (gt (len .FrameworkPlanModifierPackages) 0) (gt (len .ProviderPlanModifierPackages) 0) }}"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"{{- end}}
{{if gt (len .FrameworkPlanModifierPackages) 0 }}"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"{{- end}}
{{- range .FrameworkPlanModifierPackages }}
"github.com/hashicorp/terraform-plugin-framework/resource/schema/{{ . }}"
{{- end}}
Expand All @@ -23,15 +23,14 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
{{if .ImportProviderFrameworkTypes }}fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"{{- end}}
{{- range .ProviderPlanModifierPackages }}
fw{{ . }} "github.com/hashicorp/terraform-provider-aws/internal/framework/{{ . }}"
{{- end}}
{{ range .GoImports -}}
{{ if .Alias }}{{ .Alias }} {{ end }}"{{ .Path }}"
{{ end }}
)

// @FrameworkResource("{{ .TFTypeName }}")
func newResource{{ .Name }}(context.Context) (resource.ResourceWithConfigure, error) {
r := &resource{{ .Name }}{}
r.SetMigratedFromPluginSDK(true)
{{- if gt .DefaultCreateTimeout 0 }}
r.SetDefaultCreateTimeout({{ .DefaultCreateTimeout }} * time.Nanosecond) // TODO Convert to more human-friendly duration.
{{- end}}
Expand Down

0 comments on commit c082eaf

Please sign in to comment.