diff --git a/internal/sdkprovider/service/project/sweep.go b/internal/sdkprovider/service/project/sweep.go index 6ae9195c9..48a63ec89 100644 --- a/internal/sdkprovider/service/project/sweep.go +++ b/internal/sdkprovider/service/project/sweep.go @@ -42,6 +42,14 @@ func init() { "aiven_redis", }, }) + + resource.AddTestSweepers("aiven_billing_group", &resource.Sweeper{ + Name: "aiven_billing_group", + F: sweepBillingGroups(ctx, client), + Dependencies: []string{ + "aiven_project", + }, + }) } func sweepProjects(ctx context.Context, client *aiven.Client) func(region string) error { @@ -75,3 +83,33 @@ func sweepProjects(ctx context.Context, client *aiven.Client) func(region string return nil } } + +func sweepBillingGroups(ctx context.Context, client *aiven.Client) func(region string) error { + return func(region string) error { + billingGroups, err := client.BillingGroup.ListAll(ctx) + if err != nil { + return fmt.Errorf("error retrieving a list of billing groups : %s", err) + } + + for _, billingGroup := range billingGroups { + if strings.Contains(billingGroup.BillingGroupName, "test-acc-") { + if err := client.BillingGroup.Delete(ctx, billingGroup.Id); err != nil { + e := err.(aiven.Error) + + // billing group not found + if e.Status == 404 { + continue + } + + return fmt.Errorf( + "error destroying billing group %s during sweep: %s", + billingGroup.BillingGroupName, + err, + ) + } + } + } + + return nil + } +}