Skip to content

Commit

Permalink
Merge pull request #31339 from hashicorp/td-quicksight-sweepers
Browse files Browse the repository at this point in the history
[TechDebt]: QuickSight sweepers
  • Loading branch information
jar-b authored May 11, 2023
2 parents 4215a1a + cdfb0cb commit 7f5d8b6
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 9 deletions.
7 changes: 4 additions & 3 deletions internal/service/quicksight/data_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ func resourceDataSetUpdate(ctx context.Context, d *schema.ResourceData, meta int
func resourceDataSetDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).QuickSightConn()

log.Printf("[INFO] Deleting QuickSight Data Set %s", d.Id())
awsAccountId, dataSetId, err := ParseDataSetID(d.Id())
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -2701,11 +2702,11 @@ func flattenTagRules(apiObject []*quicksight.RowLevelPermissionTagRule) []interf
func ParseDataSetID(id string) (string, string, error) {
parts := strings.SplitN(id, ",", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", "", fmt.Errorf("unexpected format of ID (%s), expected AWS_ACCOUNT_ID,DATA_SOURCE_ID", id)
return "", "", fmt.Errorf("unexpected format of ID (%s), expected AWS_ACCOUNT_ID,DATA_SET_ID", id)
}
return parts[0], parts[1], nil
}

func createDataSetID(awsAccountID, dataSourceID string) string {
return fmt.Sprintf("%s,%s", awsAccountID, dataSourceID)
func createDataSetID(awsAccountID, dataSetID string) string {
return fmt.Sprintf("%s,%s", awsAccountID, dataSetID)
}
234 changes: 228 additions & 6 deletions internal/service/quicksight/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package quicksight
import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/quicksight"
Expand All @@ -16,13 +17,88 @@ import (
)

func init() {
resource.AddTestSweepers("aws_quicksight_data_set", &resource.Sweeper{
Name: "aws_quicksight_data_set",
F: sweepDataSets,
})
resource.AddTestSweepers("aws_quicksight_data_source", &resource.Sweeper{
Name: "aws_quicksight_data_source",
F: sweepsDataSource,
F: sweepDataSources,
})
resource.AddTestSweepers("aws_quicksight_folder", &resource.Sweeper{
Name: "aws_quicksight_folder",
F: sweepFolders,
})
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,
})
}

func sweepsDataSource(region string) error {
const (
// Defined locally to avoid cyclic import from internal/acctest
acctestResourcePrefix = "tf-acc-test"
)

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

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

conn := client.(*conns.AWSClient).QuickSightConn()
sweepResources := make([]sweep.Sweepable, 0)
var errs *multierror.Error

awsAccountId := client.(*conns.AWSClient).AccountID

input := &quicksight.ListDataSetsInput{
AwsAccountId: aws.String(awsAccountId),
}

err = conn.ListDataSetsPagesWithContext(ctx, input, func(page *quicksight.ListDataSetsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, ds := range page.DataSetSummaries {
if ds == nil {
continue
}

r := ResourceDataSet()
d := r.Data(nil)
d.SetId(fmt.Sprintf("%s,%s", awsAccountId, aws.StringValue(ds.DataSetId)))

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("listing QuickSight Data Sets: %w", err))
}

if err := sweep.SweepOrchestratorWithContext(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("sweeping QuickSight Data Sets for %s: %w", region, err))
}

if sweep.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping QuickSight Data Set sweep for %s: %s", region, errs)
return nil
}

return errs.ErrorOrNil()
}

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

Expand Down Expand Up @@ -51,9 +127,7 @@ func sweepsDataSource(region string) error {
}

r := ResourceDataSource()

d := r.Data(nil)

d.SetId(fmt.Sprintf("%s/%s", awsAccountId, aws.StringValue(ds.DataSourceId)))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
Expand All @@ -63,11 +137,11 @@ func sweepsDataSource(region string) error {
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error describing QuickSigth Data Sources: %w", err))
errs = multierror.Append(errs, fmt.Errorf("listing QuickSight Data Sources: %w", err))
}

if err := sweep.SweepOrchestratorWithContext(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping QuickSight Data Sources for %s: %w", region, err))
errs = multierror.Append(errs, fmt.Errorf("sweeping QuickSight Data Sources for %s: %w", region, err))
}

if sweep.SkipSweepError(errs.ErrorOrNil()) {
Expand All @@ -77,3 +151,151 @@ func sweepsDataSource(region string) error {

return errs.ErrorOrNil()
}

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

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

conn := client.(*conns.AWSClient).QuickSightConn()
awsAccountId := client.(*conns.AWSClient).AccountID
sweepResources := make([]sweep.Sweepable, 0)
var errs *multierror.Error

input := &quicksight.ListFoldersInput{
AwsAccountId: aws.String(awsAccountId),
}

out, err := conn.ListFoldersWithContext(ctx, input)
for _, folder := range out.FolderSummaryList {
if folder.FolderId == nil {
continue
}

r := ResourceFolder()
d := r.Data(nil)
d.SetId(fmt.Sprintf("%s,%s", awsAccountId, aws.StringValue(folder.FolderId)))

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

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("listing QuickSight Folder for %s: %w", region, err))
}

if err := sweep.SweepOrchestrator(sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("sweeping QuickSight Folder for %s: %w", region, err))
}

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

return errs.ErrorOrNil()
}

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

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

conn := client.(*conns.AWSClient).QuickSightConn()
sweepResources := make([]sweep.Sweepable, 0)
var errs *multierror.Error

awsAccountId := client.(*conns.AWSClient).AccountID

input := &quicksight.ListTemplatesInput{
AwsAccountId: aws.String(awsAccountId),
}

err = conn.ListTemplatesPagesWithContext(ctx, input, func(page *quicksight.ListTemplatesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, tmpl := range page.TemplateSummaryList {
if tmpl == nil {
continue
}

r := ResourceTemplate()
d := r.Data(nil)
d.SetId(fmt.Sprintf("%s,%s", awsAccountId, aws.StringValue(tmpl.TemplateId)))

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("listing QuickSight Templates: %w", err))
}

if err := sweep.SweepOrchestratorWithContext(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("sweeping QuickSight Templates for %s: %w", region, err))
}

if sweep.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping QuickSight Template sweep for %s: %s", region, errs)
return nil
}

return errs.ErrorOrNil()
}

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

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

conn := client.(*conns.AWSClient).QuickSightConn()
awsAccountId := client.(*conns.AWSClient).AccountID
sweepResources := make([]sweep.Sweepable, 0)
var errs *multierror.Error

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

out, err := conn.ListUsersWithContext(ctx, input)
for _, user := range out.UserList {
username := aws.StringValue(user.UserName)
if !strings.HasPrefix(username, acctestResourcePrefix) {
continue
}

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

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

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("listing QuickSight Users for %s: %w", region, err))
}

if err := sweep.SweepOrchestrator(sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("sweeping QuickSight Users for %s: %w", region, err))
}

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

return errs.ErrorOrNil()
}

0 comments on commit 7f5d8b6

Please sign in to comment.