Skip to content

Commit

Permalink
Fix import issue on auth0_user_roles (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught authored May 24, 2023
1 parent 37e779a commit 218e02c
Show file tree
Hide file tree
Showing 3 changed files with 1,104 additions and 8 deletions.
16 changes: 8 additions & 8 deletions internal/auth0/user/resource_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

"github.com/auth0/go-auth0"
"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/auth0/terraform-provider-auth0/internal/config"
)

// NewRolesResource will returns a new auth0_user_roles (1:many) resource.
// NewRolesResource will return a new auth0_user_roles (1:many) resource.
func NewRolesResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -63,9 +64,7 @@ func upsertUserRoles(ctx context.Context, data *schema.ResourceData, meta interf
func readUserRoles(_ context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*config.Config).GetAPI()

userID := data.Get("user_id").(string)

rolesList, err := api.User.Roles(userID)
rolesList, err := api.User.Roles(data.Id())
if err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
data.SetId("")
Expand All @@ -80,11 +79,12 @@ func readUserRoles(_ context.Context, data *schema.ResourceData, meta interface{
userRoles = append(userRoles, role.GetID())
}

if err := data.Set("roles", userRoles); err != nil {
return diag.FromErr(err)
}
result := multierror.Append(
data.Set("user_id", data.Id()),
data.Set("roles", userRoles),
)

return nil
return diag.FromErr(result.ErrorOrNil())
}

func deleteUserRoles(_ context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
86 changes: 86 additions & 0 deletions internal/auth0/user/resource_roles_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package user_test

import (
"os"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"

"github.com/auth0/terraform-provider-auth0/internal/acctest"
)
Expand Down Expand Up @@ -153,3 +155,87 @@ func TestAccUserRoles(t *testing.T) {
},
})
}

const testAccUserRolesImport = `
resource auth0_role owner {
name = "owner"
description = "Owner"
}
resource auth0_role admin {
name = "admin"
description = "Administrator"
}
resource auth0_user user {
depends_on = [auth0_role.owner, auth0_role.admin]
connection_name = "Username-Password-Authentication"
email = "{{.testName}}@acceptance.test.com"
password = "passpass$12$12"
lifecycle {
ignore_changes = [ roles, connection_name, password ]
}
}
resource auth0_user_roles user_roles {
depends_on = [ auth0_user.user ]
user_id = auth0_user.user.id
roles = [ auth0_role.owner.id, auth0_role.admin.id ]
}
`

func TestAccUserRolesImport(t *testing.T) {
if os.Getenv("AUTH0_DOMAIN") != acctest.RecordingsDomain {
// The test runs only with recordings as it requires an initial setup.
t.Skip()
}

testName := strings.ToLower(t.Name())

acctest.Test(t, resource.TestCase{
Steps: []resource.TestStep{
{
Config: acctest.ParseTestName(testAccUserRolesImport, testName),
ResourceName: "auth0_role.owner",
ImportState: true,
ImportStateId: "rol_XLLMqPwfx8kdG63e",
ImportStatePersist: true,
},
{
Config: acctest.ParseTestName(testAccUserRolesImport, testName),
ResourceName: "auth0_role.admin",
ImportState: true,
ImportStateId: "rol_LjLyGzVZE5K34IaY",
ImportStatePersist: true,
},
{
Config: acctest.ParseTestName(testAccUserRolesImport, testName),
ResourceName: "auth0_user.user",
ImportState: true,
ImportStateId: "auth0|646cbae1e37ebde3a5ad6fd0",
ImportStatePersist: true,
},
{
Config: acctest.ParseTestName(testAccUserRolesImport, testName),
ResourceName: "auth0_user_roles.user_roles",
ImportState: true,
ImportStateId: "auth0|646cbae1e37ebde3a5ad6fd0",
ImportStatePersist: true,
},
{
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
Config: acctest.ParseTestName(testAccUserRolesImport, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_user_roles.user_roles", "roles.#", "2"),
),
},
},
})
}
Loading

0 comments on commit 218e02c

Please sign in to comment.