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

generators: Makes AWS SDK v2 default for listpages and tags generators #39280

Merged
merged 4 commits into from
Sep 13, 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
8 changes: 4 additions & 4 deletions docs/resource-tagging.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ If the API does not support tagging on creation, pass the `-CreateTags` flag to

### Specifying the AWS SDK for Go version

The majority of the Terraform AWS Provider is implemented using [version 1 of the AWS SDK for Go](https://github.com/aws/aws-sdk-go).
For new services, however, [version 2 of the SDK](https://github.com/aws/aws-sdk-go-v2) is required.
The majority of the Terraform AWS Provider is implemented using [version 2 of the AWS SDK for Go](https://github.com/aws/aws-sdk-go-v2).
Some services, however, are only present in [version 1 of the SDK](https://github.com/aws/aws-sdk-go).

By default, the generated code uses the AWS SDK for Go v1.
To generate code using the AWS SDK for Go v2, pass the flag `-AwsSdkVersion=2`.
By default, the generated code uses the AWS SDK for Go v2.
To generate code using the AWS SDK for Go v1, pass the flag `-AwsSdkVersion=1`.

For more information, see the [documentation on AWS SDK versions](./aws-go-sdk-versions.md).

Expand Down
1 change: 0 additions & 1 deletion internal/generate/listpages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Optional Flags:

* `-Paginator`: Name of the pagination token field (default `NextToken`)
* `-Export`: Whether to export the generated functions
* `-AWSSDKVersion`: Version of the AWS Go SDK to use i.e. 1 or 2 (default `1`)
* `-V2Suffix`: Whether to append a V2 suffix to the list functions

To use with `go generate`, add the following directive to a Go file
Expand Down
82 changes: 19 additions & 63 deletions internal/generate/listpages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var (
outputPaginator = flag.String("OutputPaginator", "", "name of the output pagination token field")
paginator = flag.String("Paginator", "NextToken", "name of the pagination token field")
export = flag.Bool("Export", false, "whether to export the list functions")
sdkVersion = flag.Int("AWSSDKVersion", sdkV1, "Version of the AWS Go SDK to use i.e. 1 or 2")
v2Suffix = flag.Bool("V2Suffix", false, "whether to append a V2 suffix to the list functions")
)

Expand All @@ -52,10 +51,6 @@ func main() {
flag.Usage = usage
flag.Parse()

if *sdkVersion != sdkV1 && *sdkVersion != sdkV2 {
log.Fatalf("AWSSDKVersion must be either 1 or 2, got %d", *sdkVersion)
}

if (*inputPaginator != "" && *outputPaginator == "") || (*inputPaginator == "" && *outputPaginator != "") {
log.Fatal("both InputPaginator and OutputPaginator must be specified if one is")
}
Expand All @@ -80,47 +75,31 @@ func main() {
log.Fatalf("encountered: %s", err)
}

awsService := service.GoV1Package()
if *sdkVersion == sdkV2 {
awsService = service.GoV2Package()
}
awsService := service.GoV2Package()

functions := strings.Split(*listOps, ",")
sort.Strings(functions)

tmpl := template.Must(template.New("function").Parse(functionTemplateV1))
if *sdkVersion == sdkV2 {
tmpl = template.Must(template.New("function").Parse(functionTemplateV2))
tmpl := template.Must(template.New("function").Parse(functionTemplate))

}
g := Generator{
tmpl: tmpl,
inputPaginator: *inputPaginator,
outputPaginator: *outputPaginator,
}

sourcePackage := fmt.Sprintf("github.com/aws/aws-sdk-go/service/%[1]s", awsService)
if *sdkVersion == sdkV2 {
sourcePackage = fmt.Sprintf("github.com/aws/aws-sdk-go-v2/service/%[1]s", awsService)
}
sourcePackage := fmt.Sprintf("github.com/aws/aws-sdk-go-v2/service/%[1]s", awsService)

g.parsePackage(sourcePackage)

g.printHeader(HeaderInfo{
Parameters: strings.Join(os.Args[1:], " "),
DestinationPackage: servicePackage,
SourcePackage: sourcePackage,
SourceIntfPackage: fmt.Sprintf("github.com/aws/aws-sdk-go/service/%[1]s/%[1]siface", awsService),
}, *sdkVersion)

clientTypeName := service.ClientTypeName(*sdkVersion)

if err != nil {
log.Fatalf("encountered: %s", err)
}
})

for _, functionName := range functions {
g.generateFunction(functionName, awsService, clientTypeName, *export, *sdkVersion, *v2Suffix)
g.generateFunction(functionName, awsService, *export)
}

src := g.format()
Expand All @@ -135,7 +114,6 @@ type HeaderInfo struct {
Parameters string
DestinationPackage string
SourcePackage string
SourceIntfPackage string
}

type Generator struct {
Expand All @@ -159,12 +137,8 @@ type Package struct {
files []*PackageFile
}

func (g *Generator) printHeader(headerInfo HeaderInfo, sdkVersion int) {
header := template.Must(template.New("header").Parse(headerTemplateV1))

if sdkVersion == sdkV2 {
header = template.Must(template.New("header").Parse(headerTemplateV2))
}
func (g *Generator) printHeader(headerInfo HeaderInfo) {
header := template.Must(template.New("header").Parse(headerTemplate))

err := header.Execute(&g.buf, headerInfo)
if err != nil {
Expand Down Expand Up @@ -202,15 +176,14 @@ func (g *Generator) addPackage(pkg *packages.Package) {
type FuncSpec struct {
Name string
AWSName string
RecvType string
AWSService string
ParamType string
ResultType string
InputPaginator string
OutputPaginator string
V2Suffix bool
}

func (g *Generator) generateFunction(functionName, awsService, clientTypeName string, export bool, sdkVersion int, v2Suffix bool) {
func (g *Generator) generateFunction(functionName, awsService string, export bool) {
var function *ast.FuncDecl

for _, file := range g.pkg.files {
Expand Down Expand Up @@ -239,21 +212,14 @@ func (g *Generator) generateFunction(functionName, awsService, clientTypeName st
funcName = fmt.Sprintf("%s%s", strings.ToLower(funcName[0:1]), funcName[1:])
}

recvType := fmt.Sprintf("%[1]siface.%[2]sAPI", awsService, clientTypeName)

if sdkVersion == sdkV2 {
recvType = fmt.Sprintf("*%[1]s.%[2]s", awsService, clientTypeName)
}

funcSpec := FuncSpec{
Name: fixUpFuncName(funcName, clientTypeName),
Name: fixSomeInitialisms(funcName),
AWSName: function.Name.Name,
RecvType: recvType,
ParamType: g.expandTypeField(function.Type.Params, sdkVersion, false), // Assumes there is a single input parameter
ResultType: g.expandTypeField(function.Type.Results, sdkVersion, true), // Assumes we can take the first return parameter
AWSService: awsService,
ParamType: g.expandTypeField(function.Type.Params, false), // Assumes there is a single input parameter
ResultType: g.expandTypeField(function.Type.Results, true), // Assumes we can take the first return parameter
InputPaginator: g.inputPaginator,
OutputPaginator: g.outputPaginator,
V2Suffix: v2Suffix,
}

err := g.tmpl.Execute(&g.buf, funcSpec)
Expand All @@ -262,10 +228,10 @@ func (g *Generator) generateFunction(functionName, awsService, clientTypeName st
}
}

func (g *Generator) expandTypeField(field *ast.FieldList, sdkVersion int, result bool) string {
func (g *Generator) expandTypeField(field *ast.FieldList, result bool) string {
typeValue := field.List[0].Type

if sdkVersion == sdkV2 && !result {
if !result {
typeValue = field.List[1].Type
}

Expand All @@ -286,21 +252,11 @@ func (g *Generator) expandTypeExpr(expr ast.Expr) string {
return ""
}

func fixUpFuncName(funcName, service string) string {
return strings.ReplaceAll(fixSomeInitialisms(funcName), service, "")
}

//go:embed v1/header.tmpl
var headerTemplateV1 string

//go:embed v1/function.tmpl
var functionTemplateV1 string

//go:embed v2/header.tmpl
var headerTemplateV2 string
//go:embed v2/header.gtpl
var headerTemplate string

//go:embed v2/function.tmpl
var functionTemplateV2 string
//go:embed v2/function.gtpl
var functionTemplate string

func (g *Generator) format() []byte {
src, err := format.Source(g.buf.Bytes())
Expand Down
17 changes: 0 additions & 17 deletions internal/generate/listpages/v1/function.tmpl

This file was deleted.

11 changes: 0 additions & 11 deletions internal/generate/listpages/v1/header.tmpl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func {{ .Name }}Pages{{ if .V2Suffix }}V2{{ end }}(ctx context.Context, conn {{ .RecvType }}, input {{ .ParamType }}, fn func({{ .ResultType }}, bool) bool) error {
func {{ .Name }}Pages(ctx context.Context, conn *{{ .AWSService }}.Client, input {{ .ParamType }}, fn func({{ .ResultType }}, bool) bool) error {
for {
output, err := conn.{{ .AWSName }}(ctx, input)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/tags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var (
parentNotFoundErrMsg = flag.String("ParentNotFoundErrMsg", "", "Parent 'NotFound' Error Message")

sdkServicePackage = flag.String("AWSSDKServicePackage", "", "AWS Go SDK package to use. Defaults to the provider service package name.")
sdkVersion = flag.Int("AWSSDKVersion", sdkV1, "Version of the AWS Go SDK to use i.e. 1 or 2")
sdkVersion = flag.Int("AWSSDKVersion", sdkV2, "Version of the AWS Go SDK to use i.e. 1 or 2")
kvtValues = flag.Bool("KVTValues", false, "Whether KVT string map is of string pointers")
emptyMap = flag.Bool("EmptyMap", false, "Whether KVT string map should be empty for no tags")
skipAWSImp = flag.Bool("SkipAWSImp", false, "Whether to skip importing the AWS Go SDK aws package") // nosemgrep:ci.aws-in-var-name
Expand Down
14 changes: 7 additions & 7 deletions internal/generate/tags/templates/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import (
_ "embed"
)

//go:embed header_body.tmpl
//go:embed header_body.gtpl
var HeaderBody string

//go:embed get_tag_body.tmpl
//go:embed get_tag_body.gtpl
var GetTagBody string

//go:embed list_tags_body.tmpl
//go:embed list_tags_body.gtpl
var ListTagsBody string

//go:embed service_tags_map_body.tmpl
//go:embed service_tags_map_body.gtpl
var ServiceTagsMapBody string

//go:embed service_tags_slice_body.tmpl
//go:embed service_tags_slice_body.gtpl
var ServiceTagsSliceBody string

//go:embed update_tags_body.tmpl
//go:embed update_tags_body.gtpl
var UpdateTagsBody string

//go:embed wait_tags_propagated_body.tmpl
//go:embed wait_tags_propagated_body.gtpl
var WaitTagsPropagatedBody string
2 changes: 1 addition & 1 deletion internal/service/accessanalyzer/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
2 changes: 1 addition & 1 deletion internal/service/acm/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ListTagsOp=ListTagsForCertificate -ListTagsInIDElem=CertificateArn -ServiceTagsSlice -TagOp=AddTagsToCertificate -TagInIDElem=CertificateArn -UntagOp=RemoveTagsFromCertificate -UntagInNeedTagType -UntagInTagsElem=Tags -UpdateTags
//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsOp=ListTagsForCertificate -ListTagsInIDElem=CertificateArn -ServiceTagsSlice -TagOp=AddTagsToCertificate -TagInIDElem=CertificateArn -UntagOp=RemoveTagsFromCertificate -UntagInNeedTagType -UntagInTagsElem=Tags -UpdateTags
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
2 changes: 1 addition & 1 deletion internal/service/acmpca/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsOp=ListTags -ListTagsOpPaginated -ListTagsInIDElem=CertificateAuthorityArn -ServiceTagsSlice -TagOp=TagCertificateAuthority -TagInIDElem=CertificateAuthorityArn -UntagOp=UntagCertificateAuthority -UntagInNeedTagType -UntagInTagsElem=Tags -UpdateTags -AWSSDKVersion=2
//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsOp=ListTags -ListTagsOpPaginated -ListTagsInIDElem=CertificateAuthorityArn -ServiceTagsSlice -TagOp=TagCertificateAuthority -TagInIDElem=CertificateAuthorityArn -UntagOp=UntagCertificateAuthority -UntagInNeedTagType -UntagInTagsElem=Tags -UpdateTags
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
2 changes: 1 addition & 1 deletion internal/service/amp/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ListTagsInIDElem=ResourceArn -ServiceTagsMap -TagInIDElem=ResourceArn -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsInIDElem=ResourceArn -ServiceTagsMap -TagInIDElem=ResourceArn -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
2 changes: 1 addition & 1 deletion internal/service/amplify/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsMap -UpdateTags -AWSSDKVersion=2 -KVTValues -SkipTypesImp
//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
4 changes: 2 additions & 2 deletions internal/service/apigateway/generate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/listpages/main.go -ListOps=GetAuthorizers -Paginator=Position -AWSSDKVersion=2
//go:generate go run ../../generate/tags/main.go -ServiceTagsMap -UpdateTags -AWSSDKVersion=2 -KVTValues -SkipTypesImp -ListTags -ListTagsOp=GetTags
//go:generate go run ../../generate/listpages/main.go -ListOps=GetAuthorizers -Paginator=Position
//go:generate go run ../../generate/tags/main.go -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp -ListTags -ListTagsOp=GetTags
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
2 changes: 1 addition & 1 deletion internal/service/apigateway/list_pages_gen.go

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

4 changes: 2 additions & 2 deletions internal/service/apigatewayv2/generate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/listpages/main.go -ListOps=GetApis,GetApiMappings,GetDomainNames,GetVpcLinks -AWSSDKVersion=2
//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ListTagsOp=GetTags -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/listpages/main.go -ListOps=GetApis,GetApiMappings,GetDomainNames,GetVpcLinks
//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsOp=GetTags -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
2 changes: 1 addition & 1 deletion internal/service/apigatewayv2/list_pages_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/appautoscaling/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ListTagsInIDElem=ResourceARN -ServiceTagsMap -TagInIDElem=ResourceARN -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsInIDElem=ResourceARN -ServiceTagsMap -TagInIDElem=ResourceARN -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
2 changes: 1 addition & 1 deletion internal/service/appconfig/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
2 changes: 1 addition & 1 deletion internal/service/appfabric/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -ServiceTagsSlice -AWSSDKVersion=2 -ListTags -UpdateTags
//go:generate go run ../../generate/tags/main.go -ServiceTagsSlice -ListTags -UpdateTags
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
2 changes: 1 addition & 1 deletion internal/service/appflow/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ServiceTagsMap -KVTValues -SkipTypesImp -ListTags -UpdateTags
//go:generate go run ../../generate/tags/main.go -ServiceTagsMap -KVTValues -SkipTypesImp -ListTags -UpdateTags
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
Loading
Loading