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

Tech debt: Remove AWS SDK for Go v1 support from skaff #39924

Merged
merged 6 commits into from
Oct 29, 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
2 changes: 0 additions & 2 deletions docs/skaff.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ Flags:
-n, --name string name of the entity
-p, --plugin-sdkv2 generate for Terraform Plugin SDK V2
-s, --snakename string if skaff doesn't get it right, explicitly give name in snake case (e.g., db_vpc_instance)
-o, --v1 generate for AWS Go SDK v1 (some existing services)
```

### Function
Expand Down Expand Up @@ -154,5 +153,4 @@ Flags:
-n, --name string name of the entity
-p, --plugin-sdkv2 generate for Terraform Plugin SDK V2
-s, --snakename string if skaff doesn't get it right, explicitly give name in snake case (e.g., db_vpc_instance)
-o, --v1 generate for AWS Go SDK v1 (some existing services)
```
3 changes: 1 addition & 2 deletions skaff/cmd/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var datasourceCmd = &cobra.Command{
Use: "datasource",
Short: "Create scaffolding for a data source",
RunE: func(cmd *cobra.Command, args []string) error {
return datasource.Create(name, snakeName, !clearComments, force, !v1, !pluginSDKV2, includeTags)
return datasource.Create(name, snakeName, !clearComments, force, !pluginSDKV2, includeTags)
},
}

Expand All @@ -22,7 +22,6 @@ func init() {
datasourceCmd.Flags().BoolVarP(&clearComments, "clear-comments", "c", false, "do not include instructional comments in source")
datasourceCmd.Flags().StringVarP(&name, "name", "n", "", "name of the entity")
datasourceCmd.Flags().BoolVarP(&force, "force", "f", false, "force creation, overwriting existing files")
datasourceCmd.Flags().BoolVarP(&v1, "v1", "o", false, "generate for AWS Go SDK v1 (some existing services)")
datasourceCmd.Flags().BoolVarP(&pluginSDKV2, "plugin-sdkv2", "p", false, "generate for Terraform Plugin SDK V2")
datasourceCmd.Flags().BoolVarP(&includeTags, "include-tags", "t", false, "Indicate that this resource has tags and the code for tagging should be generated")
}
4 changes: 1 addition & 3 deletions skaff/cmd/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var (
clearComments bool
name string
force bool
v1 bool
pluginSDKV2 bool
includeTags bool
)
Expand All @@ -22,7 +21,7 @@ var resourceCmd = &cobra.Command{
Use: "resource",
Short: "Create scaffolding for a resource",
RunE: func(cmd *cobra.Command, args []string) error {
return resource.Create(name, snakeName, !clearComments, force, !v1, !pluginSDKV2, includeTags)
return resource.Create(name, snakeName, !clearComments, force, !pluginSDKV2, includeTags)
},
}

Expand All @@ -32,7 +31,6 @@ func init() {
resourceCmd.Flags().BoolVarP(&clearComments, "clear-comments", "c", false, "do not include instructional comments in source")
resourceCmd.Flags().StringVarP(&name, "name", "n", "", "name of the entity")
resourceCmd.Flags().BoolVarP(&force, "force", "f", false, "force creation, overwriting existing files")
resourceCmd.Flags().BoolVarP(&v1, "v1", "o", false, "generate for AWS Go SDK v1 (some existing services)")
resourceCmd.Flags().BoolVarP(&pluginSDKV2, "plugin-sdkv2", "p", false, "generate for Terraform Plugin SDK V2")
resourceCmd.Flags().BoolVarP(&includeTags, "include-tags", "t", false, "Indicate that this resource has tags and the code for tagging should be generated")
}
16 changes: 0 additions & 16 deletions skaff/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,6 @@ import (
"github.com/YakDriver/regexache"
)

// ToSnakeCase converts a camel cased string to snake case
//
// If the override argument is a non-empty string, its value is returned
// unchanged.
func ToSnakeCase(upper string, override string) string {
if override != "" {
return override
}

re := regexache.MustCompile(`([a-z])([A-Z]{2,})`)
upper = re.ReplaceAllString(upper, `${1}_${2}`)

re2 := regexache.MustCompile(`([A-Z][a-z])`)
return strings.TrimPrefix(strings.ToLower(re2.ReplaceAllString(upper, `_$1`)), "_")
}

// ToHumanResName converts a camel cased string to a human readable name
func ToHumanResName(upper string) string {
re := regexache.MustCompile(`([a-z])([A-Z]{2,})`)
Expand Down
49 changes: 0 additions & 49 deletions skaff/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,6 @@ import (
"testing"
)

func TestToSnakeName(t *testing.T) {
testCases := []struct {
TestName string
Input string
Expected string
}{
{
TestName: "empty",
Input: "",
Expected: "",
},
{
TestName: "simple",
Input: "Cheese",
Expected: "cheese",
},
{
TestName: "two word",
Input: "CityLights",
Expected: "city_lights",
},
{
TestName: "three word",
Input: "OpenEndResource",
Expected: "open_end_resource",
},
{
TestName: "initialism",
Input: "DBInstance",
Expected: "db_instance",
},
{
TestName: "initialisms",
Input: "DBInstanceVPCEndpoint",
Expected: "db_instance_vpc_endpoint",
},
}

for _, testCase := range testCases {
t.Run(testCase.TestName, func(t *testing.T) {
got := ToSnakeCase(testCase.Input, "")

if got != testCase.Expected {
t.Errorf("got %s, expected %s", got, testCase.Expected)
}
})
}
}

func TestToHumanResName(t *testing.T) {
testCases := []struct {
TestName string
Expand Down
9 changes: 5 additions & 4 deletions skaff/datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"text/template"

"github.com/hashicorp/terraform-provider-aws/names"
"github.com/hashicorp/terraform-provider-aws/names/data"
"github.com/hashicorp/terraform-provider-aws/skaff/convert"
)
Expand Down Expand Up @@ -42,13 +43,12 @@ type TemplateData struct {
Service string
ServiceLower string
AWSServiceName string
AWSGoSDKV2 bool
PluginFramework bool
HumanDataSourceName string
ProviderResourceName string
}

func Create(dsName, snakeName string, comments, force, v2, pluginFramework, tags bool) error {
func Create(dsName, snakeName string, comments, force, pluginFramework, tags bool) error {
wd, err := os.Getwd() // os.Getenv("GOPACKAGE") not available since this is not run with go generate
if err != nil {
return fmt.Errorf("error reading working directory: %s", err)
Expand All @@ -68,7 +68,9 @@ func Create(dsName, snakeName string, comments, force, v2, pluginFramework, tags
return fmt.Errorf("error checking: snake name should be all lower case with underscores, if needed (e.g., db_instance)")
}

snakeName = convert.ToSnakeCase(dsName, snakeName)
if snakeName == "" {
snakeName = names.ToSnakeCase(dsName)
}

service, err := data.LookupService(servicePackage)
if err != nil {
Expand All @@ -87,7 +89,6 @@ func Create(dsName, snakeName string, comments, force, v2, pluginFramework, tags
Service: service.ProviderNameUpper(),
ServiceLower: strings.ToLower(service.ProviderNameUpper()),
AWSServiceName: service.FullHumanFriendly(),
AWSGoSDKV2: v2,
PluginFramework: pluginFramework,
HumanDataSourceName: convert.ToHumanResName(dsName),
ProviderResourceName: convert.ToProviderResourceName(servicePackage, snakeName),
Expand Down
12 changes: 3 additions & 9 deletions skaff/datasource/datasource.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (
//
// The provider linter wants your imports to be in two groups: first,
// standard library (i.e., "fmt" or "strings"), second, everything else.
{{- end }}
{{- if and .IncludeComments .AWSGoSDKV2 }}
//
// Also, AWS Go SDK v2 may handle nested structures differently than v1,
// using the services/{{ .SDKPackage }}/types package. If so, you'll
Expand All @@ -47,14 +45,10 @@ import (
"regexp"
"strings"
"time"
{{ if .AWSGoSDKV2 }}

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/{{ .SDKPackage }}"
"github.com/aws/aws-sdk-go-v2/service/{{ .SDKPackage }}/types"
{{- else }}
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/{{ .SDKPackage }}"
{{- end }}
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -172,7 +166,7 @@ func dataSource{{ .DataSource }}Read(ctx context.Context, d *schema.ResourceData

// TIP: -- 1. Get a client connection to the relevant service
{{- end }}
conn := meta.(*conns.AWSClient).{{ .Service }}{{ if .AWSGoSDKV2 }}Client(ctx){{ else }}Conn(ctx){{ end }}
conn := meta.(*conns.AWSClient).{{ .Service }}Client(ctx)
{{ if .IncludeComments }}
// TIP: -- 2. Get information about a resource from AWS using an API Get,
// List, or Describe-type function, or, better yet, using a finder. Data
Expand Down Expand Up @@ -225,7 +219,7 @@ func dataSource{{ .DataSource }}Read(ctx context.Context, d *schema.ResourceData
{{ if .IncludeComments }}
// TIP: Setting a JSON string to avoid errorneous diffs.
{{- end }}
p, err := verify.SecondJSONUnlessEquivalent(d.Get("policy").(string), {{ if .AWSGoSDKV2 }}aws.ToString{{ else }}aws.StringValue{{ end }}(out.Policy))
p, err := verify.SecondJSONUnlessEquivalent(d.Get("policy").(string), aws.ToString(out.Policy))
if err != nil {
return create.AppendDiagError(diags, names.{{ .Service }}, create.ErrActionSetting, DSName{{ .DataSource }}, d.Id(), err)
}
Expand Down
8 changes: 2 additions & 6 deletions skaff/datasource/datasourcefw.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,19 @@ import (
//
// The provider linter wants your imports to be in two groups: first,
// standard library (i.e., "fmt" or "strings"), second, everything else.
{{- end }}
{{- if and .IncludeComments .AWSGoSDKV2 }}
//
// Also, AWS Go SDK v2 may handle nested structures differently than v1,
// using the services/{{ .SDKPackage }}/types package. If so, you'll
// need to import types and reference the nested types, e.g., as
// awstypes.<Type Name>.
{{- end }}
"context"
{{ if .AWSGoSDKV2 }}

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/{{ .SDKPackage }}"
awstypes "github.com/aws/aws-sdk-go-v2/service/{{ .SDKPackage }}/types"
{{- else }}
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/{{ .SDKPackage }}"
{{- end }}
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -178,7 +174,7 @@ func (d *dataSource{{ .DataSource }}) Read(ctx context.Context, req datasource.R
{{- if .IncludeComments }}
// TIP: -- 1. Get a client connection to the relevant service
{{- end }}
conn := d.Meta().{{ .Service }}{{ if .AWSGoSDKV2 }}Client(ctx){{ else }}Conn(ctx){{ end }}
conn := d.Meta().{{ .Service }}Client(ctx)
{{ if .IncludeComments }}
// TIP: -- 2. Fetch the config
{{- end }}
Expand Down
17 changes: 0 additions & 17 deletions skaff/datasource/datasourcetest.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (
//
// The provider linter wants your imports to be in two groups: first,
// standard library (i.e., "fmt" or "strings"), second, everything else.
{{- end }}
{{- if and .IncludeComments .AWSGoSDKV2 }}
//
// Also, AWS Go SDK v2 may handle nested structures differently than v1,
// using the services/{{ .SDKPackage }}/types package. If so, you'll
Expand All @@ -44,14 +42,9 @@ import (
"testing"

"github.com/YakDriver/regexache"
{{- if .AWSGoSDKV2 }}
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/{{ .SDKPackage }}"
"github.com/aws/aws-sdk-go-v2/service/{{ .SDKPackage }}/types"
{{- else }}
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/{{ .SDKPackage }}"
{{- end }}
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand All @@ -67,9 +60,7 @@ import (
// any normal context constants, variables, or functions.
{{- end }}
tf{{ .ServicePackage }} "github.com/hashicorp/terraform-provider-aws/internal/service/{{ .ServicePackage }}"
{{- if .AWSGoSDKV2 }}
"github.com/hashicorp/terraform-provider-aws/names"
{{- end }}
)
{{ if .IncludeComments }}
// TIP: File Structure. The basic outline for all test files should be as
Expand Down Expand Up @@ -177,18 +168,10 @@ func TestAcc{{ .Service }}{{ .DataSource }}DataSource_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
{{- if .AWSGoSDKV2 }}
acctest.PreCheckPartitionHasService(t, names.{{ .Service }}EndpointID)
{{- else }}
acctest.PreCheckPartitionHasService(t, {{ .ServicePackage }}.EndpointsID)
{{- end }}
testAccPreCheck(ctx, t)
},
{{- if .AWSGoSDKV2 }}
ErrorCheck: acctest.ErrorCheck(t, names.{{ .Service }}ServiceID),
{{- else }}
ErrorCheck: acctest.ErrorCheck(t, {{ .ServicePackage }}.EndpointsID),
{{- end }}
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheck{{ .DataSource }}Destroy(ctx),
Steps: []resource.TestStep{
Expand Down
5 changes: 4 additions & 1 deletion skaff/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"text/template"

"github.com/hashicorp/terraform-provider-aws/names"
"github.com/hashicorp/terraform-provider-aws/skaff/convert"
)

Expand Down Expand Up @@ -43,7 +44,9 @@ func Create(name, snakeName, description string, comments, force bool) error {
return fmt.Errorf("snake name should be all lower case with underscores, if needed (e.g., arn_build)")
}

snakeName = convert.ToSnakeCase(name, snakeName)
if snakeName == "" {
snakeName = names.ToSnakeCase(name)
}

templateData := TemplateData{
Function: name,
Expand Down
1 change: 1 addition & 0 deletions skaff/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.59 // indirect
github.com/hashicorp/hcl/v2 v2.22.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions skaff/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.59 h1:j5ZrYJbLfZLJ9X5Bnp43z+ygN7kf6rbLCGIBGCIWWEA=
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.59/go.mod h1:jZEzCETkIzEinF749zPXsqpPmsA9P0fbMqRyoBh5UNo=
github.com/hashicorp/hcl/v2 v2.22.0 h1:hkZ3nCtqeJsDhPRFz5EA9iwcG1hNWGePOTw6oyul12M=
github.com/hashicorp/hcl/v2 v2.22.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
9 changes: 5 additions & 4 deletions skaff/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"text/template"

"github.com/hashicorp/terraform-provider-aws/names"
"github.com/hashicorp/terraform-provider-aws/names/data"
"github.com/hashicorp/terraform-provider-aws/skaff/convert"
)
Expand Down Expand Up @@ -42,13 +43,12 @@ type TemplateData struct {
Service string
ServiceLower string
AWSServiceName string
AWSGoSDKV2 bool
PluginFramework bool
HumanResourceName string
ProviderResourceName string
}

func Create(resName, snakeName string, comments, force, v2, pluginFramework, tags bool) error {
func Create(resName, snakeName string, comments, force, pluginFramework, tags bool) error {
wd, err := os.Getwd() // os.Getenv("GOPACKAGE") not available since this is not run with go generate
if err != nil {
return fmt.Errorf("error reading working directory: %s", err)
Expand All @@ -68,7 +68,9 @@ func Create(resName, snakeName string, comments, force, v2, pluginFramework, tag
return fmt.Errorf("error checking: snake name should be all lower case with underscores, if needed (e.g., db_instance)")
}

snakeName = convert.ToSnakeCase(resName, snakeName)
if snakeName == "" {
snakeName = names.ToSnakeCase(resName)
}

service, err := data.LookupService(servicePackage)
if err != nil {
Expand All @@ -87,7 +89,6 @@ func Create(resName, snakeName string, comments, force, v2, pluginFramework, tag
Service: service.ProviderNameUpper(),
ServiceLower: strings.ToLower(service.ProviderNameUpper()),
AWSServiceName: service.FullHumanFriendly(),
AWSGoSDKV2: v2,
PluginFramework: pluginFramework,
HumanResourceName: convert.ToHumanResName(resName),
ProviderResourceName: convert.ToProviderResourceName(servicePackage, snakeName),
Expand Down
Loading
Loading