Skip to content

Commit

Permalink
Merge pull request #38363 from DanielRieske/f/migrate-vpc-sdkv2
Browse files Browse the repository at this point in the history
Migrate remaining `EC2` service resources to AWS SDK V2
  • Loading branch information
ewbankkit authored Jul 19, 2024
2 parents ecbf793 + 8b16be0 commit ac15f48
Show file tree
Hide file tree
Showing 318 changed files with 13,541 additions and 15,000 deletions.
40 changes: 7 additions & 33 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
dstypes "github.com/aws/aws-sdk-go-v2/service/directoryservice/types"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/iam"
awstypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
"github.com/aws/aws-sdk-go-v2/service/inspector2"
inspector2types "github.com/aws/aws-sdk-go-v2/service/inspector2/types"
organizationstypes "github.com/aws/aws-sdk-go-v2/service/organizations/types"
Expand All @@ -40,7 +40,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/outposts"
"github.com/aws/aws-sdk-go/service/pinpoint"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
Expand Down Expand Up @@ -1188,7 +1187,7 @@ func PreCheckIAMServiceLinkedRoleWithProvider(ctx context.Context, t *testing.T,
input := &iam.ListRolesInput{
PathPrefix: aws.String(pathPrefix),
}
var role awstypes.Role
var role iamtypes.Role

pages := iam.NewListRolesPaginator(conn, input)
for pages.HasMorePages() {
Expand Down Expand Up @@ -2231,7 +2230,7 @@ data "aws_ec2_instance_type_offering" "%[1]s" {
`, name, strings.Join(preferredInstanceTypes, "\", \""))
}

func configLatestAmazonLinux2HVMEBSAMI(architecture string) string {
func configLatestAmazonLinux2HVMEBSAMI(architecture ec2types.ArchitectureValues) string {
return fmt.Sprintf(`
data "aws_ami" "amzn2-ami-minimal-hvm-ebs-%[1]s" {
most_recent = true
Expand Down Expand Up @@ -2259,14 +2258,14 @@ data "aws_ami" "amzn2-ami-minimal-hvm-ebs-%[1]s" {
// describes the latest Amazon Linux 2 x86_64 AMI using HVM virtualization and an EBS root device.
// The data source is named 'amzn2-ami-minimal-hvm-ebs-x86_64'.
func ConfigLatestAmazonLinux2HVMEBSX8664AMI() string {
return configLatestAmazonLinux2HVMEBSAMI(ec2.ArchitectureValuesX8664)
return configLatestAmazonLinux2HVMEBSAMI(ec2types.ArchitectureValuesX8664)
}

// ConfigLatestAmazonLinux2HVMEBSARM64AMI returns the configuration for a data source that
// describes the latest Amazon Linux 2 arm64 AMI using HVM virtualization and an EBS root device.
// The data source is named 'amzn2-ami-minimal-hvm-ebs-arm64'.
func ConfigLatestAmazonLinux2HVMEBSARM64AMI() string {
return configLatestAmazonLinux2HVMEBSAMI(ec2.ArchitectureValuesArm64)
return configLatestAmazonLinux2HVMEBSAMI(ec2types.ArchitectureValuesArm64)
}

func ConfigLambdaBase(policyName, roleName, sgName string) string {
Expand Down Expand Up @@ -2494,32 +2493,7 @@ resource "aws_subnet" "test" {
)
}

func CheckVPCExists(ctx context.Context, n string, v *ec2.Vpc) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no VPC ID is set")
}

conn := Provider.Meta().(*conns.AWSClient).EC2Conn(ctx)

output, err := tfec2.FindVPCByID(ctx, conn, rs.Primary.ID)

if err != nil {
return err
}

*v = *output

return nil
}
}

func CheckVPCExistsV2(ctx context.Context, n string, v *ec2types.Vpc) resource.TestCheckFunc {
func CheckVPCExists(ctx context.Context, n string, v *ec2types.Vpc) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -2532,7 +2506,7 @@ func CheckVPCExistsV2(ctx context.Context, n string, v *ec2types.Vpc) resource.T

conn := Provider.Meta().(*conns.AWSClient).EC2Client(ctx)

output, err := tfec2.FindVPCByIDV2(ctx, conn, rs.Primary.ID)
output, err := tfec2.FindVPCByID(ctx, conn, rs.Primary.ID)

if err != nil {
return err
Expand Down
5 changes: 0 additions & 5 deletions internal/conns/awsclient_gen.go

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

20 changes: 20 additions & 0 deletions internal/framework/flex/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ func StringFromFramework(ctx context.Context, v basetypes.StringValuable) *strin
return output
}

// StringValueFromFramework converts a Framework String value to a string.
// A null String is converted to an empty string.
func StringValueFromFramework(ctx context.Context, v basetypes.StringValuable) string {
var output string

must(Expand(ctx, v, &output))

return output
}

// StringFromFramework converts a single Framework String value to a string pointer slice.
// A null String is converted to a nil slice.
func StringSliceFromFramework(ctx context.Context, v basetypes.StringValuable) []*string {
Expand All @@ -32,6 +42,16 @@ func StringSliceFromFramework(ctx context.Context, v basetypes.StringValuable) [
return []*string{StringFromFramework(ctx, v)}
}

// StringSliceValueFromFramework converts a single Framework String value to a string slice.
// A null String is converted to a nil slice.
func StringSliceValueFromFramework(ctx context.Context, v basetypes.StringValuable) []string {
if v.IsNull() || v.IsUnknown() {
return nil
}

return []string{StringValueFromFramework(ctx, v)}
}

// StringValueToFramework converts a string value to a Framework String value.
// An empty string is converted to a null String.
func StringValueToFramework[T ~string](ctx context.Context, v T) types.String {
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/namevaluesfilters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Full documentation for this package can be found on [GoDoc](https://godoc.org/gi

Many AWS Go SDK services that support resource filtering have their service-specific Go type conversion functions to and from `NameValuesFilters` code generated. Converting from `NameValuesFilters` to AWS Go SDK types is done via `{SERVICE}Filters()` functions on the type. For more information about this code generation, see the [`generators/servicefilters` README](generators/servicefilters/README.md).

Any filtering functions that cannot be generated should be hand implemented in a service-specific source file (e.g. `ec2_filters.go`) and follow the format of similar generated code wherever possible. The first line of the source file should be `// +build !generate`. This prevents the file's inclusion during the code generation phase.
Any filtering functions that cannot be generated should be hand implemented in a service-specific source file and follow the format of similar generated code wherever possible. The first line of the source file should be `// +build !generate`. This prevents the file's inclusion during the code generation phase.

## Code Structure

Expand Down
24 changes: 0 additions & 24 deletions internal/generate/namevaluesfilters/ec2_filters.go

This file was deleted.

54 changes: 0 additions & 54 deletions internal/generate/namevaluesfilters/ec2_filters_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import (

const filename = `service_filters_gen.go`

// Representing types such as []*ec2.Filter, []*rds.Filter, ...
// Representing types such as []*fsx.Filter, []*rds.Filter, ...
var sliceServiceNames = []string{
"autoscaling",
"databasemigrationservice",
"docdb",
"ec2",
"elasticinference",
"elasticsearchservice",
"fsx",
Expand Down
23 changes: 0 additions & 23 deletions internal/generate/namevaluesfilters/service_filters_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/generate/tagresource/resource.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func resourceTagCreate(ctx context.Context, d *schema.ResourceData, meta interfa
value := d.Get(names.AttrValue).(string)

{{ if eq .ServicePackage "ec2" }}
if err := createTagsV2(ctx, conn, identifier, TagsV2(tftags.New(ctx, map[string]string{key: value}))); err != nil {
if err := createTags(ctx, conn, identifier, Tags(tftags.New(ctx, map[string]string{key: value}))); err != nil {
{{- else }}
if err := {{ .UpdateTagsFunc }}(ctx, conn, identifier, nil, map[string]string{key: value}); err != nil {
{{- end }}
Expand Down
8 changes: 4 additions & 4 deletions internal/service/batch/job_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"log"
"testing"

"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/batch"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -802,15 +802,15 @@ resource "aws_batch_compute_environment" "more" {

func testAccCheckLaunchTemplateDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_launch_template" {
continue
}

resp, err := conn.DescribeLaunchTemplatesWithContext(ctx, &ec2.DescribeLaunchTemplatesInput{
LaunchTemplateIds: []*string{aws.String(rs.Primary.ID)},
resp, err := conn.DescribeLaunchTemplates(ctx, &ec2.DescribeLaunchTemplatesInput{
LaunchTemplateIds: []string{rs.Primary.ID},
})

if err == nil {
Expand Down
Loading

0 comments on commit ac15f48

Please sign in to comment.