Skip to content

Commit

Permalink
test: add sweepers for organization resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-savciuc committed Jan 11, 2024
1 parent 75cde0c commit ca3abd6
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
157 changes: 157 additions & 0 deletions internal/sdkprovider/service/organization/sweep.go
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
}
}
1 change: 1 addition & 0 deletions internal/sweep/sweep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
_ "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/m3db"
_ "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/mysql"
_ "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/opensearch"
_ "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/organization"
_ "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/pg"
_ "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/project"
_ "github.com/aiven/terraform-provider-aiven/internal/sdkprovider/service/redis"
Expand Down

0 comments on commit ca3abd6

Please sign in to comment.