From 7418ea7b29176287dfbef1627d31423e6f7f2a70 Mon Sep 17 00:00:00 2001 From: Myroslav Vivcharyk Date: Fri, 6 Dec 2024 13:41:39 +0100 Subject: [PATCH] refactor(organization_user_group): replaced client-v2 with avngen (#1930) --- .../organization/organization_user_group.go | 110 +++++++++--------- .../organization_user_group_data_source.go | 30 ++--- .../organization_user_group_test.go | 11 +- 3 files changed, 79 insertions(+), 72 deletions(-) diff --git a/internal/sdkprovider/service/organization/organization_user_group.go b/internal/sdkprovider/service/organization/organization_user_group.go index 5efb4f2bb..6bd0c271f 100644 --- a/internal/sdkprovider/service/organization/organization_user_group.go +++ b/internal/sdkprovider/service/organization/organization_user_group.go @@ -3,10 +3,11 @@ package organization import ( "context" - "github.com/aiven/aiven-go-client/v2" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + avngen "github.com/aiven/go-client-codegen" + "github.com/aiven/go-client-codegen/handler/usergroup" "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" "github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig" ) @@ -49,10 +50,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, }, @@ -62,88 +63,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 diff --git a/internal/sdkprovider/service/organization/organization_user_group_data_source.go b/internal/sdkprovider/service/organization/organization_user_group_data_source.go index a69fe9183..0019b8d8a 100644 --- a/internal/sdkprovider/service/organization/organization_user_group_data_source.go +++ b/internal/sdkprovider/service/organization/organization_user_group_data_source.go @@ -2,9 +2,11 @@ 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" @@ -12,7 +14,7 @@ import ( 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", @@ -20,22 +22,24 @@ func DatasourceOrganizationUserGroup() *schema.Resource { } } -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) } 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 {