Skip to content

Commit

Permalink
Add 'aws_ec2_transit_gateway_vpc_attachment_accepter' resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed May 19, 2019
1 parent 57015df commit 60ddf7c
Show file tree
Hide file tree
Showing 12 changed files with 571 additions and 9 deletions.
23 changes: 20 additions & 3 deletions aws/ec2_transit_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,24 @@ func waitForEc2TransitGatewayRouteTableAssociationDeletion(conn *ec2.EC2, transi
return err
}

func waitForEc2TransitGatewayRouteTableAttachmentCreation(conn *ec2.EC2, transitGatewayAttachmentID string) error {
func waitForEc2TransitGatewayVpcAttachmentAcceptance(conn *ec2.EC2, transitGatewayAttachmentID string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{
ec2.TransitGatewayAttachmentStatePending,
ec2.TransitGatewayAttachmentStatePendingAcceptance,
},
Target: []string{ec2.TransitGatewayAttachmentStateAvailable},
Refresh: ec2TransitGatewayVpcAttachmentRefreshFunc(conn, transitGatewayAttachmentID),
Timeout: 10 * time.Minute,
}

log.Printf("[DEBUG] Waiting for EC2 Transit Gateway VPC Attachment (%s) availability", transitGatewayAttachmentID)
_, err := stateConf.WaitForState()

return err
}

func waitForEc2TransitGatewayVpcAttachmentCreation(conn *ec2.EC2, transitGatewayAttachmentID string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{ec2.TransitGatewayAttachmentStatePending},
Target: []string{
Expand All @@ -558,7 +575,7 @@ func waitForEc2TransitGatewayRouteTableAttachmentCreation(conn *ec2.EC2, transit
return err
}

func waitForEc2TransitGatewayRouteTableAttachmentDeletion(conn *ec2.EC2, transitGatewayAttachmentID string) error {
func waitForEc2TransitGatewayVpcAttachmentDeletion(conn *ec2.EC2, transitGatewayAttachmentID string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{
ec2.TransitGatewayAttachmentStateAvailable,
Expand All @@ -580,7 +597,7 @@ func waitForEc2TransitGatewayRouteTableAttachmentDeletion(conn *ec2.EC2, transit
return err
}

func waitForEc2TransitGatewayRouteTableAttachmentUpdate(conn *ec2.EC2, transitGatewayAttachmentID string) error {
func waitForEc2TransitGatewayVpcAttachmentUpdate(conn *ec2.EC2, transitGatewayAttachmentID string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{ec2.TransitGatewayAttachmentStateModifying},
Target: []string{ec2.TransitGatewayAttachmentStateAvailable},
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ func Provider() terraform.ResourceProvider {
"aws_ec2_transit_gateway_route_table_association": resourceAwsEc2TransitGatewayRouteTableAssociation(),
"aws_ec2_transit_gateway_route_table_propagation": resourceAwsEc2TransitGatewayRouteTablePropagation(),
"aws_ec2_transit_gateway_vpc_attachment": resourceAwsEc2TransitGatewayVpcAttachment(),
"aws_ec2_transit_gateway_vpc_attachment_accepter": resourceAwsEc2TransitGatewayVpcAttachmentAccepter(),
"aws_ecr_lifecycle_policy": resourceAwsEcrLifecyclePolicy(),
"aws_ecr_repository": resourceAwsEcrRepository(),
"aws_ecr_repository_policy": resourceAwsEcrRepositoryPolicy(),
Expand Down
6 changes: 3 additions & 3 deletions aws/resource_aws_ec2_transit_gateway_vpc_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func resourceAwsEc2TransitGatewayVpcAttachmentCreate(d *schema.ResourceData, met

d.SetId(aws.StringValue(output.TransitGatewayVpcAttachment.TransitGatewayAttachmentId))

if err := waitForEc2TransitGatewayRouteTableAttachmentCreation(conn, d.Id()); err != nil {
if err := waitForEc2TransitGatewayVpcAttachmentCreation(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for EC2 Transit Gateway VPC Attachment (%s) availability: %s", d.Id(), err)
}

Expand Down Expand Up @@ -238,7 +238,7 @@ func resourceAwsEc2TransitGatewayVpcAttachmentUpdate(d *schema.ResourceData, met
return fmt.Errorf("error modifying EC2 Transit Gateway VPC Attachment (%s): %s", d.Id(), err)
}

if err := waitForEc2TransitGatewayRouteTableAttachmentUpdate(conn, d.Id()); err != nil {
if err := waitForEc2TransitGatewayVpcAttachmentUpdate(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for EC2 Transit Gateway VPC Attachment (%s) update: %s", d.Id(), err)
}
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func resourceAwsEc2TransitGatewayVpcAttachmentDelete(d *schema.ResourceData, met
return fmt.Errorf("error deleting EC2 Transit Gateway VPC Attachment: %s", err)
}

if err := waitForEc2TransitGatewayRouteTableAttachmentDeletion(conn, d.Id()); err != nil {
if err := waitForEc2TransitGatewayVpcAttachmentDeletion(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for EC2 Transit Gateway VPC Attachment (%s) deletion: %s", d.Id(), err)
}

Expand Down
149 changes: 149 additions & 0 deletions aws/resource_aws_ec2_transit_gateway_vpc_attachment_accepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsEc2TransitGatewayVpcAttachmentAccepter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEc2TransitGatewayVpcAttachmentAccepterCreate,
Read: resourceAwsEc2TransitGatewayVpcAttachmentAccepterRead,
Update: resourceAwsEc2TransitGatewayVpcAttachmentAccepterUpdate,
Delete: resourceAwsEc2TransitGatewayVpcAttachmentAccepterDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"dns_support": {
Type: schema.TypeString,
Computed: true,
},
"ipv6_support": {
Type: schema.TypeString,
Computed: true,
},
"subnet_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsSchema(),
"transit_gateway_attachment_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"transit_gateway_id": {
Type: schema.TypeString,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
},
"vpc_owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAwsEc2TransitGatewayVpcAttachmentAccepterCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

input := &ec2.AcceptTransitGatewayVpcAttachmentInput{
TransitGatewayAttachmentId: aws.String(d.Get("transit_gateway_attachment_id").(string)),
}

log.Printf("[DEBUG] Accepting EC2 Transit Gateway VPC Attachment: %s", input)
output, err := conn.AcceptTransitGatewayVpcAttachment(input)
if err != nil {
return fmt.Errorf("error accepting EC2 Transit Gateway VPC Attachment: %s", err)
}

d.SetId(aws.StringValue(output.TransitGatewayVpcAttachment.TransitGatewayAttachmentId))

if err := waitForEc2TransitGatewayVpcAttachmentAcceptance(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for EC2 Transit Gateway VPC Attachment (%s) availability: %s", d.Id(), err)
}

if err := setTags(conn, d); err != nil {
return fmt.Errorf("error updating EC2 Transit Gateway VPC Attachment (%s) tags: %s", d.Id(), err)
}

return resourceAwsEc2TransitGatewayVpcAttachmentAccepterRead(d, meta)
}

func resourceAwsEc2TransitGatewayVpcAttachmentAccepterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

transitGatewayVpcAttachment, err := ec2DescribeTransitGatewayVpcAttachment(conn, d.Id())

if isAWSErr(err, "InvalidTransitGatewayAttachmentID.NotFound", "") {
log.Printf("[WARN] EC2 Transit Gateway VPC Attachment (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading EC2 Transit Gateway VPC Attachment: %s", err)
}

if transitGatewayVpcAttachment == nil {
log.Printf("[WARN] EC2 Transit Gateway VPC Attachment (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if aws.StringValue(transitGatewayVpcAttachment.State) == ec2.TransitGatewayAttachmentStateDeleting || aws.StringValue(transitGatewayVpcAttachment.State) == ec2.TransitGatewayAttachmentStateDeleted {
log.Printf("[WARN] EC2 Transit Gateway VPC Attachment (%s) in deleted state (%s), removing from state", d.Id(), aws.StringValue(transitGatewayVpcAttachment.State))
d.SetId("")
return nil
}

if transitGatewayVpcAttachment.Options == nil {
return fmt.Errorf("error reading EC2 Transit Gateway VPC Attachment (%s): missing options", d.Id())
}

d.Set("dns_support", transitGatewayVpcAttachment.Options.DnsSupport)
d.Set("ipv6_support", transitGatewayVpcAttachment.Options.Ipv6Support)

if err := d.Set("subnet_ids", aws.StringValueSlice(transitGatewayVpcAttachment.SubnetIds)); err != nil {
return fmt.Errorf("error setting subnet_ids: %s", err)
}

if err := d.Set("tags", tagsToMap(transitGatewayVpcAttachment.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("transit_gateway_attachment_id", aws.StringValue(transitGatewayVpcAttachment.TransitGatewayAttachmentId))
d.Set("transit_gateway_id", aws.StringValue(transitGatewayVpcAttachment.TransitGatewayId))
d.Set("vpc_id", aws.StringValue(transitGatewayVpcAttachment.VpcId))
d.Set("vpc_owner_id", aws.StringValue(transitGatewayVpcAttachment.VpcOwnerId))

return nil
}

func resourceAwsEc2TransitGatewayVpcAttachmentAccepterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

if d.HasChange("tags") {
if err := setTags(conn, d); err != nil {
return fmt.Errorf("error updating EC2 Transit Gateway VPC Attachment (%s) tags: %s", d.Id(), err)
}
}

return nil
}

func resourceAwsEc2TransitGatewayVpcAttachmentAccepterDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] Will not delete EC2 Transit Gateway VPC Attachment. Terraform will remove this resource from the state file, however resources may remain.")
return nil
}
Loading

0 comments on commit 60ddf7c

Please sign in to comment.