-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add sweepers for organization resources
- Loading branch information
1 parent
75cde0c
commit ca3abd6
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
//go:build sweep | ||
|
||
package organization | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
|
||
"github.com/aiven/aiven-go-client/v2" | ||
"github.com/aiven/terraform-provider-aiven/internal/sweep" | ||
) | ||
|
||
func init() { | ||
ctx := context.Background() | ||
|
||
client, err := sweep.SharedClient() | ||
if err != nil { | ||
panic(fmt.Sprintf("error getting client: %s", err)) | ||
} | ||
|
||
resource.AddTestSweepers("aiven_organization", &resource.Sweeper{ | ||
Name: "aiven_organization", | ||
F: sweepOrganizations(ctx, client), | ||
}) | ||
|
||
resource.AddTestSweepers("aiven_organization_application_user", &resource.Sweeper{ | ||
Name: "aiven_organization_application_user", | ||
F: sweepOrganizationApplicationUsers(ctx, client), | ||
Dependencies: []string{ | ||
"aiven_organization", | ||
}, | ||
}) | ||
|
||
resource.AddTestSweepers("aiven_organization_user", &resource.Sweeper{ | ||
Name: "aiven_organization_user", | ||
F: sweepOrganizationUsers(ctx, client), | ||
Dependencies: []string{ | ||
"aiven_organization", | ||
}, | ||
}) | ||
|
||
resource.AddTestSweepers("aiven_organization_user_group", &resource.Sweeper{ | ||
Name: "aiven_organization_user_group", | ||
F: sweepOrganizationUserGroups(ctx, client), | ||
Dependencies: []string{ | ||
"aiven_organization", | ||
}, | ||
}) | ||
|
||
} | ||
|
||
func sweepOrganizations(ctx context.Context, client *aiven.Client) func(string) error { | ||
return func(id string) error { | ||
organizations, err := client.Accounts.List(ctx) | ||
if err != nil && !aiven.IsNotFound(err) { | ||
return fmt.Errorf("error retrieving a list of organizations: %w", err) | ||
} | ||
|
||
if organizations == nil { | ||
return nil | ||
} | ||
|
||
for _, organization := range organizations.Accounts { | ||
if !strings.HasPrefix(organization.Name, "test-acc") { | ||
continue | ||
} | ||
|
||
err = client.Accounts.Delete(ctx, organization.Id) | ||
if err != nil && !aiven.IsNotFound(err) { | ||
return fmt.Errorf("error deleting organization %s: %w", organization.Name, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func sweepOrganizationApplicationUsers(ctx context.Context, client *aiven.Client) func(string) error { | ||
return func(id string) error { | ||
organizationApplicationUsers, err := client.OrganizationApplicationUserHandler.List(ctx, id) | ||
if err != nil && !aiven.IsNotFound(err) { | ||
return fmt.Errorf("error retrieving a list of organization application users: %w", err) | ||
} | ||
|
||
if organizationApplicationUsers == nil { | ||
return nil | ||
} | ||
|
||
for _, organizationApplicationUser := range organizationApplicationUsers.Users { | ||
if !strings.HasPrefix(organizationApplicationUser.Name, "test-acc") { | ||
continue | ||
} | ||
|
||
err = client.OrganizationApplicationUserHandler.Delete(ctx, id, organizationApplicationUser.UserID) | ||
if err != nil && !aiven.IsNotFound(err) { | ||
return fmt.Errorf("error deleting organization application user %s: %w", organizationApplicationUser.Name, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func sweepOrganizationUserGroups(ctx context.Context, client *aiven.Client) func(string) error { | ||
return func(id string) error { | ||
organizationUserGroups, err := client.OrganizationUserGroups.List(ctx, id) | ||
if err != nil && !aiven.IsNotFound(err) { | ||
return fmt.Errorf("error retrieving a list of organization user groups: %w", err) | ||
} | ||
|
||
if organizationUserGroups == nil { | ||
return nil | ||
} | ||
|
||
for _, organizationUserGroup := range organizationUserGroups.UserGroups { | ||
if !strings.HasPrefix(organizationUserGroup.UserGroupName, "test-acc") { | ||
continue | ||
} | ||
|
||
err = client.OrganizationUserGroups.Delete(ctx, id, organizationUserGroup.UserGroupID) | ||
if err != nil && !aiven.IsNotFound(err) { | ||
return fmt.Errorf("error deleting organization user group %s: %w", organizationUserGroup.UserGroupName, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func sweepOrganizationUsers(ctx context.Context, client *aiven.Client) func(string) error { | ||
return func(id string) error { | ||
organizationUsers, err := client.OrganizationUser.List(ctx, id) | ||
if err != nil && !aiven.IsNotFound(err) { | ||
return fmt.Errorf("error retrieving a list of organization users: %w", err) | ||
} | ||
|
||
if organizationUsers == nil { | ||
return nil | ||
} | ||
|
||
for _, organizationUser := range organizationUsers.Users { | ||
if !strings.Contains(organizationUser.UserInfo.UserEmail, "test-acc") { | ||
continue | ||
} | ||
|
||
err = client.OrganizationUser.Delete(ctx, id, organizationUser.UserID) | ||
if err != nil && !aiven.IsNotFound(err) { | ||
return fmt.Errorf("error deleting organization user %s: %w", organizationUser.UserID, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters