Skip to content

Commit

Permalink
Merge pull request #111 from auth0/patch/DXCDT-80-role-rule
Browse files Browse the repository at this point in the history
DXCDT-80 Stop ignoring errors when setting resource data within the role and rule
  • Loading branch information
sergiught authored Mar 31, 2022
2 parents 0157942 + 4b527f4 commit 5c5bac3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 80 deletions.
66 changes: 33 additions & 33 deletions auth0/resource_auth0_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import (

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

func newRole() *schema.Resource {
return &schema.Resource{

Create: createRole,
Update: updateRole,
Read: readRole,
Delete: deleteRole,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -56,33 +55,33 @@ func newRole() *schema.Resource {
}

func createRole(d *schema.ResourceData, m interface{}) error {

c := expandRole(d)
role := expandRole(d)
api := m.(*management.Management)
if err := api.Role.Create(c); err != nil {
if err := api.Role.Create(role); err != nil {
return err
}
d.SetId(auth0.StringValue(c.ID))

d.SetId(auth0.StringValue(role.ID))

// Enable partial state mode. Sub-resources can potentially cause partial
// state. Therefore we must explicitly tell Terraform what is safe to
// state. Therefore, we must explicitly tell Terraform what is safe to
// persist and what is not.
//
// See: https://www.terraform.io/docs/extend/writing-custom-providers.html
d.Partial(true)
if err := assignRolePermissions(d, m); err != nil {
return err
}
// We succeeded, disable partial mode. This causes Terraform to save
// all fields again.
// We succeeded, disable partial mode.
// This causes Terraform to save all fields again.
d.Partial(false)

return readRole(d, m)
}

func readRole(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
c, err := api.Role.Read(d.Id())
role, err := api.Role.Read(d.Id())
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
Expand All @@ -93,59 +92,62 @@ func readRole(d *schema.ResourceData, m interface{}) error {
return err
}

d.SetId(c.GetID())
d.Set("name", c.Name)
d.Set("description", c.Description)
d.SetId(role.GetID())

var permissions []*management.Permission
result := multierror.Append(
d.Set("name", role.Name),
d.Set("description", role.Description),
)

var permissions []*management.Permission
var page int
for {
l, err := api.Role.Permissions(d.Id(), management.Page(page))
permissionList, err := api.Role.Permissions(d.Id(), management.Page(page))
if err != nil {
return err
}
for _, permission := range l.Permissions {
for _, permission := range permissionList.Permissions {
permissions = append(permissions, permission)
}
if !l.HasNext() {
if !permissionList.HasNext() {
break
}
page++
}

d.Set("permissions", flattenRolePermissions(permissions))
result = multierror.Append(result, d.Set("permissions", flattenRolePermissions(permissions)))

return nil
return result.ErrorOrNil()
}

func updateRole(d *schema.ResourceData, m interface{}) error {
c := expandRole(d)
role := expandRole(d)
api := m.(*management.Management)
err := api.Role.Update(d.Id(), c)
if err != nil {
if err := api.Role.Update(d.Id(), role); err != nil {
return err
}

d.Partial(true)
if err := assignRolePermissions(d, m); err != nil {
return err
}
d.Partial(false)

return readRole(d, m)
}

func deleteRole(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
err := api.Role.Delete(d.Id())
if err != nil {
if err := api.Role.Delete(d.Id()); err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
}
}
return err

return nil
}

func expandRole(d *schema.ResourceData) *management.Role {
Expand All @@ -156,7 +158,6 @@ func expandRole(d *schema.ResourceData) *management.Role {
}

func assignRolePermissions(d *schema.ResourceData, m interface{}) error {

add, rm := Diff(d, "permissions")

var addPermissions []*management.Permission
Expand All @@ -180,30 +181,29 @@ func assignRolePermissions(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)

if len(rmPermissions) > 0 {
err := api.Role.RemovePermissions(d.Id(), rmPermissions)
if err != nil {
if err := api.Role.RemovePermissions(d.Id(), rmPermissions); err != nil {
return err
}
}

if len(addPermissions) > 0 {
err := api.Role.AssociatePermissions(d.Id(), addPermissions)
if err != nil {
if err := api.Role.AssociatePermissions(d.Id(), addPermissions); err != nil {
return err
}
}

d.SetPartial("permissions")

return nil
}

func flattenRolePermissions(permissions []*management.Permission) []interface{} {
var v []interface{}
var result []interface{}
for _, permission := range permissions {
v = append(v, map[string]interface{}{
result = append(result, map[string]interface{}{
"name": permission.Name,
"resource_server_identifier": permission.ResourceServerIdentifier,
})
}
return v
return result
}
6 changes: 0 additions & 6 deletions auth0/resource_auth0_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func init() {
}

func TestAccRole(t *testing.T) {

rand := random.String(6)

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -78,7 +77,6 @@ func TestAccRole(t *testing.T) {
}

const testAccRoleAux = `
resource auth0_resource_server matrix {
name = "Role - Acceptance Test - {{.random}}"
identifier = "https://{{.random}}.matrix.com/"
Expand All @@ -93,7 +91,6 @@ resource auth0_resource_server matrix {
}`

const testAccRoleCreate = testAccRoleAux + `
resource auth0_role the_one {
name = "The One - Acceptance Test - {{.random}}"
description = "The One - Acceptance Test"
Expand All @@ -105,7 +102,6 @@ resource auth0_role the_one {
`

const testAccRoleUpdate = testAccRoleAux + `
resource auth0_role the_one {
name = "The One - Acceptance Test - {{.random}}"
description = "The One who will bring peace - Acceptance Test"
Expand All @@ -121,7 +117,6 @@ resource auth0_role the_one {
`

func TestAccRolePermissions(t *testing.T) {

rand := random.String(6)

resource.Test(t, resource.TestCase{
Expand All @@ -142,7 +137,6 @@ func TestAccRolePermissions(t *testing.T) {
}

const testAccRolePermissions = `
locals {
permissions = {
"permission:1" = "Permission 1"
Expand Down
38 changes: 21 additions & 17 deletions auth0/resource_auth0_rule.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/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
Expand All @@ -14,15 +15,13 @@ var ruleNameRegexp = regexp.MustCompile("^[^\\s-][\\w -]+[^\\s-]$")

func newRule() *schema.Resource {
return &schema.Resource{

Create: createRule,
Read: readRule,
Update: updateRule,
Delete: deleteRule,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -50,18 +49,20 @@ func newRule() *schema.Resource {
}

func createRule(d *schema.ResourceData, m interface{}) error {
c := buildRule(d)
rule := buildRule(d)
api := m.(*management.Management)
if err := api.Rule.Create(c); err != nil {
if err := api.Rule.Create(rule); err != nil {
return err
}
d.SetId(auth0.StringValue(c.ID))

d.SetId(auth0.StringValue(rule.ID))

return readRule(d, m)
}

func readRule(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
c, err := api.Rule.Read(d.Id())
rule, err := api.Rule.Read(d.Id())
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
Expand All @@ -72,27 +73,29 @@ func readRule(d *schema.ResourceData, m interface{}) error {
return err
}

d.Set("name", c.Name)
d.Set("script", c.Script)
d.Set("order", c.Order)
d.Set("enabled", c.Enabled)
return nil
result := multierror.Append(
d.Set("name", rule.Name),
d.Set("script", rule.Script),
d.Set("order", rule.Order),
d.Set("enabled", rule.Enabled),
)

return result.ErrorOrNil()
}

func updateRule(d *schema.ResourceData, m interface{}) error {
c := buildRule(d)
rule := buildRule(d)
api := m.(*management.Management)
err := api.Rule.Update(d.Id(), c)
if err != nil {
if err := api.Rule.Update(d.Id(), rule); err != nil {
return err
}

return readRule(d, m)
}

func deleteRule(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
err := api.Rule.Delete(d.Id())
if err != nil {
if err := api.Rule.Delete(d.Id()); err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
Expand All @@ -101,7 +104,8 @@ func deleteRule(d *schema.ResourceData, m interface{}) error {
}
return err
}
return err

return nil
}

func buildRule(d *schema.ResourceData) *management.Rule {
Expand Down
Loading

0 comments on commit 5c5bac3

Please sign in to comment.