Skip to content

Commit

Permalink
Merge pull request #22181 from steweg/feature/tgw_connect2
Browse files Browse the repository at this point in the history
Adding aws_transit_gateway_connect and peers
  • Loading branch information
ewbankkit authored Mar 2, 2022
2 parents 8791153 + 76ea75e commit 8d7b251
Show file tree
Hide file tree
Showing 32 changed files with 3,314 additions and 296 deletions.
27 changes: 27 additions & 0 deletions .changelog/22181.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
```release-note:new-resource
aws_ec2_transit_gateway_connect
```

```release-note:new-resource
aws_ec2_transit_gateway_connect_peer
```

```release-note:new-data-source
aws_ec2_transit_gateway_connect
```

```release-note:new-data-source
aws_ec2_transit_gateway_connect_peer
```

```release-note:enhancement
resource/aws_ec2_transit_gateway: Add `transit_gateway_cidr_blocks` argument
```

```release-note:enhancement
data-source/aws_ec2_transit_gateway: Add `transit_gateway_cidr_blocks` attribute
```

```release-note:enhancement
resource/aws_ec2_transit_gateway: Add [custom `timeouts`](https://www.terraform.io/docs/language/resources/syntax.html#operation-timeouts) block
```
4 changes: 4 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ func Provider() *schema.Provider {
"aws_ec2_managed_prefix_list": ec2.DataSourceManagedPrefixList(),
"aws_ec2_spot_price": ec2.DataSourceSpotPrice(),
"aws_ec2_transit_gateway": ec2.DataSourceTransitGateway(),
"aws_ec2_transit_gateway_connect": ec2.DataSourceTransitGatewayConnect(),
"aws_ec2_transit_gateway_connect_peer": ec2.DataSourceTransitGatewayConnectPeer(),
"aws_ec2_transit_gateway_dx_gateway_attachment": ec2.DataSourceTransitGatewayDxGatewayAttachment(),
"aws_ec2_transit_gateway_multicast_domain": ec2.DataSourceTransitGatewayMulticastDomain(),
"aws_ec2_transit_gateway_peering_attachment": ec2.DataSourceTransitGatewayPeeringAttachment(),
Expand Down Expand Up @@ -1205,6 +1207,8 @@ func Provider() *schema.Provider {
"aws_ec2_traffic_mirror_session": ec2.ResourceTrafficMirrorSession(),
"aws_ec2_traffic_mirror_target": ec2.ResourceTrafficMirrorTarget(),
"aws_ec2_transit_gateway": ec2.ResourceTransitGateway(),
"aws_ec2_transit_gateway_connect": ec2.ResourceTransitGatewayConnect(),
"aws_ec2_transit_gateway_connect_peer": ec2.ResourceTransitGatewayConnectPeer(),
"aws_ec2_transit_gateway_multicast_domain": ec2.ResourceTransitGatewayMulticastDomain(),
"aws_ec2_transit_gateway_multicast_domain_association": ec2.ResourceTransitGatewayMulticastDomainAssociation(),
"aws_ec2_transit_gateway_multicast_group_member": ec2.ResourceTransitGatewayMulticastGroupMember(),
Expand Down
1 change: 1 addition & 0 deletions internal/service/ec2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const (
ErrCodeInvalidSubnetIDNotFound = "InvalidSubnetID.NotFound"
ErrCodeInvalidSubnetIdNotFound = "InvalidSubnetId.NotFound"
ErrCodeInvalidTransitGatewayAttachmentIDNotFound = "InvalidTransitGatewayAttachmentID.NotFound"
ErrCodeInvalidTransitGatewayConnectPeerIDNotFound = "InvalidTransitGatewayConnectPeerID.NotFound"
ErrCodeInvalidTransitGatewayIDNotFound = "InvalidTransitGatewayID.NotFound"
ErrCodeInvalidTransitGatewayMulticastDomainIdNotFound = "InvalidTransitGatewayMulticastDomainId.NotFound"
ErrCodeInvalidVolumeNotFound = "InvalidVolume.NotFound"
Expand Down
159 changes: 157 additions & 2 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,7 @@ func FindTransitGateway(conn *ec2.EC2, input *ec2.DescribeTransitGatewaysInput)
return nil, err
}

if len(output) == 0 || output[0] == nil {
if len(output) == 0 || output[0] == nil || output[0].Options == nil {
return nil, tfresource.NewEmptyResultError(input)
}

Expand Down Expand Up @@ -2270,6 +2270,161 @@ func FindTransitGatewayAttachmentByID(conn *ec2.EC2, id string) (*ec2.TransitGat
return output, nil
}

func FindTransitGatewayConnect(conn *ec2.EC2, input *ec2.DescribeTransitGatewayConnectsInput) (*ec2.TransitGatewayConnect, error) {
output, err := FindTransitGatewayConnects(conn, input)

if err != nil {
return nil, err
}

if len(output) == 0 || output[0] == nil || output[0].Options == nil {
return nil, tfresource.NewEmptyResultError(input)
}

if count := len(output); count > 1 {
return nil, tfresource.NewTooManyResultsError(count, input)
}

return output[0], nil
}

func FindTransitGatewayConnects(conn *ec2.EC2, input *ec2.DescribeTransitGatewayConnectsInput) ([]*ec2.TransitGatewayConnect, error) {
var output []*ec2.TransitGatewayConnect

err := conn.DescribeTransitGatewayConnectsPages(input, func(page *ec2.DescribeTransitGatewayConnectsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.TransitGatewayConnects {
if v != nil {
output = append(output, v)
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, ErrCodeInvalidTransitGatewayAttachmentIDNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

return output, nil
}

func FindTransitGatewayConnectByID(conn *ec2.EC2, id string) (*ec2.TransitGatewayConnect, error) {
input := &ec2.DescribeTransitGatewayConnectsInput{
TransitGatewayAttachmentIds: aws.StringSlice([]string{id}),
}

output, err := FindTransitGatewayConnect(conn, input)

if err != nil {
return nil, err
}

if state := aws.StringValue(output.State); state == ec2.TransitGatewayAttachmentStateDeleted {
return nil, &resource.NotFoundError{
Message: state,
LastRequest: input,
}
}

// Eventual consistency check.
if aws.StringValue(output.TransitGatewayAttachmentId) != id {
return nil, &resource.NotFoundError{
LastRequest: input,
}
}

return output, nil
}

func FindTransitGatewayConnectPeer(conn *ec2.EC2, input *ec2.DescribeTransitGatewayConnectPeersInput) (*ec2.TransitGatewayConnectPeer, error) {
output, err := FindTransitGatewayConnectPeers(conn, input)

if err != nil {
return nil, err
}

if len(output) == 0 || output[0] == nil || output[0].ConnectPeerConfiguration == nil ||
len(output[0].ConnectPeerConfiguration.BgpConfigurations) == 0 || output[0].ConnectPeerConfiguration.BgpConfigurations[0] == nil {
return nil, tfresource.NewEmptyResultError(input)
}

if count := len(output); count > 1 {
return nil, tfresource.NewTooManyResultsError(count, input)
}

return output[0], nil
}

func FindTransitGatewayConnectPeers(conn *ec2.EC2, input *ec2.DescribeTransitGatewayConnectPeersInput) ([]*ec2.TransitGatewayConnectPeer, error) {
var output []*ec2.TransitGatewayConnectPeer

err := conn.DescribeTransitGatewayConnectPeersPages(input, func(page *ec2.DescribeTransitGatewayConnectPeersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.TransitGatewayConnectPeers {
if v != nil {
output = append(output, v)
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, ErrCodeInvalidTransitGatewayConnectPeerIDNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

return output, nil
}

func FindTransitGatewayConnectPeerByID(conn *ec2.EC2, id string) (*ec2.TransitGatewayConnectPeer, error) {
input := &ec2.DescribeTransitGatewayConnectPeersInput{
TransitGatewayConnectPeerIds: aws.StringSlice([]string{id}),
}

output, err := FindTransitGatewayConnectPeer(conn, input)

if err != nil {
return nil, err
}

if state := aws.StringValue(output.State); state == ec2.TransitGatewayConnectPeerStateDeleted {
return nil, &resource.NotFoundError{
Message: state,
LastRequest: input,
}
}

// Eventual consistency check.
if aws.StringValue(output.TransitGatewayConnectPeerId) != id {
return nil, &resource.NotFoundError{
LastRequest: input,
}
}

return output, nil
}

func FindTransitGatewayMulticastDomain(conn *ec2.EC2, input *ec2.DescribeTransitGatewayMulticastDomainsInput) (*ec2.TransitGatewayMulticastDomain, error) {
output, err := FindTransitGatewayMulticastDomains(conn, input)

Expand Down Expand Up @@ -2732,7 +2887,7 @@ func FindTransitGatewayVPCAttachment(conn *ec2.EC2, input *ec2.DescribeTransitGa
return nil, err
}

if len(output) == 0 || output[0] == nil {
if len(output) == 0 || output[0] == nil || output[0].Options == nil {
return nil, tfresource.NewEmptyResultError(input)
}

Expand Down
32 changes: 32 additions & 0 deletions internal/service/ec2/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,38 @@ func StatusTransitGatewayState(conn *ec2.EC2, id string) resource.StateRefreshFu
}
}

func StatusTransitGatewayConnectState(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindTransitGatewayConnectByID(conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, aws.StringValue(output.State), nil
}
}

func StatusTransitGatewayConnectPeerState(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindTransitGatewayConnectPeerByID(conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, aws.StringValue(output.State), nil
}
}

func StatusTransitGatewayMulticastDomainState(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindTransitGatewayMulticastDomainByID(conn, id)
Expand Down
Loading

0 comments on commit 8d7b251

Please sign in to comment.