Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to tfsdk2fw #38386

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions tools/tfsdk2fw/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/databasemigrationservice v1.40.3 // indirect
github.com/aws/aws-sdk-go-v2/service/databrew v1.31.3 // indirect
github.com/aws/aws-sdk-go-v2/service/dataexchange v1.30.3 // indirect
github.com/aws/aws-sdk-go-v2/service/datapipeline v1.23.3 // indirect
github.com/aws/aws-sdk-go-v2/service/datasync v1.40.3 // indirect
github.com/aws/aws-sdk-go-v2/service/datazone v1.13.2 // indirect
github.com/aws/aws-sdk-go-v2/service/dax v1.21.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions tools/tfsdk2fw/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ github.com/aws/aws-sdk-go-v2/service/databrew v1.31.3 h1:tFFs24+oIWlHLbTyluhnQIH
github.com/aws/aws-sdk-go-v2/service/databrew v1.31.3/go.mod h1:WP7xXB608MyVv3yFzduKlLeYmU0AxMo7zeF9Cuwbvwc=
github.com/aws/aws-sdk-go-v2/service/dataexchange v1.30.3 h1:GndlSdjdgcW1r+mGL635+6ZlwXgdu/663aHHyBJ6Jtk=
github.com/aws/aws-sdk-go-v2/service/dataexchange v1.30.3/go.mod h1:xUxKkSfH4sCQixoxh3pYc7C4N+OH2POgS0dhkOzR+u8=
github.com/aws/aws-sdk-go-v2/service/datapipeline v1.23.3 h1:kA26fZh30b6kOZZIkxr/1M4f4TnIsXBw3RcHEFuFxcs=
github.com/aws/aws-sdk-go-v2/service/datapipeline v1.23.3/go.mod h1:9Z4AiKwAlu2eXOPFEDfkLV/wTpI9o2FX09M4l6E4VE4=
github.com/aws/aws-sdk-go-v2/service/datasync v1.40.3 h1:ZrKMl8jsL5YHurOLf0YVLb7JBYxGtqQQAknJ5g4MTz4=
github.com/aws/aws-sdk-go-v2/service/datasync v1.40.3/go.mod h1:+ObRlRcKO/p38yJSkpVZKlCU3t9PqXMORXC+xTkb9NU=
github.com/aws/aws-sdk-go-v2/service/datazone v1.13.2 h1:9l6JiWZz/2Sp3ne9E/AXECwnzi7NASQUJnQ7xts/8oA=
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
}
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
Loading