Skip to content

Commit

Permalink
Tidy up test sweepers for EventBus Schemas.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed May 26, 2021
1 parent d566593 commit 73058d6
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 166 deletions.
66 changes: 34 additions & 32 deletions aws/resource_aws_cloudwatch_event_bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
tfevents "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cloudwatchevents"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cloudwatchevents/lister"
)

func init() {
Expand All @@ -22,6 +24,7 @@ func init() {
Dependencies: []string{
"aws_cloudwatch_event_rule",
"aws_cloudwatch_event_target",
"aws_schemas_discoverer",
},
})
}
Expand All @@ -32,46 +35,45 @@ func testSweepCloudWatchEventBuses(region string) error {
return fmt.Errorf("Error getting client: %w", err)
}
conn := client.(*AWSClient).cloudwatcheventsconn

input := &events.ListEventBusesInput{}
var sweeperErrs *multierror.Error

for {
output, err := conn.ListEventBuses(input)
if err != nil {
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping CloudWatch Events event bus sweep for %s: %s", region, err)
return nil
}
return fmt.Errorf("Error retrieving CloudWatch Events event bus: %w", err)
err = lister.ListEventBusesPages(conn, input, func(page *events.ListEventBusesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

if len(output.EventBuses) == 0 {
log.Print("[DEBUG] No CloudWatch Events event buses to sweep")
return nil
}

for _, eventBus := range output.EventBuses {
for _, eventBus := range page.EventBuses {
name := aws.StringValue(eventBus.Name)
if name == "default" {
if name == tfevents.DefaultEventBusName {
continue
}

log.Printf("[INFO] Deleting CloudWatch Events event bus (%s)", name)
_, err := conn.DeleteEventBus(&events.DeleteEventBusInput{
Name: aws.String(name),
})
r := resourceAwsCloudWatchEventBus()
d := r.Data(nil)
d.SetId(name)
err = r.Delete(d, client)

if err != nil {
return fmt.Errorf("Error deleting CloudWatch Events event bus (%s): %w", name, err)
log.Printf("[ERROR] %s", err)
sweeperErrs = multierror.Append(sweeperErrs, err)
continue
}
}

if output.NextToken == nil {
break
}
input.NextToken = output.NextToken
return !lastPage
})

if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping CloudWatch Events event bus sweep for %s: %s", region, err)
return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors
}

return nil
if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing CloudWatch Events event buses: %w", err))
}

return sweeperErrs.ErrorOrNil()
}

func TestAccAWSCloudWatchEventBus_basic(t *testing.T) {
Expand All @@ -83,7 +85,7 @@ func TestAccAWSCloudWatchEventBus_basic(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, cloudwatchevents.EndpointsID),
ErrorCheck: testAccErrorCheck(t, events.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -134,7 +136,7 @@ func TestAccAWSCloudWatchEventBus_tags(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, cloudwatchevents.EndpointsID),
ErrorCheck: testAccErrorCheck(t, events.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -185,7 +187,7 @@ func TestAccAWSCloudWatchEventBus_tags(t *testing.T) {
func TestAccAWSCloudWatchEventBus_default(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, cloudwatchevents.EndpointsID),
ErrorCheck: testAccErrorCheck(t, events.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy,
Steps: []resource.TestStep{
Expand All @@ -205,7 +207,7 @@ func TestAccAWSCloudWatchEventBus_disappears(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, cloudwatchevents.EndpointsID),
ErrorCheck: testAccErrorCheck(t, events.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -233,7 +235,7 @@ func TestAccAWSCloudWatchEventBus_PartnerEventSource(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, cloudwatchevents.EndpointsID),
ErrorCheck: testAccErrorCheck(t, events.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy,
Steps: []resource.TestStep{
Expand Down
54 changes: 25 additions & 29 deletions aws/resource_aws_schemas_discoverer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,48 @@ import (
func init() {
resource.AddTestSweepers("aws_schemas_discoverer", &resource.Sweeper{
Name: "aws_schemas_discoverer",
F: testSweepSchemasDiscoverer,
Dependencies: []string{
"aws_cloudwatch_event_bus",
},
F: testSweepSchemasDiscoverers,
})
}

func testSweepSchemasDiscoverer(region string) error {
func testSweepSchemasDiscoverers(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("Error getting client: %w", err)
}
conn := client.(*AWSClient).schemasconn

input := &schemas.ListDiscoverersInput{}
var sweeperErrs *multierror.Error

input := &schemas.ListDiscoverersInput{
Limit: aws.Int64(100),
}
var discoverers []*schemas.DiscovererSummary
for {
output, err := conn.ListDiscoverers(input)
if err != nil {
return err
err = conn.ListDiscoverersPages(input, func(page *schemas.ListDiscoverersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
discoverers = append(discoverers, output.Discoverers...)

if aws.StringValue(output.NextToken) == "" {
break
for _, discoverer := range page.Discoverers {
r := resourceAwsSchemasDiscoverer()
d := r.Data(nil)
d.SetId(aws.StringValue(discoverer.DiscovererId))
err = r.Delete(d, client)

if err != nil {
log.Printf("[ERROR] %s", err)
sweeperErrs = multierror.Append(sweeperErrs, err)
continue
}
}
input.NextToken = output.NextToken
}

for _, discoverer := range discoverers {
return !lastPage
})

input := &schemas.DeleteDiscovererInput{
DiscovererId: discoverer.DiscovererId,
}
_, err := conn.DeleteDiscoverer(input)
if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("Error deleting Schemas Discoverer (%s): %w", *discoverer.DiscovererId, err))
continue
}
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping EventBridge Schemas Discoverer sweep for %s: %s", region, err)
return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors
}

log.Printf("[INFO] Deleted %d Schemas Discoverers", len(discoverers))
if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing EventBridge Schemas Discoverers: %w", err))
}

return sweeperErrs.ErrorOrNil()
}
Expand Down
93 changes: 67 additions & 26 deletions aws/resource_aws_schemas_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -11,56 +12,96 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
tfschemas "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/schemas"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/schemas/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func init() {
resource.AddTestSweepers("aws_schemas_registry", &resource.Sweeper{
Name: "aws_schemas_registry",
F: testSweepSchemasRegistry,
F: testSweepSchemasRegistries,
})
}

func testSweepSchemasRegistry(region string) error {
func testSweepSchemasRegistries(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("Error getting client: %w", err)
}
conn := client.(*AWSClient).schemasconn

input := &schemas.ListRegistriesInput{}
var sweeperErrs *multierror.Error

input := &schemas.ListRegistriesInput{
Limit: aws.Int64(100),
}
var registries []*schemas.RegistrySummary
for {
output, err := conn.ListRegistries(input)
if err != nil {
return err
err = conn.ListRegistriesPages(input, func(page *schemas.ListRegistriesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
registries = append(registries, output.Registries...)

if aws.StringValue(output.NextToken) == "" {
break
for _, registry := range page.Registries {
registryName := aws.StringValue(registry.RegistryName)

input := &schemas.ListSchemasInput{
RegistryName: aws.String(registryName),
}

err = conn.ListSchemasPages(input, func(page *schemas.ListSchemasOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, schema := range page.Schemas {
schemaName := aws.StringValue(schema.SchemaName)
if strings.HasPrefix(schemaName, "aws.") {
continue
}

r := resourceAwsSchemasSchema()
d := r.Data(nil)
d.SetId(tfschemas.SchemaCreateResourceID(schemaName, registryName))
err = r.Delete(d, client)

if err != nil {
log.Printf("[ERROR] %s", err)
sweeperErrs = multierror.Append(sweeperErrs, err)
continue
}
}

return !lastPage
})

if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing EventBridge Schemas Schemas: %w", err))
}

if strings.HasPrefix(registryName, "aws.") {
continue
}

r := resourceAwsSchemasRegistry()
d := r.Data(nil)
d.SetId(registryName)
err = r.Delete(d, client)

if err != nil {
log.Printf("[ERROR] %s", err)
sweeperErrs = multierror.Append(sweeperErrs, err)
continue
}
}
input.NextToken = output.NextToken
}

for _, registry := range registries {
return !lastPage
})

input := &schemas.DeleteRegistryInput{
RegistryName: registry.RegistryName,
}
_, err := conn.DeleteRegistry(input)
if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("Error deleting Schemas Registry (%s): %w", *registry.RegistryName, err))
continue
}
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping EventBridge Schemas Registry sweep for %s: %s", region, err)
return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors
}

log.Printf("[INFO] Deleted %d Schemas Registries", len(registries))
if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing EventBridge Schemas Registries: %w", err))
}

return sweeperErrs.ErrorOrNil()
}
Expand Down
Loading

0 comments on commit 73058d6

Please sign in to comment.