Skip to content

Commit

Permalink
Merge pull request #300 from ekristen/ec2-tgw-peer
Browse files Browse the repository at this point in the history
feat: add resource ec2-tgw-connect-peer
  • Loading branch information
ekristen authored Sep 26, 2024
2 parents d55389e + 1e202fc commit 382498b
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions resources/ec2-tgw-connect-peer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package resources

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go/service/ec2"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)

const EC2TGWConnectPeerResource = "EC2TGWConnectPeer"

func init() {
registry.Register(&registry.Registration{
Name: EC2TGWConnectPeerResource,
Scope: nuke.Account,
Lister: &EC2TGWConnectPeerLister{},
})
}

type EC2TGWConnectPeerLister struct{}

func (l *EC2TGWConnectPeerLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := ec2.New(opts.Session)
resources := make([]resource.Resource, 0)

params := &ec2.DescribeTransitGatewayConnectPeersInput{}

resp, err := svc.DescribeTransitGatewayConnectPeers(params)
if err != nil {
return nil, err
}

for _, connectPeer := range resp.TransitGatewayConnectPeers {
resources = append(resources, &EC2TGWConnectPeer{
svc: svc,
ID: connectPeer.TransitGatewayConnectPeerId,
State: connectPeer.State,
CreationTime: connectPeer.CreationTime,
Tags: connectPeer.Tags,
})
}

return resources, nil
}

type EC2TGWConnectPeer struct {
svc *ec2.EC2
ID *string
State *string
CreationTime *time.Time
Tags []*ec2.Tag
}

func (r *EC2TGWConnectPeer) Filter() error {
if *r.State == "deleted" {
return fmt.Errorf("already deleted")
}
return nil
}

func (r *EC2TGWConnectPeer) Remove(_ context.Context) error {
_, err := r.svc.DeleteTransitGatewayConnectPeer(&ec2.DeleteTransitGatewayConnectPeerInput{
TransitGatewayConnectPeerId: r.ID,
})
if err != nil {
return err
}

return nil
}

func (r *EC2TGWConnectPeer) Properties() types.Properties {
return types.NewPropertiesFromStruct(r)
}

func (r *EC2TGWConnectPeer) String() string {
return *r.ID
}

0 comments on commit 382498b

Please sign in to comment.