Skip to content

Commit

Permalink
refactor(account sweeper): replaced client-v2 with avngen
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav committed Jan 3, 2025
1 parent c63150d commit 3fa047e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 49 deletions.
5 changes: 4 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ issues:
- path: "internal/sdkprovider/service/account/account_team.*"
linters:
- staticcheck
text: ".*is deprecated.*"
- path: "internal/sdkprovider/service/account/sweep.go"
linters:
- staticcheck
text: "SA1019: (.*) is deprecated"
exclude:
# TODO: We can have a whitelist for revive's var-naming rule.
- 'var-naming: .*'
Expand Down
90 changes: 44 additions & 46 deletions internal/sdkprovider/service/account/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package account

import (
"context"
"errors"
"fmt"
"strings"

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

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/sweep"
)

Expand Down Expand Up @@ -50,21 +50,21 @@ func init() {
})
}

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

var testAccounts []aiven.Account
var testAccounts []account.AccountOut

r, err := client.Accounts.List(ctx)
resp, err := client.AccountList(ctx)
if err != nil {
return nil, err
}

for _, a := range r.Accounts {
if strings.Contains(a.Name, "test-acc-ac-") {
for _, a := range resp {
if strings.Contains(a.AccountName, "test-acc-ac-") {
testAccounts = append(testAccounts, a)
}
}
Expand All @@ -74,7 +74,7 @@ func listTestAccounts(ctx context.Context) ([]aiven.Account, error) {

func sweepAccountAuthentications(ctx context.Context) func(region string) error {
return func(_ string) error {
client, err := sweep.SharedClient()
client, err := sweep.SharedGenClient()
if err != nil {
return err
}
Expand All @@ -86,17 +86,17 @@ func sweepAccountAuthentications(ctx context.Context) func(region string) error
}

for _, a := range accounts {
rr, err := client.AccountAuthentications.List(ctx, a.Id)
aal, err := client.AccountAuthenticationMethodsList(ctx, a.AccountId)
if err != nil {
return fmt.Errorf("cannot get account authentications list: %w", err)
}

for _, m := range rr.AuthenticationMethods {
err := client.AccountAuthentications.Delete(ctx, a.Id, m.AuthenticationMethodID)
if err != nil {
for _, m := range aal {
if err = client.AccountAuthenticationMethodDelete(ctx, a.AccountId, m.AuthenticationMethodId); err != nil {
if strings.Contains(err.Error(), "Internal authentication methods cannot be deleted") {
continue
}

return fmt.Errorf("cannot delete account authentication: %w", err)
}
}
Expand All @@ -108,7 +108,7 @@ func sweepAccountAuthentications(ctx context.Context) func(region string) error

func sweepAccounts(ctx context.Context) func(region string) error {
return func(_ string) error {
client, err := sweep.SharedClient()
client, err := sweep.SharedGenClient()
if err != nil {
return err
}
Expand All @@ -119,13 +119,10 @@ func sweepAccounts(ctx context.Context) func(region string) error {
}

for _, a := range accounts {
if err := client.Accounts.Delete(ctx, a.Id); err != nil {
var e aiven.Error
if errors.As(err, &e) && e.Status == 404 {
continue
if err = client.AccountDelete(ctx, a.AccountId); err != nil {
if common.IsCritical(err) {
return fmt.Errorf("error destroying account %s during sweep: %w", a.AccountName, err)
}

return fmt.Errorf("error destroying account %s during sweep: %w", a.Name, err)
}
}

Expand All @@ -135,7 +132,7 @@ func sweepAccounts(ctx context.Context) func(region string) error {

func sweepAccountTeams(ctx context.Context) func(region string) error {
return func(_ string) error {
client, err := sweep.SharedClient()
client, err := sweep.SharedGenClient()
if err != nil {
return err
}
Expand All @@ -146,28 +143,28 @@ func sweepAccountTeams(ctx context.Context) func(region string) error {
}

for _, a := range accounts {
tr, err := client.AccountTeams.List(ctx, a.Id)
atl, err := client.AccountTeamList(ctx, a.AccountId)
if err != nil {
return fmt.Errorf("error retrieving a list of account teams : %w", err)
}

for _, t := range tr.Teams {
if strings.Contains(t.Name, "test-acc-team-") {
err = client.AccountTeams.Delete(ctx, t.AccountId, t.Id)
for _, at := range atl {
if strings.Contains(at.TeamName, "test-acc-team-") {
err = client.AccountTeamDelete(ctx, a.AccountId, at.TeamId)
if err != nil {
return fmt.Errorf("cannot delete account team: %w", err)
}
}

}
}

return nil
}
}

func sweepAccountTeamMembers(ctx context.Context) func(region string) error {
return func(_ string) error {
client, err := sweep.SharedClient()
client, err := sweep.SharedGenClient()
if err != nil {
return err
}
Expand All @@ -178,40 +175,42 @@ func sweepAccountTeamMembers(ctx context.Context) func(region string) error {
}

for _, a := range accounts {
tr, err := client.AccountTeams.List(ctx, a.Id)
atl, err := client.AccountTeamList(ctx, a.AccountId)
if err != nil {
return fmt.Errorf("error retrieving a list of account teams : %w", err)
}

for _, t := range tr.Teams {
if strings.Contains(t.Name, "test-acc-team-") {
for _, t := range atl {
if strings.Contains(t.TeamName, "test-acc-team-") {
// delete all account team invitations
mi, err := client.AccountTeamInvites.List(ctx, t.AccountId, t.Id)
if t.AccountId == nil {
return fmt.Errorf("account id is empty for the team %q", t.TeamName)
}

mi, err := client.AccountTeamInvitesList(ctx, *t.AccountId, t.TeamId)
if err != nil {
return fmt.Errorf("error retrieving a list of account team invitations : %w", err)
}

for _, i := range mi.Invites {
err := client.AccountTeamInvites.Delete(ctx, i.AccountId, i.TeamId, i.UserEmail)
for _, i := range mi {
err = client.AccountTeamMemberCancelInvite(ctx, *t.AccountId, t.TeamId, i.UserEmail)
if err != nil {
return fmt.Errorf("cannot delete account team invitation : %w", err)
}
}

// delete all account team members
mr, err := client.AccountTeamMembers.List(ctx, t.AccountId, t.Id)
tml, err := client.AccountTeamMembersList(ctx, *t.AccountId, t.TeamId)
if err != nil {
return fmt.Errorf("error retrieving a list of account team members : %w", err)
}

for _, m := range mr.Members {
err := client.AccountTeamMembers.Delete(ctx, t.AccountId, t.Id, m.UserId)
if err != nil {
for _, tm := range tml {
if err = client.AccountTeamMembersDelete(ctx, *t.AccountId, t.TeamId, tm.UserId); err != nil {
return fmt.Errorf("cannot delete account team member : %w", err)
}
}
}

}
}

Expand All @@ -221,7 +220,7 @@ func sweepAccountTeamMembers(ctx context.Context) func(region string) error {

func sweepAccountTeamProjects(ctx context.Context) func(region string) error {
return func(_ string) error {
client, err := sweep.SharedClient()
client, err := sweep.SharedGenClient()
if err != nil {
return err
}
Expand All @@ -232,26 +231,25 @@ func sweepAccountTeamProjects(ctx context.Context) func(region string) error {
}

for _, a := range accounts {
tr, err := client.AccountTeams.List(ctx, a.Id)
atl, err := client.AccountTeamList(ctx, a.AccountId)
if err != nil {
return fmt.Errorf("error retrieving a list of account teams : %w", err)
}

for _, t := range tr.Teams {
if strings.Contains(t.Name, "test-acc-team-") {
pr, err := client.AccountTeamProjects.List(ctx, t.AccountId, t.Id)
for _, t := range atl {
if strings.Contains(t.TeamName, "test-acc-team-") {
pl, err := client.AccountTeamProjectList(ctx, a.AccountId, t.TeamId)
if err != nil {
return fmt.Errorf("error retrieving a list of account team projects : %w", err)
}

for _, p := range pr.Projects {
err := client.AccountTeamProjects.Delete(ctx, t.AccountId, t.Id, p.ProjectName)
for _, p := range pl {
err := client.AccountTeamProjectDisassociate(ctx, a.AccountId, t.TeamId, p.ProjectName)
if err != nil {
return fmt.Errorf("cannot delete account team project : %w", err)
}
}
}

}
}

Expand Down
30 changes: 28 additions & 2 deletions internal/sweep/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ import (
"fmt"
"os"
"strings"
"sync"

"github.com/aiven/aiven-go-client/v2"
avngen "github.com/aiven/go-client-codegen"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"golang.org/x/exp/maps"

"github.com/aiven/terraform-provider-aiven/internal/common"
)

var sharedClient *aiven.Client
var sweeperFuncs map[string]struct{}
var (
sharedClient *aiven.Client
sweeperFuncs map[string]struct{}

sharedGenClient avngen.Client
initOnce sync.Once
initError error
)

func init() {
sweeperFuncs = make(map[string]struct{})
Expand All @@ -38,6 +46,24 @@ func SharedClient() (*aiven.Client, error) {
return sharedClient, nil
}

func SharedGenClient() (avngen.Client, error) {
if os.Getenv("AIVEN_PROJECT_NAME") == "" {
return nil, fmt.Errorf("must provide environment variable AIVEN_PROJECT_NAME ")
}

initOnce.Do(func() {
var err error
sharedGenClient, err = common.NewAivenGenClient()
if err != nil {
initError = err

return
}
})

return sharedGenClient, initError
}

func SweepServices(ctx context.Context, t string) error {
client, err := SharedClient()
if err != nil {
Expand Down

0 comments on commit 3fa047e

Please sign in to comment.