Skip to content

Commit

Permalink
resource/aws_vpc_endpoint: Add 'arn' attribute and tags test (#13853)
Browse files Browse the repository at this point in the history
Output from acceptance testing:

```
--- PASS: TestAccAWSVpcEndpoint_gatewayBasic (24.73s)
--- PASS: TestAccAWSVpcEndpoint_disappears (35.81s)
--- PASS: TestAccAWSVpcEndpoint_gatewayPolicy (35.89s)
--- PASS: TestAccAWSVpcEndpoint_tags (36.30s)
--- PASS: TestAccAWSVpcEndpoint_gatewayWithRouteTableAndPolicy (40.73s)
--- PASS: TestAccAWSVpcEndpoint_interfaceBasic (68.67s)
--- PASS: TestAccAWSVpcEndpoint_interfaceNonAWSService (258.14s)
--- PASS: TestAccAWSVpcEndpoint_interfaceWithSubnetAndSecurityGroup (471.69s)
```
  • Loading branch information
ewbankkit authored Jun 23, 2020
1 parent e50d830 commit 521ec5e
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 68 deletions.
66 changes: 41 additions & 25 deletions aws/resource_aws_vpc_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"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/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -26,6 +27,10 @@ func resourceAwsVpcEndpoint() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"auto_accept": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -215,6 +220,15 @@ func resourceAwsVpcEndpointRead(d *schema.ResourceData, meta interface{}) error

vpce := vpceRaw.(*ec2.VpcEndpoint)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "ec2",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("vpc-endpoint/%s", d.Id()),
}.String()
d.Set("arn", arn)

serviceName := aws.StringValue(vpce.ServiceName)
d.Set("service_name", serviceName)
d.Set("state", vpce.State)
Expand Down Expand Up @@ -293,38 +307,40 @@ func resourceAwsVpcEndpointUpdate(d *schema.ResourceData, meta interface{}) erro
}
}

req := &ec2.ModifyVpcEndpointInput{
VpcEndpointId: aws.String(d.Id()),
}

if d.HasChange("policy") {
policy, err := structure.NormalizeJsonString(d.Get("policy"))
if err != nil {
return fmt.Errorf("policy contains an invalid JSON: %s", err)
if d.HasChanges("policy", "route_table_ids", "subnet_ids", "security_group_ids", "private_dns_enabled") {
req := &ec2.ModifyVpcEndpointInput{
VpcEndpointId: aws.String(d.Id()),
}

if policy == "" {
req.ResetPolicy = aws.Bool(true)
} else {
req.PolicyDocument = aws.String(policy)
if d.HasChange("policy") {
policy, err := structure.NormalizeJsonString(d.Get("policy"))
if err != nil {
return fmt.Errorf("policy contains an invalid JSON: %s", err)
}

if policy == "" {
req.ResetPolicy = aws.Bool(true)
} else {
req.PolicyDocument = aws.String(policy)
}
}
}

setVpcEndpointUpdateLists(d, "route_table_ids", &req.AddRouteTableIds, &req.RemoveRouteTableIds)
setVpcEndpointUpdateLists(d, "subnet_ids", &req.AddSubnetIds, &req.RemoveSubnetIds)
setVpcEndpointUpdateLists(d, "security_group_ids", &req.AddSecurityGroupIds, &req.RemoveSecurityGroupIds)
setVpcEndpointUpdateLists(d, "route_table_ids", &req.AddRouteTableIds, &req.RemoveRouteTableIds)
setVpcEndpointUpdateLists(d, "subnet_ids", &req.AddSubnetIds, &req.RemoveSubnetIds)
setVpcEndpointUpdateLists(d, "security_group_ids", &req.AddSecurityGroupIds, &req.RemoveSecurityGroupIds)

if d.HasChange("private_dns_enabled") {
req.PrivateDnsEnabled = aws.Bool(d.Get("private_dns_enabled").(bool))
}
if d.HasChange("private_dns_enabled") {
req.PrivateDnsEnabled = aws.Bool(d.Get("private_dns_enabled").(bool))
}

log.Printf("[DEBUG] Updating VPC Endpoint: %#v", req)
if _, err := conn.ModifyVpcEndpoint(req); err != nil {
return fmt.Errorf("Error updating VPC Endpoint: %s", err)
}
log.Printf("[DEBUG] Updating VPC Endpoint: %#v", req)
if _, err := conn.ModifyVpcEndpoint(req); err != nil {
return fmt.Errorf("Error updating VPC Endpoint: %s", err)
}

if err := vpcEndpointWaitUntilAvailable(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return err
if err := vpcEndpointWaitUntilAvailable(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return err
}
}

if d.HasChange("tags") {
Expand Down
Loading

0 comments on commit 521ec5e

Please sign in to comment.