Skip to content

Commit

Permalink
refactor(account_team): replaced client-v2 with avngen (#1966)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav authored Dec 31, 2024
1 parent 3cdca29 commit 10e60f6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 91 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ linters:
- prealloc

issues:
exclude-rules:
- path: "internal/sdkprovider/service/account/account_team.*"
linters:
- staticcheck
text: ".*is deprecated.*"
exclude:
# TODO: We can have a whitelist for revive's var-naming rule.
- 'var-naming: .*'
Expand Down
102 changes: 48 additions & 54 deletions internal/sdkprovider/service/account/account_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package account

import (
"context"
"fmt"

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
avngen "github.com/aiven/go-client-codegen"
"github.com/aiven/go-client-codegen/handler/accountteam"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/aiven/terraform-provider-aiven/internal/common"
Expand Down Expand Up @@ -42,10 +43,10 @@ var aivenAccountTeamSchema = map[string]*schema.Schema{
func ResourceAccountTeam() *schema.Resource {
return &schema.Resource{
Description: `Creates and manages a team.`,
CreateContext: resourceAccountTeamCreate,
ReadContext: resourceAccountTeamRead,
UpdateContext: resourceAccountTeamUpdate,
DeleteContext: resourceAccountTeamDelete,
CreateContext: common.WithGenClient(resourceAccountTeamCreate),
ReadContext: common.WithGenClient(resourceAccountTeamRead),
UpdateContext: common.WithGenClient(resourceAccountTeamUpdate),
DeleteContext: common.WithGenClient(resourceAccountTeamDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -63,89 +64,82 @@ migration guide for more information: https://aiven.io/docs/tools/terraform/howt
}
}

func resourceAccountTeamCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)
name := d.Get("name").(string)
accountID := d.Get("account_id").(string)

r, err := client.AccountTeams.Create(
ctx,
accountID,
aiven.AccountTeam{
Name: name,
},
func resourceAccountTeamCreate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
var (
name = d.Get("name").(string)
accountID = d.Get("account_id").(string)
)

resp, err := client.AccountTeamCreate(ctx, accountID, &accountteam.AccountTeamCreateIn{
TeamName: name,
})
if err != nil {
return diag.FromErr(err)
return err
}

d.SetId(schemautil.BuildResourceID(r.Team.AccountId, r.Team.Id))
if resp.AccountId == nil {
return fmt.Errorf("account team create response missing account_id field")
}

return resourceAccountTeamRead(ctx, d, m)
}
d.SetId(schemautil.BuildResourceID(*resp.AccountId, resp.TeamId))

func resourceAccountTeamRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)
return resourceAccountTeamRead(ctx, d, client)
}

func resourceAccountTeamRead(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
accountID, teamID, err := schemautil.SplitResourceID2(d.Id())
if err != nil {
return diag.FromErr(err)
return err
}

r, err := client.AccountTeams.Get(ctx, accountID, teamID)
resp, err := client.AccountTeamGet(ctx, accountID, teamID)
if err != nil {
return diag.FromErr(schemautil.ResourceReadHandleNotFound(err, d))
return schemautil.ResourceReadHandleNotFound(err, d)
}

if err := d.Set("account_id", r.Team.AccountId); err != nil {
return diag.FromErr(err)
}
if err := d.Set("team_id", r.Team.Id); err != nil {
return diag.FromErr(err)
}
if err := d.Set("name", r.Team.Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("create_time", r.Team.CreateTime.String()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("update_time", r.Team.UpdateTime.String()); err != nil {
return diag.FromErr(err)
if err = schemautil.ResourceDataSet(
aivenAccountTeamSchema,
d,
resp,
schemautil.RenameAlias("team_name", "name"),
); err != nil {
return err
}

return nil
}

func resourceAccountTeamUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)
func resourceAccountTeamUpdate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
accountID, teamID, err := schemautil.SplitResourceID2(d.Id())
if err != nil {
return diag.FromErr(err)
return err
}

r, err := client.AccountTeams.Update(ctx, accountID, teamID, aiven.AccountTeam{
Name: d.Get("name").(string),
resp, err := client.AccountTeamUpdate(ctx, accountID, teamID, &accountteam.AccountTeamUpdateIn{
TeamName: d.Get("name").(string),
})
if err != nil {
return diag.FromErr(err)
return err
}

d.SetId(schemautil.BuildResourceID(r.Team.AccountId, r.Team.Id))
if resp.AccountId == nil {
return fmt.Errorf("account team update response missing account_id field")
}

return resourceAccountTeamRead(ctx, d, m)
}
d.SetId(schemautil.BuildResourceID(*resp.AccountId, resp.TeamId))

func resourceAccountTeamDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)
return resourceAccountTeamRead(ctx, d, client)
}

func resourceAccountTeamDelete(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
accountID, teamID, err := schemautil.SplitResourceID2(d.Id())
if err != nil {
return diag.FromErr(err)
return err
}

err = client.AccountTeams.Delete(ctx, accountID, teamID)
err = client.AccountTeamDelete(ctx, accountID, teamID)
if common.IsCritical(err) {
return diag.FromErr(err)
return err
}

return nil
Expand Down
31 changes: 18 additions & 13 deletions internal/sdkprovider/service/account/account_team_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,45 @@ package account

import (
"context"
"fmt"

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
avngen "github.com/aiven/go-client-codegen"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

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

func DatasourceAccountTeam() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceAccountTeamRead,
ReadContext: common.WithGenClient(datasourceAccountTeamRead),
Description: "The Account Team data source provides information about the existing Account Team.",
Schema: schemautil.ResourceSchemaAsDatasourceSchema(aivenAccountTeamSchema,
"account_id", "name"),
}
}

func datasourceAccountTeamRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)

func datasourceAccountTeamRead(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
name := d.Get("name").(string)
accountID := d.Get("account_id").(string)

r, err := client.AccountTeams.List(ctx, accountID)
resp, err := client.AccountTeamList(ctx, accountID)
if err != nil {
return diag.FromErr(err)
return err
}

for _, t := range r.Teams {
if t.Name == name {
d.SetId(schemautil.BuildResourceID(t.AccountId, t.Id))
return resourceAccountTeamRead(ctx, d, m)
for _, at := range resp {
if at.TeamName == name {
if at.AccountId == nil {
return fmt.Errorf("account team %q not found", name)
}

d.SetId(schemautil.BuildResourceID(*at.AccountId, at.TeamId))

return resourceAccountTeamRead(ctx, d, client)
}

}

return diag.Errorf("account team %s not found", name)
return fmt.Errorf("account team %q not found", name)
}
39 changes: 15 additions & 24 deletions internal/sdkprovider/service/account/account_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package account_test

import (
"context"
"errors"
"fmt"
"log"
"os"
"testing"

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

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

Expand Down Expand Up @@ -67,7 +66,10 @@ data "aiven_account_team" "team" {
}

func testAccCheckAivenAccountTeamResourceDestroy(s *terraform.State) error {
c := acc.GetTestAivenClient()
c, err := acc.GetTestGenAivenClient()
if err != nil {
return fmt.Errorf("error instantiating client: %w", err)
}

ctx := context.Background()

Expand All @@ -82,36 +84,25 @@ func testAccCheckAivenAccountTeamResourceDestroy(s *terraform.State) error {
return err
}

r, err := c.Accounts.List(ctx)
if err != nil {
var e aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

return nil
resp, err := c.AccountList(ctx)
if common.IsCritical(err) {
return err
}

for _, ac := range r.Accounts {
if ac.Id == accountID {
rl, err := c.AccountTeams.List(ctx, accountID)
if err != nil {
var e aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

return nil
for _, account := range resp {
if account.AccountId == accountID {
respTL, err := c.AccountTeamList(ctx, accountID)
if common.IsCritical(err) {
return err
}

for _, team := range rl.Teams {
if team.Id == teamID {
for _, team := range respTL {
if team.TeamId == teamID {
return fmt.Errorf("account team (%s) still exists", rs.Primary.ID)
}
}
}
}

}

return nil
Expand Down

0 comments on commit 10e60f6

Please sign in to comment.