Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_ec2_transit_gateway_peering_attachment: Get acceptance tests pa… #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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