Skip to content

Commit

Permalink
refactor(OrganizationUserGroup): replaced client-v2 with avngen
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav committed Dec 5, 2024
1 parent ddb4ac3 commit 69188ca
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 37 deletions.
101 changes: 67 additions & 34 deletions internal/sdkprovider/service/organization/organization_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package organization
import (
"context"

"github.com/aiven/go-client-codegen/handler/usergroup"
"github.com/aiven/terraform-provider-aiven/internal/common"

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

Expand Down Expand Up @@ -49,10 +53,10 @@ var aivenOrganizationUserGroupSchema = map[string]*schema.Schema{
func ResourceOrganizationUserGroup() *schema.Resource {
return &schema.Resource{
Description: "Creates and manages a [user group](https://aiven.io/docs/platform/howto/list-groups) in an organization.",
CreateContext: resourceOrganizationUserGroupCreate,
ReadContext: resourceOrganizationUserGroupRead,
UpdateContext: resourceOrganizationUserGroupUpdate,
DeleteContext: resourceOrganizationUserGroupDelete,
CreateContext: common.WithGenClient(resourceOrganizationUserGroupCreate),
ReadContext: common.WithGenClient(resourceOrganizationUserGroupReadGen),
UpdateContext: common.WithGenClient(resourceOrganizationUserGroupUpdate),
DeleteContext: common.WithGenClient(resourceOrganizationUserGroupDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -62,25 +66,55 @@ func ResourceOrganizationUserGroup() *schema.Resource {
}
}

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

orgID := d.Get("organization_id").(string)
r, err := client.OrganizationUserGroups.Create(
ctx,
orgID,
aiven.OrganizationUserGroupRequest{
UserGroupName: d.Get("name").(string),
Description: d.Get("description").(string),
},
func resourceOrganizationUserGroupCreate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
var (
orgID = d.Get("organization_id").(string)
req usergroup.UserGroupCreateIn
)

// replace the key in terraform with the correct key in the API
if err := schemautil.ResourceDataGet(
d,
&req,
schemautil.RenameAliases(map[string]string{"name": "user_group_name"}),
); err != nil {
return err
}

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

d.SetId(schemautil.BuildResourceID(orgID, r.UserGroupID))
d.SetId(schemautil.BuildResourceID(orgID, resp.UserGroupId))

return resourceOrganizationUserGroupRead(ctx, d, m)
return resourceOrganizationUserGroupReadGen(ctx, d, client)
}

func resourceOrganizationUserGroupReadGen(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
orgID, userGroupID, err := schemautil.SplitResourceID2(d.Id())
if err != nil {
return err
}

resp, err := client.UserGroupGet(ctx, orgID, userGroupID)
if err != nil {
return err
}

if err = schemautil.ResourceDataSet(
aivenOrganizationUserGroupSchema,
d,
resp,
schemautil.RenameAliases(map[string]string{
"user_group_name": "name",
"user_group_id": "group_id",
}),
); err != nil {
return err
}

return nil
}

func resourceOrganizationUserGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -115,35 +149,34 @@ func resourceOrganizationUserGroupRead(ctx context.Context, d *schema.ResourceDa
return nil
}

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

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

_, err = client.OrganizationUserGroups.Update(ctx, orgID, userGroupID, aiven.OrganizationUserGroupRequest{
UserGroupName: d.Get("name").(string),
Description: d.Get("description").(string),
})
var req usergroup.UserGroupUpdateIn

if err = schemautil.ResourceDataGet(d, &req); err != nil {
return err
}

_, err = client.UserGroupUpdate(ctx, orgID, userGroupID, &req)
if err != nil {
return diag.FromErr(err)
return err
}

return resourceOrganizationUserGroupRead(ctx, d, m)
return resourceOrganizationUserGroupReadGen(ctx, d, client)
}

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

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

if err = client.OrganizationUserGroups.Delete(ctx, orgID, userGroupID); err != nil && !aiven.IsNotFound(err) {
return diag.FromErr(err)
if err = client.UserGroupDelete(ctx, orgID, userGroupID); err != nil {
return err
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ data "aiven_organization_user_group" "bar" {
}

func testAccCheckAivenOrganizationUserGroupResourceDestroy(s *terraform.State) error {
c := acc.GetTestAivenClient()
var (
c, err = acc.GetTestGenAivenClient()
ctx = context.Background()
)

ctx := context.Background()
if err != nil {
return fmt.Errorf("failed to instantiate GenAiven client: %w", err)
}

// loop through the resources in state, verifying each organization user group is destroyed
for _, rs := range s.RootModule().Resources {
Expand All @@ -75,7 +80,7 @@ func testAccCheckAivenOrganizationUserGroupResourceDestroy(s *terraform.State) e
return err
}

r, err := c.OrganizationUserGroups.Get(ctx, orgID, userGroupID)
r, err := c.UserGroupGet(ctx, orgID, userGroupID)
if err != nil {
var e aiven.Error
if errors.As(err, &e) && e.Status != 404 {
Expand Down

0 comments on commit 69188ca

Please sign in to comment.