Skip to content

Commit

Permalink
fix(aiven_organization_user_group): fixed resource import (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav authored Feb 7, 2025
1 parent d17b833 commit f194109
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func resourceOrganizationUserGroupRead(ctx context.Context, d *schema.ResourceDa
return err
}

// set the organization_id directly as it is not returned by the API and may not be set in case of import
if err = d.Set("organization_id", orgID); err != nil {
return err
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"errors"
"fmt"
"log"
"os"
"testing"

"github.com/aiven/aiven-go-client/v2"
"github.com/aiven/go-client-codegen/handler/usergroup"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

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

Expand All @@ -31,6 +34,7 @@ func TestAccAivenOrganizationUserGroup_basic(t *testing.T) {
testAccCheckAivenOrganizationUserGroupAttributes("data.aiven_organization_user_group.bar"),
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("test-acc-u-grp-%s", rName)),
resource.TestCheckResourceAttrSet(resourceName, "organization_id"),
resource.TestCheckResourceAttrSet(resourceName, "group_id"),
resource.TestCheckResourceAttr(resourceName, "description", "test"),
resource.TestCheckResourceAttrSet(resourceName, "create_time"),
resource.TestCheckResourceAttrSet(resourceName, "update_time"),
Expand Down Expand Up @@ -128,3 +132,78 @@ func testAccCheckAivenOrganizationUserGroupAttributes(n string) resource.TestChe
return nil
}
}

func TestAccAivenOrganizationUserGroup_Import(t *testing.T) {
var (
resourceName = "aiven_organization_user_group.import_group"
rName = acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
orgID string
groupID string
)

orgID = os.Getenv("AIVEN_ORG_ID")
if orgID == "" {
t.Skip("Skipping test due to missing AIVEN_ORG_ID environment variable")
}

// create organization and group before test
c, err := acc.GetTestGenAivenClient()
if err != nil {
t.Fatalf("failed to get client: %s", err)
}

userGroup, err := c.UserGroupCreate(context.Background(), orgID, &usergroup.UserGroupCreateIn{
Description: "Imported group",
UserGroupName: fmt.Sprintf("test-acc-import-group-%s", rName),
})
if err != nil {
t.Fatalf("failed to create user group: %s", err)
}
groupID = userGroup.UserGroupId

// cleanup the user group in case of failure
t.Cleanup(func() {
if err = c.UserGroupDelete(context.Background(), orgID, groupID); common.IsCritical(err) {
t.Errorf("failed to delete user group: %s", err)
}
})

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acc.TestProtoV6ProviderFactories,
CheckDestroy: testAccCheckAivenOrganizationUserGroupResourceDestroy,
Steps: []resource.TestStep{
// first verify that plan is empty
{
Config: testAccOrganizationUserGroupImportConfig(orgID, fmt.Sprintf("test-acc-import-group-%s", rName), groupID),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
// then actually perform the import and verify attributes
{
Config: testAccOrganizationUserGroupImportConfig(orgID, fmt.Sprintf("test-acc-import-group-%s", rName), groupID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("test-acc-import-group-%s", rName)),
resource.TestCheckResourceAttr(resourceName, "description", "Imported group"),
resource.TestCheckResourceAttr(resourceName, "organization_id", orgID),
resource.TestCheckResourceAttr(resourceName, "group_id", groupID),
),
},
},
})
}

func testAccOrganizationUserGroupImportConfig(orgID, name, groupID string) string {
return fmt.Sprintf(`
resource "aiven_organization_user_group" "import_group" {
organization_id = "%s"
name = "%s"
description = "Imported group"
}
import {
id = "%s/%s"
to = aiven_organization_user_group.import_group
}
`, orgID, name, orgID, groupID)
}

0 comments on commit f194109

Please sign in to comment.