Skip to content

Commit

Permalink
Refactor rule_config resource
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Mar 31, 2022
1 parent e2f9bfe commit 4b527f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
34 changes: 17 additions & 17 deletions auth0/resource_auth0_rule_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import (

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

Create: createRuleConfig,
Read: readRuleConfig,
Update: updateRuleConfig,
Delete: deleteRuleConfig,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Expand All @@ -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 {
Expand All @@ -58,33 +58,33 @@ 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("")
return nil
}
}
}
return err

return nil
}

func buildRuleConfig(d *schema.ResourceData) *management.RuleConfig {
Expand Down
4 changes: 0 additions & 4 deletions auth0/resource_auth0_rule_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func init() {
}

func TestAccRuleConfig(t *testing.T) {

rand := random.String(4)

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -76,23 +75,20 @@ func TestAccRuleConfig(t *testing.T) {
}

const testAccRuleConfigCreate = `
resource "auth0_rule_config" "foo" {
key = "acc_test_{{.random}}"
value = "bar"
}
`

const testAccRuleConfigUpdateValue = `
resource "auth0_rule_config" "foo" {
key = "acc_test_{{.random}}"
value = "foo"
}
`

const testAccRuleConfigUpdateKey = `
resource "auth0_rule_config" "foo" {
key = "acc_test_key_{{.random}}"
value = "foo"
Expand Down

0 comments on commit 4b527f4

Please sign in to comment.