Skip to content

Commit

Permalink
Merge pull request #40950 from hashicorp/td-fw-resource-annotation-re…
Browse files Browse the repository at this point in the history
…quire-type

Tech debt: Require type name value for `@EphemeralResource` and `@FrameworkResource` annotations
  • Loading branch information
ewbankkit authored Jan 15, 2025
2 parents e4f8794 + 0d53198 commit 94283e5
Show file tree
Hide file tree
Showing 184 changed files with 865 additions and 631 deletions.
22 changes: 8 additions & 14 deletions internal/generate/servicepackage/file.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ type servicePackage struct {}
{{- if .EphemeralResources }}
func (p *servicePackage) EphemeralResources(ctx context.Context) []*types.ServicePackageEphemeralResource {
return []*types.ServicePackageEphemeralResource {
{{- range .EphemeralResources }}
{{- range $key, $value := .EphemeralResources }}
{
Factory: {{ .FactoryName }},
{{- if ne .Name "" }}
Name: "{{ .Name }}",
{{- end }}
Factory: {{ $value.FactoryName }},
TypeName: "{{ $key }}",
Name: "{{ $value.Name }}",
},
{{- end }}
}
Expand Down Expand Up @@ -58,12 +57,11 @@ func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.Serv

func (p *servicePackage) FrameworkResources(ctx context.Context) []*types.ServicePackageFrameworkResource {
return []*types.ServicePackageFrameworkResource {
{{- range .FrameworkResources }}
{{- range $key, $value := .FrameworkResources }}
{
Factory: {{ .FactoryName }},
{{- if ne .Name "" }}
Name: "{{ .Name }}",
{{- end }}
Factory: {{ $value.FactoryName }},
TypeName: "{{ $key }}",
Name: "{{ $value.Name }}",
{{- if .TransparentTagging }}
Tags: &types.ServicePackageResourceTags {
{{- if ne .TagsIdentifierAttribute "" }}
Expand All @@ -85,9 +83,7 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac
{
Factory: {{ $value.FactoryName }},
TypeName: "{{ $key }}",
{{- if ne $value.Name "" }}
Name: "{{ $value.Name }}",
{{- end }}
{{- if $value.TransparentTagging }}
Tags: &types.ServicePackageResourceTags {
{{- if ne $value.TagsIdentifierAttribute "" }}
Expand All @@ -109,9 +105,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka
{
Factory: {{ $value.FactoryName }},
TypeName: "{{ $key }}",
{{- if ne $value.Name "" }}
Name: "{{ $value.Name }}",
{{- end }}
{{- if $value.TransparentTagging }}
Tags: &types.ServicePackageResourceTags {
{{- if ne $value.TagsIdentifierAttribute "" }}
Expand Down
78 changes: 47 additions & 31 deletions internal/generate/servicepackage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func main() {
v := &visitor{
g: g,

ephemeralResources: make([]ResourceDatum, 0),
ephemeralResources: make(map[string]ResourceDatum, 0),
frameworkDataSources: make([]ResourceDatum, 0),
frameworkResources: make([]ResourceDatum, 0),
frameworkResources: make(map[string]ResourceDatum, 0),
sdkDataSources: make(map[string]ResourceDatum),
sdkResources: make(map[string]ResourceDatum),
}
Expand All @@ -87,9 +87,6 @@ func main() {
slices.SortStableFunc(s.FrameworkDataSources, func(a, b ResourceDatum) int {
return cmp.Compare(a.FactoryName, b.FactoryName)
})
slices.SortStableFunc(s.FrameworkResources, func(a, b ResourceDatum) int {
return cmp.Compare(a.FactoryName, b.FactoryName)
})

d := g.NewGoFileDestination(filename)

Expand Down Expand Up @@ -133,9 +130,9 @@ type ServiceDatum struct {
GoV2Package string // AWS SDK for Go v2 package name
ProviderPackage string
ProviderNameUpper string
EphemeralResources []ResourceDatum
EphemeralResources map[string]ResourceDatum
FrameworkDataSources []ResourceDatum
FrameworkResources []ResourceDatum
FrameworkResources map[string]ResourceDatum
SDKDataSources map[string]ResourceDatum
SDKResources map[string]ResourceDatum
}
Expand All @@ -159,9 +156,9 @@ type visitor struct {
functionName string
packageName string

ephemeralResources []ResourceDatum
ephemeralResources map[string]ResourceDatum
frameworkDataSources []ResourceDatum
frameworkResources []ResourceDatum
frameworkResources map[string]ResourceDatum
sdkDataSources map[string]ResourceDatum
sdkResources map[string]ResourceDatum
}
Expand Down Expand Up @@ -244,34 +241,51 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {

switch annotationName := m[1]; annotationName {
case "EphemeralResource":
if slices.ContainsFunc(v.ephemeralResources, func(d ResourceDatum) bool { return d.FactoryName == v.functionName }) {
v.errs = append(v.errs, fmt.Errorf("duplicate Ephemeral Resource: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
} else {
v.ephemeralResources = append(v.ephemeralResources, d)
if len(args.Positional) == 0 {
v.errs = append(v.errs, fmt.Errorf("no type name: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
continue
}

typeName := args.Positional[0]

if d.Name == "" {
v.g.Errorf("%s missing name: %s.%s", annotationName, v.packageName, v.functionName)
v.errs = append(v.errs, fmt.Errorf("no friendly name: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
continue
}

if _, ok := v.ephemeralResources[typeName]; ok {
v.errs = append(v.errs, fmt.Errorf("duplicate Ephemeral Resource (%s): %s", typeName, fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
} else {
v.ephemeralResources[typeName] = d
}
case "FrameworkDataSource":
if d.Name == "" {
v.errs = append(v.errs, fmt.Errorf("no friendly name: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
continue
}

if slices.ContainsFunc(v.frameworkDataSources, func(d ResourceDatum) bool { return d.FactoryName == v.functionName }) {
v.errs = append(v.errs, fmt.Errorf("duplicate Framework Data Source: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
} else {
v.frameworkDataSources = append(v.frameworkDataSources, d)
}

if d.Name == "" {
v.g.Errorf("%s missing name: %s.%s", annotationName, v.packageName, v.functionName)
}
case "FrameworkResource":
if slices.ContainsFunc(v.frameworkResources, func(d ResourceDatum) bool { return d.FactoryName == v.functionName }) {
v.errs = append(v.errs, fmt.Errorf("duplicate Framework Resource: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
} else {
v.frameworkResources = append(v.frameworkResources, d)
if len(args.Positional) == 0 {
v.errs = append(v.errs, fmt.Errorf("no type name: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
continue
}

typeName := args.Positional[0]

if d.Name == "" {
v.g.Errorf("%s missing name: %s.%s", annotationName, v.packageName, v.functionName)
v.errs = append(v.errs, fmt.Errorf("no friendly name: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
continue
}

if _, ok := v.frameworkResources[typeName]; ok {
v.errs = append(v.errs, fmt.Errorf("duplicate Framework Resource (%s): %s", typeName, fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
} else {
v.frameworkResources[typeName] = d
}
case "SDKDataSource":
if len(args.Positional) == 0 {
Expand All @@ -281,15 +295,16 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {

typeName := args.Positional[0]

if d.Name == "" {
v.errs = append(v.errs, fmt.Errorf("no friendly name: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
continue
}

if _, ok := v.sdkDataSources[typeName]; ok {
v.errs = append(v.errs, fmt.Errorf("duplicate SDK Data Source (%s): %s", typeName, fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
} else {
v.sdkDataSources[typeName] = d
}

if d.Name == "" {
v.g.Errorf("%s missing name: %s/%s", annotationName, v.packageName, typeName)
}
case "SDKResource":
if len(args.Positional) == 0 {
v.errs = append(v.errs, fmt.Errorf("no type name: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
Expand All @@ -298,15 +313,16 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {

typeName := args.Positional[0]

if d.Name == "" {
v.errs = append(v.errs, fmt.Errorf("no friendly name: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
continue
}

if _, ok := v.sdkResources[typeName]; ok {
v.errs = append(v.errs, fmt.Errorf("duplicate SDK Resource (%s): %s", typeName, fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
} else {
v.sdkResources[typeName] = d
}

if d.Name == "" {
v.g.Errorf("%s missing name: %s/%s", annotationName, v.packageName, typeName)
}
case "Tags":
// Handled above.
case "Testing":
Expand Down
5 changes: 3 additions & 2 deletions internal/service/amp/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions internal/service/apigateway/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/service/appconfig/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions internal/service/appfabric/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions internal/service/apprunner/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/service/appsync/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/service/auditmanager/assessment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (

const iamPropagationTimeout = 2 * time.Minute

// @FrameworkResource(name="Assessment")
// @FrameworkResource("aws_auditmanager_assessment", name="Assessment")
// @Tags(identifierAttribute="arn")
func newResourceAssessment(_ context.Context) (resource.ResourceWithConfigure, error) {
return &resourceAssessment{}, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/service/auditmanager/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkResource(name="Control")
// @FrameworkResource("aws_auditmanager_control", name="Control")
// @Tags(identifierAttribute="arn")
func newResourceControl(_ context.Context) (resource.ResourceWithConfigure, error) {
return &resourceControl{}, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/service/auditmanager/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkResource(name="Framework")
// @FrameworkResource("aws_auditmanager_framework", name="Framework")
// @Tags(identifierAttribute="arn")
func newResourceFramework(_ context.Context) (resource.ResourceWithConfigure, error) {
return &resourceFramework{}, nil
Expand Down
Loading

0 comments on commit 94283e5

Please sign in to comment.