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

f-aws_ec2_transit_gateway_peering_attachment support for dynamic routing #36902

3 changes: 3 additions & 0 deletions .changelog/36902.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_ec2_transit_gateway_peering_attachment: Add `options` argument
```
3 changes: 1 addition & 2 deletions internal/experimental/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package sync

import (
"log"
"os"
"strconv"
"sync"
Expand Down Expand Up @@ -59,7 +58,7 @@ func (s Semaphore) Notify() {
select {
case <-s:
default:
log.Println("[WARN] Notifying semaphore without Wait")
// log.Println("[WARN] Notifying semaphore without Wait")
}
}

Expand Down
52 changes: 51 additions & 1 deletion internal/service/ec2/transitgateway_peering_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -66,6 +67,22 @@ func resourceTransitGatewayPeeringAttachment() *schema.Resource {
Required: true,
ForceNew: true,
},
"options": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dynamic_routing": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateDiagFunc: enum.Validate[awstypes.DynamicRoutingValue](),
},
},
},
},
},
}
}
Expand All @@ -86,7 +103,10 @@ func resourceTransitGatewayPeeringAttachmentCreate(ctx context.Context, d *schem
TransitGatewayId: aws.String(d.Get(names.AttrTransitGatewayID).(string)),
}

log.Printf("[DEBUG] Creating EC2 Transit Gateway Peering Attachment: %+v", input)
if v, ok := d.GetOk("options"); ok {
input.Options = expandCreateTransitGatewayPeeringAttachmentRequestOptions(v.([]interface{}))
}

output, err := conn.CreateTransitGatewayPeeringAttachment(ctx, input)

if err != nil {
Expand Down Expand Up @@ -124,6 +144,10 @@ func resourceTransitGatewayPeeringAttachmentRead(ctx context.Context, d *schema.
d.Set(names.AttrState, transitGatewayPeeringAttachment.State)
d.Set(names.AttrTransitGatewayID, transitGatewayPeeringAttachment.RequesterTgwInfo.TransitGatewayId)

if err := d.Set("options", flattenTransitGatewayPeeringAttachmentOptions(transitGatewayPeeringAttachment.Options)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting options: %s", err)
}

setTagsOutV2(ctx, transitGatewayPeeringAttachment.Tags)

return diags
Expand Down Expand Up @@ -160,3 +184,29 @@ func resourceTransitGatewayPeeringAttachmentDelete(ctx context.Context, d *schem

return diags
}

func expandCreateTransitGatewayPeeringAttachmentRequestOptions(tfMap []interface{}) *awstypes.CreateTransitGatewayPeeringAttachmentRequestOptions {
if len(tfMap) == 0 || tfMap[0] == nil {
return nil
}

apiObject := &awstypes.CreateTransitGatewayPeeringAttachmentRequestOptions{}

m := tfMap[0].(map[string]interface{})

if v, ok := m["dynamic_routing"].(string); ok {
apiObject.DynamicRouting = awstypes.DynamicRoutingValue(v)
}

return apiObject
}

func flattenTransitGatewayPeeringAttachmentOptions(apiObject *awstypes.TransitGatewayPeeringAttachmentOptions) []interface{} {
if apiObject == nil {
return nil
}

return []interface{}{map[string]interface{}{
"dynamic_routing": apiObject.DynamicRouting,
}}
}
52 changes: 52 additions & 0 deletions internal/service/ec2/transitgateway_peering_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func testAccTransitGatewayPeeringAttachment_basic(t *testing.T, semaphore tfsync
Config: testAccTransitGatewayPeeringAttachmentConfig_sameAccount(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTransitGatewayPeeringAttachmentExists(ctx, resourceName, &transitGatewayPeeringAttachment),
resource.TestCheckResourceAttr(resourceName, "options.#", acctest.Ct0),
acctest.CheckResourceAttrAccountID(resourceName, "peer_account_id"),
resource.TestCheckResourceAttr(resourceName, "peer_region", acctest.AlternateRegion()),
resource.TestCheckResourceAttrPair(resourceName, "peer_transit_gateway_id", transitGatewayResourceNamePeer, names.AttrID),
Expand All @@ -61,6 +62,43 @@ func testAccTransitGatewayPeeringAttachment_basic(t *testing.T, semaphore tfsync
})
}

func testAccTransitGatewayPeeringAttachment_options(t *testing.T, semaphore tfsync.Semaphore) {
acctest.Skip(t, "IncorrectState: You cannot create a dynamic peering attachment")

ctx := acctest.Context(t)
var transitGatewayPeeringAttachment awstypes.TransitGatewayPeeringAttachment
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_ec2_transit_gateway_peering_attachment.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckTransitGatewaySynchronize(t, semaphore)
acctest.PreCheck(ctx, t)
testAccPreCheckTransitGateway(ctx, t)
acctest.PreCheckMultipleRegion(t, 2)
},
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5FactoriesAlternate(ctx, t),
CheckDestroy: testAccCheckTransitGatewayPeeringAttachmentDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccTransitGatewayPeeringAttachmentConfig_options_sameAccount(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTransitGatewayPeeringAttachmentExists(ctx, resourceName, &transitGatewayPeeringAttachment),
resource.TestCheckResourceAttr(resourceName, "options.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "options.dynamic_routing", "enable"),
),
},
{
Config: testAccTransitGatewayPeeringAttachmentConfig_options_sameAccount(rName),
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccTransitGatewayPeeringAttachment_disappears(t *testing.T, semaphore tfsync.Semaphore) {
ctx := acctest.Context(t)
var transitGatewayPeeringAttachment awstypes.TransitGatewayPeeringAttachment
Expand Down Expand Up @@ -284,6 +322,20 @@ resource "aws_ec2_transit_gateway_peering_attachment" "test" {
`, acctest.AlternateRegion()))
}

func testAccTransitGatewayPeeringAttachmentConfig_options_sameAccount(rName string) string {
return acctest.ConfigCompose(testAccTransitGatewayPeeringAttachmentConfig_sameAccount_base(rName), fmt.Sprintf(`
resource "aws_ec2_transit_gateway_peering_attachment" "test" {
peer_region = %[1]q
peer_transit_gateway_id = aws_ec2_transit_gateway.peer.id
transit_gateway_id = aws_ec2_transit_gateway.test.id

options {
dynamic_routing = "enable"
}
}
`, acctest.AlternateRegion()))
}

func testAccTransitGatewayPeeringAttachmentConfig_differentAccount(rName string) string {
return acctest.ConfigCompose(testAccTransitGatewayPeeringAttachmentConfig_differentAccount_base(rName), fmt.Sprintf(`
resource "aws_ec2_transit_gateway_peering_attachment" "test" {
Expand Down
1 change: 1 addition & 0 deletions internal/service/ec2/transitgateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestAccTransitGateway_serial(t *testing.T) {
acctest.CtDisappears: testAccTransitGatewayPeeringAttachment_disappears,
"tags": testAccTransitGatewayPeeringAttachment_tags,
"DifferentAccount": testAccTransitGatewayPeeringAttachment_differentAccount,
"options": testAccTransitGatewayPeeringAttachment_options,
},
"PeeringAttachmentAccepter": {
acctest.CtBasic: testAccTransitGatewayPeeringAttachmentAccepter_basic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,16 @@ This resource supports the following arguments:
* `peer_account_id` - (Optional) Account ID of EC2 Transit Gateway to peer with. Defaults to the account ID the [AWS provider][1] is currently connected to.
* `peer_region` - (Required) Region of EC2 Transit Gateway to peer with.
* `peer_transit_gateway_id` - (Required) Identifier of EC2 Transit Gateway to peer with.
* `options` - (Optional) Describes whether dynamic routing is enabled or disabled for the transit gateway peering request. See [options](#options) below for more details!
* `tags` - (Optional) Key-value tags for the EC2 Transit Gateway Peering Attachment. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
* `transit_gateway_id` - (Required) Identifier of EC2 Transit Gateway.

### options

The `options` block supports the following:

* `dynamic_routing` - (Optional) Indicates whether dynamic routing is enabled or disabled.. Supports `enable` and `disable`.

## Attribute Reference

This resource exports the following attributes in addition to the arguments above:
Expand Down
Loading