Skip to content

Commit

Permalink
refactor: replaced client-v2 with avngen in organization_user_group
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav committed Dec 5, 2024
1 parent ddb4ac3 commit 55257f4
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 @@ -2,8 +2,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 +52,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 +65,56 @@ 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
)

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

req.UserGroupName = d.Get("name").(string)

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 = d.Set("name", resp.UserGroupName); err != nil {
return err
}
if err = d.Set("description", resp.Description); err != nil {
return err
}
if err = d.Set("create_time", resp.CreateTime.String()); err != nil {
return err
}
if err = d.Set("update_time", resp.UpdateTime.String()); err != nil {
return err
}
if err = d.Set("group_id", resp.UserGroupId); 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 55257f4

Please sign in to comment.