Skip to content

Commit

Permalink
ci: add sweep check (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-savciuc authored Jan 19, 2024
1 parent d00799d commit 7dce9e5
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 129 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,16 @@ jobs:

- run: make docs
- run: git diff --exit-code

sweep_check:
runs-on: ubuntu-latest
if: (github.event_name == 'pull_request' && !contains(github.event.pull_request.labels.*.name, 'skip workflows')) || github.event_name == 'push'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version-file: go.mod

- run: make sweep-check
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ sweep:
TF_SWEEP=1 $(GO) test ./internal/sweep -v -sweep=$(SWEEP) $(SWEEP_ARGS) -timeout 15m

sweep-check:
TF_SWEEP=1 $(GO) test ./internal/sweep -v -run TestCheckSweepers
$(GO) test ./internal/sweep -v -run TestCheckSweepers

#################################################
# Generate
Expand Down
74 changes: 47 additions & 27 deletions internal/sdkprovider/service/account/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package account
import (
"context"
"fmt"
"os"
"strings"

"github.com/aiven/aiven-go-client/v2"
Expand All @@ -13,53 +12,49 @@ import (
)

func init() {
if os.Getenv("TF_SWEEP") == "" {
return
}

ctx := context.Background()

client, err := sweep.SharedClient()
if err != nil {
panic(fmt.Sprintf("error getting client: %s", err))
}

sweep.AddTestSweepers("aiven_account_team_member", &resource.Sweeper{
Name: "aiven_account_team_member",
F: sweepAccountTeamMembers(ctx, client),
F: sweepAccountTeamMembers(ctx),
Dependencies: []string{"aiven_account_authentication"},
})

sweep.AddTestSweepers("aiven_account_team_project", &resource.Sweeper{
Name: "aiven_account_team_project",
F: sweepAccountTeamProjects(ctx, client),
F: sweepAccountTeamProjects(ctx),
Dependencies: []string{"aiven_account_authentication"},
})

sweep.AddTestSweepers("aiven_account_team", &resource.Sweeper{
Name: "aiven_account_team",
F: sweepAccountTeams(ctx, client),
F: sweepAccountTeams(ctx),
Dependencies: []string{"aiven_account_team_member", "aiven_account_authentication"},
})

sweep.AddTestSweepers("aiven_account", &resource.Sweeper{
Name: "aiven_account",
F: sweepAccounts(ctx, client),
F: sweepAccounts(ctx),
Dependencies: []string{"aiven_project", "aiven_account_team", "aiven_account_team_project", "aiven_account_authentication"},
})

sweep.AddTestSweepers("aiven_organizational_unit", &resource.Sweeper{
Name: "aiven_organizational_unit",
F: sweepAccounts(ctx, client),
F: sweepAccounts(ctx),
})

sweep.AddTestSweepers("aiven_account_authentication", &resource.Sweeper{
Name: "aiven_account_authentication",
F: sweepAccountAuthentications(ctx, client),
F: sweepAccountAuthentications(ctx),
})
}

func listTestAccounts(ctx context.Context, client *aiven.Client) ([]aiven.Account, error) {
func listTestAccounts(ctx context.Context) ([]aiven.Account, error) {
client, err := sweep.SharedClient()
if err != nil {
return nil, err
}

var testAccounts []aiven.Account

r, err := client.Accounts.List(ctx)
Expand All @@ -76,9 +71,14 @@ func listTestAccounts(ctx context.Context, client *aiven.Client) ([]aiven.Accoun
return testAccounts, nil
}

func sweepAccountAuthentications(ctx context.Context, client *aiven.Client) func(region string) error {
func sweepAccountAuthentications(ctx context.Context) func(region string) error {
return func(region string) error {
accounts, err := listTestAccounts(ctx, client)
client, err := sweep.SharedClient()
if err != nil {
return err
}

accounts, err := listTestAccounts(ctx)

if err != nil {
return fmt.Errorf("error retrieving a list of accounts : %w", err)
Expand All @@ -105,9 +105,14 @@ func sweepAccountAuthentications(ctx context.Context, client *aiven.Client) func
}
}

func sweepAccounts(ctx context.Context, client *aiven.Client) func(region string) error {
func sweepAccounts(ctx context.Context) func(region string) error {
return func(region string) error {
accounts, err := listTestAccounts(ctx, client)
client, err := sweep.SharedClient()
if err != nil {
return err
}

accounts, err := listTestAccounts(ctx)
if err != nil {
return fmt.Errorf("error retrieving a list of accounts : %w", err)
}
Expand All @@ -126,9 +131,14 @@ func sweepAccounts(ctx context.Context, client *aiven.Client) func(region string
}
}

func sweepAccountTeams(ctx context.Context, client *aiven.Client) func(region string) error {
func sweepAccountTeams(ctx context.Context) func(region string) error {
return func(region string) error {
accounts, err := listTestAccounts(ctx, client)
client, err := sweep.SharedClient()
if err != nil {
return err
}

accounts, err := listTestAccounts(ctx)
if err != nil {
return fmt.Errorf("error retrieving a list of accounts : %w", err)
}
Expand All @@ -153,9 +163,14 @@ func sweepAccountTeams(ctx context.Context, client *aiven.Client) func(region st
return nil
}
}
func sweepAccountTeamMembers(ctx context.Context, client *aiven.Client) func(region string) error {
func sweepAccountTeamMembers(ctx context.Context) func(region string) error {
return func(region string) error {
accounts, err := listTestAccounts(ctx, client)
client, err := sweep.SharedClient()
if err != nil {
return err
}

accounts, err := listTestAccounts(ctx)
if err != nil {
return fmt.Errorf("error retrieving a list of accounts : %s", err)
}
Expand Down Expand Up @@ -202,9 +217,14 @@ func sweepAccountTeamMembers(ctx context.Context, client *aiven.Client) func(reg
}
}

func sweepAccountTeamProjects(ctx context.Context, client *aiven.Client) func(region string) error {
func sweepAccountTeamProjects(ctx context.Context) func(region string) error {
return func(region string) error {
accounts, err := listTestAccounts(ctx, client)
client, err := sweep.SharedClient()
if err != nil {
return err
}

accounts, err := listTestAccounts(ctx)
if err != nil {
return fmt.Errorf("error retrieving a list of accounts : %s", err)
}
Expand Down
19 changes: 7 additions & 12 deletions internal/sdkprovider/service/connectionpool/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/common"
Expand All @@ -14,26 +13,22 @@ import (
)

func init() {
if os.Getenv("TF_SWEEP") == "" {
return
}

ctx := context.Background()

client, err := sweep.SharedClient()
if err != nil {
panic(fmt.Sprintf("error getting client: %s", err))
}

sweep.AddTestSweepers("aiven_connection_pool", &resource.Sweeper{
Name: "aiven_connection_pool",
F: sweepConnectionPoll(ctx, client),
F: sweepConnectionPoll(ctx),
})

}

func sweepConnectionPoll(ctx context.Context, client *aiven.Client) func(string) error {
func sweepConnectionPoll(ctx context.Context) func(string) error {
return func(id string) error {
client, err := sweep.SharedClient()
if err != nil {
return err
}

projectName := os.Getenv("AIVEN_PROJECT_NAME")

services, err := client.Services.List(ctx, projectName)
Expand Down
47 changes: 28 additions & 19 deletions internal/sdkprovider/service/organization/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package organization
import (
"context"
"fmt"
"os"
"strings"

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/common"
Expand All @@ -16,50 +14,46 @@ import (
const defaultPrefix = "test-acc"

func init() {
if os.Getenv("TF_SWEEP") == "" {
return
}

ctx := context.Background()

client, err := sweep.SharedClient()
if err != nil {
panic(fmt.Sprintf("error getting client: %s", err))
}

sweep.AddTestSweepers("aiven_organization", &resource.Sweeper{
Name: "aiven_organization",
F: sweepOrganizations(ctx, client),
F: sweepOrganizations(ctx),
})

sweep.AddTestSweepers("aiven_organization_application_user", &resource.Sweeper{
Name: "aiven_organization_application_user",
F: sweepOrganizationApplicationUsers(ctx, client),
F: sweepOrganizationApplicationUsers(ctx),
Dependencies: []string{
"aiven_organization",
},
})

sweep.AddTestSweepers("aiven_organization_user", &resource.Sweeper{
Name: "aiven_organization_user",
F: sweepOrganizationUsers(ctx, client),
F: sweepOrganizationUsers(ctx),
Dependencies: []string{
"aiven_organization",
},
})

sweep.AddTestSweepers("aiven_organization_user_group", &resource.Sweeper{
Name: "aiven_organization_user_group",
F: sweepOrganizationUserGroups(ctx, client),
F: sweepOrganizationUserGroups(ctx),
Dependencies: []string{
"aiven_organization",
},
})

}

func sweepOrganizations(ctx context.Context, client *aiven.Client) func(string) error {
func sweepOrganizations(ctx context.Context) func(string) error {
return func(id string) error {
client, err := sweep.SharedClient()
if err != nil {
return err
}

organizations, err := client.Accounts.List(ctx)
if common.IsCritical(err) {
return fmt.Errorf("error retrieving a list of organizations: %w", err)
Expand All @@ -84,8 +78,13 @@ func sweepOrganizations(ctx context.Context, client *aiven.Client) func(string)
}
}

func sweepOrganizationApplicationUsers(ctx context.Context, client *aiven.Client) func(string) error {
func sweepOrganizationApplicationUsers(ctx context.Context) func(string) error {
return func(id string) error {
client, err := sweep.SharedClient()
if err != nil {
return err
}

organizationApplicationUsers, err := client.OrganizationApplicationUserHandler.List(ctx, id)
if common.IsCritical(err) {
return fmt.Errorf("error retrieving a list of organization application users: %w", err)
Expand All @@ -110,8 +109,13 @@ func sweepOrganizationApplicationUsers(ctx context.Context, client *aiven.Client
}
}

func sweepOrganizationUserGroups(ctx context.Context, client *aiven.Client) func(string) error {
func sweepOrganizationUserGroups(ctx context.Context) func(string) error {
return func(id string) error {
client, err := sweep.SharedClient()
if err != nil {
return err
}

organizationUserGroups, err := client.OrganizationUserGroups.List(ctx, id)
if common.IsCritical(err) {
return fmt.Errorf("error retrieving a list of organization user groups: %w", err)
Expand All @@ -136,8 +140,13 @@ func sweepOrganizationUserGroups(ctx context.Context, client *aiven.Client) func
}
}

func sweepOrganizationUsers(ctx context.Context, client *aiven.Client) func(string) error {
func sweepOrganizationUsers(ctx context.Context) func(string) error {
return func(id string) error {
client, err := sweep.SharedClient()
if err != nil {
return err
}

organizationUsers, err := client.OrganizationUser.List(ctx, id)
if common.IsCritical(err) {
return fmt.Errorf("error retrieving a list of organization users: %w", err)
Expand Down
Loading

0 comments on commit 7dce9e5

Please sign in to comment.