Skip to content

Commit

Permalink
Fix import issue on auth0_user_permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed May 23, 2023
1 parent f6d517e commit a6c41d5
Show file tree
Hide file tree
Showing 3 changed files with 785 additions and 5 deletions.
12 changes: 7 additions & 5 deletions internal/auth0/user/resource_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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"

Expand Down Expand Up @@ -117,9 +118,7 @@ func upsertUserPermissions(ctx context.Context, data *schema.ResourceData, meta
func readUserPermissions(_ context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*config.Config).GetAPI()

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

permissions, err := api.User.Permissions(userID)
permissions, err := api.User.Permissions(data.Id())
if err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
data.SetId("")
Expand All @@ -128,9 +127,12 @@ func readUserPermissions(_ context.Context, data *schema.ResourceData, meta inte
return diag.FromErr(err)
}

err = data.Set("permissions", flattenUserPermissions(permissions))
result := multierror.Append(
data.Set("user_id", data.Id()),
data.Set("permissions", flattenUserPermissions(permissions)),
)

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

func deleteUserPermissions(_ context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
93 changes: 93 additions & 0 deletions internal/auth0/user/resource_permissions_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 @@ -111,3 +113,94 @@ func TestAccUserPermissions(t *testing.T) {
},
})
}

const testAccUserPermissionsImport = `
resource "auth0_resource_server" "resource_server" {
name = "Acceptance Test - {{.testName}}"
identifier = "https://uat.api.terraform-provider-auth0.com/{{.testName}}"
scopes {
value = "read:foo"
description = "Can read Foo"
}
scopes {
value = "create:foo"
description = "Can create Foo"
}
}
resource "auth0_user" "user" {
depends_on = [ auth0_resource_server.resource_server ]
connection_name = "Username-Password-Authentication"
password = "passpass$12$12"
email = "{{.testName}}@acceptance.test.com"
lifecycle {
ignore_changes = [ connection_name, password ]
}
}
resource "auth0_user_permissions" "user_permissions" {
depends_on = [ auth0_resource_server.resource_server, auth0_user.user ]
user_id = auth0_user.user.id
permissions {
resource_server_identifier = auth0_resource_server.resource_server.identifier
name = "read:foo"
}
permissions {
resource_server_identifier = auth0_resource_server.resource_server.identifier
name = "create:foo"
}
}
`

func TestAccUserPermissionsImport(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(testAccUserPermissionsImport, testName),
ResourceName: "auth0_resource_server.resource_server",
ImportState: true,
ImportStateId: "646cad063390a55e156ee4cd",
ImportStatePersist: true,
},
{
Config: acctest.ParseTestName(testAccUserPermissionsImport, testName),
ResourceName: "auth0_user.user",
ImportState: true,
ImportStateId: "auth0|646cad06363aac78e30fc478",
ImportStatePersist: true,
},
{
Config: acctest.ParseTestName(testAccUserPermissionsImport, testName),
ResourceName: "auth0_user_permissions.user_permissions",
ImportState: true,
ImportStateId: "auth0|646cad06363aac78e30fc478",
ImportStatePersist: true,
},
{
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
Config: acctest.ParseTestName(testAccUserPermissionsImport, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_user_permissions.user_permissions", "permissions.#", "2"),
),
},
},
})
}
Loading

0 comments on commit a6c41d5

Please sign in to comment.