Skip to content

Commit

Permalink
Merge pull request #32368 from mattburgess/vpc-sdkv2-migration
Browse files Browse the repository at this point in the history
r/aws_vpc and r/aws_default_vpc use AWS SDKv2
  • Loading branch information
ewbankkit authored Jul 24, 2023
2 parents d71a417 + b7ef359 commit a932c7b
Show file tree
Hide file tree
Showing 16 changed files with 806 additions and 228 deletions.
26 changes: 26 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2261,6 +2262,31 @@ func CheckVPCExists(ctx context.Context, n string, v *ec2.Vpc) resource.TestChec
}
}

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

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

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

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

if err != nil {
return err
}

*v = *output

return nil
}
}

func CheckCallerIdentityAccountID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
1 change: 1 addition & 0 deletions internal/generate/tags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
3 changes: 3 additions & 0 deletions internal/generate/tags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -170,6 +171,7 @@ type TemplateData struct {
TagInCustomVal string
TagInIDElem string
TagInIDNeedSlice string
TagInIDNeedValueSlice string
TagInTagsElem string
TagKeyType string
TagOp string
Expand Down Expand Up @@ -316,6 +318,7 @@ func main() {
TagInCustomVal: *tagInCustomVal,
TagInIDElem: *tagInIDElem,
TagInIDNeedSlice: *tagInIDNeedSlice,
TagInIDNeedValueSlice: *tagInIDNeedValueSlice,
TagInTagsElem: *tagInTagsElem,
TagKeyType: *tagKeyType,
TagOp: *tagOp,
Expand Down
6 changes: 6 additions & 0 deletions internal/generate/tags/templates/v1/update_tags_body.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down
6 changes: 6 additions & 0 deletions internal/generate/tags/templates/v2/update_tags_body.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down
8 changes: 8 additions & 0 deletions internal/service/ec2/exports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ec2

var (
FindVPCByIDV2 = findVPCByIDV2
)
3 changes: 2 additions & 1 deletion internal/service/ec2/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ var (
ResourceSecurityGroupEgressRule = newResourceSecurityGroupEgressRule
ResourceSecurityGroupIngressRule = newResourceSecurityGroupIngressRule

UpdateTags = updateTags
UpdateTags = updateTags
UpdateTagsV2 = updateTagsV2
)
31 changes: 31 additions & 0 deletions internal/service/ec2/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
}
}
Loading

0 comments on commit a932c7b

Please sign in to comment.