Skip to content

Commit aac92c3

Browse files
committed
r/aws_vpc_endpoint_connection_service_allowed_principal: convert to aws sdk v2
```console % make testacc PKG=ec2 TESTS="TestAccVPCEndpointServiceAllowedPrincipal_" ==> Checking that code complies with gofmt requirements... TF_ACC=1 go1.22.2 test ./internal/service/ec2/... -v -count 1 -parallel 20 -run='TestAccVPCEndpointServiceAllowedPrincipal_' -timeout 360m --- PASS: TestAccVPCEndpointServiceAllowedPrincipal_basic (211.30s) --- PASS: TestAccVPCEndpointServiceAllowedPrincipal_multiple (220.94s) --- PASS: TestAccVPCEndpointServiceAllowedPrincipal_tags (231.80s) --- PASS: TestAccVPCEndpointServiceAllowedPrincipal_migrateID (236.87s) --- PASS: TestAccVPCEndpointServiceAllowedPrincipal_migrateAndTag (247.32s) PASS ok github.com/hashicorp/terraform-provider-aws/internal/service/ec2 252.532s ```
1 parent 8381af5 commit aac92c3

4 files changed

+46
-20
lines changed

internal/service/ec2/exports_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ var (
5252
FindVPCEndpointConnectionNotificationByIDV2 = findVPCEndpointConnectionNotificationByIDV2
5353
FindVPCEndpointRouteTableAssociationExistsV2 = findVPCEndpointRouteTableAssociationExistsV2
5454
FindVPCEndpointSecurityGroupAssociationExistsV2 = findVPCEndpointSecurityGroupAssociationExistsV2
55-
FindVPCEndpointSubnetAssociationExistsV2 = findVPCEndpointSubnetAssociationExistsV2
5655
FindVPCEndpointServiceConfigurationByIDV2 = findVPCEndpointServiceConfigurationByIDV2
56+
FindVPCEndpointServicePermissionV2 = findVPCEndpointServicePermissionV2
57+
FindVPCEndpointSubnetAssociationExistsV2 = findVPCEndpointSubnetAssociationExistsV2
5758
FindVPNGatewayRoutePropagationExistsV2 = findVPNGatewayRoutePropagationExistsV2
5859
FlattenNetworkInterfacePrivateIPAddresses = flattenNetworkInterfacePrivateIPAddresses
5960
IPAMServicePrincipal = ipamServicePrincipal

internal/service/ec2/findv2.go

+22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types"
1313
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
15+
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
1516
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
1617
"github.com/hashicorp/terraform-provider-aws/internal/types"
1718
"github.com/hashicorp/terraform-provider-aws/names"
@@ -970,3 +971,24 @@ func findVPCEndpointConnectionNotificationByIDV2(ctx context.Context, conn *ec2.
970971

971972
return output, nil
972973
}
974+
975+
func findVPCEndpointServicePermissionV2(ctx context.Context, conn *ec2.Client, serviceID, principalARN string) (*awstypes.AllowedPrincipal, error) {
976+
// Applying a server-side filter on "principal" can lead to errors like
977+
// "An error occurred (InvalidFilter) when calling the DescribeVpcEndpointServicePermissions operation: The filter value arn:aws:iam::123456789012:role/developer contains unsupported characters".
978+
// Apply the filter client-side.
979+
input := &ec2.DescribeVpcEndpointServicePermissionsInput{
980+
ServiceId: aws.String(serviceID),
981+
}
982+
983+
allowedPrincipals, err := findVPCEndpointServicePermissionsV2(ctx, conn, input)
984+
985+
if err != nil {
986+
return nil, err
987+
}
988+
989+
allowedPrincipals = tfslices.Filter(allowedPrincipals, func(v awstypes.AllowedPrincipal) bool {
990+
return aws.ToString(v.Principal) == principalARN
991+
})
992+
993+
return tfresource.AssertSingleValueResult(allowedPrincipals)
994+
}

internal/service/ec2/vpc_endpoint_service_allowed_principal.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"context"
88
"log"
99

10-
"github.com/aws/aws-sdk-go/aws"
11-
"github.com/aws/aws-sdk-go/service/ec2"
12-
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
10+
"github.com/aws/aws-sdk-go-v2/aws"
11+
"github.com/aws/aws-sdk-go-v2/service/ec2"
12+
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1515
"github.com/hashicorp/terraform-provider-aws/internal/conns"
@@ -41,13 +41,13 @@ func ResourceVPCEndpointServiceAllowedPrincipal() *schema.Resource {
4141

4242
func resourceVPCEndpointServiceAllowedPrincipalCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
4343
var diags diag.Diagnostics
44-
conn := meta.(*conns.AWSClient).EC2Conn(ctx)
44+
conn := meta.(*conns.AWSClient).EC2Client(ctx)
4545

4646
serviceID := d.Get("vpc_endpoint_service_id").(string)
4747
principalARN := d.Get("principal_arn").(string)
4848

49-
output, err := conn.ModifyVpcEndpointServicePermissionsWithContext(ctx, &ec2.ModifyVpcEndpointServicePermissionsInput{
50-
AddAllowedPrincipals: aws.StringSlice([]string{principalARN}),
49+
output, err := conn.ModifyVpcEndpointServicePermissions(ctx, &ec2.ModifyVpcEndpointServicePermissionsInput{
50+
AddAllowedPrincipals: []string{principalARN},
5151
ServiceId: aws.String(serviceID),
5252
})
5353

@@ -56,8 +56,8 @@ func resourceVPCEndpointServiceAllowedPrincipalCreate(ctx context.Context, d *sc
5656
}
5757

5858
for _, v := range output.AddedPrincipals {
59-
if aws.StringValue(v.Principal) == principalARN {
60-
d.SetId(aws.StringValue(v.ServicePermissionId))
59+
if aws.ToString(v.Principal) == principalARN {
60+
d.SetId(aws.ToString(v.ServicePermissionId))
6161
}
6262
}
6363

@@ -66,12 +66,12 @@ func resourceVPCEndpointServiceAllowedPrincipalCreate(ctx context.Context, d *sc
6666

6767
func resourceVPCEndpointServiceAllowedPrincipalRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
6868
var diags diag.Diagnostics
69-
conn := meta.(*conns.AWSClient).EC2Conn(ctx)
69+
conn := meta.(*conns.AWSClient).EC2Client(ctx)
7070

7171
serviceID := d.Get("vpc_endpoint_service_id").(string)
7272
principalARN := d.Get("principal_arn").(string)
7373

74-
output, err := FindVPCEndpointServicePermission(ctx, conn, serviceID, principalARN)
74+
output, err := findVPCEndpointServicePermissionV2(ctx, conn, serviceID, principalARN)
7575

7676
if !d.IsNewResource() && tfresource.NotFound(err) {
7777
log.Printf("[WARN] EC2 VPC Endpoint Service Allowed Principal %s not found, removing from state", d.Id())
@@ -83,20 +83,20 @@ func resourceVPCEndpointServiceAllowedPrincipalRead(ctx context.Context, d *sche
8383
return sdkdiag.AppendErrorf(diags, "reading EC2 VPC Endpoint Service (%s) Allowed Principal (%s): %s", serviceID, principalARN, err)
8484
}
8585

86-
d.SetId(aws.StringValue(output.ServicePermissionId))
86+
d.SetId(aws.ToString(output.ServicePermissionId))
8787

8888
return diags
8989
}
9090

9191
func resourceVPCEndpointServiceAllowedPrincipalDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
9292
var diags diag.Diagnostics
93-
conn := meta.(*conns.AWSClient).EC2Conn(ctx)
93+
conn := meta.(*conns.AWSClient).EC2Client(ctx)
9494

9595
serviceID := d.Get("vpc_endpoint_service_id").(string)
9696
principalARN := d.Get("principal_arn").(string)
9797

98-
_, err := conn.ModifyVpcEndpointServicePermissionsWithContext(ctx, &ec2.ModifyVpcEndpointServicePermissionsInput{
99-
RemoveAllowedPrincipals: aws.StringSlice([]string{principalARN}),
98+
_, err := conn.ModifyVpcEndpointServicePermissions(ctx, &ec2.ModifyVpcEndpointServicePermissionsInput{
99+
RemoveAllowedPrincipals: []string{principalARN},
100100
ServiceId: aws.String(serviceID),
101101
})
102102

internal/service/ec2/vpc_endpoint_service_allowed_principal_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ func TestAccVPCEndpointServiceAllowedPrincipal_migrateID(t *testing.T) {
123123
{
124124
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
125125
Config: testAccVPCEndpointServiceAllowedPrincipalConfig_basic(rName),
126-
PlanOnly: true,
126+
Check: resource.ComposeAggregateTestCheckFunc(
127+
testAccCheckVPCEndpointServiceAllowedPrincipalExists(ctx, resourceName),
128+
resource.TestMatchResourceAttr(resourceName, names.AttrID, regexache.MustCompile(`^vpce-svc-perm-\w{17}$`)),
129+
),
127130
},
128131
},
129132
})
@@ -171,14 +174,14 @@ func TestAccVPCEndpointServiceAllowedPrincipal_migrateAndTag(t *testing.T) {
171174

172175
func testAccCheckVPCEndpointServiceAllowedPrincipalDestroy(ctx context.Context) resource.TestCheckFunc {
173176
return func(s *terraform.State) error {
174-
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx)
177+
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx)
175178

176179
for _, rs := range s.RootModule().Resources {
177180
if rs.Type != "aws_vpc_endpoint_service_allowed_principal" {
178181
continue
179182
}
180183

181-
_, err := tfec2.FindVPCEndpointServicePermission(ctx, conn, rs.Primary.Attributes["vpc_endpoint_service_id"], rs.Primary.Attributes["principal_arn"])
184+
_, err := tfec2.FindVPCEndpointServicePermissionV2(ctx, conn, rs.Primary.Attributes["vpc_endpoint_service_id"], rs.Primary.Attributes["principal_arn"])
182185

183186
if tfresource.NotFound(err) {
184187
continue
@@ -206,9 +209,9 @@ func testAccCheckVPCEndpointServiceAllowedPrincipalExists(ctx context.Context, n
206209
return fmt.Errorf("No EC2 VPC Endpoint Service Allowed Principal ID is set")
207210
}
208211

209-
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx)
212+
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx)
210213

211-
_, err := tfec2.FindVPCEndpointServicePermission(ctx, conn, rs.Primary.Attributes["vpc_endpoint_service_id"], rs.Primary.Attributes["principal_arn"])
214+
_, err := tfec2.FindVPCEndpointServicePermissionV2(ctx, conn, rs.Primary.Attributes["vpc_endpoint_service_id"], rs.Primary.Attributes["principal_arn"])
212215

213216
return err
214217
}

0 commit comments

Comments
 (0)