Skip to content

Commit

Permalink
Merge pull request hashicorp#33571 from hashicorp/td-sweeper-fixes
Browse files Browse the repository at this point in the history
Tech debt: Various sweeper fixes
  • Loading branch information
ewbankkit authored Sep 21, 2023
2 parents 589425d + 408c4fc commit 7eb91b1
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 29 deletions.
9 changes: 9 additions & 0 deletions internal/service/codestarconnections/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
"github.com/hashicorp/terraform-provider-aws/names"
)

func init() {
Expand All @@ -34,6 +35,10 @@ func init() {

func sweepConnections(region string) error {
ctx := sweep.Context(region)
if region == names.USGovEast1RegionID || region == names.USGovWest1RegionID {
log.Printf("[WARN] Skipping CodeStar Connections Connection sweep for region: %s", region)
return nil
}
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
Expand Down Expand Up @@ -75,6 +80,10 @@ func sweepConnections(region string) error {

func sweepHosts(region string) error {
ctx := sweep.Context(region)
if region == names.USGovEast1RegionID || region == names.USGovWest1RegionID {
log.Printf("[WARN] Skipping CodeStar Connections Host sweep for region: %s", region)
return nil
}
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion internal/service/codestarnotifications/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/codestarnotifications"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
)

func init() {
Expand All @@ -37,7 +38,7 @@ func sweepNotificationRules(region string) error {
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if sweep.SkipSweepError(err) {
if awsv2.SkipSweepError(err) {
log.Printf("[WARN] Skipping CodeStar Notification Rule sweep for %s: %s", region, err)
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions internal/service/elasticsearch/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func sweepDomains(region string) error {

name := aws.StringValue(domainInfo.DomainName)

if engineType := aws.StringValue(domainInfo.EngineType); engineType != elasticsearchservice.EngineTypeElasticsearch {
log.Printf("[INFO] Skipping Elasticsearch Domain %s: EngineType = %s", name, engineType)
continue
}

// Elasticsearch Domains have regularly gotten stuck in a "being deleted" state
// e.g. Deleted and Processing are both true for days in the API
// Filter out domains that are Deleted already.
Expand Down
3 changes: 2 additions & 1 deletion internal/service/emrserverless/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/emrserverless/types"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
)

func init() {
Expand All @@ -38,7 +39,7 @@ func sweepApplications(region string) error {
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if sweep.SkipSweepError(err) {
if awsv2.SkipSweepError(err) {
log.Printf("[WARN] Skipping EMR Serverless Application sweep for %s: %s", region, err)
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions internal/service/iam/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func init() {
"aws_iam_group",
"aws_iam_role",
"aws_iam_user",
"aws_quicksight_group",
"aws_quicksight_user",
},
})

Expand Down Expand Up @@ -430,6 +432,12 @@ func sweepPolicies(region string) error {

for _, v := range page.Policies {
arn := aws.StringValue(v.Arn)

if n := aws.Int64Value(v.AttachmentCount); n > 0 {
log.Printf("[INFO] Skipping IAM Policy %s: AttachmentCount=%d", arn, n)
continue
}

r := ResourcePolicy()
d := r.Data(nil)
d.SetId(arn)
Expand Down
7 changes: 3 additions & 4 deletions internal/service/opensearch/inbound_connection_accepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ func resourceInboundConnectionRead(ctx context.Context, d *schema.ResourceData,
func resourceInboundConnectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).OpenSearchConn(ctx)

req := &opensearchservice.DeleteInboundConnectionInput{
log.Printf("[DEBUG] Deleting OpenSearch Inbound Connection: %s", d.Id())
_, err := conn.DeleteInboundConnectionWithContext(ctx, &opensearchservice.DeleteInboundConnectionInput{
ConnectionId: aws.String(d.Id()),
}

_, err := conn.DeleteInboundConnectionWithContext(ctx, req)
})

if tfawserr.ErrCodeEquals(err, "ResourceNotFoundException") {
return nil
Expand Down
7 changes: 3 additions & 4 deletions internal/service/opensearch/outbound_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ func resourceOutboundConnectionRead(ctx context.Context, d *schema.ResourceData,
func resourceOutboundConnectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).OpenSearchConn(ctx)

req := &opensearchservice.DeleteOutboundConnectionInput{
log.Printf("[DEBUG] Deleting OpenSearch Outbound Connection: %s", d.Id())
_, err := conn.DeleteOutboundConnectionWithContext(ctx, &opensearchservice.DeleteOutboundConnectionInput{
ConnectionId: aws.String(d.Id()),
}

_, err := conn.DeleteOutboundConnectionWithContext(ctx, req)
})

if tfawserr.ErrCodeEquals(err, "ResourceNotFoundException") {
return nil
Expand Down
117 changes: 115 additions & 2 deletions internal/service/opensearch/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,29 @@ func init() {
resource.AddTestSweepers("aws_opensearch_domain", &resource.Sweeper{
Name: "aws_opensearch_domain",
F: sweepDomains,
Dependencies: []string{
"aws_opensearch_inbound_connection_accepter",
"aws_opensearch_outbound_connection",
},
})

resource.AddTestSweepers("aws_opensearch_inbound_connection_accepter", &resource.Sweeper{
Name: "aws_opensearch_inbound_connection_accepter",
F: sweepInboundConnections,
})

resource.AddTestSweepers("aws_opensearch_outbound_connection", &resource.Sweeper{
Name: "aws_opensearch_outbound_connection",
F: sweepOutboundConnections,
})
}

func sweepDomains(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)

if err != nil {
return fmt.Errorf("error getting client: %w", err)
}

conn := client.OpenSearchConn(ctx)
sweepResources := make([]sweep.Sweepable, 0)
var errs *multierror.Error
Expand Down Expand Up @@ -65,6 +77,11 @@ func sweepDomains(region string) error {

name := aws.StringValue(domainInfo.DomainName)

if engineType := aws.StringValue(domainInfo.EngineType); engineType != opensearchservice.EngineTypeOpenSearch {
log.Printf("[INFO] Skipping OpenSearch Domain %s: EngineType = %s", name, engineType)
continue
}

// OpenSearch Domains have regularly gotten stuck in a "being deleted" state
// e.g. Deleted and Processing are both true for days in the API
// Filter out domains that are Deleted already.
Expand Down Expand Up @@ -101,3 +118,99 @@ func sweepDomains(region string) error {

return errs.ErrorOrNil()
}

func sweepInboundConnections(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}
conn := client.OpenSearchConn(ctx)
input := &opensearchservice.DescribeInboundConnectionsInput{}
sweepResources := make([]sweep.Sweepable, 0)

err = conn.DescribeInboundConnectionsPagesWithContext(ctx, input, func(page *opensearchservice.DescribeInboundConnectionsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.Connections {
if aws.StringValue(v.ConnectionStatus.StatusCode) != opensearchservice.InboundConnectionStatusCodeDeleted {
continue
}

r := ResourceInboundConnectionAccepter()
d := r.Data(nil)
d.SetId(aws.StringValue(v.ConnectionId))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

return !lastPage
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping OpenSearch Inbound Connection sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error listing OpenSearch Inbound Connections: %w", err)
}

err = sweep.SweepOrchestrator(ctx, sweepResources)

if err != nil {
return fmt.Errorf("error sweeping OpenSearch Inbound Connections (%s): %w", region, err)
}

return nil
}

func sweepOutboundConnections(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}
conn := client.OpenSearchConn(ctx)
input := &opensearchservice.DescribeOutboundConnectionsInput{}
sweepResources := make([]sweep.Sweepable, 0)

err = conn.DescribeOutboundConnectionsPagesWithContext(ctx, input, func(page *opensearchservice.DescribeOutboundConnectionsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.Connections {
if aws.StringValue(v.ConnectionStatus.StatusCode) != opensearchservice.OutboundConnectionStatusCodeDeleted {
continue
}

r := ResourceOutboundConnection()
d := r.Data(nil)
d.SetId(aws.StringValue(v.ConnectionId))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

return !lastPage
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping OpenSearch Outbound Connection sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error listing OpenSearch Outbound Connections: %w", err)
}

err = sweep.SweepOrchestrator(ctx, sweepResources)

if err != nil {
return fmt.Errorf("error sweeping OpenSearch Outbound Connections (%s): %w", region, err)
}

return nil
}
56 changes: 56 additions & 0 deletions internal/service/quicksight/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ func init() {
Name: "aws_quicksight_folder",
F: sweepFolders,
})
resource.AddTestSweepers("aws_quicksight_group", &resource.Sweeper{
Name: "aws_quicksight_group",
F: sweepGroups,
})
resource.AddTestSweepers("aws_quicksight_template", &resource.Sweeper{
Name: "aws_quicksight_template",
F: sweepTemplates,
})
resource.AddTestSweepers("aws_quicksight_user", &resource.Sweeper{
Name: "aws_quicksight_user",
F: sweepUsers,
Dependencies: []string{
"aws_quicksight_group",
},
})
}

Expand All @@ -50,6 +57,10 @@ const (
acctestResourcePrefix = "tf-acc-test"
)

// TODO
// TODO Use paginated listers.
// TODO

func sweepDashboards(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
Expand Down Expand Up @@ -248,7 +259,52 @@ func sweepFolders(region string) error {
}

return nil
}

func sweepGroups(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)

if err != nil {
return fmt.Errorf("getting client: %w", err)
}

conn := client.QuickSightConn(ctx)
awsAccountId := client.AccountID
sweepResources := make([]sweep.Sweepable, 0)

input := &quicksight.ListGroupsInput{
AwsAccountId: aws.String(awsAccountId),
Namespace: aws.String(DefaultUserNamespace),
}

out, err := conn.ListGroupsWithContext(ctx, input)
for _, user := range out.GroupList {
groupname := aws.StringValue(user.GroupName)
if !strings.HasPrefix(groupname, acctestResourcePrefix) {
continue
}

r := ResourceGroup()
d := r.Data(nil)
d.SetId(fmt.Sprintf("%s/%s/%s", awsAccountId, DefaultUserNamespace, groupname))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

if skipSweepUserError(err) {
log.Printf("[WARN] Skipping QuickSight Group sweep for %s: %s", region, err)
return nil
}
if err != nil {
return fmt.Errorf("listing QuickSight Groups: %w", err)
}

if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
return fmt.Errorf("sweeping QuickSight Groups for %s: %w", region, err)
}

return nil
}

func sweepTemplates(region string) error {
Expand Down
Loading

0 comments on commit 7eb91b1

Please sign in to comment.