Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(account_team_project): replaced client-v2 with avngen #1968

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 48 additions & 51 deletions internal/sdkprovider/service/account/account_team_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package account

import (
"context"
"fmt"

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

Expand Down Expand Up @@ -33,7 +34,7 @@ var aivenAccountTeamProjectSchema = map[string]*schema.Schema{
"team_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(account.TeamTypeChoices(), false),
ValidateFunc: validation.StringInSlice(accountteam.TeamTypeChoices(), false),
Description: userconfig.Desc("The Account team project type").PossibleValuesString(account.TeamTypeChoices()...).Build(),
},
}
Expand All @@ -43,10 +44,10 @@ func ResourceAccountTeamProject() *schema.Resource {
Description: `
Links an existing project to an existing team. Both the project and team should have the same ` + "`account_id`" + `.
`,
CreateContext: resourceAccountTeamProjectCreate,
ReadContext: resourceAccountTeamProjectRead,
UpdateContext: resourceAccountTeamProjectUpdate,
DeleteContext: resourceAccountTeamProjectDelete,
CreateContext: common.WithGenClient(resourceAccountTeamProjectCreate),
ReadContext: common.WithGenClient(resourceAccountTeamProjectRead),
UpdateContext: common.WithGenClient(resourceAccountTeamProjectUpdate),
DeleteContext: common.WithGenClient(resourceAccountTeamProjectDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -64,107 +65,103 @@ migration guide for more information: https://aiven.io/docs/tools/terraform/howt
}
}

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

func resourceAccountTeamProjectCreate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
accountID := d.Get("account_id").(string)
teamID := d.Get("team_id").(string)
projectName := d.Get("project_name").(string)
teamType := d.Get("team_type").(string)

err := client.AccountTeamProjects.Create(
if err := client.AccountTeamProjectAssociate(
ctx,
accountID,
teamID,
aiven.AccountTeamProject{
ProjectName: projectName,
TeamType: teamType,
projectName,
&accountteam.AccountTeamProjectAssociateIn{
TeamType: accountteam.TeamType(teamType),
},
)
if err != nil {
return diag.FromErr(err)
); err != nil {
return err
}

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

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

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

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

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

var project aiven.AccountTeamProject
for _, p := range r.Projects {
var project accountteam.ProjectOut
for _, p := range resp {
if p.ProjectName == projectName {
project = p
}
}

if project.ProjectName == "" {
return diag.Errorf("account team project %s not found", d.Id())
return fmt.Errorf("account team project %q not found", d.Id())
}

if err := d.Set("account_id", accountID); err != nil {
return diag.FromErr(err)
if err = d.Set("account_id", accountID); err != nil {
return err
}
if err := d.Set("team_id", teamID); err != nil {
return diag.FromErr(err)
if err = d.Set("team_id", teamID); err != nil {
return err
}
if err := d.Set("project_name", project.ProjectName); err != nil {
return diag.FromErr(err)
if err = d.Set("project_name", project.ProjectName); err != nil {
return err
}
if err := d.Set("team_type", project.TeamType); err != nil {
return diag.FromErr(err)
if err = d.Set("team_type", project.TeamType); err != nil {
return err
}

return nil
}

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

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

newProjectName := d.Get("project_name").(string)
teamType := d.Get("team_type").(string)

err = client.AccountTeamProjects.Update(ctx, accountID, teamID, aiven.AccountTeamProject{
TeamType: teamType,
ProjectName: newProjectName,
})
err = client.AccountTeamProjectAssociationUpdate(
ctx,
accountID,
teamID,
newProjectName,
&accountteam.AccountTeamProjectAssociationUpdateIn{
TeamType: accountteam.TeamType(teamType),
},
)
if err != nil {
return diag.FromErr(err)
return err
}

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

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

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

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

err = client.AccountTeamProjects.Delete(ctx, accountID, teamID, projectName)
err = client.AccountTeamProjectDisassociate(ctx, accountID, teamID, projectName)
if common.IsCritical(err) {
return diag.FromErr(err)
return err
}

return nil
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 DatasourceAccountTeamProject() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceAccountTeamProjectRead,
ReadContext: common.WithGenClient(datasourceAccountTeamProjectRead),
Description: "The Account Team Project data source provides information about the existing Account Team Project.",
Schema: schemautil.ResourceSchemaAsDatasourceSchema(aivenAccountTeamProjectSchema,
"account_id", "team_id", "project_name"),
}
}

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

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

return resourceAccountTeamProjectRead(ctx, d, m)
return resourceAccountTeamProjectRead(ctx, d, client)
}
38 changes: 15 additions & 23 deletions internal/sdkprovider/service/account/account_team_project_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 @@ -79,7 +78,10 @@ data "aiven_account_team_project" "project" {
}

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

ctx := context.Background()

Expand All @@ -94,31 +96,21 @@ func testAccCheckAivenAccountTeamProjectResourceDestroy(s *terraform.State) erro
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 {
rp, err := c.AccountTeamProjects.List(ctx, accountID, teamID)
if err != nil {
var e aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

return nil
for _, a := range resp {
if a.AccountId == accountID {
respTP, err := c.AccountTeamProjectList(ctx, accountID, teamID)
if common.IsCritical(err) {
return err
}

for _, p := range rp.Projects {
for _, p := range respTP {
if p.ProjectName == projectName {
return fmt.Errorf("account team project (%s) still exists", rs.Primary.ID)
return fmt.Errorf("account team project (%q) still exists", rs.Primary.ID)
}
}
}
Expand Down
Loading