Skip to content

Commit

Permalink
migrate: aws v2 - vpc lattice
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 committed Oct 30, 2024
1 parent 05db675 commit ece883f
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 76 deletions.
79 changes: 71 additions & 8 deletions aws/resources/vpc_lattice_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package resources

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/vpclattice"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/vpclattice"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/go-commons/errors"
)

func (network *VPCLatticeService) getAll(_ context.Context, configObj config.Config) ([]*string, error) {
output, err := network.Client.ListServicesWithContext(network.Context, nil)
output, err := network.Client.ListServices(network.Context, nil)
if err != nil {
return nil, errors.WithStackTrace(err)
}
Expand All @@ -31,6 +33,69 @@ func (network *VPCLatticeService) getAll(_ context.Context, configObj config.Con
return ids, nil
}

func (network *VPCLatticeService) nukeServiceAssociations(id *string) error {
// list service associations
associations, err := network.Client.ListServiceNetworkServiceAssociations(network.Context, &vpclattice.ListServiceNetworkServiceAssociationsInput{
ServiceIdentifier: id,
})

if err != nil {
return errors.WithStackTrace(err)
}

for _, item := range associations.Items {
// list service associations
_, err := network.Client.DeleteServiceNetworkServiceAssociation(network.Context, &vpclattice.DeleteServiceNetworkServiceAssociationInput{
ServiceNetworkServiceAssociationIdentifier: item.Id,
})
if err != nil {
return errors.WithStackTrace(err)
}
}
return nil
}

func (network *VPCLatticeService) nukeService(id *string) error {
_, err := network.Client.DeleteService(network.Context, &vpclattice.DeleteServiceInput{
ServiceIdentifier: id,
})
return err
}

func (network *VPCLatticeService) nuke(id *string) error {
if err := network.nukeServiceAssociations(id); err != nil {
return err
}

if err := network.waitUntilAllServiceAssociationDeleted(id); err != nil {
return err
}
if err := network.nukeService(id); err != nil {
return err
}

return nil
}
func (network *VPCLatticeService) waitUntilAllServiceAssociationDeleted(id *string) error {
for i := 0; i < 10; i++ {
output, err := network.Client.ListServiceNetworkServiceAssociations(network.Context, &vpclattice.ListServiceNetworkServiceAssociationsInput{
ServiceIdentifier: id,
})

if err != nil {
return errors.WithStackTrace(err)
}
if len(output.Items) == 0 {
return nil
}
logging.Info("Waiting for service associations to be deleted...")
time.Sleep(10 * time.Second)
}

return fmt.Errorf("timed out waiting for service associations to be successfully deleted")

}

func (network *VPCLatticeService) nukeAll(identifiers []*string) error {
if len(identifiers) == 0 {
logging.Debugf("No %s to nuke in region %s", network.ResourceServiceName(), network.Region)
Expand All @@ -43,13 +108,11 @@ func (network *VPCLatticeService) nukeAll(identifiers []*string) error {
deletedCount := 0
for _, id := range identifiers {

_, err := network.Client.DeleteServiceWithContext(network.Context, &vpclattice.DeleteServiceInput{
ServiceIdentifier: id,
})
err := network.nuke(id)

// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(id),
Identifier: aws.ToString(id),
ResourceType: network.ResourceServiceName(),
Error: err,
}
Expand All @@ -59,7 +122,7 @@ func (network *VPCLatticeService) nukeAll(identifiers []*string) error {
logging.Debugf("[Failed] %s", err)
} else {
deletedCount++
logging.Debugf("Deleted %s: %s", network.ResourceServiceName(), aws.StringValue(id))
logging.Debugf("Deleted %s: %s", network.ResourceServiceName(), aws.ToString(id))
}
}

Expand Down
81 changes: 73 additions & 8 deletions aws/resources/vpc_lattice_service_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package resources

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/vpclattice"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/vpclattice"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/go-commons/errors"
)

func (network *VPCLatticeServiceNetwork) getAll(_ context.Context, configObj config.Config) ([]*string, error) {
output, err := network.Client.ListServiceNetworksWithContext(network.Context, nil)
output, err := network.Client.ListServiceNetworks(network.Context, nil)
if err != nil {
return nil, errors.WithStackTrace(err)
}
Expand All @@ -31,6 +33,29 @@ func (network *VPCLatticeServiceNetwork) getAll(_ context.Context, configObj con
return ids, nil
}

func (network *VPCLatticeServiceNetwork) nukeServiceAssociations(id *string) error {
// list service associations
associations, err := network.Client.ListServiceNetworkServiceAssociations(network.Context, &vpclattice.ListServiceNetworkServiceAssociationsInput{
ServiceNetworkIdentifier: id,
})

if err != nil {
return errors.WithStackTrace(err)
}

for _, item := range associations.Items {
// list service associations
_, err := network.Client.DeleteServiceNetworkServiceAssociation(network.Context, &vpclattice.DeleteServiceNetworkServiceAssociationInput{
ServiceNetworkServiceAssociationIdentifier: item.Id,
})
if err != nil {
return errors.WithStackTrace(err)
}

}
return nil
}

func (network *VPCLatticeServiceNetwork) nukeAll(identifiers []*string) error {
if len(identifiers) == 0 {
logging.Debugf("No %s to nuke in region %s", network.ResourceServiceName(), network.Region)
Expand All @@ -43,13 +68,11 @@ func (network *VPCLatticeServiceNetwork) nukeAll(identifiers []*string) error {
deletedCount := 0
for _, id := range identifiers {

_, err := network.Client.DeleteServiceNetworkWithContext(network.Context, &vpclattice.DeleteServiceNetworkInput{
ServiceNetworkIdentifier: id,
})
err := network.nuke(id)

// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(id),
Identifier: aws.ToString(id),
ResourceType: network.ResourceServiceName(),
Error: err,
}
Expand All @@ -59,10 +82,52 @@ func (network *VPCLatticeServiceNetwork) nukeAll(identifiers []*string) error {
logging.Debugf("[Failed] %s", err)
} else {
deletedCount++
logging.Debugf("Deleted %s: %s", network.ResourceServiceName(), aws.StringValue(id))
logging.Debugf("Deleted %s: %s", network.ResourceServiceName(), aws.ToString(id))
}
}

logging.Debugf("[OK] %d %s(s) terminated in %s", deletedCount, network.ResourceServiceName(), network.Region)
return nil
}

func (network *VPCLatticeServiceNetwork) nukeServiceNetwork(id *string) error {
_, err := network.Client.DeleteServiceNetwork(network.Context, &vpclattice.DeleteServiceNetworkInput{
ServiceNetworkIdentifier: id,
})
return err
}

func (network *VPCLatticeServiceNetwork) nuke(id *string) error {
if err := network.nukeServiceAssociations(id); err != nil {
return err
}

if err := network.waitUntilAllServiceAssociationDeleted(id); err != nil {
return err
}
if err := network.nukeServiceNetwork(id); err != nil {
return err
}

return nil
}

func (network *VPCLatticeServiceNetwork) waitUntilAllServiceAssociationDeleted(id *string) error {
for i := 0; i < 10; i++ {
output, err := network.Client.ListServiceNetworkServiceAssociations(network.Context, &vpclattice.ListServiceNetworkServiceAssociationsInput{
ServiceNetworkIdentifier: id,
})

if err != nil {
return err
}
if len(output.Items) == 0 {
return nil
}
logging.Info("Waiting for service associations to be deleted...")
time.Sleep(10 * time.Second)
}

return fmt.Errorf("timed out waiting for service associations to be successfully deleted")

}
30 changes: 19 additions & 11 deletions aws/resources/vpc_lattice_service_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,39 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/vpclattice"
"github.com/aws/aws-sdk-go/service/vpclattice/vpclatticeiface"
awsgo "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/vpclattice"
"github.com/aws/aws-sdk-go-v2/service/vpclattice/types"
"github.com/gruntwork-io/cloud-nuke/aws/resources"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/util"
"github.com/stretchr/testify/require"
)

type mockedVPCLatticeServiceNetwork struct {
vpclatticeiface.VPCLatticeAPI
resources.VPCLatticeServiceNetworkAPI
DeleteServiceNetworkOutput vpclattice.DeleteServiceNetworkOutput
ListServiceNetworksOutput vpclattice.ListServiceNetworksOutput

ListServiceNetworkServiceAssociationsOutput vpclattice.ListServiceNetworkServiceAssociationsOutput
DeleteServiceNetworkServiceAssociationOutput vpclattice.DeleteServiceNetworkServiceAssociationOutput
}

func (m mockedVPCLatticeServiceNetwork) ListServiceNetworksWithContext(aws.Context, *vpclattice.ListServiceNetworksInput, ...request.Option) (*vpclattice.ListServiceNetworksOutput, error) {
func (m mockedVPCLatticeServiceNetwork) ListServiceNetworks(ctx context.Context, params *vpclattice.ListServiceNetworksInput, optFns ...func(*vpclattice.Options)) (*vpclattice.ListServiceNetworksOutput, error) {
return &m.ListServiceNetworksOutput, nil
}

func (m mockedVPCLatticeServiceNetwork) DeleteServiceNetworkWithContext(aws.Context, *vpclattice.DeleteServiceNetworkInput, ...request.Option) (*vpclattice.DeleteServiceNetworkOutput, error) {
func (m mockedVPCLatticeServiceNetwork) DeleteServiceNetwork(ctx context.Context, params *vpclattice.DeleteServiceNetworkInput, optFns ...func(*vpclattice.Options)) (*vpclattice.DeleteServiceNetworkOutput, error) {
return &m.DeleteServiceNetworkOutput, nil
}

func (m mockedVPCLatticeServiceNetwork) ListServiceNetworkServiceAssociations(ctx context.Context, params *vpclattice.ListServiceNetworkServiceAssociationsInput, optFns ...func(*vpclattice.Options)) (*vpclattice.ListServiceNetworkServiceAssociationsOutput, error) {
return &m.ListServiceNetworkServiceAssociationsOutput, nil
}
func (m mockedVPCLatticeServiceNetwork) DeleteServiceNetworkServiceAssociation(ctx context.Context, params *vpclattice.DeleteServiceNetworkServiceAssociationInput, optFns ...func(*vpclattice.Options)) (*vpclattice.DeleteServiceNetworkServiceAssociationOutput, error) {
return &m.DeleteServiceNetworkServiceAssociationOutput, nil
}

func TestVPCLatticeServiceNetwork_GetAll(t *testing.T) {

t.Parallel()
Expand All @@ -44,15 +52,15 @@ func TestVPCLatticeServiceNetwork_GetAll(t *testing.T) {
obj := resources.VPCLatticeServiceNetwork{
Client: mockedVPCLatticeServiceNetwork{
ListServiceNetworksOutput: vpclattice.ListServiceNetworksOutput{
Items: []*vpclattice.ServiceNetworkSummary{
Items: []types.ServiceNetworkSummary{
{
Arn: awsgo.String(id1),
Name: awsgo.String(id1),
CreatedAt: aws.Time(now),
CreatedAt: awsgo.Time(now),
}, {
Arn: awsgo.String(id2),
Name: awsgo.String(id2),
CreatedAt: aws.Time(now.Add(1 * time.Hour)),
CreatedAt: awsgo.Time(now.Add(1 * time.Hour)),
},
},
},
Expand Down
23 changes: 15 additions & 8 deletions aws/resources/vpc_lattice_service_network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@ package resources
import (
"context"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/vpclattice"
"github.com/aws/aws-sdk-go/service/vpclattice/vpclatticeiface"
awsgo "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/vpclattice"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/go-commons/errors"
)

type VPCLatticeServiceNetworkAPI interface{
ListServiceNetworks(ctx context.Context, params *vpclattice.ListServiceNetworksInput, optFns ...func(*vpclattice.Options)) (*vpclattice.ListServiceNetworksOutput, error)
DeleteServiceNetwork(ctx context.Context, params *vpclattice.DeleteServiceNetworkInput, optFns ...func(*vpclattice.Options)) (*vpclattice.DeleteServiceNetworkOutput, error)
ListServiceNetworkServiceAssociations(ctx context.Context, params *vpclattice.ListServiceNetworkServiceAssociationsInput, optFns ...func(*vpclattice.Options)) (*vpclattice.ListServiceNetworkServiceAssociationsOutput, error)
DeleteServiceNetworkServiceAssociation(ctx context.Context, params *vpclattice.DeleteServiceNetworkServiceAssociationInput, optFns ...func(*vpclattice.Options)) (*vpclattice.DeleteServiceNetworkServiceAssociationOutput, error)
}

type VPCLatticeServiceNetwork struct {
BaseAwsResource
Client vpclatticeiface.VPCLatticeAPI
Client VPCLatticeServiceNetworkAPI
Region string
ARNs []string
}

func (n *VPCLatticeServiceNetwork) Init(session *session.Session) {
n.Client = vpclattice.New(session)
func (sch *VPCLatticeServiceNetwork) InitV2(cfg awsgo.Config) {
sch.Client = vpclattice.NewFromConfig(cfg)
}

func (sch *VPCLatticeServiceNetwork) IsUsingV2() bool { return true }

// ResourceName - the simple name of the aws resource
func (n *VPCLatticeServiceNetwork) ResourceName() string {
return "vpc-lattice-service-network"
Expand Down Expand Up @@ -50,7 +57,7 @@ func (n *VPCLatticeServiceNetwork) GetAndSetIdentifiers(c context.Context, confi
return nil, err
}

n.ARNs = awsgo.StringValueSlice(identifiers)
n.ARNs = awsgo.ToStringSlice(identifiers)
return n.ARNs, nil
}

Expand Down
Loading

0 comments on commit ece883f

Please sign in to comment.