Skip to content

Commit

Permalink
Merge pull request #4 from ewbankkit/add_transit_gw_peering
Browse files Browse the repository at this point in the history
r/aws_ec2_transit_gateway_peering_attachment: Get acceptance tests pa…
  • Loading branch information
Omarimcblack authored Jan 10, 2020
2 parents 73e0a1c + 7c3e613 commit 4e48f9f
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 199 deletions.
23 changes: 5 additions & 18 deletions aws/ec2_transit_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,12 @@ func waitForEc2TransitGatewayPeeringAttachmentDeletion(conn *ec2.EC2, transitGat
Pending: []string{
ec2.TransitGatewayAttachmentStateAvailable,
ec2.TransitGatewayAttachmentStateDeleting,
ec2.TransitGatewayAttachmentStatePendingAcceptance,
ec2.TransitGatewayAttachmentStateRejected,
},
Target: []string{ec2.TransitGatewayAttachmentStateDeleted},
Refresh: ec2TransitGatewayVpcAttachmentRefreshFunc(conn, transitGatewayAttachmentID),
Timeout: 10 * time.Minute,
NotFoundChecks: 1,
Target: []string{ec2.TransitGatewayAttachmentStateDeleted},
Refresh: ec2TransitGatewayPeeringAttachmentRefreshFunc(conn, transitGatewayAttachmentID),
Timeout: 10 * time.Minute,
}

log.Printf("[DEBUG] Waiting for EC2 Transit Gateway Peering Attachment (%s) deletion", transitGatewayAttachmentID)
Expand All @@ -601,20 +602,6 @@ func waitForEc2TransitGatewayPeeringAttachmentDeletion(conn *ec2.EC2, transitGat
return err
}

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

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

return err
}

func waitForEc2TransitGatewayVpcAttachmentAcceptance(conn *ec2.EC2, transitGatewayAttachmentID string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{
Expand Down
63 changes: 16 additions & 47 deletions aws/resource_aws_ec2_transit_gateway_peering_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,25 @@ func resourceAwsEc2TransitGatewayPeeringAttachment() *schema.Resource {
Schema: map[string]*schema.Schema{
"peer_account_id": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
Computed: true,
},
"peer_region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"peer_transit_gateway_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"tags": tagsSchema(),
"transit_gateway_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
Expand All @@ -46,14 +51,16 @@ func resourceAwsEc2TransitGatewayPeeringAttachment() *schema.Resource {
func resourceAwsEc2TransitGatewayPeeringAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

transitGatewayID := d.Get("transit_gateway_id").(string)

peerAccountId := meta.(*AWSClient).accountid
if v, ok := d.GetOk("peer_account_id"); ok {
peerAccountId = v.(string)
}
input := &ec2.CreateTransitGatewayPeeringAttachmentInput{
PeerAccountId: aws.String(d.Get("peer_account_id").(string)),
PeerAccountId: aws.String(peerAccountId),
PeerRegion: aws.String(d.Get("peer_region").(string)),
PeerTransitGatewayId: aws.String(d.Get("peer_transit_gateway_id").(string)),
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeTransitGatewayAttachment),
TransitGatewayId: aws.String(transitGatewayID),
TransitGatewayId: aws.String(d.Get("transit_gateway_id").(string)),
}

log.Printf("[DEBUG] Creating EC2 Transit Gateway Peering Attachment: %s", input)
Expand All @@ -68,15 +75,6 @@ func resourceAwsEc2TransitGatewayPeeringAttachmentCreate(d *schema.ResourceData,
return fmt.Errorf("error waiting for EC2 Transit Gateway Peering Attachment (%s) availability: %s", d.Id(), err)
}

transitGateway, err := ec2DescribeTransitGateway(conn, transitGatewayID)
if err != nil {
return fmt.Errorf("error describing EC2 Transit Gateway (%s): %s", transitGatewayID, err)
}

if transitGateway.Options == nil {
return fmt.Errorf("error describing EC2 Transit Gateway (%s): missing options", transitGatewayID)
}

return resourceAwsEc2TransitGatewayPeeringAttachmentRead(d, meta)
}

Expand Down Expand Up @@ -107,50 +105,21 @@ func resourceAwsEc2TransitGatewayPeeringAttachmentRead(d *schema.ResourceData, m
return nil
}

transitGatewayID := aws.StringValue(transitGatewayPeeringAttachment.RequesterTgwInfo.TransitGatewayId)
transitGateway, err := ec2DescribeTransitGateway(conn, transitGatewayID)
if err != nil {
return fmt.Errorf("error describing EC2 Transit Gateway (%s): %s", transitGatewayID, err)
}

if transitGateway.Options == nil {
return fmt.Errorf("error describing EC2 Transit Gateway (%s): missing options", transitGatewayID)
}
d.Set("peer_account_id", transitGatewayPeeringAttachment.AccepterTgwInfo.OwnerId)
d.Set("peer_region", transitGatewayPeeringAttachment.AccepterTgwInfo.Region)
d.Set("peer_transit_gateway_id", transitGatewayPeeringAttachment.AccepterTgwInfo.TransitGatewayId)
d.Set("transit_gateway_id", transitGatewayPeeringAttachment.RequesterTgwInfo.TransitGatewayId)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(transitGatewayPeeringAttachment.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("peer_account_id", (transitGatewayPeeringAttachment.AccepterTgwInfo.OwnerId != nil))
d.Set("peer_region", (transitGatewayPeeringAttachment.AccepterTgwInfo.Region != nil))
d.Set("peer_transit_gateway_id", (transitGatewayPeeringAttachment.AccepterTgwInfo.TransitGatewayId != nil))
d.Set("tags", (transitGatewayPeeringAttachment.Tags))
d.Set("transit_gateway_id", (transitGatewayPeeringAttachment.RequesterTgwInfo.TransitGatewayId))

return nil
}

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

if d.HasChange("peer_account_id") || d.HasChange("peer_region") || d.HasChange("peer_transit_gateway_id") || d.HasChange("transit_gateway_id") {
transitGatewayID := d.Get("transit_gateway_id").(string)

transitGateway, err := ec2DescribeTransitGateway(conn, transitGatewayID)
if err != nil {
return fmt.Errorf("error describing EC2 Transit Gateway (%s): %s", transitGatewayID, err)
}

if transitGateway.Options == nil {
return fmt.Errorf("error describing EC2 Transit Gateway (%s): missing options", transitGatewayID)
}

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

}

if d.HasChange("tags") {
o, n := d.GetChange("tags")

Expand Down
Loading

0 comments on commit 4e48f9f

Please sign in to comment.