Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import issue on auth0_role_permissions #597

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions internal/auth0/role/resource_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ 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"
"github.com/auth0/terraform-provider-auth0/internal/value"
)

// NewPermissionsResource will return a new auth0_role_permissions resource.
// NewPermissionsResource will return a new auth0_role_permissions (1:many) resource.
func NewPermissionsResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -117,9 +118,7 @@ func upsertRolePermissions(ctx context.Context, data *schema.ResourceData, meta
func readRolePermissions(_ context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*config.Config).GetAPI()

roleID := data.Get("role_id").(string)

permissions, err := api.Role.Permissions(roleID)
permissions, err := api.Role.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 readRolePermissions(_ context.Context, data *schema.ResourceData, meta inte
return diag.FromErr(err)
}

err = data.Set("permissions", flattenRolePermissions(permissions.Permissions))
result := multierror.Append(
data.Set("role_id", data.Id()),
data.Set("permissions", flattenRolePermissions(permissions.Permissions)),
)

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

func deleteRolePermissions(_ context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
90 changes: 90 additions & 0 deletions internal/auth0/role/resource_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package role_test

import (
"fmt"
"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,91 @@ func TestAccRolePermissions(t *testing.T) {
},
})
}

const testAccRolePermissionsImport = `
resource "auth0_resource_server" "resource_server" {
name = "Acceptance Test - {{.testName}}"
identifier = "https://uat.{{.testName}}.terraform-provider-auth0.com/api"

scopes {
value = "read:foo"
description = "Can read Foo"
}

scopes {
value = "create:foo"
description = "Can create Foo"
}
}

resource "auth0_role" "role" {
depends_on = [ auth0_resource_server.resource_server ]

name = "Acceptance Test - {{.testName}}"
description = "Acceptance Test Role - {{.testName}}"

lifecycle {
ignore_changes = [ permissions ]
}
}

resource "auth0_role_permissions" "role_permissions" {
role_id = auth0_role.role.id

permissions {
resource_server_identifier = auth0_resource_server.resource_server.identifier
name = "create:foo"
}

permissions {
resource_server_identifier = auth0_resource_server.resource_server.identifier
name = "read:foo"
}
}
`

func TestAccRolePermissionsImport(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(testAccRolePermissionsImport, testName),
ResourceName: "auth0_resource_server.resource_server",
ImportState: true,
ImportStateId: "646cbf5e8ffa2057b6112389",
ImportStatePersist: true,
},
{
Config: acctest.ParseTestName(testAccRolePermissionsImport, testName),
ResourceName: "auth0_role.role",
ImportState: true,
ImportStateId: "rol_y36rul7VuBQGVpNa",
ImportStatePersist: true,
},
{
Config: acctest.ParseTestName(testAccRolePermissionsImport, testName),
ResourceName: "auth0_role_permissions.role_permissions",
ImportState: true,
ImportStateId: "rol_y36rul7VuBQGVpNa",
ImportStatePersist: true,
},
{
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
Config: acctest.ParseTestName(testAccRolePermissionsImport, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_role_permissions.role_permissions", "permissions.#", "2"),
),
},
},
})
}
Loading