diff --git a/internal/sdkprovider/service/organization/organization_user_group.go b/internal/sdkprovider/service/organization/organization_user_group.go index 5efb4f2bb..475e87130 100644 --- a/internal/sdkprovider/service/organization/organization_user_group.go +++ b/internal/sdkprovider/service/organization/organization_user_group.go @@ -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" @@ -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, }, @@ -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 { @@ -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 diff --git a/internal/sdkprovider/service/organization/organization_user_group_test.go b/internal/sdkprovider/service/organization/organization_user_group_test.go index 3023bf72e..e39699200 100644 --- a/internal/sdkprovider/service/organization/organization_user_group_test.go +++ b/internal/sdkprovider/service/organization/organization_user_group_test.go @@ -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 { @@ -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 {