forked from gruntwork-io/cloud-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented vpc lattice api (gruntwork-io#706)
- Loading branch information
1 parent
40df16f
commit 298fb3e
Showing
14 changed files
with
691 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/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) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
|
||
var ids []*string | ||
for _, item := range output.Items { | ||
|
||
if configObj.VPCLatticeService.ShouldInclude(config.ResourceValue{ | ||
Name: item.Name, | ||
Time: item.CreatedAt, | ||
}) { | ||
ids = append(ids, item.Arn) | ||
} | ||
} | ||
|
||
return ids, nil | ||
} | ||
|
||
func (network *VPCLatticeService) nukeAll(identifiers []*string) error { | ||
if len(identifiers) == 0 { | ||
logging.Debugf("No %s to nuke in region %s", network.ResourceServiceName(), network.Region) | ||
return nil | ||
|
||
} | ||
|
||
logging.Debugf("Deleting all %s in region %s", network.ResourceServiceName(), network.Region) | ||
|
||
deletedCount := 0 | ||
for _, id := range identifiers { | ||
|
||
_, err := network.Client.DeleteServiceWithContext(network.Context, &vpclattice.DeleteServiceInput{ | ||
ServiceIdentifier: id, | ||
}) | ||
|
||
// Record status of this resource | ||
e := report.Entry{ | ||
Identifier: aws.StringValue(id), | ||
ResourceType: network.ResourceServiceName(), | ||
Error: err, | ||
} | ||
report.Record(e) | ||
|
||
if err != nil { | ||
logging.Debugf("[Failed] %s", err) | ||
} else { | ||
deletedCount++ | ||
logging.Debugf("Deleted %s: %s", network.ResourceServiceName(), aws.StringValue(id)) | ||
} | ||
} | ||
|
||
logging.Debugf("[OK] %d %s(s) terminated in %s", deletedCount, network.ResourceServiceName(), network.Region) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/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) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
|
||
var ids []*string | ||
for _, item := range output.Items { | ||
|
||
if configObj.VPCLatticeServiceNetwork.ShouldInclude(config.ResourceValue{ | ||
Name: item.Name, | ||
Time: item.CreatedAt, | ||
}) { | ||
ids = append(ids, item.Arn) | ||
} | ||
} | ||
|
||
return ids, 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) | ||
return nil | ||
|
||
} | ||
|
||
logging.Debugf("Deleting all %s in region %s", network.ResourceServiceName(), network.Region) | ||
|
||
deletedCount := 0 | ||
for _, id := range identifiers { | ||
|
||
_, err := network.Client.DeleteServiceNetworkWithContext(network.Context, &vpclattice.DeleteServiceNetworkInput{ | ||
ServiceNetworkIdentifier: id, | ||
}) | ||
|
||
// Record status of this resource | ||
e := report.Entry{ | ||
Identifier: aws.StringValue(id), | ||
ResourceType: network.ResourceServiceName(), | ||
Error: err, | ||
} | ||
report.Record(e) | ||
|
||
if err != nil { | ||
logging.Debugf("[Failed] %s", err) | ||
} else { | ||
deletedCount++ | ||
logging.Debugf("Deleted %s: %s", network.ResourceServiceName(), aws.StringValue(id)) | ||
} | ||
} | ||
|
||
logging.Debugf("[OK] %d %s(s) terminated in %s", deletedCount, network.ResourceServiceName(), network.Region) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package resources_test | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
"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" | ||
"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 | ||
DeleteServiceNetworkOutput vpclattice.DeleteServiceNetworkOutput | ||
ListServiceNetworksOutput vpclattice.ListServiceNetworksOutput | ||
} | ||
|
||
func (m mockedVPCLatticeServiceNetwork) ListServiceNetworksWithContext(aws.Context, *vpclattice.ListServiceNetworksInput, ...request.Option) (*vpclattice.ListServiceNetworksOutput, error) { | ||
return &m.ListServiceNetworksOutput, nil | ||
} | ||
|
||
func (m mockedVPCLatticeServiceNetwork) DeleteServiceNetworkWithContext(aws.Context, *vpclattice.DeleteServiceNetworkInput, ...request.Option) (*vpclattice.DeleteServiceNetworkOutput, error) { | ||
return &m.DeleteServiceNetworkOutput, nil | ||
} | ||
|
||
func TestVPCLatticeServiceNetwork_GetAll(t *testing.T) { | ||
|
||
t.Parallel() | ||
|
||
var ( | ||
id1 = "aws-nuke-test-" + util.UniqueID() | ||
id2 = "aws-nuke-test-" + util.UniqueID() | ||
now = time.Now() | ||
) | ||
|
||
obj := resources.VPCLatticeServiceNetwork{ | ||
Client: mockedVPCLatticeServiceNetwork{ | ||
ListServiceNetworksOutput: vpclattice.ListServiceNetworksOutput{ | ||
Items: []*vpclattice.ServiceNetworkSummary{ | ||
{ | ||
Arn: awsgo.String(id1), | ||
Name: awsgo.String(id1), | ||
CreatedAt: aws.Time(now), | ||
}, { | ||
Arn: awsgo.String(id2), | ||
Name: awsgo.String(id2), | ||
CreatedAt: aws.Time(now.Add(1 * time.Hour)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
tests := map[string]struct { | ||
configObj config.ResourceType | ||
expected []string | ||
}{ | ||
"emptyFilter": { | ||
configObj: config.ResourceType{}, | ||
expected: []string{id1, id2}, | ||
}, | ||
"nameExclusionFilter": { | ||
configObj: config.ResourceType{ | ||
ExcludeRule: config.FilterRule{ | ||
NamesRegExp: []config.Expression{{ | ||
RE: *regexp.MustCompile(id2), | ||
}}}, | ||
}, | ||
expected: []string{id1}, | ||
}, | ||
"timeAfterExclusionFilter": { | ||
configObj: config.ResourceType{ | ||
ExcludeRule: config.FilterRule{ | ||
TimeAfter: awsgo.Time(now), | ||
}}, | ||
expected: []string{id1}, | ||
}, | ||
} | ||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
names, err := obj.GetAndSetIdentifiers(context.Background(), config.Config{ | ||
VPCLatticeServiceNetwork: tc.configObj, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, names) | ||
}) | ||
} | ||
} | ||
|
||
func TestVPCLatticeServiceNetwork__NukeAll(t *testing.T) { | ||
t.Parallel() | ||
|
||
obj := resources.VPCLatticeServiceNetwork{ | ||
Client: mockedVPCLatticeServiceNetwork{ | ||
ListServiceNetworksOutput: vpclattice.ListServiceNetworksOutput{}, | ||
}, | ||
} | ||
err := obj.Nuke([]string{"test"}) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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" | ||
"github.com/gruntwork-io/cloud-nuke/config" | ||
"github.com/gruntwork-io/go-commons/errors" | ||
) | ||
|
||
type VPCLatticeServiceNetwork struct { | ||
BaseAwsResource | ||
Client vpclatticeiface.VPCLatticeAPI | ||
Region string | ||
ARNs []string | ||
} | ||
|
||
func (n *VPCLatticeServiceNetwork) Init(session *session.Session) { | ||
n.Client = vpclattice.New(session) | ||
} | ||
|
||
// ResourceName - the simple name of the aws resource | ||
func (n *VPCLatticeServiceNetwork) ResourceName() string { | ||
return "vpc-lattice-service-network" | ||
} | ||
|
||
// ResourceIdentifiers - the arns of the aws certificate manager certificates | ||
func (n *VPCLatticeServiceNetwork) ResourceIdentifiers() []string { | ||
return n.ARNs | ||
} | ||
|
||
func (n *VPCLatticeServiceNetwork) GetAndSetResourceConfig(configObj config.Config) config.ResourceType { | ||
return configObj.VPCLatticeServiceNetwork | ||
} | ||
|
||
func (n *VPCLatticeServiceNetwork) ResourceServiceName() string { | ||
return "VPC Lattice Service Network" | ||
} | ||
|
||
func (n *VPCLatticeServiceNetwork) MaxBatchSize() int { | ||
return maxBatchSize | ||
} | ||
|
||
func (n *VPCLatticeServiceNetwork) GetAndSetIdentifiers(c context.Context, configObj config.Config) ([]string, error) { | ||
identifiers, err := n.getAll(c, configObj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
n.ARNs = awsgo.StringValueSlice(identifiers) | ||
return n.ARNs, nil | ||
} | ||
|
||
// Nuke - nuke 'em all!!! | ||
func (n *VPCLatticeServiceNetwork) Nuke(arns []string) error { | ||
if err := n.nukeAll(awsgo.StringSlice(arns)); err != nil { | ||
return errors.WithStackTrace(err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.