Skip to content

Commit

Permalink
refactor(organization_user_group): replaced client-v2 with avngen
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav committed Dec 6, 2024
1 parent ddb4ac3 commit 2c8aa60
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 72 deletions.
111 changes: 55 additions & 56 deletions internal/sdkprovider/service/organization/organization_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package organization
import (
"context"

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/aiven/go-client-codegen/handler/usergroup"
"github.com/aiven/terraform-provider-aiven/internal/common"

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

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
Expand Down Expand Up @@ -49,10 +51,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(resourceOrganizationUserGroupRead),
UpdateContext: common.WithGenClient(resourceOrganizationUserGroupUpdate),
DeleteContext: common.WithGenClient(resourceOrganizationUserGroupDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -62,88 +64,85 @@ 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 resourceOrganizationUserGroupRead(ctx, d, client)
}

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

func resourceOrganizationUserGroupRead(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
}

r, err := client.OrganizationUserGroups.Get(ctx, orgID, userGroupID)
resp, err := client.UserGroupGet(ctx, orgID, userGroupID)
if err != nil {
return diag.FromErr(schemautil.ResourceReadHandleNotFound(err, d))
return err
}

if err := d.Set("name", r.UserGroupName); err != nil {
return diag.FromErr(err)
}
if err := d.Set("description", r.Description); err != nil {
return diag.FromErr(err)
}
if err := d.Set("create_time", r.CreateTime.String()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("update_time", r.UpdateTime.String()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("group_id", r.UserGroupID); err != nil {
return diag.FromErr(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 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 resourceOrganizationUserGroupRead(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 @@ -2,40 +2,44 @@ package organization

import (
"context"
"fmt"

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/aiven/terraform-provider-aiven/internal/common"

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

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
)

func DatasourceOrganizationUserGroup() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceOrganizationUserGroupRead,
ReadContext: common.WithGenClient(datasourceOrganizationUserGroupRead),
Description: "Gets information about an existing user group in an organization.",
Schema: schemautil.ResourceSchemaAsDatasourceSchema(
aivenOrganizationUserGroupSchema, "organization_id", "name",
),
}
}

func datasourceOrganizationUserGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
organizationID := d.Get("organization_id").(string)
name := d.Get("name").(string)
func datasourceOrganizationUserGroupRead(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
var (
organizationID = d.Get("organization_id").(string)
name = d.Get("name").(string)
)

client := m.(*aiven.Client)
list, err := client.OrganizationUserGroups.List(ctx, organizationID)
list, err := client.UserGroupsList(ctx, organizationID)
if err != nil {
return diag.FromErr(err)
return err
}

for _, ug := range list.UserGroups {
for _, ug := range list {
if ug.UserGroupName == name {
d.SetId(schemautil.BuildResourceID(organizationID, ug.UserGroupID))
return resourceOrganizationUserGroupRead(ctx, d, m)
d.SetId(schemautil.BuildResourceID(organizationID, ug.UserGroupId))

return resourceOrganizationUserGroupRead(ctx, d, client)
}
}

return diag.Errorf("organization user group %s not found", name)
return fmt.Errorf("organization user group %s not found", name)
}
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 2c8aa60

Please sign in to comment.