Skip to content

Commit

Permalink
tools/awssdkpatch: add multiclient support (hashicorp#37540)
Browse files Browse the repository at this point in the history
The new `-multiclient` flag supports services where both V1 and V2 AWS SDK clients are used simultaneously. This is a common pattern for large services (e.g. `ec2`), where the migration will take place in parts. When set, the generated patch file will include additional patches to migrate tagging and filter helper functions to the appropriate V2 variant.
  • Loading branch information
jar-b authored May 15, 2024
1 parent 382faaf commit a74ea0f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ awssdkpatch-gen: awssdkpatch ## Generate a patch file using awssdkpatch
echo "PKG=foo make awssdkpatch-gen" ; \
exit 1 ; \
fi
@awssdkpatch -service $(PKG)
@awssdkpatch $(AWSSDKPATCH_OPTS) -service $(PKG)

awssdkpatch: prereq-go ## Install awssdkpatch
cd tools/awssdkpatch && $(GO_VER) install github.com/hashicorp/terraform-provider-aws/tools/awssdkpatch
Expand Down
10 changes: 9 additions & 1 deletion docs/aws-go-sdk-migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ PKG=ec2 make awssdkpatch-apply
You may also optionally generate the patch and use [`gopatch`](https://github.com/uber-go/gopatch) to preview differences before modfiying any files.

```console
PKG=ec2 make awssdkpatch-gen
make awssdkpatch-gen PKG=ec2
gopatch -d -p awssdk.patch ./internal/service/ec2/...
```

#### Custom options

To set additional `awssdkpatch` flags during patch generation, use the `AWSSDKPATCH_OPTS` environment variable.

```console
make awssdkpatch-gen PKG=ec2 AWSSDKPATCH_OPTS="-multiclient"
```

## Imports

In each go source file with a V1 SDK import, the library should be replaced with V2:
Expand Down
10 changes: 10 additions & 0 deletions tools/awssdkpatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Usage: awssdkpatch [flags]
Flags:
-importalias string
alias that the service package is imported as (optional)
-multiclient
whether the service supports both v1 and v2 clients (optional)
-out string
output file (optional) (default "awssdk.patch")
-service string
Expand Down Expand Up @@ -51,3 +53,11 @@ If the service uses an import alias, include the `-importalias` flag when genera
```console
awssdkpatch -service dms -importalias dms
```

### Multiple clients

If the service supports both V1 and V2 AWS SDK clients (a common pattern in large services which are migrated in parts), include the `-multiclient` flag when generating the patch file:

```console
awssdkpatch -service ec2 -multiclient
```
4 changes: 4 additions & 0 deletions tools/awssdkpatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

var (
importalias string
multiclient bool
out string
service string

Expand All @@ -32,6 +33,7 @@ type TemplateData struct {
GoV1Package string
GoV1ClientTypeName string
GoV2Package string
MultiClient bool
ImportAlias string
ProviderPackage string
InputOutputTypes []string
Expand All @@ -48,6 +50,7 @@ func main() {
flag.PrintDefaults()
}
flag.StringVar(&importalias, "importalias", "", "alias that the service package is imported as (optional)")
flag.BoolVar(&multiclient, "multiclient", false, "whether the service supports both v1 and v2 clients (optional)")
flag.StringVar(&out, "out", "awssdk.patch", "output file (optional)")
flag.StringVar(&service, "service", "", "service to migrate (required)")
flag.Parse()
Expand Down Expand Up @@ -113,6 +116,7 @@ func getPackageData(sd data.ServiceRecord) (TemplateData, error) {
GoV1ClientTypeName: sd.GoV1ClientTypeName(),
GoV2Package: sd.GoV2Package(),
ImportAlias: importalias,
MultiClient: multiclient,
ProviderPackage: providerPackage,
}

Expand Down
56 changes: 56 additions & 0 deletions tools/awssdkpatch/patch.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,59 @@ var x identifier
@@
+import "github.com/hashicorp/terraform-provider-aws/internal/enum"
enum.x

{{- if .MultiClient }}
# Replace generated Tags function with the V2 variant.
@@
@@
-Tags(...)
+TagsV2(...)

# Replace generated KeyValueTags function with the V2 variant.
@@
@@
-KeyValueTags(...)
+keyValueTagsV2(...)

# Replace generated getTagsIn function with the V2 variant.
@@
@@
-getTagsIn(...)
+getTagsInV2(...)

# Replace generated getTagSpecificationsIn function with the V2 variant.
@@
@@
-getTagSpecificationsIn(...)
+getTagSpecificationsInV2(...)

# Replace generated setTagsOut function with the V2 variant.
@@
@@
-setTagsOut(...)
+setTagsOutV2(...)

# Replace generated createTags function with the V2 variant.
@@
@@
-createTags(...)
+createTagsV2(...)

# Replace generated updateTags function with the V2 variant.
@@
@@
-updateTags(...)
+updateTagsV2(...)

# Replace generated newTagFilterList function with the V2 variant.
@@
@@
-newTagFilterList(...)
+newTagFilterListV2(...)

# Replace generated newCustomFilterList function with the V2 variant.
@@
@@
-newCustomFilterList(...)
+newCustomFilterListV2(...)
{{- end }}

0 comments on commit a74ea0f

Please sign in to comment.