-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathsweep.go
94 lines (73 loc) · 2.2 KB
/
sweep.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//go:build sweep
// +build sweep
package dynamodb
import (
"fmt"
"log"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
)
func init() {
resource.AddTestSweepers("aws_dynamodb_table", &resource.Sweeper{
Name: "aws_dynamodb_table",
F: sweepTables,
})
}
func sweepTables(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*conns.AWSClient).DynamoDBConn
sweepResources := make([]*sweep.SweepResource, 0)
var errs *multierror.Error
var g multierror.Group
var mutex = &sync.Mutex{}
err = conn.ListTablesPages(&dynamodb.ListTablesInput{}, func(page *dynamodb.ListTablesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
for _, tableName := range page.TableNames {
r := ResourceTable()
d := r.Data(nil)
id := aws.StringValue(tableName)
d.SetId(id)
// read concurrently and gather errors
g.Go(func() error {
// Need to Read first to fill in `replica` attribute
err := r.Read(d, client)
if err != nil {
return err
}
// In case it was already deleted
if d.Id() == "" {
return nil
}
mutex.Lock()
defer mutex.Unlock()
sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
return nil
})
}
return !lastPage
})
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing DynamoDB Tables for %s: %w", region, err))
}
if err = g.Wait().ErrorOrNil(); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error concurrently reading DynamoDB Tables: %w", err))
}
if err = sweep.SweepOrchestrator(sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping DynamoDB Tables for %s: %w", region, err))
}
if sweep.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping DynamoDB Tables sweep for %s: %s", region, errs)
return nil
}
return errs.ErrorOrNil()
}