Skip to content

Commit

Permalink
refactor(account): replaced client-v2 with avngen
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav committed Dec 30, 2024
1 parent 0f2962e commit f973c67
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 91 deletions.
116 changes: 52 additions & 64 deletions internal/sdkprovider/service/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"strings"
"time"

"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/account"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"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"
)

Expand Down Expand Up @@ -67,10 +68,10 @@ func ResourceAccount() *schema.Resource {
**This resource is deprecated.** Use ` + "`aiven_organization`" + ` instead.
`,
CreateContext: resourceAccountCreate,
ReadContext: resourceAccountRead,
UpdateContext: resourceAccountUpdate,
DeleteContext: resourceAccountDelete,
CreateContext: common.WithGenClient(resourceAccountCreate),
ReadContext: common.WithGenClient(resourceAccountRead),
UpdateContext: common.WithGenClient(resourceAccountUpdate),
DeleteContext: common.WithGenClient(resourceAccountDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -82,99 +83,86 @@ func ResourceAccount() *schema.Resource {
}
}

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

r, err := client.Accounts.Create(
ctx,
aiven.Account{
Name: name,
PrimaryBillingGroupId: bgID,
},
func resourceAccountCreate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
var (
req account.AccountCreateIn
)

if err := schemautil.ResourceDataGet(
d,
&req,
schemautil.RenameAlias("name", "account_name"),
); err != nil {
return err
}

resp, err := client.AccountCreate(ctx, &req)
if err != nil {
return diag.FromErr(err)
return err
}

d.SetId(r.Account.Id)
d.SetId(resp.AccountId)

return resourceAccountRead(ctx, d, m)
return resourceAccountRead(ctx, d, client)
}

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

r, err := client.Accounts.Get(ctx, d.Id())
func resourceAccountRead(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
resp, err := client.AccountGet(ctx, d.Id())
if err != nil {
return diag.FromErr(schemautil.ResourceReadHandleNotFound(err, d))
return schemautil.ResourceReadHandleNotFound(err, d)
}

if err := d.Set("account_id", r.Account.Id); err != nil {
return diag.FromErr(err)
}
if err := d.Set("name", r.Account.Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("primary_billing_group_id", r.Account.PrimaryBillingGroupId); err != nil {
return diag.FromErr(err)
}
if err := d.Set("owner_team_id", r.Account.OwnerTeamId); err != nil {
return diag.FromErr(err)
}
if err := d.Set("tenant_id", r.Account.TenantId); err != nil {
return diag.FromErr(err)
}
if err := d.Set("create_time", r.Account.CreateTime.String()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("update_time", r.Account.UpdateTime.String()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("is_account_owner", r.Account.IsAccountOwner); err != nil {
return diag.FromErr(err)
if err = schemautil.ResourceDataSet(
aivenAccountSchema,
d,
resp,
schemautil.RenameAliases(map[string]string{
"account_name": "name",
"account_owner_team_id": "owner_team_id",
}),
); err != nil {
return err
}

return nil
}

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

r, err := client.Accounts.Update(ctx, d.Id(), aiven.Account{
Name: d.Get("name").(string),
PrimaryBillingGroupId: d.Get("primary_billing_group_id").(string),
func resourceAccountUpdate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
var (
name = d.Get("name").(string)
bgID = d.Get("primary_billing_group_id").(string)
)
resp, err := client.AccountUpdate(ctx, d.Id(), &account.AccountUpdateIn{
AccountName: &name,
PrimaryBillingGroupId: &bgID,
})
if err != nil {
return diag.FromErr(err)
return err
}

d.SetId(r.Account.Id)
d.SetId(resp.AccountId)

return resourceAccountRead(ctx, d, m)
return resourceAccountRead(ctx, d, client)
}

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

func resourceAccountDelete(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
// Sometimes deleting an account fails with "Billing group with existing projects cannot be deleted", which
// happens due to a race condition between deleting projects and deleting the account. To avoid this, we retry
// the deletion until it succeeds or fails with a different error.
//
// TODO: Ideally, this should be fixed in the Aiven API. This is a temporary workaround, and should be removed
// once the API is fixed.
if err := retry.RetryContext(ctx, time.Second*30, func() *retry.RetryError {
err := client.Accounts.Delete(ctx, d.Id())
if err != nil {
if err := client.AccountDelete(ctx, d.Id()); err != nil {
return &retry.RetryError{
Err: err,
Retryable: strings.Contains(err.Error(), "Billing group with existing projects cannot be deleted"),
}
}

return nil
}); err != nil && !aiven.IsNotFound(err) {
return diag.FromErr(err)
}); err != nil && !avngen.IsNotFound(err) {
return err
}

return nil
Expand Down
26 changes: 13 additions & 13 deletions internal/sdkprovider/service/account/account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@ 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 DatasourceAccount() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceAccountRead,
ReadContext: common.WithGenClient(datasourceAccountRead),
Description: "The Account data source provides information about the existing Aiven Account.",
Schema: schemautil.ResourceSchemaAsDatasourceSchema(aivenAccountSchema, "name"),
}
}

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

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

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

for _, ac := range r.Accounts {
if ac.Name == name {
d.SetId(ac.Id)
return resourceAccountRead(ctx, d, m)
for _, ac := range resp {
if ac.AccountName == name {
d.SetId(ac.AccountId)

return resourceAccountRead(ctx, d, client)
}
}

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

import (
"context"
"errors"
"fmt"
"log"
"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"
)

func TestAccAivenAccount_basic(t *testing.T) {
Expand All @@ -33,6 +32,18 @@ func TestAccAivenAccount_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "primary_billing_group_id"),
),
},
{
// check that the account is not recreated redundantly
Config: testAccAccountResource(rName),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
{
// change the account name and check that it will be updated
Config: testAccAccountResource(fmt.Sprintf("%s-new", rName)),
PlanOnly: true,
ExpectNonEmptyPlan: true,
},
{
Config: testAccAccountToProject(rName),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -76,7 +87,10 @@ data "aiven_project" "pr" {
}

func testAccCheckAivenAccountResourceDestroy(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 @@ -86,19 +100,14 @@ func testAccCheckAivenAccountResourceDestroy(s *terraform.State) error {
continue
}

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 _, a := range r.Accounts {
if a.Id == rs.Primary.ID {
return fmt.Errorf("account (%s) still exists", rs.Primary.ID)
for _, account := range resp {
if account.AccountId == rs.Primary.ID {
return fmt.Errorf("account (%q) still exists", rs.Primary.ID)
}
}
}
Expand Down

0 comments on commit f973c67

Please sign in to comment.