Skip to content

Commit

Permalink
Merge pull request #101 from ekristen/feat-delete-core-network
Browse files Browse the repository at this point in the history
feat: delete core network manager resources
  • Loading branch information
ekristen authored Feb 24, 2024
2 parents 8b816f2 + 877864b commit 92780b7
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 2 deletions.
7 changes: 5 additions & 2 deletions resources/ec2-vpn-connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package resources

import (
"context"

"fmt"

"github.com/gotidy/ptr"

"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/pkg/awsutil"
"github.com/ekristen/aws-nuke/pkg/nuke"
)

Expand Down Expand Up @@ -56,9 +58,10 @@ type EC2VPNConnection struct {
}

func (v *EC2VPNConnection) Filter() error {
if *v.conn.State == "deleted" {
if ptr.ToString(v.conn.State) == awsutil.StateDeleted {
return fmt.Errorf("already deleted")
}

return nil
}

Expand Down
93 changes: 93 additions & 0 deletions resources/networkmanager-connect-peers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package resources

import (
"context"
"fmt"
"strings"

"github.com/gotidy/ptr"

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

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

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

type NetworkManagerConnectPeer struct {
svc *networkmanager.NetworkManager
peer *networkmanager.ConnectPeerSummary
}

const NetworkManagerConnectPeerResource = "NetworkManagerConnectPeer"

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

type NetworkManagerConnectPeerLister struct{}

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

svc := networkmanager.New(opts.Session)
params := &networkmanager.ListConnectPeersInput{}
resources := make([]resource.Resource, 0)

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

for _, connectPeer := range resp.ConnectPeers {
resources = append(resources, &NetworkManagerConnectPeer{
svc: svc,
peer: connectPeer,
})
}

return resources, nil
}

func (n *NetworkManagerConnectPeer) Remove(_ context.Context) error {
params := &networkmanager.DeleteConnectPeerInput{
ConnectPeerId: n.peer.ConnectPeerId,
}

_, err := n.svc.DeleteConnectPeer(params)
if err != nil {
return err
}

return nil
}

func (n *NetworkManagerConnectPeer) Filter() error {
if strings.EqualFold(ptr.ToString(n.peer.ConnectPeerState), awsutil.StateDeleted) {
return fmt.Errorf("already deleted")
}

return nil
}

func (n *NetworkManagerConnectPeer) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("ID", n.peer.ConnectPeerId)

for _, tagValue := range n.peer.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}

return properties
}

func (n *NetworkManagerConnectPeer) String() string {
return *n.peer.ConnectPeerId
}
94 changes: 94 additions & 0 deletions resources/networkmanager-core-network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package resources

import (
"context"
"fmt"
"strings"

"github.com/gotidy/ptr"

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

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

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

type NetworkManagerCoreNetwork struct {
svc *networkmanager.NetworkManager
network *networkmanager.CoreNetworkSummary
}

const NetworkManagerCoreNetworkResource = "NetworkManagerCoreNetwork"

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

type NetworkManagerCoreNetworkLister struct{}

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

svc := networkmanager.New(opts.Session)
params := &networkmanager.ListCoreNetworksInput{}
resources := make([]resource.Resource, 0)

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

for _, network := range resp.CoreNetworks {
resources = append(resources, &NetworkManagerCoreNetwork{
svc: svc,
network: network,
})
}

return resources, nil
}

func (n *NetworkManagerCoreNetwork) Remove(_ context.Context) error {
params := &networkmanager.DeleteCoreNetworkInput{
CoreNetworkId: n.network.CoreNetworkId,
}

_, err := n.svc.DeleteCoreNetwork(params)
if err != nil {
return err
}

return nil
}

func (n *NetworkManagerCoreNetwork) Filter() error {
if strings.EqualFold(ptr.ToString(n.network.State), awsutil.StateDeleted) {
return fmt.Errorf("already deleted")
}

return nil
}

func (n *NetworkManagerCoreNetwork) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("ID", n.network.CoreNetworkId)
properties.Set("ARN", n.network.CoreNetworkArn)

for _, tagValue := range n.network.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}

return properties
}

func (n *NetworkManagerCoreNetwork) String() string {
return *n.network.CoreNetworkId
}
106 changes: 106 additions & 0 deletions resources/networkmanager-global-network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package resources

import (
"context"
"fmt"
"strings"

"github.com/gotidy/ptr"

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

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

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

type NetworkManagerGlobalNetwork struct {
svc *networkmanager.NetworkManager
network *networkmanager.GlobalNetwork
}

const NetworkManagerGlobalNetworkResource = "NetworkManagerGlobalNetwork"

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

type NetworkManagerGlobalNetworkLister struct{}

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

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

params := &networkmanager.DescribeGlobalNetworksInput{
MaxResults: aws.Int64(100),
}

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

for _, network := range resp.GlobalNetworks {
resources = append(resources, &NetworkManagerGlobalNetwork{
svc: svc,
network: network,
})
}
if resp.NextToken == nil {
break
}

params.NextToken = resp.NextToken
}

return resources, nil
}

func (n *NetworkManagerGlobalNetwork) Remove(_ context.Context) error {
params := &networkmanager.DeleteGlobalNetworkInput{
GlobalNetworkId: n.network.GlobalNetworkId,
}

_, err := n.svc.DeleteGlobalNetwork(params)
if err != nil {
return err
}

return nil
}

func (n *NetworkManagerGlobalNetwork) Filter() error {
if strings.EqualFold(ptr.ToString(n.network.State), awsutil.StateDeleted) {
return fmt.Errorf("already deleted")
}

return nil
}

func (n *NetworkManagerGlobalNetwork) Properties() types.Properties {
properties := types.NewProperties()

properties.Set("ID", n.network.GlobalNetworkId)
properties.Set("ARN", n.network.GlobalNetworkArn)

for _, tagValue := range n.network.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}

return properties
}

func (n *NetworkManagerGlobalNetwork) String() string {
return *n.network.GlobalNetworkId
}
Loading

0 comments on commit 92780b7

Please sign in to comment.