diff --git a/auth0/resource_auth0_role.go b/auth0/resource_auth0_role.go index e9e048262..63a134cbd 100644 --- a/auth0/resource_auth0_role.go +++ b/auth0/resource_auth0_role.go @@ -5,12 +5,12 @@ 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, @@ -18,7 +18,6 @@ func newRole() *schema.Resource { Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, - Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -56,16 +55,16 @@ 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 @@ -73,8 +72,8 @@ func createRole(d *schema.ResourceData, m interface{}) error { 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) @@ -82,7 +81,7 @@ func createRole(d *schema.ResourceData, m interface{}) error { 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 { @@ -93,51 +92,53 @@ 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("") @@ -145,7 +146,8 @@ func deleteRole(d *schema.ResourceData, m interface{}) error { } } } - return err + + return nil } func expandRole(d *schema.ResourceData) *management.Role { @@ -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 @@ -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 } diff --git a/auth0/resource_auth0_role_test.go b/auth0/resource_auth0_role_test.go index 96a238635..842b49fd2 100644 --- a/auth0/resource_auth0_role_test.go +++ b/auth0/resource_auth0_role_test.go @@ -50,7 +50,6 @@ func init() { } func TestAccRole(t *testing.T) { - rand := random.String(6) resource.Test(t, resource.TestCase{ @@ -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/" @@ -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" @@ -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" @@ -121,7 +117,6 @@ resource auth0_role the_one { ` func TestAccRolePermissions(t *testing.T) { - rand := random.String(6) resource.Test(t, resource.TestCase{ @@ -142,7 +137,6 @@ func TestAccRolePermissions(t *testing.T) { } const testAccRolePermissions = ` - locals { permissions = { "permission:1" = "Permission 1" diff --git a/auth0/resource_auth0_rule.go b/auth0/resource_auth0_rule.go index b4e10e7e8..cdb492746 100644 --- a/auth0/resource_auth0_rule.go +++ b/auth0/resource_auth0_rule.go @@ -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" ) @@ -14,7 +15,6 @@ var ruleNameRegexp = regexp.MustCompile("^[^\\s-][\\w -]+[^\\s-]$") func newRule() *schema.Resource { return &schema.Resource{ - Create: createRule, Read: readRule, Update: updateRule, @@ -22,7 +22,6 @@ func newRule() *schema.Resource { Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, - Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -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 { @@ -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("") @@ -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 { diff --git a/auth0/resource_auth0_rule_config.go b/auth0/resource_auth0_rule_config.go index b8103a965..df30056d0 100644 --- a/auth0/resource_auth0_rule_config.go +++ b/auth0/resource_auth0_rule_config.go @@ -10,7 +10,6 @@ import ( func newRuleConfig() *schema.Resource { return &schema.Resource{ - Create: createRuleConfig, Read: readRuleConfig, Update: updateRuleConfig, @@ -18,7 +17,6 @@ func newRuleConfig() *schema.Resource { Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, - Schema: map[string]*schema.Schema{ "key": { Type: schema.TypeString, @@ -35,20 +33,22 @@ func newRuleConfig() *schema.Resource { } func createRuleConfig(d *schema.ResourceData, m interface{}) error { - r := buildRuleConfig(d) - key := auth0.StringValue(r.Key) - r.Key = nil + ruleConfig := buildRuleConfig(d) + key := auth0.StringValue(ruleConfig.Key) + ruleConfig.Key = nil api := m.(*management.Management) - if err := api.RuleConfig.Upsert(key, r); err != nil { + if err := api.RuleConfig.Upsert(key, ruleConfig); err != nil { return err } - d.SetId(auth0.StringValue(r.Key)) + + d.SetId(auth0.StringValue(ruleConfig.Key)) + return readRuleConfig(d, m) } func readRuleConfig(d *schema.ResourceData, m interface{}) error { api := m.(*management.Management) - r, err := api.RuleConfig.Read(d.Id()) + ruleConfig, err := api.RuleConfig.Read(d.Id()) if err != nil { if mErr, ok := err.(management.Error); ok { if mErr.Status() == http.StatusNotFound { @@ -58,25 +58,24 @@ func readRuleConfig(d *schema.ResourceData, m interface{}) error { } return err } - d.Set("key", r.Key) - return nil + + return d.Set("key", ruleConfig.Key) } func updateRuleConfig(d *schema.ResourceData, m interface{}) error { - r := buildRuleConfig(d) - r.Key = nil + ruleConfig := buildRuleConfig(d) + ruleConfig.Key = nil api := m.(*management.Management) - err := api.RuleConfig.Upsert(d.Id(), r) - if err != nil { + if err := api.RuleConfig.Upsert(d.Id(), ruleConfig); err != nil { return err } + return readRuleConfig(d, m) } func deleteRuleConfig(d *schema.ResourceData, m interface{}) error { api := m.(*management.Management) - err := api.RuleConfig.Delete(d.Id()) - if err != nil { + if err := api.RuleConfig.Delete(d.Id()); err != nil { if mErr, ok := err.(management.Error); ok { if mErr.Status() == http.StatusNotFound { d.SetId("") @@ -84,7 +83,8 @@ func deleteRuleConfig(d *schema.ResourceData, m interface{}) error { } } } - return err + + return nil } func buildRuleConfig(d *schema.ResourceData) *management.RuleConfig { diff --git a/auth0/resource_auth0_rule_config_test.go b/auth0/resource_auth0_rule_config_test.go index deb3ad1c0..594dad71d 100644 --- a/auth0/resource_auth0_rule_config_test.go +++ b/auth0/resource_auth0_rule_config_test.go @@ -39,7 +39,6 @@ func init() { } func TestAccRuleConfig(t *testing.T) { - rand := random.String(4) resource.Test(t, resource.TestCase{ @@ -76,7 +75,6 @@ func TestAccRuleConfig(t *testing.T) { } const testAccRuleConfigCreate = ` - resource "auth0_rule_config" "foo" { key = "acc_test_{{.random}}" value = "bar" @@ -84,7 +82,6 @@ resource "auth0_rule_config" "foo" { ` const testAccRuleConfigUpdateValue = ` - resource "auth0_rule_config" "foo" { key = "acc_test_{{.random}}" value = "foo" @@ -92,7 +89,6 @@ resource "auth0_rule_config" "foo" { ` const testAccRuleConfigUpdateKey = ` - resource "auth0_rule_config" "foo" { key = "acc_test_key_{{.random}}" value = "foo" diff --git a/auth0/resource_auth0_rule_test.go b/auth0/resource_auth0_rule_test.go index 1f8b2f9cb..36910d8fa 100644 --- a/auth0/resource_auth0_rule_test.go +++ b/auth0/resource_auth0_rule_test.go @@ -11,7 +11,6 @@ import ( ) func TestAccRule(t *testing.T) { - rand := random.String(6) resource.Test(t, resource.TestCase{ @@ -32,7 +31,6 @@ func TestAccRule(t *testing.T) { } const testAccRule = ` - resource "auth0_rule" "my_rule" { name = "acceptance-test-{{.random}}" script = "function (user, context, callback) { callback(null, user, context); }" @@ -41,7 +39,6 @@ resource "auth0_rule" "my_rule" { ` func TestRuleNameRegexp(t *testing.T) { - vf := validation.StringMatch(ruleNameRegexp, "invalid name") for name, valid := range map[string]bool{