-
Notifications
You must be signed in to change notification settings - Fork 10k
list-resource/aws_vpc: Reduces API calls when listing #46935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7c0a5a3
a1837b3
5e58a5a
6756ab6
1745175
017065e
d29e8ee
5d375a5
a6892b2
9218bc0
4a1433a
f2cbeeb
2d11a0b
d261cc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1723,6 +1723,17 @@ func findVPCDefaultNetworkACL(ctx context.Context, conn *ec2.Client, id string) | |
| return findNetworkACL(ctx, conn, &input) | ||
| } | ||
|
|
||
| func batchFindVPCDefaultNetworkACLs(ctx context.Context, conn *ec2.Client, ids []string) (map[string]*awstypes.NetworkAcl, error) { | ||
| input := ec2.DescribeNetworkAclsInput{ | ||
| Filters: newMultiValueAttributeFilterList(map[string][]string{ | ||
| "default": {"true"}, | ||
| "vpc-id": ids, | ||
| }), | ||
| } | ||
|
|
||
| return batchFindNetworkACLs(ctx, conn, &input) | ||
| } | ||
|
|
||
| func findNATGateway(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNatGatewaysInput) (*awstypes.NatGateway, error) { | ||
| output, err := findNATGateways(ctx, conn, input) | ||
|
|
||
|
|
@@ -1850,6 +1861,21 @@ func findNetworkACL(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNe | |
| return tfresource.AssertSingleValueResult(output) | ||
| } | ||
|
|
||
| func batchFindNetworkACLs(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNetworkAclsInput) (map[string]*awstypes.NetworkAcl, error) { | ||
| output, err := findNetworkACLs(ctx, conn, input) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| results := make(map[string]*awstypes.NetworkAcl, len(output)) | ||
| for i, v := range output { | ||
| results[aws.ToString(v.VpcId)] = &output[i] | ||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| func findNetworkACLs(ctx context.Context, conn *ec2.Client, input *ec2.DescribeNetworkAclsInput) ([]awstypes.NetworkAcl, error) { | ||
| var output []awstypes.NetworkAcl | ||
|
|
||
|
|
@@ -1940,6 +1966,17 @@ func findVPCDefaultSecurityGroup(ctx context.Context, conn *ec2.Client, id strin | |
| return findSecurityGroup(ctx, conn, &input) | ||
| } | ||
|
|
||
| func batchFindVPCDefaultSecurityGroups(ctx context.Context, conn *ec2.Client, ids []string) (map[string]*awstypes.SecurityGroup, error) { | ||
| input := ec2.DescribeSecurityGroupsInput{ | ||
| Filters: newMultiValueAttributeFilterList(map[string][]string{ | ||
| "group-name": {defaultSecurityGroupName}, | ||
| "vpc-id": ids, | ||
| }), | ||
| } | ||
|
|
||
| return batchFindSecurityGroups(ctx, conn, &input) | ||
| } | ||
|
|
||
| func findVPCDHCPOptionsAssociation(ctx context.Context, conn *ec2.Client, vpcID string, dhcpOptionsID string) error { | ||
| vpc, err := findVPCByID(ctx, conn, vpcID) | ||
|
|
||
|
|
@@ -1967,6 +2004,17 @@ func findVPCMainRouteTable(ctx context.Context, conn *ec2.Client, id string) (*a | |
| return findRouteTable(ctx, conn, &input) | ||
| } | ||
|
|
||
| func batchFindVPCMainRouteTables(ctx context.Context, conn *ec2.Client, ids []string) (map[string]*awstypes.RouteTable, error) { | ||
| input := ec2.DescribeRouteTablesInput{ | ||
| Filters: newMultiValueAttributeFilterList(map[string][]string{ | ||
| "association.main": {"true"}, | ||
| "vpc-id": ids, | ||
| }), | ||
| } | ||
|
|
||
| return batchFindRouteTables(ctx, conn, &input) | ||
| } | ||
|
|
||
| func findRouteTable(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) (*awstypes.RouteTable, error) { | ||
| output, err := findRouteTables(ctx, conn, input) | ||
|
|
||
|
|
@@ -1977,6 +2025,21 @@ func findRouteTable(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRo | |
| return tfresource.AssertSingleValueResult(output) | ||
| } | ||
|
|
||
| func batchFindRouteTables(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) (map[string]*awstypes.RouteTable, error) { | ||
| output, err := findRouteTables(ctx, conn, input) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| results := make(map[string]*awstypes.RouteTable, len(output)) | ||
| for i, v := range output { | ||
| results[aws.ToString(v.VpcId)] = &output[i] | ||
| } | ||
|
Comment on lines
+2035
to
+2038
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it will overwrite the map entry for each route table in the result. Should this instead check for the key in Edit: I see now the only current call to this function is from |
||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| func findRouteTables(ctx context.Context, conn *ec2.Client, input *ec2.DescribeRouteTablesInput) ([]awstypes.RouteTable, error) { | ||
| var output []awstypes.RouteTable | ||
|
|
||
|
|
@@ -2010,6 +2073,21 @@ func findSecurityGroup(ctx context.Context, conn *ec2.Client, input *ec2.Describ | |
| return tfresource.AssertSingleValueResult(output) | ||
| } | ||
|
|
||
| func batchFindSecurityGroups(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) (map[string]*awstypes.SecurityGroup, error) { | ||
| output, err := findSecurityGroups(ctx, conn, input) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| results := make(map[string]*awstypes.SecurityGroup, len(output)) | ||
| for i, v := range output { | ||
| results[aws.ToString(v.VpcId)] = &output[i] | ||
| } | ||
|
Comment on lines
+2083
to
+2086
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for iterating security group results (multiple per VPC). |
||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| func findSecurityGroups(ctx context.Context, conn *ec2.Client, input *ec2.DescribeSecurityGroupsInput) ([]awstypes.SecurityGroup, error) { | ||
| var output []awstypes.SecurityGroup | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Copyright IBM Corp. 2014, 2026 | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| resource "aws_vpc" "test" { | ||
| count = var.resource_count | ||
|
|
||
| cidr_block = "10.1.0.0/16" | ||
|
|
||
| tags = var.resource_tags | ||
| } | ||
|
|
||
| variable "resource_count" { | ||
| description = "Number of resources to create" | ||
| type = number | ||
| nullable = false | ||
| } | ||
|
|
||
| variable "resource_tags" { | ||
| description = "Tags to set on resource" | ||
| type = map(string) | ||
| nullable = false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright IBM Corp. 2014, 2026 | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| list "aws_vpc" "test" { | ||
| provider = aws | ||
|
|
||
| include_resource = true | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for iterating network ACLs (one per subnet, but a VPC may have multiple).