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.
add aws v2 sdk to support newer resources (gruntwork-io#745)
add support for Managed Prometheus
- Loading branch information
Showing
12 changed files
with
366 additions
and
9 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
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,77 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/amp" | ||
"github.com/aws/aws-sdk-go-v2/service/amp/types" | ||
"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 (a *ManagedPrometheus) nukeAll(identifiers []*string) error { | ||
if len(identifiers) == 0 { | ||
logging.Debugf("[Managed Prometheus] No Prometheus Workspaces found in region %s", a.Region) | ||
return nil | ||
} | ||
|
||
logging.Debugf("[Managed Prometheus] Deleting all Prometheus Workspaces in %s", a.Region) | ||
var deleted []*string | ||
|
||
for _, identifier := range identifiers { | ||
logging.Debugf("[Managed Prometheus] Deleting Prometheus Workspace %s in region %s", *identifier, a.Region) | ||
|
||
_, err := a.Client.DeleteWorkspace(a.Context, &.DeleteWorkspaceInput{ | ||
WorkspaceId: identifier, | ||
ClientToken: nil, | ||
}) | ||
if err != nil { | ||
logging.Debugf("[Managed Prometheus] Error deleting Workspace %s in region %s", *identifier, a.Region) | ||
} else { | ||
deleted = append(deleted, identifier) | ||
logging.Debugf("[Managed Prometheus] Deleted Workspace %s in region %s", *identifier, a.Region) | ||
} | ||
|
||
e := report.Entry{ | ||
Identifier: aws.ToString(identifier), | ||
ResourceType: a.ResourceName(), | ||
Error: err, | ||
} | ||
report.Record(e) | ||
} | ||
|
||
logging.Debugf("[OK] %d Prometheus Workspace(s) deleted in %s", len(deleted), a.Region) | ||
return nil | ||
} | ||
|
||
func (a *ManagedPrometheus) getAll(ctx context.Context, cnfObj config.Config) ([]*string, error) { | ||
paginator := amp.NewListWorkspacesPaginator(a.Client, &.ListWorkspacesInput{}) | ||
|
||
var identifiers []*string | ||
for paginator.HasMorePages() { | ||
workspaces, err := paginator.NextPage(ctx) | ||
if err != nil { | ||
logging.Debugf("[Managed Prometheus] Failed to list workspaces: %s", err) | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
|
||
for _, workspace := range workspaces.Workspaces { | ||
if workspace.Status.StatusCode != types.WorkspaceStatusCodeActive { | ||
continue | ||
} | ||
|
||
if cnfObj.ManagedPrometheus.ShouldInclude(config.ResourceValue{ | ||
Name: workspace.Alias, | ||
Time: workspace.CreatedAt, | ||
Tags: workspace.Tags, | ||
}) { | ||
identifiers = append(identifiers, workspace.WorkspaceId) | ||
} | ||
} | ||
} | ||
|
||
return identifiers, 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,119 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/amp" | ||
"github.com/aws/aws-sdk-go-v2/service/amp/types" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/gruntwork-io/cloud-nuke/config" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockedManagedPrometheusService struct { | ||
ManagedPrometheusAPI | ||
ListServiceOutput amp.ListWorkspacesOutput | ||
DeleteWorkspaceOutput amp.DeleteWorkspaceOutput | ||
} | ||
|
||
func (m mockedManagedPrometheusService) ListWorkspaces(ctx context.Context, input *amp.ListWorkspacesInput, f ...func(*amp.Options)) (*amp.ListWorkspacesOutput, error) { | ||
return &m.ListServiceOutput, nil | ||
} | ||
|
||
func (m mockedManagedPrometheusService) DeleteWorkspace(ctx context.Context, params *amp.DeleteWorkspaceInput, optFns ...func(*amp.Options)) (*amp.DeleteWorkspaceOutput, error) { | ||
return &m.DeleteWorkspaceOutput, nil | ||
} | ||
|
||
func Test_ManagedPrometheus_GetAll(t *testing.T) { | ||
|
||
t.Parallel() | ||
|
||
now := time.Now() | ||
|
||
workSpace1 := "test-workspace-1" | ||
workSpace2 := "test-workspace-2" | ||
|
||
service := ManagedPrometheus{ | ||
Client: mockedManagedPrometheusService{ | ||
ListServiceOutput: amp.ListWorkspacesOutput{ | ||
Workspaces: []types.WorkspaceSummary{ | ||
{ | ||
Arn: aws.String(fmt.Sprintf("arn::%s", workSpace1)), | ||
Alias: aws.String(workSpace1), | ||
CreatedAt: &now, | ||
Status: &types.WorkspaceStatus{ | ||
StatusCode: types.WorkspaceStatusCodeActive, | ||
}, | ||
WorkspaceId: aws.String(workSpace1), | ||
}, | ||
{ | ||
Arn: aws.String(fmt.Sprintf("arn::%s", workSpace2)), | ||
Alias: aws.String(workSpace2), | ||
CreatedAt: aws.Time(now.Add(time.Hour)), | ||
Status: &types.WorkspaceStatus{ | ||
StatusCode: types.WorkspaceStatusCodeActive, | ||
}, | ||
WorkspaceId: aws.String(workSpace2), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
tests := map[string]struct { | ||
configObj config.ResourceType | ||
expected []string | ||
}{ | ||
"emptyFilter": { | ||
configObj: config.ResourceType{}, | ||
expected: []string{workSpace1, workSpace2}, | ||
}, | ||
"nameExclusionFilter": { | ||
configObj: config.ResourceType{ | ||
ExcludeRule: config.FilterRule{ | ||
NamesRegExp: []config.Expression{{ | ||
RE: *regexp.MustCompile(workSpace1), | ||
}}, | ||
}}, | ||
expected: []string{workSpace2}, | ||
}, | ||
"timeAfterExclusionFilter": { | ||
configObj: config.ResourceType{ | ||
ExcludeRule: config.FilterRule{ | ||
TimeAfter: aws.Time(now.Add(-1 * time.Hour)), | ||
}}, | ||
expected: []string{}, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
workspaces, err := service.getAll( | ||
context.Background(), | ||
config.Config{ManagedPrometheus: tc.configObj}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, aws.StringValueSlice(workspaces)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_ManagedPrometheus_NukeAll(t *testing.T) { | ||
|
||
t.Parallel() | ||
|
||
workspaceName := "test-workspace-1" | ||
service := ManagedPrometheus{ | ||
Client: mockedManagedPrometheusService{ | ||
DeleteWorkspaceOutput: amp.DeleteWorkspaceOutput{}, | ||
}, | ||
} | ||
|
||
err := service.nukeAll([]*string{&workspaceName}) | ||
assert.NoError(t, err) | ||
} |
Oops, something went wrong.