Skip to content

Commit

Permalink
refactor(account_team_member): replaced client-v2 with avngen
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav committed Dec 27, 2024
1 parent 0f2962e commit 5570918
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 118 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_member.*"
linters:
- staticcheck
text: ".*is deprecated.*"
exclude:
# TODO: We can have a whitelist for revive's var-naming rule.
- 'var-naming: .*'
Expand Down
165 changes: 72 additions & 93 deletions internal/sdkprovider/service/account/account_team_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"log"

"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/accountteammember"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/aiven/terraform-provider-aiven/internal/common"
Expand Down Expand Up @@ -62,9 +62,9 @@ During the creation of this resource, an invite is sent to the address specified
The user is added to the team after they accept the invite. Deleting ` + "`aiven_account_team_member`" + `
deletes the pending invite if not accepted or removes the user from the team if they already accepted the invite.
`,
CreateContext: resourceAccountTeamMemberCreate,
ReadContext: resourceAccountTeamMemberRead,
DeleteContext: resourceAccountTeamMemberDelete,
CreateContext: common.WithGenClient(resourceAccountTeamMemberCreate),
ReadContext: common.WithGenClient(resourceAccountTeamMemberRead),
DeleteContext: common.WithGenClient(resourceAccountTeamMemberDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -82,142 +82,121 @@ migration guide for more information: https://aiven.io/docs/tools/terraform/howt
}
}

func resourceAccountTeamMemberCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)
accountID := d.Get("account_id").(string)
teamID := d.Get("team_id").(string)
userEmail := d.Get("user_email").(string)
func resourceAccountTeamMemberCreate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
var (
accountID = d.Get("account_id").(string)
teamID = d.Get("team_id").(string)
userEmail = d.Get("user_email").(string)
)

err := client.AccountTeamMembers.Invite(
if err := client.AccountTeamMembersInvite(
ctx,
accountID,
teamID,
userEmail,
)
if err != nil {
return diag.FromErr(err)
&accountteammember.AccountTeamMembersInviteIn{Email: userEmail},
); err != nil {
return err
}

d.SetId(schemautil.BuildResourceID(accountID, teamID, userEmail))

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

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

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

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

for _, invite := range r.Invites {
for _, invite := range resp {
if invite.UserEmail == userEmail {
found = true

if err := d.Set("account_id", invite.AccountId); err != nil {
return diag.FromErr(err)
}
if err := d.Set("team_id", invite.TeamId); err != nil {
return diag.FromErr(err)
if err = schemautil.ResourceDataSet(
aivenAccountTeamMemberSchema,
d,
invite,
schemautil.RenameAliases(map[string]string{}),
); err != nil {
return err
}
if err := d.Set("user_email", invite.UserEmail); err != nil {
return diag.FromErr(err)
}
if err := d.Set("invited_by_user_email", invite.InvitedByUserEmail); err != nil {
return diag.FromErr(err)
}
if err := d.Set("create_time", invite.CreateTime.String()); err != nil {
return diag.FromErr(err)

if err = d.Set("account_id", accountID); err != nil {
return err
}

// if a user is in the invitations list, it means invitation was sent but not yet accepted
if err := d.Set("accepted", false); err != nil {
return diag.FromErr(err)
if err = d.Set("accepted", false); err != nil {
return err
}

return nil
}
}

if !found {
rm, err := client.AccountTeamMembers.List(ctx, accountID, teamID)
if err != nil {
return diag.FromErr(err)
}
respTI, err := client.AccountTeamMembersList(ctx, accountID, teamID)
if err != nil {
return err
}

for _, member := range respTI {
if member.UserEmail == userEmail {
if err = schemautil.ResourceDataSet(
aivenAccountTeamMemberSchema,
d,
member,
); err != nil {
return err
}

if err = d.Set("account_id", accountID); err != nil {
return err
}

for _, member := range rm.Members {
if member.UserEmail == userEmail {
found = true

if err := d.Set("account_id", accountID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("team_id", member.TeamId); err != nil {
return diag.FromErr(err)
}
if err := d.Set("user_email", member.UserEmail); err != nil {
return diag.FromErr(err)
}
if err := d.Set("create_time", member.CreateTime.String()); err != nil {
return diag.FromErr(err)
}

// when a user accepts an invitation, it will appear in the member's list
// and disappear from invitations list
if err := d.Set("accepted", true); err != nil {
return diag.FromErr(err)
}
// when a user accepts an invitation, it will appear in the member's list
// and disappear from invitations list
if err = d.Set("accepted", true); err != nil {
return err
}

return nil
}
}

if !found {
log.Printf("[WARNING] cannot find user invitation for %s", d.Id())
if !d.Get("accepted").(bool) {
log.Printf("[DEBUG] resending account team member invitation ")
return resourceAccountTeamMemberCreate(ctx, d, m)
}
log.Printf("[WARNING] cannot find user invitation for %s", d.Id())
if !d.Get("accepted").(bool) {
log.Printf("[DEBUG] resending account team member invitation ")
return resourceAccountTeamMemberCreate(ctx, d, client)
}

return nil
}

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

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

// delete account team user invitation
err = client.AccountTeamInvites.Delete(ctx, accountID, teamID, userEmail)
if common.IsCritical(err) {
return diag.FromErr(err)
if err = client.AccountTeamMemberCancelInvite(ctx, accountID, teamID, userEmail); common.IsCritical(err) {
return err
}

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

if len(r.Members) == 0 {
return nil
return err
}

// delete account team member
for _, m := range r.Members {
for _, m := range resp {
if m.UserEmail == userEmail {
err = client.AccountTeamMembers.Delete(ctx, accountID, teamID, m.UserId)
if common.IsCritical(err) {
return diag.FromErr(err)
if err = client.AccountTeamMembersDelete(ctx, accountID, teamID, m.UserId); common.IsCritical(err) {
return err
}
break
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ package account
import (
"context"

"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 DatasourceAccountTeamMember() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceAccountTeamMemberRead,
ReadContext: common.WithGenClient(datasourceAccountTeamMemberRead),
Description: "The Account Team Member data source provides information about the existing Aiven Account Team Member.",
Schema: schemautil.ResourceSchemaAsDatasourceSchema(aivenAccountTeamMemberSchema,
"account_id", "team_id", "user_email"),
}
}

func datasourceAccountTeamMemberRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func datasourceAccountTeamMemberRead(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
accountID := d.Get("account_id").(string)
teamID := d.Get("team_id").(string)
userEmail := d.Get("user_email").(string)

d.SetId(schemautil.BuildResourceID(accountID, teamID, userEmail))

return resourceAccountTeamMemberRead(ctx, d, m)
return resourceAccountTeamMemberRead(ctx, d, client)
}
49 changes: 28 additions & 21 deletions internal/sdkprovider/service/account/account_team_member_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 @@ -73,7 +72,10 @@ data "aiven_account_team_member" "member" {
}

func testAccCheckAivenAccountTeamMemberResourceDestroy(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 @@ -88,30 +90,35 @@ func testAccCheckAivenAccountTeamMemberResourceDestroy(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 _, a := range r.Accounts {
if a.Id == accountID {
ri, err := c.AccountTeamInvites.List(ctx, accountID, teamID)
if err != nil {
var e aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
for _, r := range resp {
if r.AccountId == accountID {
respTI, err := c.AccountTeamInvitesList(ctx, accountID, teamID)
if common.IsCritical(err) {
return err
}

for _, invite := range respTI {
if invite.UserEmail == userEmail {
return fmt.Errorf("account team member (%s) still exists", rs.Primary.ID)
}
}
}
}

return nil
for _, r := range resp {
if r.AccountId == accountID {
respTM, err := c.AccountTeamMembersList(ctx, accountID, teamID)
if common.IsCritical(err) {
return err
}

for _, i := range ri.Invites {
if i.UserEmail == userEmail {
for _, member := range respTM {
if member.UserEmail == userEmail {
return fmt.Errorf("account team member (%s) still exists", rs.Primary.ID)
}
}
Expand Down

0 comments on commit 5570918

Please sign in to comment.