From faa6723a1b08181e0b7213d982c769a506df57a3 Mon Sep 17 00:00:00 2001 From: Myroslav Vivcharyk Date: Tue, 25 Feb 2025 16:39:38 +0100 Subject: [PATCH] fix(organization_project): added sweeper --- .../sdkprovider/service/organization/sweep.go | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/internal/sdkprovider/service/organization/sweep.go b/internal/sdkprovider/service/organization/sweep.go index ca5db8004..a54f22137 100644 --- a/internal/sdkprovider/service/organization/sweep.go +++ b/internal/sdkprovider/service/organization/sweep.go @@ -16,6 +16,11 @@ const defaultPrefix = "test-acc" func init() { ctx := context.Background() + sweep.AddTestSweepers("aiven_organization_project", &resource.Sweeper{ + Name: "aiven_organization_project", + F: sweepOrganizationProjects(ctx), + }) + sweep.AddTestSweepers("aiven_organization", &resource.Sweeper{ Name: "aiven_organization", F: sweepOrganizations(ctx), @@ -78,6 +83,47 @@ func sweepOrganizations(ctx context.Context) func(string) error { } } +func sweepOrganizationProjects(ctx context.Context) func(string) error { + return func(_ string) error { + client, err := sweep.SharedGenClient() + if err != nil { + return err + } + + organizations, err := client.AccountList(ctx) + if common.IsCritical(err) { + return fmt.Errorf("error retrieving a list of organizations: %w", err) + } + + if organizations == nil { + return nil + } + + for _, organization := range organizations { + if !strings.HasPrefix(organization.AccountName, "test-acc") { + continue + } + + projects, err := client.OrganizationProjectsList(ctx, organization.OrganizationId) + if common.IsCritical(err) { + return fmt.Errorf("error retrieving a list of projects: %w", err) + } + + for _, project := range projects.Projects { + if !strings.HasPrefix(project.ProjectId, "test-acc") { + continue + } + + if err = client.OrganizationProjectsDelete(ctx, organization.OrganizationId, project.ProjectId); common.IsCritical(err) { + return fmt.Errorf("error deleting project %s: %w", project.ProjectId, err) + } + } + } + + return nil + } +} + func sweepOrganizationApplicationUsers(ctx context.Context) func(string) error { return func(id string) error { client, err := sweep.SharedClient()