Skip to content

Commit 9606b44

Browse files
committed
Allows creating tag after migration
1 parent 09ec0a3 commit 9606b44

4 files changed

+60
-38
lines changed

internal/service/ec2/find.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -3345,30 +3345,21 @@ func FindVPCEndpointServicePermissions(ctx context.Context, conn *ec2.EC2, input
33453345
return output, nil
33463346
}
33473347

3348-
func FindVPCEndpointServicePermissionsByID(ctx context.Context, conn *ec2.EC2, id string) ([]*ec2.AllowedPrincipal, error) {
3348+
func FindVPCEndpointServicePermissionsByServiceID(ctx context.Context, conn *ec2.EC2, id string) ([]*ec2.AllowedPrincipal, error) {
33493349
input := &ec2.DescribeVpcEndpointServicePermissionsInput{
33503350
ServiceId: aws.String(id),
33513351
}
33523352

33533353
return FindVPCEndpointServicePermissions(ctx, conn, input)
33543354
}
33553355

3356-
func FindVPCEndpointServicePermissionExists(ctx context.Context, conn *ec2.EC2, serviceID, principalARN string) error {
3357-
allowedPrincipals, err := FindVPCEndpointServicePermissionsByID(ctx, conn, serviceID)
3358-
3356+
func FindVPCEndpointServicePermission(ctx context.Context, conn *ec2.EC2, serviceID, principalARN string) (*ec2.AllowedPrincipal, error) {
3357+
allowedPrincipals, err := FindVPCEndpointServicePermissionsByServiceID(ctx, conn, serviceID)
33593358
if err != nil {
3360-
return err
3361-
}
3362-
3363-
for _, v := range allowedPrincipals {
3364-
if aws.StringValue(v.Principal) == principalARN {
3365-
return nil
3366-
}
3359+
return nil, err
33673360
}
33683361

3369-
return &retry.NotFoundError{
3370-
LastError: fmt.Errorf("VPC Endpoint Service (%s) Principal (%s) not found", serviceID, principalARN),
3371-
}
3362+
return tfresource.AssertSingleResult(allowedPrincipals)
33723363
}
33733364

33743365
// FindVPCEndpointRouteTableAssociationExists returns NotFoundError if no association for the specified VPC endpoint and route table IDs is found.

internal/service/ec2/vpc_endpoint_service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func resourceVPCEndpointServiceRead(ctx context.Context, d *schema.ResourceData,
248248

249249
SetTagsOut(ctx, svcCfg.Tags)
250250

251-
allowedPrincipals, err := FindVPCEndpointServicePermissionsByID(ctx, conn, d.Id())
251+
allowedPrincipals, err := FindVPCEndpointServicePermissionsByServiceID(ctx, conn, d.Id())
252252

253253
if err != nil {
254254
return sdkdiag.AppendErrorf(diags, "reading EC2 VPC Endpoint Service (%s) permissions: %s", d.Id(), err)

internal/service/ec2/vpc_endpoint_service_allowed_principal.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func resourceVPCEndpointServiceAllowedPrincipalRead(ctx context.Context, d *sche
6868
serviceID := d.Get("vpc_endpoint_service_id").(string)
6969
principalARN := d.Get("principal_arn").(string)
7070

71-
err := FindVPCEndpointServicePermissionExists(ctx, conn, serviceID, principalARN)
71+
output, err := FindVPCEndpointServicePermission(ctx, conn, serviceID, principalARN)
7272

7373
if !d.IsNewResource() && tfresource.NotFound(err) {
7474
log.Printf("[WARN] EC2 VPC Endpoint Service Allowed Principal %s not found, removing from state", d.Id())
@@ -80,6 +80,8 @@ func resourceVPCEndpointServiceAllowedPrincipalRead(ctx context.Context, d *sche
8080
return sdkdiag.AppendErrorf(diags, "reading EC2 VPC Endpoint Service (%s) Allowed Principal (%s): %s", serviceID, principalARN, err)
8181
}
8282

83+
d.SetId(aws.StringValue(output.ServicePermissionId))
84+
8385
return diags
8486
}
8587

internal/service/ec2/vpc_endpoint_service_allowed_principal_test.go

+51-22
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import (
1818

1919
func TestAccVPCEndpointServiceAllowedPrincipal_basic(t *testing.T) {
2020
ctx := acctest.Context(t)
21-
resourceName := "aws_vpc_endpoint_service_allowed_principal.test"
2221
rName := sdkacctest.RandomWithPrefix("tfacctest")
2322

23+
resourceName := "aws_vpc_endpoint_service_allowed_principal.test"
24+
2425
resource.ParallelTest(t, resource.TestCase{
2526
PreCheck: func() { acctest.PreCheck(ctx, t) },
2627
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
@@ -68,9 +69,10 @@ func TestAccVPCEndpointServiceAllowedPrincipal_tags(t *testing.T) {
6869

6970
func TestAccVPCEndpointServiceAllowedPrincipal_migrateID(t *testing.T) {
7071
ctx := acctest.Context(t)
71-
resourceName := "aws_vpc_endpoint_service_allowed_principal.test"
7272
rName := sdkacctest.RandomWithPrefix("tfacctest")
7373

74+
resourceName := "aws_vpc_endpoint_service_allowed_principal.test"
75+
7476
resource.ParallelTest(t, resource.TestCase{
7577
PreCheck: func() { acctest.PreCheck(ctx, t) },
7678
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
@@ -97,6 +99,46 @@ func TestAccVPCEndpointServiceAllowedPrincipal_migrateID(t *testing.T) {
9799
})
98100
}
99101

102+
// Verify that the resource returns an ID usable for creating an `aws_ec2_tag`
103+
func TestAccVPCEndpointServiceAllowedPrincipal_migrateAndTag(t *testing.T) {
104+
ctx := acctest.Context(t)
105+
rName := sdkacctest.RandomWithPrefix("tfacctest")
106+
107+
resourceName := "aws_vpc_endpoint_service_allowed_principal.test"
108+
tagResourceName := "aws_ec2_tag.test"
109+
110+
resource.ParallelTest(t, resource.TestCase{
111+
PreCheck: func() { acctest.PreCheck(ctx, t) },
112+
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
113+
CheckDestroy: testAccCheckVPCEndpointServiceAllowedPrincipalDestroy(ctx),
114+
Steps: []resource.TestStep{
115+
{
116+
ExternalProviders: map[string]resource.ExternalProvider{
117+
"aws": {
118+
Source: "hashicorp/aws",
119+
VersionConstraint: "4.63.0",
120+
},
121+
},
122+
Config: testAccVPCEndpointServiceAllowedPrincipalConfig_basic(rName),
123+
Check: resource.ComposeAggregateTestCheckFunc(
124+
testAccCheckVPCEndpointServiceAllowedPrincipalExists(ctx, resourceName),
125+
),
126+
},
127+
{
128+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
129+
Config: testAccVPCEndpointServiceAllowedPrincipalConfig_tag(rName),
130+
Check: resource.ComposeAggregateTestCheckFunc(
131+
testAccCheckVPCEndpointServiceAllowedPrincipalExists(ctx, resourceName),
132+
resource.TestMatchResourceAttr(resourceName, "id", regexp.MustCompile(`^vpce-svc-perm-\w{17}$`)),
133+
resource.TestCheckResourceAttrPair(tagResourceName, "resource_id", resourceName, "id"),
134+
resource.TestCheckResourceAttr(tagResourceName, "key", "Name"),
135+
resource.TestCheckResourceAttr(tagResourceName, "value", rName),
136+
),
137+
},
138+
},
139+
})
140+
}
141+
100142
func testAccCheckVPCEndpointServiceAllowedPrincipalDestroy(ctx context.Context) resource.TestCheckFunc {
101143
return func(s *terraform.State) error {
102144
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn()
@@ -106,7 +148,7 @@ func testAccCheckVPCEndpointServiceAllowedPrincipalDestroy(ctx context.Context)
106148
continue
107149
}
108150

109-
err := tfec2.FindVPCEndpointServicePermissionExists(ctx, conn, rs.Primary.Attributes["vpc_endpoint_service_id"], rs.Primary.Attributes["principal_arn"])
151+
_, err := tfec2.FindVPCEndpointServicePermission(ctx, conn, rs.Primary.Attributes["vpc_endpoint_service_id"], rs.Primary.Attributes["principal_arn"])
110152

111153
if tfresource.NotFound(err) {
112154
continue
@@ -136,7 +178,9 @@ func testAccCheckVPCEndpointServiceAllowedPrincipalExists(ctx context.Context, n
136178

137179
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn()
138180

139-
return tfec2.FindVPCEndpointServicePermissionExists(ctx, conn, rs.Primary.Attributes["vpc_endpoint_service_id"], rs.Primary.Attributes["principal_arn"])
181+
_, err := tfec2.FindVPCEndpointServicePermission(ctx, conn, rs.Primary.Attributes["vpc_endpoint_service_id"], rs.Primary.Attributes["principal_arn"])
182+
183+
return err
140184
}
141185
}
142186

@@ -163,24 +207,9 @@ resource "aws_vpc_endpoint_service_allowed_principal" "test" {
163207
}
164208

165209
func testAccVPCEndpointServiceAllowedPrincipalConfig_tag(rName string) string {
166-
return acctest.ConfigCompose(testAccVPCEndpointServiceConfig_networkLoadBalancerBase(rName, 1), fmt.Sprintf(`
167-
data "aws_caller_identity" "current" {}
168-
169-
data "aws_iam_session_context" "current" {
170-
arn = data.aws_caller_identity.current.arn
171-
}
172-
173-
resource "aws_vpc_endpoint_service" "test" {
174-
acceptance_required = false
175-
network_load_balancer_arns = aws_lb.test[*].arn
176-
}
177-
178-
resource "aws_vpc_endpoint_service_allowed_principal" "test" {
179-
vpc_endpoint_service_id = aws_vpc_endpoint_service.test.id
180-
181-
principal_arn = data.aws_iam_session_context.current.issuer_arn
182-
}
183-
210+
return acctest.ConfigCompose(
211+
testAccVPCEndpointServiceAllowedPrincipalConfig_basic(rName),
212+
fmt.Sprintf(`
184213
resource "aws_ec2_tag" "test" {
185214
resource_id = aws_vpc_endpoint_service_allowed_principal.test.id
186215

0 commit comments

Comments
 (0)