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
11 changed files
with
346 additions
and
113 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,53 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/amp" | ||
"github.com/gruntwork-io/cloud-nuke/config" | ||
"github.com/gruntwork-io/go-commons/errors" | ||
) | ||
|
||
type ManagedPrometheus struct { | ||
BaseAwsResource | ||
Client *amp.Client | ||
Region string | ||
WorkSpaces []string | ||
} | ||
|
||
func (a *ManagedPrometheus) GetAndSetResourceConfig(configObj config.Config) config.ResourceType { | ||
return configObj.ManagedPrometheus | ||
} | ||
|
||
func (a *ManagedPrometheus) InitV2(cfg aws.Config) { | ||
a.Client = amp.NewFromConfig(cfg) | ||
} | ||
|
||
func (a *ManagedPrometheus) IsUsingV2() bool { return true } | ||
|
||
func (a *ManagedPrometheus) ResourceName() string { return "managed-prometheus" } | ||
|
||
func (a *ManagedPrometheus) ResourceIdentifiers() []string { return a.WorkSpaces } | ||
|
||
func (a *ManagedPrometheus) MaxBatchSize() int { | ||
return 100 | ||
} | ||
|
||
func (a *ManagedPrometheus) Nuke(identifiers []string) error { | ||
if err := a.nukeAll(aws.StringSlice(identifiers)); err != nil { | ||
return errors.WithStackTrace(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *ManagedPrometheus) GetAndSetIdentifiers(ctx context.Context, cnfObj config.Config) ([]string, error) { | ||
identifiers, err := a.getAll(ctx, cnfObj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
a.WorkSpaces = aws.ToStringSlice(identifiers) | ||
return a.WorkSpaces, 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
Oops, something went wrong.