diff --git a/internal/acctest/acctest.go b/internal/acctest/acctest.go index b0e3e602e2de..8c72e6064430 100644 --- a/internal/acctest/acctest.go +++ b/internal/acctest/acctest.go @@ -17,6 +17,7 @@ import ( "testing" "time" + ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/aws/endpoints" @@ -2261,6 +2262,31 @@ func CheckVPCExists(ctx context.Context, n string, v *ec2.Vpc) resource.TestChec } } +func CheckVPCExistsV2(ctx context.Context, n string, v *ec2types.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).EC2Client(ctx) + + output, err := tfec2.FindVPCByIDV2(ctx, conn, rs.Primary.ID) + + if err != nil { + return err + } + + *v = *output + + return nil + } +} + func CheckCallerIdentityAccountID(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] diff --git a/internal/generate/tags/README.md b/internal/generate/tags/README.md index 2f70b22a5498..e8c8e84093d0 100644 --- a/internal/generate/tags/README.md +++ b/internal/generate/tags/README.md @@ -36,6 +36,7 @@ Some flags control generation a certain section of code, such as whether the gen | `TagInCustomVal` | | Tag input custom value | `-TagInCustomVal=aws.StringMap(updatedTags.IgnoreAWS().Map())` | | `TagInIDElem` | `ResourceArn` | Tag input identifier element | `-TagInIDElem=ResourceARN` | | `TagInIDNeedSlice` | | Tag input identifier needs a slice | `-TagInIDNeedSlice=yes` | +| `TagInIDNeedValueSlice` | | Tag input identifier needs a slice of values, rather than a slice of pointers | `-TagInIDNeedValueSlice=yes` | | `TagInTagsElem` | Tags | Tag input tags element | `-TagInTagsElem=TagsList` | | `TagKeyType` | | Tag key type | `-TagKeyType=TagKeyOnly` | | `TagOp` | `TagResource` | Tag operation | `-TagOp=AddTags` | diff --git a/internal/generate/tags/main.go b/internal/generate/tags/main.go index 1595a4b3d14b..3a87906d16aa 100644 --- a/internal/generate/tags/main.go +++ b/internal/generate/tags/main.go @@ -56,6 +56,7 @@ var ( tagInCustomVal = flag.String("TagInCustomVal", "", "tagInCustomVal") tagInIDElem = flag.String("TagInIDElem", "ResourceArn", "tagInIDElem") tagInIDNeedSlice = flag.String("TagInIDNeedSlice", "", "tagInIDNeedSlice") + tagInIDNeedValueSlice = flag.String("TagInIDNeedValueSlice", "", "tagInIDNeedValueSlice") tagInTagsElem = flag.String("TagInTagsElem", "Tags", "tagInTagsElem") tagKeyType = flag.String("TagKeyType", "", "tagKeyType") tagOp = flag.String("TagOp", "TagResource", "tagOp") @@ -170,6 +171,7 @@ type TemplateData struct { TagInCustomVal string TagInIDElem string TagInIDNeedSlice string + TagInIDNeedValueSlice string TagInTagsElem string TagKeyType string TagOp string @@ -316,6 +318,7 @@ func main() { TagInCustomVal: *tagInCustomVal, TagInIDElem: *tagInIDElem, TagInIDNeedSlice: *tagInIDNeedSlice, + TagInIDNeedValueSlice: *tagInIDNeedValueSlice, TagInTagsElem: *tagInTagsElem, TagKeyType: *tagKeyType, TagOp: *tagOp, diff --git a/internal/generate/tags/templates/v1/update_tags_body.tmpl b/internal/generate/tags/templates/v1/update_tags_body.tmpl index 7ff616acdc8f..61ca29707f7e 100644 --- a/internal/generate/tags/templates/v1/update_tags_body.tmpl +++ b/internal/generate/tags/templates/v1/update_tags_body.tmpl @@ -29,6 +29,8 @@ func {{ .UpdateTagsFunc }}(ctx context.Context, conn {{ .ClientType }}, identifi {{- if not ( .TagTypeIDElem ) }} {{- if .TagInIDNeedSlice }} {{ .TagInIDElem }}: aws.StringSlice([]string{identifier}), + {{- else if .TagInIDNeedValueSlice }} + {{ .TagInIDElem }}: []string{identifier}, {{- else }} {{ .TagInIDElem }}: aws.String(identifier), {{- end }} @@ -74,6 +76,8 @@ func {{ .UpdateTagsFunc }}(ctx context.Context, conn {{ .ClientType }}, identifi {{- if not ( .TagTypeIDElem ) }} {{- if .TagInIDNeedSlice }} {{ .TagInIDElem }}: aws.StringSlice([]string{identifier}), + {{- else if .TagInIDNeedValueSlice }} + {{ .TagInIDElem }}: []string{identifier}, {{- else }} {{ .TagInIDElem }}: aws.String(identifier), {{- end }} @@ -114,6 +118,8 @@ func {{ .UpdateTagsFunc }}(ctx context.Context, conn {{ .ClientType }}, identifi {{- if not ( .TagTypeIDElem ) }} {{- if .TagInIDNeedSlice }} {{ .TagInIDElem }}: aws.StringSlice([]string{identifier}), + {{- else if .TagInIDNeedValueSlice }} + {{ .TagInIDElem }}: []string{identifier}, {{- else }} {{ .TagInIDElem }}: aws.String(identifier), {{- end }} diff --git a/internal/generate/tags/templates/v2/update_tags_body.tmpl b/internal/generate/tags/templates/v2/update_tags_body.tmpl index 109af5497834..9ebc65cee222 100644 --- a/internal/generate/tags/templates/v2/update_tags_body.tmpl +++ b/internal/generate/tags/templates/v2/update_tags_body.tmpl @@ -29,6 +29,8 @@ func {{ .UpdateTagsFunc }}(ctx context.Context, conn {{ .ClientType }}, identifi {{- if not ( .TagTypeIDElem ) }} {{- if .TagInIDNeedSlice }} {{ .TagInIDElem }}: aws.StringSlice([]string{identifier}), + {{- else if .TagInIDNeedValueSlice }} + {{ .TagInIDElem }}: []string{identifier}, {{- else }} {{ .TagInIDElem }}: aws.String(identifier), {{- end }} @@ -74,6 +76,8 @@ func {{ .UpdateTagsFunc }}(ctx context.Context, conn {{ .ClientType }}, identifi {{- if not ( .TagTypeIDElem ) }} {{- if .TagInIDNeedSlice }} {{ .TagInIDElem }}: aws.StringSlice([]string{identifier}), + {{- else if .TagInIDNeedValueSlice }} + {{ .TagInIDElem }}: []string{identifier}, {{- else }} {{ .TagInIDElem }}: aws.String(identifier), {{- end }} @@ -114,6 +118,8 @@ func {{ .UpdateTagsFunc }}(ctx context.Context, conn {{ .ClientType }}, identifi {{- if not ( .TagTypeIDElem ) }} {{- if .TagInIDNeedSlice }} {{ .TagInIDElem }}: aws.StringSlice([]string{identifier}), + {{- else if .TagInIDNeedValueSlice }} + {{ .TagInIDElem }}: []string{identifier}, {{- else }} {{ .TagInIDElem }}: aws.String(identifier), {{- end }} diff --git a/internal/service/ec2/exports.go b/internal/service/ec2/exports.go new file mode 100644 index 000000000000..81ab90a92cc7 --- /dev/null +++ b/internal/service/ec2/exports.go @@ -0,0 +1,8 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ec2 + +var ( + FindVPCByIDV2 = findVPCByIDV2 +) diff --git a/internal/service/ec2/exports_test.go b/internal/service/ec2/exports_test.go index 086ddd126949..871f329d292a 100644 --- a/internal/service/ec2/exports_test.go +++ b/internal/service/ec2/exports_test.go @@ -9,5 +9,6 @@ var ( ResourceSecurityGroupEgressRule = newResourceSecurityGroupEgressRule ResourceSecurityGroupIngressRule = newResourceSecurityGroupIngressRule - UpdateTags = updateTags + UpdateTags = updateTags + UpdateTagsV2 = updateTagsV2 ) diff --git a/internal/service/ec2/filter.go b/internal/service/ec2/filter.go index 66596d57128f..14a74985bf9b 100644 --- a/internal/service/ec2/filter.go +++ b/internal/service/ec2/filter.go @@ -6,6 +6,7 @@ package ec2 import ( "sort" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" ) @@ -60,3 +61,33 @@ func NewFilter(name string, values []string) *ec2.Filter { Values: aws.StringSlice(values), } } + +func buildAttributeFilterListV2(m map[string]string) []awstypes.Filter { + var filters []awstypes.Filter + + // sort the filters by name to make the output deterministic + var names []string + for k := range m { + names = append(names, k) + } + + sort.Strings(names) + + for _, name := range names { + value := m[name] + if value == "" { + continue + } + + filters = append(filters, newFilterV2(name, []string{value})) + } + + return filters +} + +func newFilterV2(name string, values []string) awstypes.Filter { + return awstypes.Filter{ + Name: aws.String(name), + Values: values, + } +} diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go new file mode 100644 index 000000000000..a30ff2a13415 --- /dev/null +++ b/internal/service/ec2/findv2.go @@ -0,0 +1,289 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ec2 + +import ( + "context" + "fmt" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" +) + +func findVPCAttributeV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName) (bool, error) { + input := &ec2.DescribeVpcAttributeInput{ + Attribute: attribute, + VpcId: aws.String(vpcID), + } + + output, err := conn.DescribeVpcAttribute(ctx, input) + + if tfawserr.ErrCodeEquals(err, errCodeInvalidVPCIDNotFound) { + return false, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return false, err + } + + if output == nil { + return false, tfresource.NewEmptyResultError(input) + } + + var v *awstypes.AttributeBooleanValue + switch attribute { + case awstypes.VpcAttributeNameEnableDnsHostnames: + v = output.EnableDnsHostnames + case awstypes.VpcAttributeNameEnableDnsSupport: + v = output.EnableDnsSupport + case awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics: + v = output.EnableNetworkAddressUsageMetrics + default: + return false, fmt.Errorf("unsupported VPC attribute: %s", attribute) + } + + if v == nil { + return false, tfresource.NewEmptyResultError(input) + } + + return aws.ToBool(v.Value), nil +} + +func findVPCV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsInput) (*awstypes.Vpc, error) { + output, err := findVPCsV2(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsInput) ([]awstypes.Vpc, error) { + var output []awstypes.Vpc + + pages := ec2.NewDescribeVpcsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if tfawserr.ErrCodeEquals(err, errCodeInvalidVPCIDNotFound) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + output = append(output, page.Vpcs...) + } + + return output, nil +} + +func findVPCByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.Vpc, error) { + input := &ec2.DescribeVpcsInput{ + VpcIds: []string{id}, + } + + return findVPCV2(ctx, conn, input) +} + +func findVPCIPv6CIDRBlockAssociationByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.VpcIpv6CidrBlockAssociation, *awstypes.Vpc, error) { + input := &ec2.DescribeVpcsInput{ + Filters: buildAttributeFilterListV2(map[string]string{ + "ipv6-cidr-block-association.association-id": id, + }), + } + + vpc, err := findVPCV2(ctx, conn, input) + + if err != nil { + return nil, nil, err + } + + for _, association := range vpc.Ipv6CidrBlockAssociationSet { + if aws.ToString(association.AssociationId) == id { + if state := association.Ipv6CidrBlockState.State; state == awstypes.VpcCidrBlockStateCodeDisassociated { + return nil, nil, &retry.NotFoundError{Message: string(state)} + } + + return &association, vpc, nil + } + } + + return nil, nil, &retry.NotFoundError{} +} + +func findVPCDefaultNetworkACLV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.NetworkAcl, error) { + input := &ec2.DescribeNetworkAclsInput{ + Filters: buildAttributeFilterListV2(map[string]string{ + "default": "true", + "vpc-id": id, + }), + } + + return findNetworkACLV2(ctx, conn, input) +} + +func findNetworkACLV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNetworkAclsInput) (*awstypes.NetworkAcl, error) { + output, err := findNetworkACLsV2(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNetworkAclsInput) ([]awstypes.NetworkAcl, error) { + var output []awstypes.NetworkAcl + + pages := ec2.NewDescribeNetworkAclsPaginator(conn, input) + + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if tfawserr.ErrCodeEquals(err, errCodeInvalidNetworkACLIDNotFound) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + output = append(output, page.NetworkAcls...) + } + + return output, nil +} + +func FindVPCDefaultSecurityGroupV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.SecurityGroup, error) { + input := &ec2.DescribeSecurityGroupsInput{ + Filters: buildAttributeFilterListV2(map[string]string{ + "group-name": DefaultSecurityGroupName, + "vpc-id": id, + }), + } + + return findSecurityGroupV2(ctx, conn, input) +} + +func findVPCMainRouteTableV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.RouteTable, error) { + input := &ec2.DescribeRouteTablesInput{ + Filters: buildAttributeFilterListV2(map[string]string{ + "association.main": "true", + "vpc-id": id, + }), + } + + return findRouteTableV2(ctx, conn, input) +} + +func findRouteTableV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) (*awstypes.RouteTable, error) { + output, err := findRouteTablesV2(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) ([]awstypes.RouteTable, error) { + var output []awstypes.RouteTable + + pages := ec2.NewDescribeRouteTablesPaginator(conn, input) + + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if tfawserr.ErrCodeEquals(err, errCodeInvalidRouteTableIDNotFound) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + output = append(output, page.RouteTables...) + } + + return output, nil +} + +func findSecurityGroupV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) (*awstypes.SecurityGroup, error) { + output, err := findSecurityGroupsV2(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) ([]awstypes.SecurityGroup, error) { + var output []awstypes.SecurityGroup + + pages := ec2.NewDescribeSecurityGroupsPaginator(conn, input) + + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if tfawserr.ErrCodeEquals(err, errCodeInvalidGroupNotFound, errCodeInvalidSecurityGroupIDNotFound) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + output = append(output, page.SecurityGroups...) + } + + return output, nil +} + +func findIPAMPoolAllocationsV2(ctx context.Context, conn *ec2.Client, input *ec2.GetIpamPoolAllocationsInput) ([]awstypes.IpamPoolAllocation, error) { + var output []awstypes.IpamPoolAllocation + + pages := ec2.NewGetIpamPoolAllocationsPaginator(conn, input) + + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if tfawserr.ErrCodeEquals(err, errCodeInvalidIPAMPoolAllocationIdNotFound, errCodeInvalidIPAMPoolIdNotFound) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + output = append(output, page.IpamPoolAllocations...) + } + + return output, nil +} diff --git a/internal/service/ec2/generate.go b/internal/service/ec2/generate.go index 6cc0934d3793..70a48d5075f7 100644 --- a/internal/service/ec2/generate.go +++ b/internal/service/ec2/generate.go @@ -3,7 +3,7 @@ //go:generate go run ../../generate/tagresource/main.go -IDAttribName=resource_id //go:generate go run ../../generate/tags/main.go -GetTag -ListTags -ListTagsOp=DescribeTags -ListTagsInFiltIDName=resource-id -ListTagsInIDElem=Resources -ServiceTagsSlice -TagOp=CreateTags -TagInIDElem=Resources -TagInIDNeedSlice=yes -TagType2=TagDescription -UntagOp=DeleteTags -UntagInNeedTagType -UntagInTagsElem=Tags -UpdateTags -//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ServiceTagsSlice -TagsFunc=TagsV2 -KeyValueTagsFunc=keyValueTagsV2 -GetTagsInFunc=getTagsInV2 -SetTagsOutFunc=setTagsOutV2 -SkipAWSServiceImp -- tagsv2_gen.go +//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ServiceTagsSlice -TagsFunc=TagsV2 -KeyValueTagsFunc=keyValueTagsV2 -GetTagsInFunc=getTagsInV2 -SetTagsOutFunc=setTagsOutV2 -TagOp=CreateTags -TagInIDElem=Resources -TagInIDNeedValueSlice=yes -UntagOp=DeleteTags -UpdateTagsFunc=updateTagsV2 -UntagInNeedTagType -UntagInTagsElem=Tags -UpdateTags -- tagsv2_gen.go //go:generate go run ../../generate/listpages/main.go -ListOps=DescribeSpotFleetInstances,DescribeSpotFleetRequestHistory,DescribeVpcEndpointServices //go:generate go run ../../generate/servicepackage/main.go // ONLY generate directives and package declaration! Do not add anything else to this file. diff --git a/internal/service/ec2/statusv2.go b/internal/service/ec2/statusv2.go new file mode 100644 index 000000000000..a37abaac2166 --- /dev/null +++ b/internal/service/ec2/statusv2.go @@ -0,0 +1,62 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ec2 + +import ( + "context" + "strconv" + + "github.com/aws/aws-sdk-go-v2/service/ec2" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" +) + +func statusVPCStateV2(ctx context.Context, conn *ec2.Client, id string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, err := findVPCByIDV2(ctx, conn, id) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output, string(output.State), nil + } +} + +func statusVPCIPv6CIDRBlockAssociationStateV2(ctx context.Context, conn *ec2.Client, id string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, _, err := findVPCIPv6CIDRBlockAssociationByIDV2(ctx, conn, id) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output.Ipv6CidrBlockState, string(output.Ipv6CidrBlockState.State), nil + } +} + +func statusVPCAttributeValueV2(ctx context.Context, conn *ec2.Client, id string, attribute awstypes.VpcAttributeName) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + attributeValue, err := findVPCAttributeV2(ctx, conn, id, attribute) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return attributeValue, strconv.FormatBool(attributeValue), nil + } +} diff --git a/internal/service/ec2/tagsv2_gen.go b/internal/service/ec2/tagsv2_gen.go index a2547c97692e..1685ebba63e2 100644 --- a/internal/service/ec2/tagsv2_gen.go +++ b/internal/service/ec2/tagsv2_gen.go @@ -3,11 +3,14 @@ package ec2 import ( "context" + "fmt" "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/types" + "github.com/hashicorp/terraform-provider-aws/names" ) // []*SERVICE.Tag handling @@ -57,3 +60,43 @@ func setTagsOutV2(ctx context.Context, tags []awstypes.Tag) { inContext.TagsOut = types.Some(keyValueTagsV2(ctx, tags)) } } + +// updateTagsV2 updates ec2 service tags. +// The identifier is typically the Amazon Resource Name (ARN), although +// it may also be a different identifier depending on the service. +func updateTagsV2(ctx context.Context, conn *ec2.Client, identifier string, oldTagsMap, newTagsMap any) error { + oldTags := tftags.New(ctx, oldTagsMap) + newTags := tftags.New(ctx, newTagsMap) + + removedTags := oldTags.Removed(newTags) + removedTags = removedTags.IgnoreSystem(names.EC2) + if len(removedTags) > 0 { + input := &ec2.DeleteTagsInput{ + Resources: []string{identifier}, + Tags: TagsV2(removedTags), + } + + _, err := conn.DeleteTags(ctx, input) + + if err != nil { + return fmt.Errorf("untagging resource (%s): %w", identifier, err) + } + } + + updatedTags := oldTags.Updated(newTags) + updatedTags = updatedTags.IgnoreSystem(names.EC2) + if len(updatedTags) > 0 { + input := &ec2.CreateTagsInput{ + Resources: []string{identifier}, + Tags: TagsV2(updatedTags), + } + + _, err := conn.CreateTags(ctx, input) + + if err != nil { + return fmt.Errorf("tagging resource (%s): %w", identifier, err) + } + } + + return nil +} diff --git a/internal/service/ec2/vpc_.go b/internal/service/ec2/vpc_.go index 0ef134971bb7..8aaa044f5cb1 100644 --- a/internal/service/ec2/vpc_.go +++ b/internal/service/ec2/vpc_.go @@ -11,16 +11,18 @@ import ( "strings" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/arn" - "github.com/aws/aws-sdk-go/service/ec2" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/aws/arn" + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" @@ -111,8 +113,8 @@ func ResourceVPC() *schema.Resource { "instance_tenancy": { Type: schema.TypeString, Optional: true, - Default: ec2.TenancyDefault, - ValidateFunc: validation.StringInSlice([]string{ec2.TenancyDefault, ec2.TenancyDedicated}, false), + Default: types.TenancyDefault, + ValidateFunc: validation.StringInSlice(enum.Slice(types.TenancyDefault, types.TenancyDedicated), false), }, "ipv4_ipam_pool_id": { Type: schema.TypeString, @@ -175,12 +177,12 @@ func ResourceVPC() *schema.Resource { func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.CreateVpcInput{ AmazonProvidedIpv6CidrBlock: aws.Bool(d.Get("assign_generated_ipv6_cidr_block").(bool)), - InstanceTenancy: aws.String(d.Get("instance_tenancy").(string)), - TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypeVpc), + InstanceTenancy: types.Tenancy(d.Get("instance_tenancy").(string)), + TagSpecifications: getTagSpecificationsInV2(ctx, types.ResourceTypeVpc), } if v, ok := d.GetOk("cidr_block"); ok { @@ -192,7 +194,7 @@ func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interfa } if v, ok := d.GetOk("ipv4_netmask_length"); ok { - input.Ipv4NetmaskLength = aws.Int64(int64(v.(int))) + input.Ipv4NetmaskLength = aws.Int32(int32(v.(int))) } if v, ok := d.GetOk("ipv6_cidr_block"); ok { @@ -208,13 +210,21 @@ func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interfa } if v, ok := d.GetOk("ipv6_netmask_length"); ok { - input.Ipv6NetmaskLength = aws.Int64(int64(v.(int))) + input.Ipv6NetmaskLength = aws.Int32(int32(v.(int))) } - outputRaw, err := tfresource.RetryWhenAWSErrMessageContains(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return conn.CreateVpcWithContext(ctx, input) - // "UnsupportedOperation: The operation AllocateIpamPoolCidr is not supported. Account 123456789012 is not monitored by IPAM ipam-07b079e3392782a55." - }, errCodeUnsupportedOperation, "is not monitored by IPAM") + outputRaw, err := tfresource.RetryWhen(ctx, ec2PropagationTimeout, + func() (interface{}, error) { + return conn.CreateVpc(ctx, input) + }, + func(err error) (bool, error) { + // "UnsupportedOperation: The operation AllocateIpamPoolCidr is not supported. Account 123456789012 is not monitored by IPAM ipam-07b079e3392782a55." + if tfawserr.ErrMessageContains(err, errCodeUnsupportedOperation, "is not monitored by IPAM") { + return true, err + } + return false, err + }, + ) if err != nil { return sdkdiag.AppendErrorf(diags, "creating EC2 VPC: %s", err) @@ -222,21 +232,19 @@ func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interfa output := outputRaw.(*ec2.CreateVpcOutput) - d.SetId(aws.StringValue(output.Vpc.VpcId)) + d.SetId(aws.ToString(output.Vpc.VpcId)) - vpc, err := WaitVPCCreated(ctx, conn, d.Id()) + vpc, err := waitVPCCreatedV2(ctx, conn, d.Id()) if err != nil { return sdkdiag.AppendErrorf(diags, "waiting for EC2 VPC (%s) create: %s", d.Id(), err) } - if len(vpc.Ipv6CidrBlockAssociationSet) > 0 && vpc.Ipv6CidrBlockAssociationSet[0] != nil { - associationID := aws.StringValue(output.Vpc.Ipv6CidrBlockAssociationSet[0].AssociationId) + if len(vpc.Ipv6CidrBlockAssociationSet) > 0 { + associationID := aws.ToString(output.Vpc.Ipv6CidrBlockAssociationSet[0].AssociationId) - _, err = WaitVPCIPv6CIDRBlockAssociationCreated(ctx, conn, associationID, vpcIPv6CIDRBlockAssociationCreatedTimeout) - - if err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for EC2 VPC (%s) IPv6 CIDR block (%s) to become associated: %s", d.Id(), associationID, err) + if _, err := waitVPCIPv6CIDRBlockAssociationCreatedV2(ctx, conn, associationID, vpcIPv6CIDRBlockAssociationCreatedTimeout); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for EC2 VPC (%s) IPv6 CIDR block (%s) associated: %s", d.Id(), associationID, err) } } @@ -256,10 +264,10 @@ func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interfa func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) outputRaw, err := tfresource.RetryWhenNewResourceNotFound(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return FindVPCByID(ctx, conn, d.Id()) + return findVPCByIDV2(ctx, conn, d.Id()) }, d.IsNewResource()) if !d.IsNewResource() && tfresource.NotFound(err) { @@ -272,12 +280,12 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s): %s", d.Id(), err) } - vpc := outputRaw.(*ec2.Vpc) + vpc := outputRaw.(*types.Vpc) - ownerID := aws.StringValue(vpc.OwnerId) + ownerID := aws.ToString(vpc.OwnerId) arn := arn.ARN{ Partition: meta.(*conns.AWSClient).Partition, - Service: ec2.ServiceName, + Service: names.EC2, Region: meta.(*conns.AWSClient).Region, AccountID: ownerID, Resource: fmt.Sprintf("vpc/%s", d.Id()), @@ -289,36 +297,36 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface d.Set("owner_id", ownerID) if v, err := tfresource.RetryWhenNewResourceNotFound(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return FindVPCAttribute(ctx, conn, d.Id(), ec2.VpcAttributeNameEnableDnsHostnames) + return findVPCAttributeV2(ctx, conn, d.Id(), types.VpcAttributeNameEnableDnsHostnames) }, d.IsNewResource()); err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), ec2.VpcAttributeNameEnableDnsHostnames, err) + return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), types.VpcAttributeNameEnableDnsHostnames, err) } else { d.Set("enable_dns_hostnames", v) } if v, err := tfresource.RetryWhenNewResourceNotFound(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return FindVPCAttribute(ctx, conn, d.Id(), ec2.VpcAttributeNameEnableDnsSupport) + return findVPCAttributeV2(ctx, conn, d.Id(), types.VpcAttributeNameEnableDnsSupport) }, d.IsNewResource()); err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), ec2.VpcAttributeNameEnableDnsSupport, err) + return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), types.VpcAttributeNameEnableDnsSupport, err) } else { d.Set("enable_dns_support", v) } if v, err := tfresource.RetryWhenNewResourceNotFound(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return FindVPCAttribute(ctx, conn, d.Id(), ec2.VpcAttributeNameEnableNetworkAddressUsageMetrics) + return findVPCAttributeV2(ctx, conn, d.Id(), types.VpcAttributeNameEnableNetworkAddressUsageMetrics) }, d.IsNewResource()); err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), ec2.VpcAttributeNameEnableNetworkAddressUsageMetrics, err) + return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), types.VpcAttributeNameEnableNetworkAddressUsageMetrics, err) } else { d.Set("enable_network_address_usage_metrics", v) } - if v, err := FindVPCDefaultNetworkACL(ctx, conn, d.Id()); err != nil { + if v, err := findVPCDefaultNetworkACLV2(ctx, conn, d.Id()); err != nil { log.Printf("[WARN] Error reading EC2 VPC (%s) default NACL: %s", d.Id(), err) } else { d.Set("default_network_acl_id", v.NetworkAclId) } - if v, err := FindVPCMainRouteTable(ctx, conn, d.Id()); err != nil { + if v, err := findVPCMainRouteTableV2(ctx, conn, d.Id()); err != nil { log.Printf("[WARN] Error reading EC2 VPC (%s) main Route Table: %s", d.Id(), err) d.Set("default_route_table_id", nil) d.Set("main_route_table_id", nil) @@ -327,7 +335,7 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface d.Set("main_route_table_id", v.RouteTableId) } - if v, err := FindVPCDefaultSecurityGroup(ctx, conn, d.Id()); err != nil { + if v, err := FindVPCDefaultSecurityGroupV2(ctx, conn, d.Id()); err != nil { log.Printf("[WARN] Error reading EC2 VPC (%s) default Security Group: %s", d.Id(), err) d.Set("default_security_group_id", nil) } else { @@ -342,8 +350,8 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface d.Set("ipv6_ipam_pool_id", nil) d.Set("ipv6_netmask_length", nil) } else { - cidrBlock := aws.StringValue(ipv6CIDRBlockAssociation.Ipv6CidrBlock) - ipv6PoolID := aws.StringValue(ipv6CIDRBlockAssociation.Ipv6Pool) + cidrBlock := aws.ToString(ipv6CIDRBlockAssociation.Ipv6CidrBlock) + ipv6PoolID := aws.ToString(ipv6CIDRBlockAssociation.Ipv6Pool) isAmazonIPv6Pool := ipv6PoolID == amazonIPv6PoolID d.Set("assign_generated_ipv6_cidr_block", isAmazonIPv6Pool) d.Set("ipv6_association_id", ipv6CIDRBlockAssociation.AssociationId) @@ -373,14 +381,14 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface } } - setTagsOut(ctx, vpc.Tags) + setTagsOutV2(ctx, vpc.Tags) return diags } func resourceVPCUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) if d.HasChange("enable_dns_hostnames") { if err := modifyVPCDNSHostnames(ctx, conn, d.Id(), d.Get("enable_dns_hostnames").(bool)); err != nil { @@ -443,7 +451,7 @@ func resourceVPCUpdate(ctx context.Context, d *schema.ResourceData, meta interfa func resourceVPCDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.DeleteVpcInput{ VpcId: aws.String(d.Id()), @@ -451,7 +459,7 @@ func resourceVPCDelete(ctx context.Context, d *schema.ResourceData, meta interfa log.Printf("[INFO] Deleting EC2 VPC: %s", d.Id()) _, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, vpcDeletedTimeout, func() (interface{}, error) { - return conn.DeleteVpcWithContext(ctx, input) + return conn.DeleteVpc(ctx, input) }, errCodeDependencyViolation) if tfawserr.ErrCodeEquals(err, errCodeInvalidVPCIDNotFound) { @@ -463,7 +471,7 @@ func resourceVPCDelete(ctx context.Context, d *schema.ResourceData, meta interfa } _, err = tfresource.RetryUntilNotFound(ctx, vpcDeletedTimeout, func() (interface{}, error) { - return FindVPCByID(ctx, conn, d.Id()) + return findVPCByIDV2(ctx, conn, d.Id()) }) if err != nil { @@ -513,7 +521,7 @@ func resourceVPCCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v in if diff.HasChange("instance_tenancy") { old, new := diff.GetChange("instance_tenancy") - if old.(string) != ec2.TenancyDedicated || new.(string) != ec2.TenancyDefault { + if old.(string) != string(types.TenancyDedicated) || new.(string) != string(types.TenancyDefault) { diff.ForceNew("instance_tenancy") } } @@ -533,32 +541,31 @@ func resourceVPCCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v in // defaultIPv6CIDRBlockAssociation returns the "default" IPv6 CIDR block. // Try and find IPv6 CIDR block information, first by any stored association ID. // Then if no IPv6 CIDR block information is available, use the first associated IPv6 CIDR block. -func defaultIPv6CIDRBlockAssociation(vpc *ec2.Vpc, associationID string) *ec2.VpcIpv6CidrBlockAssociation { - var ipv6CIDRBlockAssociation *ec2.VpcIpv6CidrBlockAssociation +func defaultIPv6CIDRBlockAssociation(vpc *types.Vpc, associationID string) *types.VpcIpv6CidrBlockAssociation { + var ipv6CIDRBlockAssociation types.VpcIpv6CidrBlockAssociation if associationID != "" { for _, v := range vpc.Ipv6CidrBlockAssociationSet { - if state := aws.StringValue(v.Ipv6CidrBlockState.State); state == ec2.VpcCidrBlockStateCodeAssociated && aws.StringValue(v.AssociationId) == associationID { + if state := string(v.Ipv6CidrBlockState.State); state == string(types.VpcCidrBlockStateCodeAssociated) && aws.ToString(v.AssociationId) == associationID { ipv6CIDRBlockAssociation = v - break } } } - if ipv6CIDRBlockAssociation == nil { + if ipv6CIDRBlockAssociation == (types.VpcIpv6CidrBlockAssociation{}) { for _, v := range vpc.Ipv6CidrBlockAssociationSet { - if aws.StringValue(v.Ipv6CidrBlockState.State) == ec2.VpcCidrBlockStateCodeAssociated { + if string(v.Ipv6CidrBlockState.State) == string(types.VpcCidrBlockStateCodeAssociated) { ipv6CIDRBlockAssociation = v } } } - return ipv6CIDRBlockAssociation + return &ipv6CIDRBlockAssociation } type vpcInfo struct { - vpc *ec2.Vpc + vpc *types.Vpc enableDnsHostnames bool enableDnsSupport bool enableNetworkAddressUsageMetrics bool @@ -566,7 +573,7 @@ type vpcInfo struct { // modifyVPCAttributesOnCreate sets VPC attributes on resource Create. // Called after new VPC creation or existing default VPC adoption. -func modifyVPCAttributesOnCreate(ctx context.Context, conn *ec2.EC2, d *schema.ResourceData, vpcInfo *vpcInfo) error { +func modifyVPCAttributesOnCreate(ctx context.Context, conn *ec2.Client, d *schema.ResourceData, vpcInfo *vpcInfo) error { if new, old := d.Get("enable_dns_hostnames").(bool), vpcInfo.enableDnsHostnames; old != new { if err := modifyVPCDNSHostnames(ctx, conn, d.Id(), new); err != nil { return err @@ -588,57 +595,57 @@ func modifyVPCAttributesOnCreate(ctx context.Context, conn *ec2.EC2, d *schema.R return nil } -func modifyVPCDNSHostnames(ctx context.Context, conn *ec2.EC2, vpcID string, v bool) error { +func modifyVPCDNSHostnames(ctx context.Context, conn *ec2.Client, vpcID string, v bool) error { input := &ec2.ModifyVpcAttributeInput{ - EnableDnsHostnames: &ec2.AttributeBooleanValue{ + EnableDnsHostnames: &types.AttributeBooleanValue{ Value: aws.Bool(v), }, VpcId: aws.String(vpcID), } - if _, err := conn.ModifyVpcAttributeWithContext(ctx, input); err != nil { + if _, err := conn.ModifyVpcAttribute(ctx, input); err != nil { return fmt.Errorf("modifying EnableDnsHostnames: %w", err) } - if _, err := WaitVPCAttributeUpdated(ctx, conn, vpcID, ec2.VpcAttributeNameEnableDnsHostnames, v); err != nil { + if _, err := waitVPCAttributeUpdatedV2(ctx, conn, vpcID, types.VpcAttributeNameEnableDnsHostnames, v); err != nil { return fmt.Errorf("modifying EnableDnsHostnames: waiting for completion: %w", err) } return nil } -func modifyVPCDNSSupport(ctx context.Context, conn *ec2.EC2, vpcID string, v bool) error { +func modifyVPCDNSSupport(ctx context.Context, conn *ec2.Client, vpcID string, v bool) error { input := &ec2.ModifyVpcAttributeInput{ - EnableDnsSupport: &ec2.AttributeBooleanValue{ + EnableDnsSupport: &types.AttributeBooleanValue{ Value: aws.Bool(v), }, VpcId: aws.String(vpcID), } - if _, err := conn.ModifyVpcAttributeWithContext(ctx, input); err != nil { + if _, err := conn.ModifyVpcAttribute(ctx, input); err != nil { return fmt.Errorf("modifying EnableDnsSupport: %w", err) } - if _, err := WaitVPCAttributeUpdated(ctx, conn, vpcID, ec2.VpcAttributeNameEnableDnsSupport, v); err != nil { + if _, err := waitVPCAttributeUpdatedV2(ctx, conn, vpcID, types.VpcAttributeNameEnableDnsSupport, v); err != nil { return fmt.Errorf("modifying EnableDnsSupport: waiting for completion: %w", err) } return nil } -func modifyVPCNetworkAddressUsageMetrics(ctx context.Context, conn *ec2.EC2, vpcID string, v bool) error { +func modifyVPCNetworkAddressUsageMetrics(ctx context.Context, conn *ec2.Client, vpcID string, v bool) error { input := &ec2.ModifyVpcAttributeInput{ - EnableNetworkAddressUsageMetrics: &ec2.AttributeBooleanValue{ + EnableNetworkAddressUsageMetrics: &types.AttributeBooleanValue{ Value: aws.Bool(v), }, VpcId: aws.String(vpcID), } - if _, err := conn.ModifyVpcAttributeWithContext(ctx, input); err != nil { + if _, err := conn.ModifyVpcAttribute(ctx, input); err != nil { return fmt.Errorf("modifying EnableNetworkAddressUsageMetrics: %w", err) } - if _, err := WaitVPCAttributeUpdated(ctx, conn, vpcID, ec2.VpcAttributeNameEnableNetworkAddressUsageMetrics, v); err != nil { + if _, err := waitVPCAttributeUpdatedV2(ctx, conn, vpcID, types.VpcAttributeNameEnableNetworkAddressUsageMetrics, v); err != nil { return fmt.Errorf("modifying EnableNetworkAddressUsageMetrics: waiting for completion: %w", err) } @@ -647,21 +654,19 @@ func modifyVPCNetworkAddressUsageMetrics(ctx context.Context, conn *ec2.EC2, vpc // modifyVPCIPv6CIDRBlockAssociation modify's a VPC's IPv6 CIDR block association. // Any exiting association is deleted and any new association's ID is returned. -func modifyVPCIPv6CIDRBlockAssociation(ctx context.Context, conn *ec2.EC2, vpcID, associationID string, amazonProvidedCIDRBlock bool, cidrBlock, ipamPoolID string, netmaskLength int, networkBorderGroup string) (string, error) { +func modifyVPCIPv6CIDRBlockAssociation(ctx context.Context, conn *ec2.Client, vpcID, associationID string, amazonProvidedCIDRBlock bool, cidrBlock, ipamPoolID string, netmaskLength int, networkBorderGroup string) (string, error) { if associationID != "" { input := &ec2.DisassociateVpcCidrBlockInput{ AssociationId: aws.String(associationID), } - _, err := conn.DisassociateVpcCidrBlockWithContext(ctx, input) + _, err := conn.DisassociateVpcCidrBlock(ctx, input) if err != nil { return "", fmt.Errorf("disassociating IPv6 CIDR block (%s): %w", associationID, err) } - _, err = WaitVPCIPv6CIDRBlockAssociationDeleted(ctx, conn, associationID, vpcIPv6CIDRBlockAssociationDeletedTimeout) - - if err != nil { + if _, err := waitVPCIPv6CIDRBlockAssociationDeletedV2(ctx, conn, associationID, vpcIPv6CIDRBlockAssociationDeletedTimeout); err != nil { return "", fmt.Errorf("disassociating IPv6 CIDR block (%s): waiting for completion: %w", associationID, err) } } @@ -688,20 +693,18 @@ func modifyVPCIPv6CIDRBlockAssociation(ctx context.Context, conn *ec2.EC2, vpcID } if netmaskLength > 0 { - input.Ipv6NetmaskLength = aws.Int64(int64(netmaskLength)) + input.Ipv6NetmaskLength = aws.Int32(int32(netmaskLength)) } - output, err := conn.AssociateVpcCidrBlockWithContext(ctx, input) + output, err := conn.AssociateVpcCidrBlock(ctx, input) if err != nil { return "", fmt.Errorf("associating IPv6 CIDR block: %w", err) } - associationID = aws.StringValue(output.Ipv6CidrBlockAssociation.AssociationId) + associationID = aws.ToString(output.Ipv6CidrBlockAssociation.AssociationId) - _, err = WaitVPCIPv6CIDRBlockAssociationCreated(ctx, conn, associationID, vpcIPv6CIDRBlockAssociationCreatedTimeout) - - if err != nil { + if _, err := waitVPCIPv6CIDRBlockAssociationCreatedV2(ctx, conn, associationID, vpcIPv6CIDRBlockAssociationCreatedTimeout); err != nil { return "", fmt.Errorf("associating IPv6 CIDR block: waiting for completion: %w", err) } } @@ -709,32 +712,32 @@ func modifyVPCIPv6CIDRBlockAssociation(ctx context.Context, conn *ec2.EC2, vpcID return associationID, nil } -func modifyVPCTenancy(ctx context.Context, conn *ec2.EC2, vpcID string, v string) error { +func modifyVPCTenancy(ctx context.Context, conn *ec2.Client, vpcID string, v string) error { input := &ec2.ModifyVpcTenancyInput{ - InstanceTenancy: aws.String(v), + InstanceTenancy: types.VpcTenancy(v), VpcId: aws.String(vpcID), } - if _, err := conn.ModifyVpcTenancyWithContext(ctx, input); err != nil { + if _, err := conn.ModifyVpcTenancy(ctx, input); err != nil { return fmt.Errorf("modifying Tenancy: %w", err) } return nil } -func findIPAMPoolAllocationsForVPC(ctx context.Context, conn *ec2.EC2, poolID, vpcID string) ([]*ec2.IpamPoolAllocation, error) { +func findIPAMPoolAllocationsForVPC(ctx context.Context, conn *ec2.Client, poolID, vpcID string) ([]types.IpamPoolAllocation, error) { input := &ec2.GetIpamPoolAllocationsInput{ IpamPoolId: aws.String(poolID), } - output, err := FindIPAMPoolAllocations(ctx, conn, input) + output, err := findIPAMPoolAllocationsV2(ctx, conn, input) if err != nil { return nil, err } - output = slices.Filter(output, func(v *ec2.IpamPoolAllocation) bool { - return aws.StringValue(v.ResourceType) == ec2.IpamPoolAllocationResourceTypeVpc && aws.StringValue(v.ResourceId) == vpcID + output = slices.Filter(output, func(v types.IpamPoolAllocation) bool { + return string(v.ResourceType) == string(types.IpamPoolAllocationResourceTypeVpc) && aws.ToString(v.ResourceId) == vpcID }) if len(output) == 0 { diff --git a/internal/service/ec2/vpc_default_vpc.go b/internal/service/ec2/vpc_default_vpc.go index 4abae394a824..09badf17c125 100644 --- a/internal/service/ec2/vpc_default_vpc.go +++ b/internal/service/ec2/vpc_default_vpc.go @@ -7,8 +7,9 @@ import ( "context" "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -154,10 +155,10 @@ func ResourceDefaultVPC() *schema.Resource { func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { // nosemgrep:ci.semgrep.tags.calling-UpdateTags-in-resource-create var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.DescribeVpcsInput{ - Filters: BuildAttributeFilterList( + Filters: buildAttributeFilterListV2( map[string]string{ "isDefault": "true", }, @@ -165,36 +166,34 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta } vpcInfo := &vpcInfo{} - vpc, err := FindVPC(ctx, conn, input) + vpc, err := findVPCV2(ctx, conn, input) if err == nil { - log.Printf("[INFO] Found existing EC2 Default VPC") - d.SetId(aws.StringValue(vpc.VpcId)) + d.SetId(aws.ToString(vpc.VpcId)) d.Set("existing_default_vpc", true) vpcInfo.vpc = vpc - if v, err := FindVPCAttribute(ctx, conn, d.Id(), ec2.VpcAttributeNameEnableDnsHostnames); err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), ec2.VpcAttributeNameEnableDnsHostnames, err) + if v, err := findVPCAttributeV2(ctx, conn, d.Id(), awstypes.VpcAttributeNameEnableDnsHostnames); err != nil { + return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), awstypes.VpcAttributeNameEnableDnsHostnames, err) } else { vpcInfo.enableDnsHostnames = v } - if v, err := FindVPCAttribute(ctx, conn, d.Id(), ec2.VpcAttributeNameEnableDnsSupport); err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), ec2.VpcAttributeNameEnableDnsSupport, err) + if v, err := findVPCAttributeV2(ctx, conn, d.Id(), awstypes.VpcAttributeNameEnableDnsSupport); err != nil { + return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), awstypes.VpcAttributeNameEnableDnsSupport, err) } else { vpcInfo.enableDnsSupport = v } - if v, err := FindVPCAttribute(ctx, conn, d.Id(), ec2.VpcAttributeNameEnableNetworkAddressUsageMetrics); err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), ec2.VpcAttributeNameEnableNetworkAddressUsageMetrics, err) + if v, err := findVPCAttributeV2(ctx, conn, d.Id(), awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics); err != nil { + return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics, err) } else { vpcInfo.enableNetworkAddressUsageMetrics = v } } else if tfresource.NotFound(err) { input := &ec2.CreateDefaultVpcInput{} - log.Printf("[DEBUG] Creating EC2 Default VPC: %s", input) - output, err := conn.CreateDefaultVpcWithContext(ctx, input) + output, err := conn.CreateDefaultVpc(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating EC2 Default VPC: %s", err) @@ -202,10 +201,10 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta vpc = output.Vpc - d.SetId(aws.StringValue(vpc.VpcId)) + d.SetId(aws.ToString(vpc.VpcId)) d.Set("existing_default_vpc", false) - vpc, err = WaitVPCCreated(ctx, conn, d.Id()) + vpc, err = waitVPCCreatedV2(ctx, conn, d.Id()) if err != nil { return sdkdiag.AppendErrorf(diags, "waiting for EC2 Default VPC (%s) create: %s", d.Id(), err) @@ -228,10 +227,10 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta var oldAssignGeneratedIPv6CIDRBlock bool if v := defaultIPv6CIDRBlockAssociation(vpcInfo.vpc, ""); v != nil { - associationID = aws.StringValue(v.AssociationId) - oldIPv6CIDRBlock = aws.StringValue(v.Ipv6CidrBlock) - oldIPv6CIDRBlockNetworkBorderGroup = aws.StringValue(v.NetworkBorderGroup) - if ipv6PoolID := aws.StringValue(v.Ipv6Pool); ipv6PoolID == amazonIPv6PoolID { + associationID = aws.ToString(v.AssociationId) + oldIPv6CIDRBlock = aws.ToString(v.Ipv6CidrBlock) + oldIPv6CIDRBlockNetworkBorderGroup = aws.ToString(v.NetworkBorderGroup) + if ipv6PoolID := aws.ToString(v.Ipv6Pool); ipv6PoolID == amazonIPv6PoolID { oldAssignGeneratedIPv6CIDRBlock = true } else { oldIPv6PoolID = ipv6PoolID @@ -272,11 +271,11 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta // Configure tags. ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig - newTags := KeyValueTags(ctx, getTagsIn(ctx)) - oldTags := KeyValueTags(ctx, vpc.Tags).IgnoreSystem(names.EC2).IgnoreConfig(ignoreTagsConfig) + newTags := keyValueTagsV2(ctx, getTagsInV2(ctx)) + oldTags := keyValueTagsV2(ctx, vpc.Tags).IgnoreSystem(names.EC2).IgnoreConfig(ignoreTagsConfig) if !oldTags.Equal(newTags) { - if err := updateTags(ctx, conn, d.Id(), oldTags, newTags); err != nil { + if err := updateTagsV2(ctx, conn, d.Id(), oldTags, newTags); err != nil { return sdkdiag.AppendErrorf(diags, "updating EC2 Default VPC (%s) tags: %s", d.Id(), err) } } diff --git a/internal/service/ec2/vpc_test.go b/internal/service/ec2/vpc_test.go index ae5c1e31e0fa..9362a0c7d12f 100644 --- a/internal/service/ec2/vpc_test.go +++ b/internal/service/ec2/vpc_test.go @@ -10,9 +10,9 @@ import ( "strings" "testing" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/aws/aws-sdk-go/aws/endpoints" - "github.com/aws/aws-sdk-go/service/ec2" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -20,23 +20,24 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" tfec2 "github.com/hashicorp/terraform-provider-aws/internal/service/ec2" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/names" ) func TestAccVPC_basic(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_basic, Check: resource.ComposeAggregateTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "ec2", regexp.MustCompile(`vpc/vpc-.+`)), resource.TestCheckResourceAttr(resourceName, "assign_generated_ipv6_cidr_block", "false"), resource.TestCheckResourceAttr(resourceName, "cidr_block", "10.1.0.0/16"), @@ -71,19 +72,19 @@ func TestAccVPC_basic(t *testing.T) { func TestAccVPC_disappears(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_basic, Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), acctest.CheckResourceDisappears(ctx, acctest.Provider, tfec2.ResourceVPC(), resourceName), ), ExpectNonEmptyPlan: true, @@ -94,19 +95,19 @@ func TestAccVPC_disappears(t *testing.T) { func TestAccVPC_tags(t *testing.T) { ctx := acctest.Context(t) - var vpc1, vpc2, vpc3 ec2.Vpc + var vpc1, vpc2, vpc3 awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_tags1("key1", "value1"), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc1), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc1), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), ), @@ -119,7 +120,7 @@ func TestAccVPC_tags(t *testing.T) { { Config: testAccVPCConfig_tags2("key1", "value1updated", "key2", "value2"), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc2), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc2), testAccCheckVPCIDsEqual(&vpc2, &vpc1), resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), @@ -129,7 +130,7 @@ func TestAccVPC_tags(t *testing.T) { { Config: testAccVPCConfig_tags1("key2", "value2"), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc3), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc3), testAccCheckVPCIDsEqual(&vpc3, &vpc2), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), @@ -141,19 +142,19 @@ func TestAccVPC_tags(t *testing.T) { func TestAccVPC_tags_computed(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_tags_computed, Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttrSet(resourceName, "tags.eip"), ), @@ -164,19 +165,19 @@ func TestAccVPC_tags_computed(t *testing.T) { func TestAccVPC_tags_null(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_tags_null, Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), }, @@ -186,12 +187,12 @@ func TestAccVPC_tags_null(t *testing.T) { func TestAccVPC_DefaultTags_zeroValue(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ @@ -201,7 +202,7 @@ func TestAccVPC_DefaultTags_zeroValue(t *testing.T) { testAccVPCConfig_basic, ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.key1", ""), @@ -213,7 +214,7 @@ func TestAccVPC_DefaultTags_zeroValue(t *testing.T) { testAccVPCConfig_tags1("key2", ""), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "2"), resource.TestCheckResourceAttr(resourceName, "tags_all.key1", ""), @@ -226,7 +227,7 @@ func TestAccVPC_DefaultTags_zeroValue(t *testing.T) { testAccVPCConfig_tags1("key2", ""), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "2"), resource.TestCheckResourceAttr(resourceName, "tags_all.key1", "value1"), @@ -239,12 +240,12 @@ func TestAccVPC_DefaultTags_zeroValue(t *testing.T) { func TestAccVPC_DefaultTags_providerOnlyTestAccVPC_DefaultTags_providerOnly(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ @@ -254,7 +255,7 @@ func TestAccVPC_DefaultTags_providerOnlyTestAccVPC_DefaultTags_providerOnly(t *t testAccVPCConfig_basic, ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.providerkey1", "providervalue1"), @@ -271,7 +272,7 @@ func TestAccVPC_DefaultTags_providerOnlyTestAccVPC_DefaultTags_providerOnly(t *t testAccVPCConfig_basic, ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "2"), resource.TestCheckResourceAttr(resourceName, "tags_all.providerkey1", "providervalue1"), @@ -284,7 +285,7 @@ func TestAccVPC_DefaultTags_providerOnlyTestAccVPC_DefaultTags_providerOnly(t *t testAccVPCConfig_basic, ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.providerkey1", "value1"), @@ -296,19 +297,19 @@ func TestAccVPC_DefaultTags_providerOnlyTestAccVPC_DefaultTags_providerOnly(t *t func TestAccVPC_DefaultTags_updateToProviderOnly(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_tags1("key1", "value1"), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), @@ -321,7 +322,7 @@ func TestAccVPC_DefaultTags_updateToProviderOnly(t *testing.T) { testAccVPCConfig_basic, ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.key1", "value1"), @@ -338,12 +339,12 @@ func TestAccVPC_DefaultTags_updateToProviderOnly(t *testing.T) { func TestAccVPC_DefaultTags_updateToResourceOnly(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ @@ -353,7 +354,7 @@ func TestAccVPC_DefaultTags_updateToResourceOnly(t *testing.T) { testAccVPCConfig_basic, ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.key1", "value1"), @@ -362,7 +363,7 @@ func TestAccVPC_DefaultTags_updateToResourceOnly(t *testing.T) { { Config: testAccVPCConfig_tags1("key1", "value1"), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), @@ -380,12 +381,12 @@ func TestAccVPC_DefaultTags_updateToResourceOnly(t *testing.T) { func TestAccVPC_DefaultTagsProviderAndResource_nonOverlappingTag(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ @@ -395,7 +396,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_nonOverlappingTag(t *testing.T) { testAccVPCConfig_tags1("resourcekey1", "resourcevalue1"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "2"), resource.TestCheckResourceAttr(resourceName, "tags.resourcekey1", "resourcevalue1"), @@ -414,7 +415,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_nonOverlappingTag(t *testing.T) { testAccVPCConfig_tags2("resourcekey1", "resourcevalue1", "resourcekey2", "resourcevalue2"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "3"), resource.TestCheckResourceAttr(resourceName, "tags.resourcekey1", "resourcevalue1"), @@ -430,7 +431,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_nonOverlappingTag(t *testing.T) { testAccVPCConfig_tags1("resourcekey3", "resourcevalue3"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "2"), resource.TestCheckResourceAttr(resourceName, "tags.resourcekey3", "resourcevalue3"), @@ -444,12 +445,12 @@ func TestAccVPC_DefaultTagsProviderAndResource_nonOverlappingTag(t *testing.T) { func TestAccVPC_DefaultTagsProviderAndResource_overlappingTag(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ @@ -459,7 +460,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_overlappingTag(t *testing.T) { testAccVPCConfig_tags1("overlapkey1", "resourcevalue1"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags.overlapkey1", "resourcevalue1"), @@ -476,7 +477,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_overlappingTag(t *testing.T) { testAccVPCConfig_tags2("overlapkey1", "resourcevalue1", "overlapkey2", "resourcevalue2"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "2"), resource.TestCheckResourceAttr(resourceName, "tags.overlapkey1", "resourcevalue1"), @@ -491,7 +492,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_overlappingTag(t *testing.T) { testAccVPCConfig_tags1("overlapkey1", "resourcevalue2"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags.overlapkey1", "resourcevalue2"), @@ -504,12 +505,12 @@ func TestAccVPC_DefaultTagsProviderAndResource_overlappingTag(t *testing.T) { func TestAccVPC_DefaultTagsProviderAndResource_duplicateTag(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: nil, Steps: []resource.TestStep{ @@ -519,7 +520,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_duplicateTag(t *testing.T) { testAccVPCConfig_tags1("overlapkey", "overlapvalue"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), ), @@ -530,12 +531,12 @@ func TestAccVPC_DefaultTagsProviderAndResource_duplicateTag(t *testing.T) { func TestAccVPC_DefaultTagsProviderAndResource_moveDuplicateTags(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: nil, Steps: []resource.TestStep{ @@ -544,7 +545,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_moveDuplicateTags(t *testing.T) { testAccVPCConfig_tags1("overlapkey", "overlapvalue"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), ), @@ -555,7 +556,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_moveDuplicateTags(t *testing.T) { acctest.ConfigDefaultTags_Tags1("overlapkey", "overlapvalue"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), ), @@ -565,7 +566,7 @@ func TestAccVPC_DefaultTagsProviderAndResource_moveDuplicateTags(t *testing.T) { testAccVPCConfig_tags1("overlapkey", "overlapvalue"), ), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"), ), @@ -581,19 +582,19 @@ func TestAccVPC_DefaultTagsProviderAndResource_moveDuplicateTags(t *testing.T) { // Reference: https://github.com/hashicorp/terraform-provider-aws/issues/18366 func TestAccVPC_DynamicResourceTagsMergedWithLocals_ignoreChanges(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_ignoreChangesDynamicTagsMergedLocals("localkey", "localvalue"), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "3"), resource.TestCheckResourceAttr(resourceName, "tags.localkey", "localvalue"), resource.TestCheckResourceAttrSet(resourceName, "tags.created_at"), @@ -611,7 +612,7 @@ func TestAccVPC_DynamicResourceTagsMergedWithLocals_ignoreChanges(t *testing.T) { Config: testAccVPCConfig_ignoreChangesDynamicTagsMergedLocals("localkey", "localvalue"), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "3"), resource.TestCheckResourceAttr(resourceName, "tags.localkey", "localvalue"), resource.TestCheckResourceAttrSet(resourceName, "tags.created_at"), @@ -636,19 +637,19 @@ func TestAccVPC_DynamicResourceTagsMergedWithLocals_ignoreChanges(t *testing.T) // Reference: https://github.com/hashicorp/terraform-provider-aws/issues/18366 func TestAccVPC_DynamicResourceTags_ignoreChanges(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_ignoreChangesDynamicTags, Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), resource.TestCheckResourceAttrSet(resourceName, "tags.created_at"), resource.TestCheckResourceAttrSet(resourceName, "tags.updated_at"), @@ -664,7 +665,7 @@ func TestAccVPC_DynamicResourceTags_ignoreChanges(t *testing.T) { { Config: testAccVPCConfig_ignoreChangesDynamicTags, Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), resource.TestCheckResourceAttrSet(resourceName, "tags.created_at"), resource.TestCheckResourceAttrSet(resourceName, "tags.updated_at"), @@ -682,19 +683,19 @@ func TestAccVPC_DynamicResourceTags_ignoreChanges(t *testing.T) { func TestAccVPC_defaultAndIgnoreTags(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_tags1("key1", "value1"), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), testAccCheckVPCUpdateTags(ctx, &vpc, nil, map[string]string{"defaultkey1": "defaultvalue1"}), ), ExpectNonEmptyPlan: true, @@ -719,19 +720,19 @@ func TestAccVPC_defaultAndIgnoreTags(t *testing.T) { func TestAccVPC_ignoreTags(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_tags1("key1", "value1"), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), testAccCheckVPCUpdateTags(ctx, &vpc, nil, map[string]string{"ignorekey1": "ignorevalue1"}), ), ExpectNonEmptyPlan: true, @@ -750,21 +751,21 @@ func TestAccVPC_ignoreTags(t *testing.T) { func TestAccVPC_tenancy(t *testing.T) { ctx := acctest.Context(t) - var vpcDedicated ec2.Vpc - var vpcDefault ec2.Vpc + var vpcDedicated awstypes.Vpc + var vpcDefault awstypes.Vpc resourceName := "aws_vpc.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_dedicatedTenancy(rName), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpcDedicated), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpcDedicated), resource.TestCheckResourceAttr(resourceName, "instance_tenancy", "dedicated"), ), }, @@ -776,7 +777,7 @@ func TestAccVPC_tenancy(t *testing.T) { { Config: testAccVPCConfig_default(rName), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpcDefault), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpcDefault), resource.TestCheckResourceAttr(resourceName, "instance_tenancy", "default"), testAccCheckVPCIDsEqual(&vpcDedicated, &vpcDefault), ), @@ -784,7 +785,7 @@ func TestAccVPC_tenancy(t *testing.T) { { Config: testAccVPCConfig_dedicatedTenancy(rName), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpcDedicated), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpcDedicated), resource.TestCheckResourceAttr(resourceName, "instance_tenancy", "dedicated"), testAccCheckVPCIDsNotEqual(&vpcDedicated, &vpcDefault), ), @@ -795,27 +796,27 @@ func TestAccVPC_tenancy(t *testing.T) { func TestAccVPC_updateDNSHostnames(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_default(rName), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "enable_dns_hostnames", "false"), ), }, { Config: testAccVPCConfig_enableDNSHostnames(rName), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "enable_dns_hostnames", "true"), ), }, @@ -826,20 +827,20 @@ func TestAccVPC_updateDNSHostnames(t *testing.T) { // https://github.com/hashicorp/terraform/issues/1301 func TestAccVPC_bothDNSOptionsSet(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_bothDNSOptions(rName), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "enable_dns_hostnames", "true"), resource.TestCheckResourceAttr(resourceName, "enable_dns_support", "true"), ), @@ -856,20 +857,20 @@ func TestAccVPC_bothDNSOptionsSet(t *testing.T) { // https://github.com/hashicorp/terraform/issues/10168 func TestAccVPC_disabledDNSSupport(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_disabledDNSSupport(rName), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "enable_dns_support", "false"), ), }, @@ -884,20 +885,20 @@ func TestAccVPC_disabledDNSSupport(t *testing.T) { func TestAccVPC_enableNetworkAddressUsageMetrics(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionNot(t, endpoints.AwsUsGovPartitionID) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_enableNetworkAddressUsageMetrics(rName), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "enable_network_address_usage_metrics", "true"), ), }, @@ -912,20 +913,20 @@ func TestAccVPC_enableNetworkAddressUsageMetrics(t *testing.T) { func TestAccVPC_assignGeneratedIPv6CIDRBlock(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_assignGeneratedIPv6CIDRBlock(rName, true), Check: resource.ComposeAggregateTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "assign_generated_ipv6_cidr_block", "true"), resource.TestCheckResourceAttr(resourceName, "cidr_block", "10.1.0.0/16"), resource.TestCheckResourceAttrSet(resourceName, "ipv6_association_id"), @@ -940,7 +941,7 @@ func TestAccVPC_assignGeneratedIPv6CIDRBlock(t *testing.T) { { Config: testAccVPCConfig_assignGeneratedIPv6CIDRBlock(rName, false), Check: resource.ComposeAggregateTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "assign_generated_ipv6_cidr_block", "false"), resource.TestCheckResourceAttr(resourceName, "cidr_block", "10.1.0.0/16"), resource.TestCheckResourceAttr(resourceName, "ipv6_association_id", ""), @@ -950,7 +951,7 @@ func TestAccVPC_assignGeneratedIPv6CIDRBlock(t *testing.T) { { Config: testAccVPCConfig_assignGeneratedIPv6CIDRBlock(rName, true), Check: resource.ComposeAggregateTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "assign_generated_ipv6_cidr_block", "true"), resource.TestCheckResourceAttr(resourceName, "cidr_block", "10.1.0.0/16"), resource.TestCheckResourceAttrSet(resourceName, "ipv6_association_id"), @@ -963,7 +964,7 @@ func TestAccVPC_assignGeneratedIPv6CIDRBlock(t *testing.T) { func TestAccVPC_assignGeneratedIPv6CIDRBlockWithNetworkBorderGroup(t *testing.T) { ctx := acctest.Context(t) - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" azDataSourceName := "data.aws_availability_zone.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -977,14 +978,14 @@ func TestAccVPC_assignGeneratedIPv6CIDRBlockWithNetworkBorderGroup(t *testing.T) // for a new or existing VPCs only for us-west-2-lax-1a and use-west-2-lax-1b. All other Local Zones don't support IPv6." testAccPreCheckLocalZoneAvailable(ctx, t, "us-west-2-lax-1") //lintignore:AWSAT003 }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_assignGeneratedIPv6CIDRBlockOptionalNetworkBorderGroup(rName, true), Check: resource.ComposeAggregateTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "assign_generated_ipv6_cidr_block", "true"), resource.TestCheckResourceAttrSet(resourceName, "ipv6_association_id"), resource.TestCheckResourceAttrSet(resourceName, "ipv6_cidr_block"), @@ -1001,7 +1002,7 @@ func TestAccVPC_assignGeneratedIPv6CIDRBlockWithNetworkBorderGroup(t *testing.T) { Config: testAccVPCConfig_assignGeneratedIPv6CIDRBlockOptionalNetworkBorderGroup(rName, false), Check: resource.ComposeAggregateTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "assign_generated_ipv6_cidr_block", "true"), resource.TestCheckResourceAttrSet(resourceName, "ipv6_association_id"), resource.TestCheckResourceAttrSet(resourceName, "ipv6_cidr_block"), @@ -1020,20 +1021,20 @@ func TestAccVPC_IPAMIPv4BasicNetmask(t *testing.T) { t.Skip("skipping long-running test in short mode") } - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_ipamIPv4(rName, 28), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), testAccCheckVPCCIDRPrefix(&vpc, "28"), ), }, @@ -1047,21 +1048,21 @@ func TestAccVPC_IPAMIPv4BasicExplicitCIDR(t *testing.T) { t.Skip("skipping long-running test in short mode") } - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" cidr := "172.2.0.32/28" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_ipamIPv4ExplicitCIDR(rName, cidr), Check: resource.ComposeTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "cidr_block", cidr), ), }, @@ -1075,21 +1076,21 @@ func TestAccVPC_IPAMIPv6(t *testing.T) { t.Skip("skipping long-running test in short mode") } - var vpc ec2.Vpc + var vpc awstypes.Vpc resourceName := "aws_vpc.test" ipamPoolResourceName := "aws_vpc_ipam_pool.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.EC2), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckVPCDestroy(ctx), Steps: []resource.TestStep{ { Config: testAccVPCConfig_ipamIPv6(rName, 28), Check: resource.ComposeAggregateTestCheckFunc( - acctest.CheckVPCExists(ctx, resourceName, &vpc), + acctest.CheckVPCExistsV2(ctx, resourceName, &vpc), resource.TestCheckResourceAttr(resourceName, "assign_generated_ipv6_cidr_block", "false"), resource.TestCheckResourceAttr(resourceName, "cidr_block", "10.1.0.0/16"), resource.TestCheckResourceAttrSet(resourceName, "ipv6_association_id"), @@ -1105,14 +1106,14 @@ func TestAccVPC_IPAMIPv6(t *testing.T) { func testAccCheckVPCDestroy(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_vpc" { continue } - _, err := tfec2.FindVPCByID(ctx, conn, rs.Primary.ID) + _, err := tfec2.FindVPCByIDV2(ctx, conn, rs.Primary.ID) if tfresource.NotFound(err) { continue @@ -1129,27 +1130,27 @@ func testAccCheckVPCDestroy(ctx context.Context) resource.TestCheckFunc { } } -func testAccCheckVPCUpdateTags(ctx context.Context, vpc *ec2.Vpc, oldTags, newTags map[string]string) resource.TestCheckFunc { +func testAccCheckVPCUpdateTags(ctx context.Context, vpc *awstypes.Vpc, oldTags, newTags map[string]string) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) - return tfec2.UpdateTags(ctx, conn, aws.StringValue(vpc.VpcId), oldTags, newTags) + return tfec2.UpdateTagsV2(ctx, conn, aws.ToString(vpc.VpcId), oldTags, newTags) } } -func testAccCheckVPCCIDRPrefix(vpc *ec2.Vpc, expected string) resource.TestCheckFunc { +func testAccCheckVPCCIDRPrefix(vpc *awstypes.Vpc, expected string) resource.TestCheckFunc { return func(s *terraform.State) error { - if strings.Split(aws.StringValue(vpc.CidrBlock), "/")[1] != expected { - return fmt.Errorf("Bad cidr prefix: got %s, expected %s", aws.StringValue(vpc.CidrBlock), expected) + if strings.Split(aws.ToString(vpc.CidrBlock), "/")[1] != expected { + return fmt.Errorf("Bad cidr prefix: got %s, expected %s", aws.ToString(vpc.CidrBlock), expected) } return nil } } -func testAccCheckVPCIDsEqual(vpc1, vpc2 *ec2.Vpc) resource.TestCheckFunc { +func testAccCheckVPCIDsEqual(vpc1, vpc2 *awstypes.Vpc) resource.TestCheckFunc { return func(s *terraform.State) error { - if aws.StringValue(vpc1.VpcId) != aws.StringValue(vpc2.VpcId) { + if aws.ToString(vpc1.VpcId) != aws.ToString(vpc2.VpcId) { return fmt.Errorf("VPC IDs are not equal") } @@ -1157,9 +1158,9 @@ func testAccCheckVPCIDsEqual(vpc1, vpc2 *ec2.Vpc) resource.TestCheckFunc { } } -func testAccCheckVPCIDsNotEqual(vpc1, vpc2 *ec2.Vpc) resource.TestCheckFunc { +func testAccCheckVPCIDsNotEqual(vpc1, vpc2 *awstypes.Vpc) resource.TestCheckFunc { return func(s *terraform.State) error { - if aws.StringValue(vpc1.VpcId) == aws.StringValue(vpc2.VpcId) { + if aws.ToString(vpc1.VpcId) == aws.ToString(vpc2.VpcId) { return fmt.Errorf("VPC IDs are equal") } diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go new file mode 100644 index 000000000000..c47658bc283c --- /dev/null +++ b/internal/service/ec2/waitv2.go @@ -0,0 +1,99 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ec2 + +import ( + "context" + "errors" + "strconv" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-provider-aws/internal/enum" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" +) + +func waitVPCCreatedV2(ctx context.Context, conn *ec2.Client, id string) (*types.Vpc, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(types.VpcStatePending), + Target: enum.Slice(types.VpcStateAvailable), + Refresh: statusVPCStateV2(ctx, conn, id), + Timeout: vpcCreatedTimeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*types.Vpc); ok { + return output, err + } + + return nil, err +} + +func waitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*types.VpcCidrBlockState, error) { //nolint:unparam + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(types.VpcCidrBlockStateCodeAssociating, types.VpcCidrBlockStateCodeDisassociated, types.VpcCidrBlockStateCodeFailing), + Target: enum.Slice(types.VpcCidrBlockStateCodeAssociated), + Refresh: statusVPCIPv6CIDRBlockAssociationStateV2(ctx, conn, id), + Timeout: timeout, + Delay: 10 * time.Second, + MinTimeout: 5 * time.Second, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*types.VpcCidrBlockState); ok { + if state := output.State; state == types.VpcCidrBlockStateCodeFailed { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StatusMessage))) + } + + return output, err + } + + return nil, err +} + +func waitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute types.VpcAttributeName, expectedValue bool) (*types.Vpc, error) { //nolint:unparam + stateConf := &retry.StateChangeConf{ + Target: []string{strconv.FormatBool(expectedValue)}, + Refresh: statusVPCAttributeValueV2(ctx, conn, vpcID, attribute), + Timeout: ec2PropagationTimeout, + Delay: 10 * time.Second, + MinTimeout: 3 * time.Second, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*types.Vpc); ok { + return output, err + } + + return nil, err +} + +func waitVPCIPv6CIDRBlockAssociationDeletedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*types.VpcCidrBlockState, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(types.VpcCidrBlockStateCodeAssociated, types.VpcCidrBlockStateCodeDisassociating, types.VpcCidrBlockStateCodeFailing), + Target: []string{}, + Refresh: statusVPCIPv6CIDRBlockAssociationStateV2(ctx, conn, id), + Timeout: timeout, + Delay: 10 * time.Second, + MinTimeout: 5 * time.Second, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*types.VpcCidrBlockState); ok { + if state := output.State; state == types.VpcCidrBlockStateCodeFailed { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StatusMessage))) + } + + return output, err + } + + return nil, err +}