From e7b659677d2486a5c0d57b9f90195c7a7ebfb766 Mon Sep 17 00:00:00 2001 From: Matt Burgess <549318+mattburgess@users.noreply.github.com> Date: Tue, 4 Jul 2023 21:28:51 +0100 Subject: [PATCH 01/12] r/aws_vpc and r/aws_default_vpc use AWS SDKv2 --- internal/generate/tags/README.md | 1 + internal/generate/tags/main.go | 3 + .../tags/templates/v1/update_tags_body.tmpl | 6 + .../tags/templates/v2/update_tags_body.tmpl | 6 + internal/service/ec2/filter.go | 31 ++ internal/service/ec2/findv2.go | 324 ++++++++++++++++++ internal/service/ec2/generate.go | 2 +- internal/service/ec2/statusv2.go | 58 ++++ internal/service/ec2/tagsv2_gen.go | 43 +++ internal/service/ec2/vpc_.go | 143 ++++---- internal/service/ec2/vpc_default_vpc.go | 44 +-- internal/service/ec2/waitv2.go | 95 +++++ 12 files changed, 663 insertions(+), 93 deletions(-) create mode 100644 internal/service/ec2/findv2.go create mode 100644 internal/service/ec2/statusv2.go create mode 100644 internal/service/ec2/waitv2.go 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/filter.go b/internal/service/ec2/filter.go index 66596d57128f..ab94e913d6d4 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..107732d726f9 --- /dev/null +++ b/internal/service/ec2/findv2.go @@ -0,0 +1,324 @@ +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/awsv1shim/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 string) (bool, error) { + input := &ec2.DescribeVpcAttributeInput{ + Attribute: awstypes.VpcAttributeName(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 string(awstypes.VpcAttributeNameEnableDnsHostnames): + v = output.EnableDnsHostnames + case string(awstypes.VpcAttributeNameEnableDnsSupport): + v = output.EnableDnsSupport + case string(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 + } + + if len(output) == 0 { + return nil, tfresource.NewEmptyResultError(input) + } + + if count := len(output); count > 1 { + return nil, tfresource.NewTooManyResultsError(count, input) + } + + return output[0], nil +} + +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 err != nil { + return nil, fmt.Errorf("reading VPCs: %s", err) + } + + for _, v := range page.Vpcs { + output = append(output, &v) + } + } + + return output, nil +} + +func FindVPCByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.Vpc, error) { + input := &ec2.DescribeVpcsInput{ + VpcIds: []string{id}, + } + + output, err := FindVPCV2(ctx, conn, input) + + if err != nil { + return nil, err + } + + // Eventual consistency check. + if aws.ToString(output.VpcId) != id { + return nil, &retry.NotFoundError{ + LastRequest: input, + } + } + + return output, nil +} + +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 := string(association.Ipv6CidrBlockState.State); state == string(awstypes.VpcCidrBlockStateCodeDisassociated) { + return nil, nil, &retry.NotFoundError{Message: 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 + } + + if len(output) == 0 { + return nil, tfresource.NewEmptyResultError(input) + } + + if count := len(output); count > 1 { + return nil, tfresource.NewTooManyResultsError(count, input) + } + + return output[0], nil +} + +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 err != nil { + return nil, fmt.Errorf("reading Network ACLs: %s", err) + } + + for _, v := range page.NetworkAcls { + output = append(output, &v) + } + } + + if len(output) == 0 { + return nil, tfresource.NewEmptyResultError(input) + } + + 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 + } + + if len(output) == 0 || output[0] == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + if count := len(output); count > 1 { + return nil, tfresource.NewTooManyResultsError(count, input) + } + + return output[0], nil +} + +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 err != nil { + return nil, fmt.Errorf("reading Route Tables: %s", err) + } + + for _, v := range page.RouteTables { + output = append(output, &v) + } + } + + if len(output) == 0 { + return nil, tfresource.NewEmptyResultError(input) + } + + return output, nil +} + +// FindSecurityGroupV2 looks up a security group using an ec2.DescribeSecurityGroupsInput. Returns a retry.NotFoundError if not found. +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 + } + + if len(output) == 0 || output[0] == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + if count := len(output); count > 1 { + return nil, tfresource.NewTooManyResultsError(count, input) + } + + return output[0], nil +} + +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 err != nil { + return nil, fmt.Errorf("reading Security Groups: %s", err) + } + + for _, v := range page.SecurityGroups { + output = append(output, &v) + } + } + + if len(output) == 0 { + return nil, tfresource.NewEmptyResultError(input) + } + + 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 err != nil { + return nil, fmt.Errorf("reading IPAM Pool Allocations: %s", err) + } + + for _, v := range page.IpamPoolAllocations { + output = append(output, &v) + } + } + + if len(output) == 0 { + return nil, tfresource.NewEmptyResultError(input) + } + + 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..a2dc282187f2 --- /dev/null +++ b/internal/service/ec2/statusv2.go @@ -0,0 +1,58 @@ +package ec2 + +import ( + "context" + "strconv" + + "github.com/aws/aws-sdk-go-v2/service/ec2" + "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 string) 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..3f9a1db06d74 100644 --- a/internal/service/ec2/vpc_.go +++ b/internal/service/ec2/vpc_.go @@ -11,9 +11,10 @@ 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/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/aws/arn" + "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/awsv1shim/v2/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" @@ -111,8 +112,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: awstypes.TenancyDefault, + ValidateFunc: validation.StringInSlice([]string{string(awstypes.TenancyDefault), string(awstypes.TenancyDedicated)}, false), }, "ipv4_ipam_pool_id": { Type: schema.TypeString, @@ -175,12 +176,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: awstypes.Tenancy(d.Get("instance_tenancy").(string)), + TagSpecifications: getTagSpecificationsInV2(ctx, awstypes.ResourceTypeVpc), } if v, ok := d.GetOk("cidr_block"); ok { @@ -192,7 +193,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,11 +209,11 @@ 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) + return conn.CreateVpc(ctx, input) // "UnsupportedOperation: The operation AllocateIpamPoolCidr is not supported. Account 123456789012 is not monitored by IPAM ipam-07b079e3392782a55." }, errCodeUnsupportedOperation, "is not monitored by IPAM") @@ -222,18 +223,18 @@ 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) + _, err = WaitVPCIPv6CIDRBlockAssociationCreatedV2(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) @@ -256,10 +257,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 +273,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.(*awstypes.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 +290,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(), string(awstypes.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(), awstypes.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(), string(awstypes.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(), awstypes.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(), string(awstypes.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(), awstypes.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 +328,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 +343,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) @@ -380,7 +381,7 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface 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 +444,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 +452,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 +464,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 +514,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(awstypes.TenancyDedicated) || new.(string) != string(awstypes.TenancyDefault) { diff.ForceNew("instance_tenancy") } } @@ -533,13 +534,13 @@ 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 *awstypes.Vpc, associationID string) *awstypes.VpcIpv6CidrBlockAssociation { + var ipv6CIDRBlockAssociation *awstypes.VpcIpv6CidrBlockAssociation if associationID != "" { for _, v := range vpc.Ipv6CidrBlockAssociationSet { - if state := aws.StringValue(v.Ipv6CidrBlockState.State); state == ec2.VpcCidrBlockStateCodeAssociated && aws.StringValue(v.AssociationId) == associationID { - ipv6CIDRBlockAssociation = v + if state := string(v.Ipv6CidrBlockState.State); state == string(awstypes.VpcCidrBlockStateCodeAssociated) && aws.ToString(v.AssociationId) == associationID { + ipv6CIDRBlockAssociation = &v break } @@ -548,8 +549,8 @@ func defaultIPv6CIDRBlockAssociation(vpc *ec2.Vpc, associationID string) *ec2.Vp if ipv6CIDRBlockAssociation == nil { for _, v := range vpc.Ipv6CidrBlockAssociationSet { - if aws.StringValue(v.Ipv6CidrBlockState.State) == ec2.VpcCidrBlockStateCodeAssociated { - ipv6CIDRBlockAssociation = v + if string(v.Ipv6CidrBlockState.State) == string(awstypes.VpcCidrBlockStateCodeAssociated) { + ipv6CIDRBlockAssociation = &v } } } @@ -558,7 +559,7 @@ func defaultIPv6CIDRBlockAssociation(vpc *ec2.Vpc, associationID string) *ec2.Vp } type vpcInfo struct { - vpc *ec2.Vpc + vpc *awstypes.Vpc enableDnsHostnames bool enableDnsSupport bool enableNetworkAddressUsageMetrics bool @@ -566,7 +567,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 +589,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: &awstypes.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, string(awstypes.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: &awstypes.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, string(awstypes.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: &awstypes.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, string(awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics), v); err != nil { return fmt.Errorf("modifying EnableNetworkAddressUsageMetrics: waiting for completion: %w", err) } @@ -647,19 +648,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) + _, err = WaitVPCIPv6CIDRBlockAssociationDeletedV2(ctx, conn, associationID, vpcIPv6CIDRBlockAssociationDeletedTimeout) if err != nil { return "", fmt.Errorf("disassociating IPv6 CIDR block (%s): waiting for completion: %w", associationID, err) @@ -688,18 +689,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) + _, err = WaitVPCIPv6CIDRBlockAssociationCreatedV2(ctx, conn, associationID, vpcIPv6CIDRBlockAssociationCreatedTimeout) if err != nil { return "", fmt.Errorf("associating IPv6 CIDR block: waiting for completion: %w", err) @@ -709,32 +710,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: awstypes.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) ([]*awstypes.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 *awstypes.IpamPoolAllocation) bool { + return string(v.ResourceType) == string(awstypes.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..72e60d8e8479 100644 --- a/internal/service/ec2/vpc_default_vpc.go +++ b/internal/service/ec2/vpc_default_vpc.go @@ -7,8 +7,10 @@ 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 +156,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 +167,36 @@ 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(), string(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(), string(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(), string(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) + log.Printf("[DEBUG] Creating EC2 Default VPC: %v", input) + output, err := conn.CreateDefaultVpc(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating EC2 Default VPC: %s", err) @@ -202,10 +204,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 +230,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 @@ -276,7 +278,7 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta oldTags := KeyValueTags(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/waitv2.go b/internal/service/ec2/waitv2.go new file mode 100644 index 000000000000..b1f5c95b10f0 --- /dev/null +++ b/internal/service/ec2/waitv2.go @@ -0,0 +1,95 @@ +package ec2 + +import ( + "context" + "errors" + "strconv" + "time" + + "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/helper/retry" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" +) + +func WaitVPCCreatedV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.Vpc, error) { + stateConf := &retry.StateChangeConf{ + Pending: []string{string(awstypes.VpcStatePending)}, + Target: []string{string(awstypes.VpcStateAvailable)}, + Refresh: StatusVPCStateV2(ctx, conn, id), + Timeout: vpcCreatedTimeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.Vpc); ok { + return output, err + } + + return nil, err +} + +func WaitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*awstypes.VpcCidrBlockState, error) { + stateConf := &retry.StateChangeConf{ + Pending: []string{string(awstypes.VpcCidrBlockStateCodeAssociating), string(awstypes.VpcCidrBlockStateCodeDisassociated), string(awstypes.VpcCidrBlockStateCodeFailing)}, + Target: []string{string(awstypes.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.(*awstypes.VpcCidrBlockState); ok { + if state := output.State; state == awstypes.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 string, expectedValue bool) (*awstypes.Vpc, error) { + 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.(*awstypes.Vpc); ok { + return output, err + } + + return nil, err +} + +func WaitVPCIPv6CIDRBlockAssociationDeletedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*awstypes.VpcCidrBlockState, error) { + stateConf := &retry.StateChangeConf{ + Pending: []string{string(awstypes.VpcCidrBlockStateCodeAssociated), string(awstypes.VpcCidrBlockStateCodeDisassociating), string(awstypes.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.(*awstypes.VpcCidrBlockState); ok { + if state := output.State; state == awstypes.VpcCidrBlockStateCodeFailed { + tfresource.SetLastError(err, errors.New(aws.ToString(output.StatusMessage))) + } + + return output, err + } + + return nil, err +} From 03ba01ff3780a772efece3369cfcdc2b636f4c22 Mon Sep 17 00:00:00 2001 From: Matt Burgess <549318+mattburgess@users.noreply.github.com> Date: Wed, 5 Jul 2023 00:11:00 +0100 Subject: [PATCH 02/12] Migrate and fix tests --- internal/acctest/acctest.go | 26 +++ internal/service/ec2/exports_test.go | 3 +- internal/service/ec2/findv2.go | 16 +- internal/service/ec2/vpc_test.go | 238 ++++++++++++++------------- 4 files changed, 156 insertions(+), 127 deletions(-) diff --git a/internal/acctest/acctest.go b/internal/acctest/acctest.go index 7b673d038ffb..e56a3e195704 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" @@ -2240,6 +2241,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/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/findv2.go b/internal/service/ec2/findv2.go index 107732d726f9..df36d8a4b18a 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -7,7 +7,7 @@ import ( "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/awsv1shim/v2/tfawserr" + "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" ) @@ -80,6 +80,13 @@ func FindVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsIn 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, fmt.Errorf("reading VPCs: %s", err) } @@ -103,13 +110,6 @@ func FindVPCByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes. return nil, err } - // Eventual consistency check. - if aws.ToString(output.VpcId) != id { - return nil, &retry.NotFoundError{ - LastRequest: input, - } - } - return output, nil } diff --git a/internal/service/ec2/vpc_test.go b/internal/service/ec2/vpc_test.go index ae5c1e31e0fa..6a9a68415f38 100644 --- a/internal/service/ec2/vpc_test.go +++ b/internal/service/ec2/vpc_test.go @@ -10,9 +10,10 @@ 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 +21,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 +73,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 +96,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 +121,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 +131,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 +143,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 +166,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 +188,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 +203,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 +215,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 +228,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 +241,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 +256,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 +273,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 +286,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 +298,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 +323,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 +340,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 +355,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 +364,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 +382,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 +397,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 +416,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 +432,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 +446,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 +461,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 +478,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 +493,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 +506,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 +521,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 +532,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 +546,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 +557,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 +567,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 +583,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 +613,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 +638,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 +666,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 +684,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 +721,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 +752,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 +778,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 +786,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 +797,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 +828,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 +858,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 +886,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 +914,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 +942,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 +952,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 +965,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 +979,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 +1003,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 +1022,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 +1049,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 +1077,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 +1107,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 +1131,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 +1159,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") } From cb95bdf7a56ae8b32c6918fc4a97f9797b52955d Mon Sep 17 00:00:00 2001 From: Matt Burgess <549318+mattburgess@users.noreply.github.com> Date: Wed, 5 Jul 2023 00:19:53 +0100 Subject: [PATCH 03/12] Add copyright headers --- internal/service/ec2/findv2.go | 3 +++ internal/service/ec2/statusv2.go | 3 +++ internal/service/ec2/waitv2.go | 3 +++ 3 files changed, 9 insertions(+) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index df36d8a4b18a..ffa4f1beda97 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -1,3 +1,6 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + package ec2 import ( diff --git a/internal/service/ec2/statusv2.go b/internal/service/ec2/statusv2.go index a2dc282187f2..1baccc918986 100644 --- a/internal/service/ec2/statusv2.go +++ b/internal/service/ec2/statusv2.go @@ -1,3 +1,6 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + package ec2 import ( diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index b1f5c95b10f0..baad27a26fe9 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -1,3 +1,6 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + package ec2 import ( From 56d2fd9b498ad60251eb84338cbcda4699ec54e2 Mon Sep 17 00:00:00 2001 From: Matt Burgess <549318+mattburgess@users.noreply.github.com> Date: Wed, 5 Jul 2023 00:33:22 +0100 Subject: [PATCH 04/12] Fix linting --- internal/service/ec2/findv2.go | 42 +++++++++--------- internal/service/ec2/vpc_.go | 59 +++++++++++++------------ internal/service/ec2/vpc_default_vpc.go | 1 - internal/service/ec2/vpc_test.go | 1 - internal/service/ec2/waitv2.go | 33 +++++++------- 5 files changed, 68 insertions(+), 68 deletions(-) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index ffa4f1beda97..7f762a3c3b7a 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -72,11 +72,11 @@ func FindVPCV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsInp return nil, tfresource.NewTooManyResultsError(count, input) } - return output[0], nil + return &output[0], nil } -func FindVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsInput) ([]*awstypes.Vpc, error) { - var output []*awstypes.Vpc +func FindVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsInput) ([]awstypes.Vpc, error) { + var output []awstypes.Vpc pages := ec2.NewDescribeVpcsPaginator(conn, input) @@ -95,7 +95,7 @@ func FindVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsIn } for _, v := range page.Vpcs { - output = append(output, &v) + output = append(output, v) } } @@ -168,11 +168,11 @@ func FindNetworkACLV2(ctx context.Context, conn *ec2.Client, input *ec2.Describe return nil, tfresource.NewTooManyResultsError(count, input) } - return output[0], nil + return &output[0], nil } -func FindNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNetworkAclsInput) ([]*awstypes.NetworkAcl, error) { - var output []*awstypes.NetworkAcl +func FindNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNetworkAclsInput) ([]awstypes.NetworkAcl, error) { + var output []awstypes.NetworkAcl pages := ec2.NewDescribeNetworkAclsPaginator(conn, input) @@ -184,7 +184,7 @@ func FindNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.Describ } for _, v := range page.NetworkAcls { - output = append(output, &v) + output = append(output, v) } } @@ -224,7 +224,7 @@ func FindRouteTableV2(ctx context.Context, conn *ec2.Client, input *ec2.Describe return nil, err } - if len(output) == 0 || output[0] == nil { + if len(output) == 0 { return nil, tfresource.NewEmptyResultError(input) } @@ -232,11 +232,11 @@ func FindRouteTableV2(ctx context.Context, conn *ec2.Client, input *ec2.Describe return nil, tfresource.NewTooManyResultsError(count, input) } - return output[0], nil + return &output[0], nil } -func FindRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) ([]*awstypes.RouteTable, error) { - var output []*awstypes.RouteTable +func FindRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) ([]awstypes.RouteTable, error) { + var output []awstypes.RouteTable pages := ec2.NewDescribeRouteTablesPaginator(conn, input) @@ -248,7 +248,7 @@ func FindRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.Describ } for _, v := range page.RouteTables { - output = append(output, &v) + output = append(output, v) } } @@ -267,7 +267,7 @@ func FindSecurityGroupV2(ctx context.Context, conn *ec2.Client, input *ec2.Descr return nil, err } - if len(output) == 0 || output[0] == nil { + if len(output) == 0 { return nil, tfresource.NewEmptyResultError(input) } @@ -275,11 +275,11 @@ func FindSecurityGroupV2(ctx context.Context, conn *ec2.Client, input *ec2.Descr return nil, tfresource.NewTooManyResultsError(count, input) } - return output[0], nil + return &output[0], nil } -func FindSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) ([]*awstypes.SecurityGroup, error) { - var output []*awstypes.SecurityGroup +func FindSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) ([]awstypes.SecurityGroup, error) { + var output []awstypes.SecurityGroup pages := ec2.NewDescribeSecurityGroupsPaginator(conn, input) @@ -291,7 +291,7 @@ func FindSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.Desc } for _, v := range page.SecurityGroups { - output = append(output, &v) + output = append(output, v) } } @@ -302,8 +302,8 @@ func FindSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.Desc return output, nil } -func FindIPAMPoolAllocationsV2(ctx context.Context, conn *ec2.Client, input *ec2.GetIpamPoolAllocationsInput) ([]*awstypes.IpamPoolAllocation, error) { - var output []*awstypes.IpamPoolAllocation +func FindIPAMPoolAllocationsV2(ctx context.Context, conn *ec2.Client, input *ec2.GetIpamPoolAllocationsInput) ([]awstypes.IpamPoolAllocation, error) { + var output []awstypes.IpamPoolAllocation pages := ec2.NewGetIpamPoolAllocationsPaginator(conn, input) @@ -315,7 +315,7 @@ func FindIPAMPoolAllocationsV2(ctx context.Context, conn *ec2.Client, input *ec2 } for _, v := range page.IpamPoolAllocations { - output = append(output, &v) + output = append(output, v) } } diff --git a/internal/service/ec2/vpc_.go b/internal/service/ec2/vpc_.go index 3f9a1db06d74..587bd6e7af96 100644 --- a/internal/service/ec2/vpc_.go +++ b/internal/service/ec2/vpc_.go @@ -14,14 +14,15 @@ import ( "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" - awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "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" @@ -112,8 +113,8 @@ func ResourceVPC() *schema.Resource { "instance_tenancy": { Type: schema.TypeString, Optional: true, - Default: awstypes.TenancyDefault, - ValidateFunc: validation.StringInSlice([]string{string(awstypes.TenancyDefault), string(awstypes.TenancyDedicated)}, false), + Default: types.TenancyDefault, + ValidateFunc: validation.StringInSlice(enum.Slice(types.TenancyDefault, types.TenancyDedicated), false), }, "ipv4_ipam_pool_id": { Type: schema.TypeString, @@ -180,8 +181,8 @@ func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interfa input := &ec2.CreateVpcInput{ AmazonProvidedIpv6CidrBlock: aws.Bool(d.Get("assign_generated_ipv6_cidr_block").(bool)), - InstanceTenancy: awstypes.Tenancy(d.Get("instance_tenancy").(string)), - TagSpecifications: getTagSpecificationsInV2(ctx, awstypes.ResourceTypeVpc), + InstanceTenancy: types.Tenancy(d.Get("instance_tenancy").(string)), + TagSpecifications: getTagSpecificationsInV2(ctx, types.ResourceTypeVpc), } if v, ok := d.GetOk("cidr_block"); ok { @@ -273,7 +274,7 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s): %s", d.Id(), err) } - vpc := outputRaw.(*awstypes.Vpc) + vpc := outputRaw.(*types.Vpc) ownerID := aws.ToString(vpc.OwnerId) arn := arn.ARN{ @@ -290,25 +291,25 @@ 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 FindVPCAttributeV2(ctx, conn, d.Id(), string(awstypes.VpcAttributeNameEnableDnsHostnames)) + return FindVPCAttributeV2(ctx, conn, d.Id(), string(types.VpcAttributeNameEnableDnsHostnames)) }, d.IsNewResource()); err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), awstypes.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 FindVPCAttributeV2(ctx, conn, d.Id(), string(awstypes.VpcAttributeNameEnableDnsSupport)) + return FindVPCAttributeV2(ctx, conn, d.Id(), string(types.VpcAttributeNameEnableDnsSupport)) }, d.IsNewResource()); err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), awstypes.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 FindVPCAttributeV2(ctx, conn, d.Id(), string(awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics)) + return FindVPCAttributeV2(ctx, conn, d.Id(), string(types.VpcAttributeNameEnableNetworkAddressUsageMetrics)) }, d.IsNewResource()); err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 VPC (%s) Attribute (%s): %s", d.Id(), awstypes.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) } @@ -514,7 +515,7 @@ func resourceVPCCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v in if diff.HasChange("instance_tenancy") { old, new := diff.GetChange("instance_tenancy") - if old.(string) != string(awstypes.TenancyDedicated) || new.(string) != string(awstypes.TenancyDefault) { + if old.(string) != string(types.TenancyDedicated) || new.(string) != string(types.TenancyDefault) { diff.ForceNew("instance_tenancy") } } @@ -534,12 +535,12 @@ 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 *awstypes.Vpc, associationID string) *awstypes.VpcIpv6CidrBlockAssociation { - var ipv6CIDRBlockAssociation *awstypes.VpcIpv6CidrBlockAssociation +func defaultIPv6CIDRBlockAssociation(vpc *types.Vpc, associationID string) *types.VpcIpv6CidrBlockAssociation { + var ipv6CIDRBlockAssociation *types.VpcIpv6CidrBlockAssociation if associationID != "" { for _, v := range vpc.Ipv6CidrBlockAssociationSet { - if state := string(v.Ipv6CidrBlockState.State); state == string(awstypes.VpcCidrBlockStateCodeAssociated) && aws.ToString(v.AssociationId) == associationID { + if state := string(v.Ipv6CidrBlockState.State); state == string(types.VpcCidrBlockStateCodeAssociated) && aws.ToString(v.AssociationId) == associationID { ipv6CIDRBlockAssociation = &v break @@ -549,7 +550,7 @@ func defaultIPv6CIDRBlockAssociation(vpc *awstypes.Vpc, associationID string) *a if ipv6CIDRBlockAssociation == nil { for _, v := range vpc.Ipv6CidrBlockAssociationSet { - if string(v.Ipv6CidrBlockState.State) == string(awstypes.VpcCidrBlockStateCodeAssociated) { + if string(v.Ipv6CidrBlockState.State) == string(types.VpcCidrBlockStateCodeAssociated) { ipv6CIDRBlockAssociation = &v } } @@ -559,7 +560,7 @@ func defaultIPv6CIDRBlockAssociation(vpc *awstypes.Vpc, associationID string) *a } type vpcInfo struct { - vpc *awstypes.Vpc + vpc *types.Vpc enableDnsHostnames bool enableDnsSupport bool enableNetworkAddressUsageMetrics bool @@ -591,7 +592,7 @@ func modifyVPCAttributesOnCreate(ctx context.Context, conn *ec2.Client, d *schem func modifyVPCDNSHostnames(ctx context.Context, conn *ec2.Client, vpcID string, v bool) error { input := &ec2.ModifyVpcAttributeInput{ - EnableDnsHostnames: &awstypes.AttributeBooleanValue{ + EnableDnsHostnames: &types.AttributeBooleanValue{ Value: aws.Bool(v), }, VpcId: aws.String(vpcID), @@ -601,7 +602,7 @@ func modifyVPCDNSHostnames(ctx context.Context, conn *ec2.Client, vpcID string, return fmt.Errorf("modifying EnableDnsHostnames: %w", err) } - if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, string(awstypes.VpcAttributeNameEnableDnsHostnames), v); err != nil { + if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, string(types.VpcAttributeNameEnableDnsHostnames), v); err != nil { return fmt.Errorf("modifying EnableDnsHostnames: waiting for completion: %w", err) } @@ -610,7 +611,7 @@ func modifyVPCDNSHostnames(ctx context.Context, conn *ec2.Client, vpcID string, func modifyVPCDNSSupport(ctx context.Context, conn *ec2.Client, vpcID string, v bool) error { input := &ec2.ModifyVpcAttributeInput{ - EnableDnsSupport: &awstypes.AttributeBooleanValue{ + EnableDnsSupport: &types.AttributeBooleanValue{ Value: aws.Bool(v), }, VpcId: aws.String(vpcID), @@ -620,7 +621,7 @@ func modifyVPCDNSSupport(ctx context.Context, conn *ec2.Client, vpcID string, v return fmt.Errorf("modifying EnableDnsSupport: %w", err) } - if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, string(awstypes.VpcAttributeNameEnableDnsSupport), v); err != nil { + if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, string(types.VpcAttributeNameEnableDnsSupport), v); err != nil { return fmt.Errorf("modifying EnableDnsSupport: waiting for completion: %w", err) } @@ -629,7 +630,7 @@ func modifyVPCDNSSupport(ctx context.Context, conn *ec2.Client, vpcID string, v func modifyVPCNetworkAddressUsageMetrics(ctx context.Context, conn *ec2.Client, vpcID string, v bool) error { input := &ec2.ModifyVpcAttributeInput{ - EnableNetworkAddressUsageMetrics: &awstypes.AttributeBooleanValue{ + EnableNetworkAddressUsageMetrics: &types.AttributeBooleanValue{ Value: aws.Bool(v), }, VpcId: aws.String(vpcID), @@ -639,7 +640,7 @@ func modifyVPCNetworkAddressUsageMetrics(ctx context.Context, conn *ec2.Client, return fmt.Errorf("modifying EnableNetworkAddressUsageMetrics: %w", err) } - if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, string(awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics), v); err != nil { + if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, string(types.VpcAttributeNameEnableNetworkAddressUsageMetrics), v); err != nil { return fmt.Errorf("modifying EnableNetworkAddressUsageMetrics: waiting for completion: %w", err) } @@ -712,7 +713,7 @@ func modifyVPCIPv6CIDRBlockAssociation(ctx context.Context, conn *ec2.Client, vp func modifyVPCTenancy(ctx context.Context, conn *ec2.Client, vpcID string, v string) error { input := &ec2.ModifyVpcTenancyInput{ - InstanceTenancy: awstypes.VpcTenancy(v), + InstanceTenancy: types.VpcTenancy(v), VpcId: aws.String(vpcID), } @@ -723,7 +724,7 @@ func modifyVPCTenancy(ctx context.Context, conn *ec2.Client, vpcID string, v str return nil } -func findIPAMPoolAllocationsForVPC(ctx context.Context, conn *ec2.Client, poolID, vpcID string) ([]*awstypes.IpamPoolAllocation, error) { +func findIPAMPoolAllocationsForVPC(ctx context.Context, conn *ec2.Client, poolID, vpcID string) ([]types.IpamPoolAllocation, error) { input := &ec2.GetIpamPoolAllocationsInput{ IpamPoolId: aws.String(poolID), } @@ -734,8 +735,8 @@ func findIPAMPoolAllocationsForVPC(ctx context.Context, conn *ec2.Client, poolID return nil, err } - output = slices.Filter(output, func(v *awstypes.IpamPoolAllocation) bool { - return string(v.ResourceType) == string(awstypes.IpamPoolAllocationResourceTypeVpc) && aws.ToString(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 72e60d8e8479..78571ff39037 100644 --- a/internal/service/ec2/vpc_default_vpc.go +++ b/internal/service/ec2/vpc_default_vpc.go @@ -10,7 +10,6 @@ import ( "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" diff --git a/internal/service/ec2/vpc_test.go b/internal/service/ec2/vpc_test.go index 6a9a68415f38..9362a0c7d12f 100644 --- a/internal/service/ec2/vpc_test.go +++ b/internal/service/ec2/vpc_test.go @@ -13,7 +13,6 @@ import ( "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" - sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index baad27a26fe9..d3c28df6ab30 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -11,32 +11,33 @@ import ( "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/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) (*awstypes.Vpc, error) { +func WaitVPCCreatedV2(ctx context.Context, conn *ec2.Client, id string) (*types.Vpc, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{string(awstypes.VpcStatePending)}, - Target: []string{string(awstypes.VpcStateAvailable)}, + 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.(*awstypes.Vpc); ok { + 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) (*awstypes.VpcCidrBlockState, error) { +func WaitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*types.VpcCidrBlockState, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{string(awstypes.VpcCidrBlockStateCodeAssociating), string(awstypes.VpcCidrBlockStateCodeDisassociated), string(awstypes.VpcCidrBlockStateCodeFailing)}, - Target: []string{string(awstypes.VpcCidrBlockStateCodeAssociated)}, + 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, @@ -45,8 +46,8 @@ func WaitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Cli outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*awstypes.VpcCidrBlockState); ok { - if state := output.State; state == awstypes.VpcCidrBlockStateCodeFailed { + if output, ok := outputRaw.(*types.VpcCidrBlockState); ok { + if state := output.State; state == types.VpcCidrBlockStateCodeFailed { tfresource.SetLastError(err, errors.New(aws.ToString(output.StatusMessage))) } @@ -56,7 +57,7 @@ func WaitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Cli return nil, err } -func WaitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute string, expectedValue bool) (*awstypes.Vpc, error) { +func WaitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute string, expectedValue bool) (*types.Vpc, error) { stateConf := &retry.StateChangeConf{ Target: []string{strconv.FormatBool(expectedValue)}, Refresh: StatusVPCAttributeValueV2(ctx, conn, vpcID, attribute), @@ -67,16 +68,16 @@ func WaitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID stri outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*awstypes.Vpc); ok { + 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) (*awstypes.VpcCidrBlockState, error) { +func WaitVPCIPv6CIDRBlockAssociationDeletedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*types.VpcCidrBlockState, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{string(awstypes.VpcCidrBlockStateCodeAssociated), string(awstypes.VpcCidrBlockStateCodeDisassociating), string(awstypes.VpcCidrBlockStateCodeFailing)}, + Pending: enum.Slice(types.VpcCidrBlockStateCodeAssociated, types.VpcCidrBlockStateCodeDisassociating, types.VpcCidrBlockStateCodeFailing), Target: []string{}, Refresh: StatusVPCIPv6CIDRBlockAssociationStateV2(ctx, conn, id), Timeout: timeout, @@ -86,8 +87,8 @@ func WaitVPCIPv6CIDRBlockAssociationDeletedV2(ctx context.Context, conn *ec2.Cli outputRaw, err := stateConf.WaitForStateContext(ctx) - if output, ok := outputRaw.(*awstypes.VpcCidrBlockState); ok { - if state := output.State; state == awstypes.VpcCidrBlockStateCodeFailed { + if output, ok := outputRaw.(*types.VpcCidrBlockState); ok { + if state := output.State; state == types.VpcCidrBlockStateCodeFailed { tfresource.SetLastError(err, errors.New(aws.ToString(output.StatusMessage))) } From bc42ae1e129758f81747a455cf4be2ceb0cdb6e0 Mon Sep 17 00:00:00 2001 From: Matt Burgess <549318+mattburgess@users.noreply.github.com> Date: Tue, 11 Jul 2023 20:58:53 +0100 Subject: [PATCH 05/12] Fix linting and tag handling --- internal/service/ec2/findv2.go | 20 +++++--------------- internal/service/ec2/vpc_.go | 13 ++++++------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index 7f762a3c3b7a..0c46ee9a66f6 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -94,9 +94,7 @@ func FindVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsIn return nil, fmt.Errorf("reading VPCs: %s", err) } - for _, v := range page.Vpcs { - output = append(output, v) - } + output = append(output, page.Vpcs...) } return output, nil @@ -183,9 +181,7 @@ func FindNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.Describ return nil, fmt.Errorf("reading Network ACLs: %s", err) } - for _, v := range page.NetworkAcls { - output = append(output, v) - } + output = append(output, page.NetworkAcls...) } if len(output) == 0 { @@ -247,9 +243,7 @@ func FindRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.Describ return nil, fmt.Errorf("reading Route Tables: %s", err) } - for _, v := range page.RouteTables { - output = append(output, v) - } + output = append(output, page.RouteTables...) } if len(output) == 0 { @@ -290,9 +284,7 @@ func FindSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.Desc return nil, fmt.Errorf("reading Security Groups: %s", err) } - for _, v := range page.SecurityGroups { - output = append(output, v) - } + output = append(output, page.SecurityGroups...) } if len(output) == 0 { @@ -314,9 +306,7 @@ func FindIPAMPoolAllocationsV2(ctx context.Context, conn *ec2.Client, input *ec2 return nil, fmt.Errorf("reading IPAM Pool Allocations: %s", err) } - for _, v := range page.IpamPoolAllocations { - output = append(output, v) - } + output = append(output, page.IpamPoolAllocations...) } if len(output) == 0 { diff --git a/internal/service/ec2/vpc_.go b/internal/service/ec2/vpc_.go index 587bd6e7af96..71b5bffba595 100644 --- a/internal/service/ec2/vpc_.go +++ b/internal/service/ec2/vpc_.go @@ -375,7 +375,7 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface } } - setTagsOut(ctx, vpc.Tags) + setTagsOutV2(ctx, vpc.Tags) return diags } @@ -536,27 +536,26 @@ func resourceVPCCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v in // 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 *types.Vpc, associationID string) *types.VpcIpv6CidrBlockAssociation { - var ipv6CIDRBlockAssociation *types.VpcIpv6CidrBlockAssociation + var ipv6CIDRBlockAssociation types.VpcIpv6CidrBlockAssociation if associationID != "" { for _, v := range vpc.Ipv6CidrBlockAssociationSet { if state := string(v.Ipv6CidrBlockState.State); state == string(types.VpcCidrBlockStateCodeAssociated) && aws.ToString(v.AssociationId) == associationID { - ipv6CIDRBlockAssociation = &v - + ipv6CIDRBlockAssociation = v break } } } - if ipv6CIDRBlockAssociation == nil { + if ipv6CIDRBlockAssociation == (types.VpcIpv6CidrBlockAssociation{}) { for _, v := range vpc.Ipv6CidrBlockAssociationSet { if string(v.Ipv6CidrBlockState.State) == string(types.VpcCidrBlockStateCodeAssociated) { - ipv6CIDRBlockAssociation = &v + ipv6CIDRBlockAssociation = v } } } - return ipv6CIDRBlockAssociation + return &ipv6CIDRBlockAssociation } type vpcInfo struct { From fc2ff667151f1dc9f4b01dd756fe2f1e522ea63f Mon Sep 17 00:00:00 2001 From: Matt Burgess <549318+mattburgess@users.noreply.github.com> Date: Tue, 11 Jul 2023 23:52:22 +0100 Subject: [PATCH 06/12] Fix IPAM allocation retry --- internal/service/ec2/vpc_.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/service/ec2/vpc_.go b/internal/service/ec2/vpc_.go index 71b5bffba595..edb3b299c6f1 100644 --- a/internal/service/ec2/vpc_.go +++ b/internal/service/ec2/vpc_.go @@ -213,10 +213,18 @@ func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interfa input.Ipv6NetmaskLength = aws.Int32(int32(v.(int))) } - outputRaw, err := tfresource.RetryWhenAWSErrMessageContains(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return conn.CreateVpc(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) From 689094bb2a6bebcc13104af42502791c3f555792 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 24 Jul 2023 10:34:24 -0400 Subject: [PATCH 07/12] Some simplifications in EC2 AWS SDK for Go v2 finders. --- internal/service/ec2/findv2.go | 118 +++++++++--------------- internal/service/ec2/statusv2.go | 3 +- internal/service/ec2/vpc_.go | 12 +-- internal/service/ec2/vpc_default_vpc.go | 8 +- internal/service/ec2/waitv2.go | 3 +- 5 files changed, 58 insertions(+), 86 deletions(-) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index 0c46ee9a66f6..97ea65c2ec29 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -15,9 +15,9 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindVPCAttributeV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute string) (bool, error) { +func FindVPCAttributeV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName) (bool, error) { input := &ec2.DescribeVpcAttributeInput{ - Attribute: awstypes.VpcAttributeName(attribute), + Attribute: attribute, VpcId: aws.String(vpcID), } @@ -40,11 +40,11 @@ func FindVPCAttributeV2(ctx context.Context, conn *ec2.Client, vpcID string, att var v *awstypes.AttributeBooleanValue switch attribute { - case string(awstypes.VpcAttributeNameEnableDnsHostnames): + case awstypes.VpcAttributeNameEnableDnsHostnames: v = output.EnableDnsHostnames - case string(awstypes.VpcAttributeNameEnableDnsSupport): + case awstypes.VpcAttributeNameEnableDnsSupport: v = output.EnableDnsSupport - case string(awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics): + case awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics: v = output.EnableNetworkAddressUsageMetrics default: return false, fmt.Errorf("unsupported VPC attribute: %s", attribute) @@ -64,22 +64,13 @@ func FindVPCV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsInp return nil, err } - if len(output) == 0 { - return nil, tfresource.NewEmptyResultError(input) - } - - if count := len(output); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) - } - - return &output[0], nil + 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) @@ -91,7 +82,7 @@ func FindVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsIn } if err != nil { - return nil, fmt.Errorf("reading VPCs: %s", err) + return nil, err } output = append(output, page.Vpcs...) @@ -105,13 +96,7 @@ func FindVPCByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes. VpcIds: []string{id}, } - output, err := FindVPCV2(ctx, conn, input) - - if err != nil { - return nil, err - } - - return output, nil + return FindVPCV2(ctx, conn, input) } func FindVPCIPv6CIDRBlockAssociationByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.VpcIpv6CidrBlockAssociation, *awstypes.Vpc, error) { @@ -129,8 +114,8 @@ func FindVPCIPv6CIDRBlockAssociationByIDV2(ctx context.Context, conn *ec2.Client for _, association := range vpc.Ipv6CidrBlockAssociationSet { if aws.ToString(association.AssociationId) == id { - if state := string(association.Ipv6CidrBlockState.State); state == string(awstypes.VpcCidrBlockStateCodeDisassociated) { - return nil, nil, &retry.NotFoundError{Message: state} + if state := association.Ipv6CidrBlockState.State; state == awstypes.VpcCidrBlockStateCodeDisassociated { + return nil, nil, &retry.NotFoundError{Message: string(state)} } return &association, vpc, nil @@ -158,15 +143,7 @@ func FindNetworkACLV2(ctx context.Context, conn *ec2.Client, input *ec2.Describe return nil, err } - if len(output) == 0 { - return nil, tfresource.NewEmptyResultError(input) - } - - if count := len(output); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) - } - - return &output[0], nil + return tfresource.AssertSingleValueResult(output) } func FindNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNetworkAclsInput) ([]awstypes.NetworkAcl, error) { @@ -177,17 +154,20 @@ func FindNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.Describ 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, fmt.Errorf("reading Network ACLs: %s", err) + return nil, err } output = append(output, page.NetworkAcls...) } - if len(output) == 0 { - return nil, tfresource.NewEmptyResultError(input) - } - return output, nil } @@ -220,15 +200,7 @@ func FindRouteTableV2(ctx context.Context, conn *ec2.Client, input *ec2.Describe return nil, err } - if len(output) == 0 { - return nil, tfresource.NewEmptyResultError(input) - } - - if count := len(output); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) - } - - return &output[0], nil + return tfresource.AssertSingleValueResult(output) } func FindRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) ([]awstypes.RouteTable, error) { @@ -239,21 +211,23 @@ func FindRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.Describ 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, fmt.Errorf("reading Route Tables: %s", err) + return nil, err } output = append(output, page.RouteTables...) } - if len(output) == 0 { - return nil, tfresource.NewEmptyResultError(input) - } - return output, nil } -// FindSecurityGroupV2 looks up a security group using an ec2.DescribeSecurityGroupsInput. Returns a retry.NotFoundError if not found. func FindSecurityGroupV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) (*awstypes.SecurityGroup, error) { output, err := FindSecurityGroupsV2(ctx, conn, input) @@ -261,15 +235,7 @@ func FindSecurityGroupV2(ctx context.Context, conn *ec2.Client, input *ec2.Descr return nil, err } - if len(output) == 0 { - return nil, tfresource.NewEmptyResultError(input) - } - - if count := len(output); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, input) - } - - return &output[0], nil + return tfresource.AssertSingleValueResult(output) } func FindSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) ([]awstypes.SecurityGroup, error) { @@ -280,17 +246,20 @@ func FindSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.Desc 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, fmt.Errorf("reading Security Groups: %s", err) + return nil, err } output = append(output, page.SecurityGroups...) } - if len(output) == 0 { - return nil, tfresource.NewEmptyResultError(input) - } - return output, nil } @@ -302,16 +271,19 @@ func FindIPAMPoolAllocationsV2(ctx context.Context, conn *ec2.Client, input *ec2 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, fmt.Errorf("reading IPAM Pool Allocations: %s", err) + return nil, err } output = append(output, page.IpamPoolAllocations...) } - if len(output) == 0 { - return nil, tfresource.NewEmptyResultError(input) - } - return output, nil } diff --git a/internal/service/ec2/statusv2.go b/internal/service/ec2/statusv2.go index 1baccc918986..5564bbeff29c 100644 --- a/internal/service/ec2/statusv2.go +++ b/internal/service/ec2/statusv2.go @@ -8,6 +8,7 @@ import ( "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" ) @@ -44,7 +45,7 @@ func StatusVPCIPv6CIDRBlockAssociationStateV2(ctx context.Context, conn *ec2.Cli } } -func StatusVPCAttributeValueV2(ctx context.Context, conn *ec2.Client, id string, attribute string) retry.StateRefreshFunc { +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) diff --git a/internal/service/ec2/vpc_.go b/internal/service/ec2/vpc_.go index edb3b299c6f1..8af084cc2a5d 100644 --- a/internal/service/ec2/vpc_.go +++ b/internal/service/ec2/vpc_.go @@ -299,7 +299,7 @@ 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 FindVPCAttributeV2(ctx, conn, d.Id(), string(types.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(), types.VpcAttributeNameEnableDnsHostnames, err) } else { @@ -307,7 +307,7 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface } if v, err := tfresource.RetryWhenNewResourceNotFound(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return FindVPCAttributeV2(ctx, conn, d.Id(), string(types.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(), types.VpcAttributeNameEnableDnsSupport, err) } else { @@ -315,7 +315,7 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface } if v, err := tfresource.RetryWhenNewResourceNotFound(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return FindVPCAttributeV2(ctx, conn, d.Id(), string(types.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(), types.VpcAttributeNameEnableNetworkAddressUsageMetrics, err) } else { @@ -609,7 +609,7 @@ func modifyVPCDNSHostnames(ctx context.Context, conn *ec2.Client, vpcID string, return fmt.Errorf("modifying EnableDnsHostnames: %w", err) } - if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, string(types.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) } @@ -628,7 +628,7 @@ func modifyVPCDNSSupport(ctx context.Context, conn *ec2.Client, vpcID string, v return fmt.Errorf("modifying EnableDnsSupport: %w", err) } - if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, string(types.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) } @@ -647,7 +647,7 @@ func modifyVPCNetworkAddressUsageMetrics(ctx context.Context, conn *ec2.Client, return fmt.Errorf("modifying EnableNetworkAddressUsageMetrics: %w", err) } - if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, string(types.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) } diff --git a/internal/service/ec2/vpc_default_vpc.go b/internal/service/ec2/vpc_default_vpc.go index 78571ff39037..2e29ca5e2c17 100644 --- a/internal/service/ec2/vpc_default_vpc.go +++ b/internal/service/ec2/vpc_default_vpc.go @@ -169,24 +169,23 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta vpc, err := FindVPCV2(ctx, conn, input) if err == nil { - log.Printf("[INFO] Found existing EC2 Default VPC") d.SetId(aws.ToString(vpc.VpcId)) d.Set("existing_default_vpc", true) vpcInfo.vpc = vpc - if v, err := FindVPCAttributeV2(ctx, conn, d.Id(), string(awstypes.VpcAttributeNameEnableDnsHostnames)); err != nil { + 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 := FindVPCAttributeV2(ctx, conn, d.Id(), string(awstypes.VpcAttributeNameEnableDnsSupport)); err != nil { + 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 := FindVPCAttributeV2(ctx, conn, d.Id(), string(awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics)); err != nil { + 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 @@ -194,7 +193,6 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta } else if tfresource.NotFound(err) { input := &ec2.CreateDefaultVpcInput{} - log.Printf("[DEBUG] Creating EC2 Default VPC: %v", input) output, err := conn.CreateDefaultVpc(ctx, input) if err != nil { diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index d3c28df6ab30..7280c4d09573 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -12,6 +12,7 @@ import ( "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" + 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/enum" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" @@ -57,7 +58,7 @@ func WaitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Cli return nil, err } -func WaitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute string, expectedValue bool) (*types.Vpc, error) { +func WaitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName, expectedValue bool) (*types.Vpc, error) { stateConf := &retry.StateChangeConf{ Target: []string{strconv.FormatBool(expectedValue)}, Refresh: StatusVPCAttributeValueV2(ctx, conn, vpcID, attribute), From c0c13c10ab485ef390cdfa4bbed29f4d8e341e8c Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 24 Jul 2023 11:17:53 -0400 Subject: [PATCH 08/12] Prefer new functions to be package private. --- internal/service/ec2/exports_test.go | 2 + internal/service/ec2/filter.go | 6 +-- internal/service/ec2/findv2.go | 54 ++++++++++++------------- internal/service/ec2/statusv2.go | 12 +++--- internal/service/ec2/vpc_.go | 38 ++++++++--------- internal/service/ec2/vpc_default_vpc.go | 12 +++--- internal/service/ec2/waitv2.go | 16 ++++---- 7 files changed, 68 insertions(+), 72 deletions(-) diff --git a/internal/service/ec2/exports_test.go b/internal/service/ec2/exports_test.go index 871f329d292a..48b1f5498a8d 100644 --- a/internal/service/ec2/exports_test.go +++ b/internal/service/ec2/exports_test.go @@ -5,6 +5,8 @@ package ec2 // Exports for use in tests only. var ( + FindVPCByIDV2 = findVPCByIDV2 + ResourceInstanceConnectEndpoint = newResourceInstanceConnectEndpoint ResourceSecurityGroupEgressRule = newResourceSecurityGroupEgressRule ResourceSecurityGroupIngressRule = newResourceSecurityGroupIngressRule diff --git a/internal/service/ec2/filter.go b/internal/service/ec2/filter.go index ab94e913d6d4..14a74985bf9b 100644 --- a/internal/service/ec2/filter.go +++ b/internal/service/ec2/filter.go @@ -62,7 +62,7 @@ func NewFilter(name string, values []string) *ec2.Filter { } } -func BuildAttributeFilterListV2(m map[string]string) []awstypes.Filter { +func buildAttributeFilterListV2(m map[string]string) []awstypes.Filter { var filters []awstypes.Filter // sort the filters by name to make the output deterministic @@ -79,13 +79,13 @@ func BuildAttributeFilterListV2(m map[string]string) []awstypes.Filter { continue } - filters = append(filters, NewFilterV2(name, []string{value})) + filters = append(filters, newFilterV2(name, []string{value})) } return filters } -func NewFilterV2(name string, values []string) awstypes.Filter { +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 index 97ea65c2ec29..a30ff2a13415 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -15,7 +15,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func FindVPCAttributeV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName) (bool, error) { +func findVPCAttributeV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName) (bool, error) { input := &ec2.DescribeVpcAttributeInput{ Attribute: attribute, VpcId: aws.String(vpcID), @@ -57,8 +57,8 @@ func FindVPCAttributeV2(ctx context.Context, conn *ec2.Client, vpcID string, att 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) +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 @@ -67,7 +67,7 @@ func FindVPCV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsInp return tfresource.AssertSingleValueResult(output) } -func FindVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsInput) ([]awstypes.Vpc, error) { +func findVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsInput) ([]awstypes.Vpc, error) { var output []awstypes.Vpc pages := ec2.NewDescribeVpcsPaginator(conn, input) @@ -91,22 +91,22 @@ func FindVPCsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeVpcsIn return output, nil } -func FindVPCByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.Vpc, error) { +func findVPCByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.Vpc, error) { input := &ec2.DescribeVpcsInput{ VpcIds: []string{id}, } - return FindVPCV2(ctx, conn, input) + return findVPCV2(ctx, conn, input) } -func FindVPCIPv6CIDRBlockAssociationByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.VpcIpv6CidrBlockAssociation, *awstypes.Vpc, error) { +func findVPCIPv6CIDRBlockAssociationByIDV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.VpcIpv6CidrBlockAssociation, *awstypes.Vpc, error) { input := &ec2.DescribeVpcsInput{ - Filters: BuildAttributeFilterListV2(map[string]string{ + Filters: buildAttributeFilterListV2(map[string]string{ "ipv6-cidr-block-association.association-id": id, }), } - vpc, err := FindVPCV2(ctx, conn, input) + vpc, err := findVPCV2(ctx, conn, input) if err != nil { return nil, nil, err @@ -125,19 +125,19 @@ func FindVPCIPv6CIDRBlockAssociationByIDV2(ctx context.Context, conn *ec2.Client return nil, nil, &retry.NotFoundError{} } -func FindVPCDefaultNetworkACLV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.NetworkAcl, error) { +func findVPCDefaultNetworkACLV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.NetworkAcl, error) { input := &ec2.DescribeNetworkAclsInput{ - Filters: BuildAttributeFilterListV2(map[string]string{ + Filters: buildAttributeFilterListV2(map[string]string{ "default": "true", "vpc-id": id, }), } - return FindNetworkACLV2(ctx, conn, input) + 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) +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 @@ -146,7 +146,7 @@ func FindNetworkACLV2(ctx context.Context, conn *ec2.Client, input *ec2.Describe return tfresource.AssertSingleValueResult(output) } -func FindNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNetworkAclsInput) ([]awstypes.NetworkAcl, error) { +func findNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNetworkAclsInput) ([]awstypes.NetworkAcl, error) { var output []awstypes.NetworkAcl pages := ec2.NewDescribeNetworkAclsPaginator(conn, input) @@ -173,28 +173,28 @@ func FindNetworkACLsV2(ctx context.Context, conn *ec2.Client, input *ec2.Describ func FindVPCDefaultSecurityGroupV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.SecurityGroup, error) { input := &ec2.DescribeSecurityGroupsInput{ - Filters: BuildAttributeFilterListV2(map[string]string{ + Filters: buildAttributeFilterListV2(map[string]string{ "group-name": DefaultSecurityGroupName, "vpc-id": id, }), } - return FindSecurityGroupV2(ctx, conn, input) + return findSecurityGroupV2(ctx, conn, input) } -func FindVPCMainRouteTableV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.RouteTable, error) { +func findVPCMainRouteTableV2(ctx context.Context, conn *ec2.Client, id string) (*awstypes.RouteTable, error) { input := &ec2.DescribeRouteTablesInput{ - Filters: BuildAttributeFilterListV2(map[string]string{ + Filters: buildAttributeFilterListV2(map[string]string{ "association.main": "true", "vpc-id": id, }), } - return FindRouteTableV2(ctx, conn, input) + 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) +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 @@ -203,7 +203,7 @@ func FindRouteTableV2(ctx context.Context, conn *ec2.Client, input *ec2.Describe return tfresource.AssertSingleValueResult(output) } -func FindRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) ([]awstypes.RouteTable, error) { +func findRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) ([]awstypes.RouteTable, error) { var output []awstypes.RouteTable pages := ec2.NewDescribeRouteTablesPaginator(conn, input) @@ -228,8 +228,8 @@ func FindRouteTablesV2(ctx context.Context, conn *ec2.Client, input *ec2.Describ return output, nil } -func FindSecurityGroupV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) (*awstypes.SecurityGroup, error) { - output, err := FindSecurityGroupsV2(ctx, conn, input) +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 @@ -238,7 +238,7 @@ func FindSecurityGroupV2(ctx context.Context, conn *ec2.Client, input *ec2.Descr return tfresource.AssertSingleValueResult(output) } -func FindSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) ([]awstypes.SecurityGroup, error) { +func findSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) ([]awstypes.SecurityGroup, error) { var output []awstypes.SecurityGroup pages := ec2.NewDescribeSecurityGroupsPaginator(conn, input) @@ -263,7 +263,7 @@ func FindSecurityGroupsV2(ctx context.Context, conn *ec2.Client, input *ec2.Desc return output, nil } -func FindIPAMPoolAllocationsV2(ctx context.Context, conn *ec2.Client, input *ec2.GetIpamPoolAllocationsInput) ([]awstypes.IpamPoolAllocation, error) { +func findIPAMPoolAllocationsV2(ctx context.Context, conn *ec2.Client, input *ec2.GetIpamPoolAllocationsInput) ([]awstypes.IpamPoolAllocation, error) { var output []awstypes.IpamPoolAllocation pages := ec2.NewGetIpamPoolAllocationsPaginator(conn, input) diff --git a/internal/service/ec2/statusv2.go b/internal/service/ec2/statusv2.go index 5564bbeff29c..a37abaac2166 100644 --- a/internal/service/ec2/statusv2.go +++ b/internal/service/ec2/statusv2.go @@ -13,9 +13,9 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func StatusVPCStateV2(ctx context.Context, conn *ec2.Client, id string) retry.StateRefreshFunc { +func statusVPCStateV2(ctx context.Context, conn *ec2.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { - output, err := FindVPCByIDV2(ctx, conn, id) + output, err := findVPCByIDV2(ctx, conn, id) if tfresource.NotFound(err) { return nil, "", nil @@ -29,9 +29,9 @@ func StatusVPCStateV2(ctx context.Context, conn *ec2.Client, id string) retry.St } } -func StatusVPCIPv6CIDRBlockAssociationStateV2(ctx context.Context, conn *ec2.Client, id string) retry.StateRefreshFunc { +func statusVPCIPv6CIDRBlockAssociationStateV2(ctx context.Context, conn *ec2.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { - output, _, err := FindVPCIPv6CIDRBlockAssociationByIDV2(ctx, conn, id) + output, _, err := findVPCIPv6CIDRBlockAssociationByIDV2(ctx, conn, id) if tfresource.NotFound(err) { return nil, "", nil @@ -45,9 +45,9 @@ func StatusVPCIPv6CIDRBlockAssociationStateV2(ctx context.Context, conn *ec2.Cli } } -func StatusVPCAttributeValueV2(ctx context.Context, conn *ec2.Client, id string, attribute awstypes.VpcAttributeName) retry.StateRefreshFunc { +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) + attributeValue, err := findVPCAttributeV2(ctx, conn, id, attribute) if tfresource.NotFound(err) { return nil, "", nil diff --git a/internal/service/ec2/vpc_.go b/internal/service/ec2/vpc_.go index 8af084cc2a5d..8aaa044f5cb1 100644 --- a/internal/service/ec2/vpc_.go +++ b/internal/service/ec2/vpc_.go @@ -234,7 +234,7 @@ func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interfa d.SetId(aws.ToString(output.Vpc.VpcId)) - vpc, err := WaitVPCCreatedV2(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) @@ -243,10 +243,8 @@ func resourceVPCCreate(ctx context.Context, d *schema.ResourceData, meta interfa if len(vpc.Ipv6CidrBlockAssociationSet) > 0 { associationID := aws.ToString(output.Vpc.Ipv6CidrBlockAssociationSet[0].AssociationId) - _, err = WaitVPCIPv6CIDRBlockAssociationCreatedV2(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) } } @@ -269,7 +267,7 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface conn := meta.(*conns.AWSClient).EC2Client(ctx) outputRaw, err := tfresource.RetryWhenNewResourceNotFound(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return FindVPCByIDV2(ctx, conn, d.Id()) + return findVPCByIDV2(ctx, conn, d.Id()) }, d.IsNewResource()) if !d.IsNewResource() && tfresource.NotFound(err) { @@ -299,7 +297,7 @@ 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 FindVPCAttributeV2(ctx, conn, d.Id(), types.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(), types.VpcAttributeNameEnableDnsHostnames, err) } else { @@ -307,7 +305,7 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface } if v, err := tfresource.RetryWhenNewResourceNotFound(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return FindVPCAttributeV2(ctx, conn, d.Id(), types.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(), types.VpcAttributeNameEnableDnsSupport, err) } else { @@ -315,20 +313,20 @@ func resourceVPCRead(ctx context.Context, d *schema.ResourceData, meta interface } if v, err := tfresource.RetryWhenNewResourceNotFound(ctx, ec2PropagationTimeout, func() (interface{}, error) { - return FindVPCAttributeV2(ctx, conn, d.Id(), types.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(), types.VpcAttributeNameEnableNetworkAddressUsageMetrics, err) } else { d.Set("enable_network_address_usage_metrics", v) } - if v, err := FindVPCDefaultNetworkACLV2(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 := FindVPCMainRouteTableV2(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) @@ -473,7 +471,7 @@ func resourceVPCDelete(ctx context.Context, d *schema.ResourceData, meta interfa } _, err = tfresource.RetryUntilNotFound(ctx, vpcDeletedTimeout, func() (interface{}, error) { - return FindVPCByIDV2(ctx, conn, d.Id()) + return findVPCByIDV2(ctx, conn, d.Id()) }) if err != nil { @@ -609,7 +607,7 @@ func modifyVPCDNSHostnames(ctx context.Context, conn *ec2.Client, vpcID string, return fmt.Errorf("modifying EnableDnsHostnames: %w", err) } - if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, types.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) } @@ -628,7 +626,7 @@ func modifyVPCDNSSupport(ctx context.Context, conn *ec2.Client, vpcID string, v return fmt.Errorf("modifying EnableDnsSupport: %w", err) } - if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, types.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) } @@ -647,7 +645,7 @@ func modifyVPCNetworkAddressUsageMetrics(ctx context.Context, conn *ec2.Client, return fmt.Errorf("modifying EnableNetworkAddressUsageMetrics: %w", err) } - if _, err := WaitVPCAttributeUpdatedV2(ctx, conn, vpcID, types.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) } @@ -668,9 +666,7 @@ func modifyVPCIPv6CIDRBlockAssociation(ctx context.Context, conn *ec2.Client, vp return "", fmt.Errorf("disassociating IPv6 CIDR block (%s): %w", associationID, err) } - _, err = WaitVPCIPv6CIDRBlockAssociationDeletedV2(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) } } @@ -708,9 +704,7 @@ func modifyVPCIPv6CIDRBlockAssociation(ctx context.Context, conn *ec2.Client, vp associationID = aws.ToString(output.Ipv6CidrBlockAssociation.AssociationId) - _, err = WaitVPCIPv6CIDRBlockAssociationCreatedV2(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) } } @@ -736,7 +730,7 @@ func findIPAMPoolAllocationsForVPC(ctx context.Context, conn *ec2.Client, poolID IpamPoolId: aws.String(poolID), } - output, err := FindIPAMPoolAllocationsV2(ctx, conn, input) + output, err := findIPAMPoolAllocationsV2(ctx, conn, input) if err != nil { return nil, err diff --git a/internal/service/ec2/vpc_default_vpc.go b/internal/service/ec2/vpc_default_vpc.go index 2e29ca5e2c17..240f18c8058a 100644 --- a/internal/service/ec2/vpc_default_vpc.go +++ b/internal/service/ec2/vpc_default_vpc.go @@ -158,7 +158,7 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.DescribeVpcsInput{ - Filters: BuildAttributeFilterListV2( + Filters: buildAttributeFilterListV2( map[string]string{ "isDefault": "true", }, @@ -166,7 +166,7 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta } vpcInfo := &vpcInfo{} - vpc, err := FindVPCV2(ctx, conn, input) + vpc, err := findVPCV2(ctx, conn, input) if err == nil { d.SetId(aws.ToString(vpc.VpcId)) @@ -174,18 +174,18 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta vpcInfo.vpc = vpc - if v, err := FindVPCAttributeV2(ctx, conn, d.Id(), awstypes.VpcAttributeNameEnableDnsHostnames); err != nil { + 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 := FindVPCAttributeV2(ctx, conn, d.Id(), awstypes.VpcAttributeNameEnableDnsSupport); err != nil { + 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 := FindVPCAttributeV2(ctx, conn, d.Id(), awstypes.VpcAttributeNameEnableNetworkAddressUsageMetrics); err != nil { + 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 @@ -204,7 +204,7 @@ func resourceDefaultVPCCreate(ctx context.Context, d *schema.ResourceData, meta d.SetId(aws.ToString(vpc.VpcId)) d.Set("existing_default_vpc", false) - vpc, err = WaitVPCCreatedV2(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) diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index 7280c4d09573..e6f5aeb57698 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -18,11 +18,11 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -func WaitVPCCreatedV2(ctx context.Context, conn *ec2.Client, id string) (*types.Vpc, error) { +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), + Refresh: statusVPCStateV2(ctx, conn, id), Timeout: vpcCreatedTimeout, } @@ -35,11 +35,11 @@ func WaitVPCCreatedV2(ctx context.Context, conn *ec2.Client, id string) (*types. return nil, err } -func WaitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*types.VpcCidrBlockState, error) { +func waitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*types.VpcCidrBlockState, error) { stateConf := &retry.StateChangeConf{ Pending: enum.Slice(types.VpcCidrBlockStateCodeAssociating, types.VpcCidrBlockStateCodeDisassociated, types.VpcCidrBlockStateCodeFailing), Target: enum.Slice(types.VpcCidrBlockStateCodeAssociated), - Refresh: StatusVPCIPv6CIDRBlockAssociationStateV2(ctx, conn, id), + Refresh: statusVPCIPv6CIDRBlockAssociationStateV2(ctx, conn, id), Timeout: timeout, Delay: 10 * time.Second, MinTimeout: 5 * time.Second, @@ -58,10 +58,10 @@ func WaitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Cli return nil, err } -func WaitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName, expectedValue bool) (*types.Vpc, error) { +func waitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName, expectedValue bool) (*types.Vpc, error) { stateConf := &retry.StateChangeConf{ Target: []string{strconv.FormatBool(expectedValue)}, - Refresh: StatusVPCAttributeValueV2(ctx, conn, vpcID, attribute), + Refresh: statusVPCAttributeValueV2(ctx, conn, vpcID, attribute), Timeout: ec2PropagationTimeout, Delay: 10 * time.Second, MinTimeout: 3 * time.Second, @@ -76,11 +76,11 @@ func WaitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID stri return nil, err } -func WaitVPCIPv6CIDRBlockAssociationDeletedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*types.VpcCidrBlockState, error) { +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), + Refresh: statusVPCIPv6CIDRBlockAssociationStateV2(ctx, conn, id), Timeout: timeout, Delay: 10 * time.Second, MinTimeout: 5 * time.Second, From 211190fc8b0d25eebe023e98bd26c599f02a9e99 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 24 Jul 2023 12:19:49 -0400 Subject: [PATCH 09/12] Add 'service/ec2/exports.go'. --- internal/service/ec2/exports.go | 8 ++++++++ internal/service/ec2/exports_test.go | 2 -- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 internal/service/ec2/exports.go 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 48b1f5498a8d..871f329d292a 100644 --- a/internal/service/ec2/exports_test.go +++ b/internal/service/ec2/exports_test.go @@ -5,8 +5,6 @@ package ec2 // Exports for use in tests only. var ( - FindVPCByIDV2 = findVPCByIDV2 - ResourceInstanceConnectEndpoint = newResourceInstanceConnectEndpoint ResourceSecurityGroupEgressRule = newResourceSecurityGroupEgressRule ResourceSecurityGroupIngressRule = newResourceSecurityGroupIngressRule From 593237d5afbccbbcd00b705777214668695fe713 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 24 Jul 2023 12:55:31 -0400 Subject: [PATCH 10/12] Fix golangci-lint 'unparam'. --- internal/service/ec2/waitv2.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index e6f5aeb57698..63169fadbba4 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -35,7 +35,7 @@ func waitVPCCreatedV2(ctx context.Context, conn *ec2.Client, id string) (*types. return nil, err } -func waitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*types.VpcCidrBlockState, error) { +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), @@ -58,7 +58,7 @@ func waitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Cli return nil, err } -func waitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName, expectedValue bool) (*types.Vpc, error) { +func waitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName, expectedValue bool) (*types.Vpc, error) { //nolint:unparam stateConf := &retry.StateChangeConf{ Target: []string{strconv.FormatBool(expectedValue)}, Refresh: statusVPCAttributeValueV2(ctx, conn, vpcID, attribute), From 22443d02a3e9adff498e7088d4200ffb904eae00 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 24 Jul 2023 12:56:58 -0400 Subject: [PATCH 11/12] Fix golangci-lint 'stylecheck'. --- internal/service/ec2/waitv2.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index 63169fadbba4..c47658bc283c 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -12,7 +12,6 @@ import ( "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" - 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/enum" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" @@ -58,7 +57,7 @@ func waitVPCIPv6CIDRBlockAssociationCreatedV2(ctx context.Context, conn *ec2.Cli return nil, err } -func waitVPCAttributeUpdatedV2(ctx context.Context, conn *ec2.Client, vpcID string, attribute awstypes.VpcAttributeName, expectedValue bool) (*types.Vpc, error) { //nolint:unparam +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), From b7ef359d0ebfc90c0f255055771409c8d741bc4c Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 24 Jul 2023 13:10:21 -0400 Subject: [PATCH 12/12] r/aws_default_vpc: Use 'keyValueTagsV2' and 'getTagsInV2'. --- internal/service/ec2/vpc_default_vpc.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/ec2/vpc_default_vpc.go b/internal/service/ec2/vpc_default_vpc.go index 240f18c8058a..09badf17c125 100644 --- a/internal/service/ec2/vpc_default_vpc.go +++ b/internal/service/ec2/vpc_default_vpc.go @@ -271,8 +271,8 @@ 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 := updateTagsV2(ctx, conn, d.Id(), oldTags, newTags); err != nil {