Skip to content

Commit

Permalink
[13/X] Refactor user resource to allow for empty fields (#348)
Browse files Browse the repository at this point in the history
* Refactor user resource to allow for empty fields

* [14/X] Refactor role and rule resources to allow for empty fields (#349)

* Refactor role and rule resources to allow for empty fields

* [15/X] Refactor log stream resource to allow for empty fields (#350)

* Refactor log stream resource to allow for empty fields

* [16/X] Refactor prompts resource to allow for empty fields (#351)

* Refactor prompts resource to allow for empty fields

* [17/X] Refactor hooks resource to allow for empty fields (#352)

Refactor hooks resource to allow for empty fields

* Fix small issue with log stream filters

* Adding user_id back in to tests

Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
sergiught and willvedd authored Oct 7, 2022
1 parent 564ea45 commit 14c0bdd
Show file tree
Hide file tree
Showing 24 changed files with 5,235 additions and 2,791 deletions.
41 changes: 19 additions & 22 deletions internal/provider/resource_auth0_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/auth0/terraform-provider-auth0/internal/value"
)

func newHook() *schema.Resource {
Expand Down Expand Up @@ -79,8 +81,9 @@ func newHook() *schema.Resource {
}

func createHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
hook := expandHook(d)
api := m.(*management.Management)

hook := expandHook(d)
if err := api.Hook.Create(hook); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -181,42 +184,36 @@ func checkForUntrackedHookSecrets(ctx context.Context, d *schema.ResourceData, m

func upsertHookSecrets(ctx context.Context, d *schema.ResourceData, m interface{}) error {
if d.IsNewResource() || d.HasChange("secrets") {
hookSecrets := expandHookSecrets(d)
api := m.(*management.Management)
return api.Hook.ReplaceSecrets(d.Id(), hookSecrets)

hookSecrets := value.MapOfStrings(d.GetRawConfig().GetAttr("secrets"))
if hookSecrets == nil {
return nil
}

return api.Hook.ReplaceSecrets(d.Id(), *hookSecrets)
}

return nil
}

func expandHook(d *schema.ResourceData) *management.Hook {
config := d.GetRawConfig()

hook := &management.Hook{
Name: String(d, "name"),
Script: String(d, "script"),
TriggerID: String(d, "trigger_id", IsNewResource()),
Enabled: Bool(d, "enabled"),
Name: value.String(config.GetAttr("name")),
Script: value.String(config.GetAttr("script")),
Enabled: value.Bool(config.GetAttr("enabled")),
Dependencies: value.MapOfStrings(config.GetAttr("dependencies")),
}

if deps := Map(d, "dependencies"); deps != nil {
hook.Dependencies = &deps
if d.IsNewResource() {
hook.TriggerID = value.String(config.GetAttr("trigger_id"))
}

return hook
}

func expandHookSecrets(d *schema.ResourceData) management.HookSecrets {
hookSecrets := management.HookSecrets{}
secrets := Map(d, "secrets")

for key, value := range secrets {
if strVal, ok := value.(string); ok {
hookSecrets[key] = strVal
}
}

return hookSecrets
}

func validateHookName() schema.SchemaValidateDiagFunc {
hookNameValidation := validation.StringMatch(
regexp.MustCompile(`^[^\s-][\w -]+[^\s-]$`),
Expand Down
35 changes: 35 additions & 0 deletions internal/provider/resource_auth0_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ func TestAccHook(t *testing.T) {
resource.Test(t, resource.TestCase{
ProviderFactories: testProviders(httpRecorder),
Steps: []resource.TestStep{
{
Config: testAccHookEmpty,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_hook.my_hook", "name", "pre-user-reg-hook"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "script", "function (user, context, callback) { callback(null, { user }); }"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "trigger_id", "pre-user-registration"),
resource.TestCheckResourceAttrSet("auth0_hook.my_hook", "enabled"),
resource.TestCheckNoResourceAttr("auth0_hook.my_hook", "secrets"),
resource.TestCheckNoResourceAttr("auth0_hook.my_hook", "dependencies"),
),
},
{
Config: fmt.Sprintf(testAccHookCreate, ""),
Check: resource.ComposeTestCheckFunc(
Expand All @@ -30,6 +41,14 @@ func TestAccHook(t *testing.T) {
})
}

const testAccHookEmpty = `
resource "auth0_hook" "my_hook" {
name = "pre-user-reg-hook"
script = "function (user, context, callback) { callback(null, { user }); }"
trigger_id = "pre-user-registration"
}
`

const testAccHookCreate = `
resource "auth0_hook" "my_hook" {
name = "pre-user-reg-hook"
Expand Down Expand Up @@ -81,6 +100,17 @@ func TestAccHookSecrets(t *testing.T) {
resource.TestCheckNoResourceAttr("auth0_hook.my_hook", "secrets.bar"),
),
},
{
Config: fmt.Sprintf(testAccHookCreate, testAccHookSecretsEmpty),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_hook.my_hook", "name", "pre-user-reg-hook"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "script", "function (user, context, callback) { callback(null, { user }); }"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "trigger_id", "pre-user-registration"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "enabled", "true"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "secrets.%", "0"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "dependencies.%", "0"),
),
},
},
})
}
Expand Down Expand Up @@ -113,6 +143,11 @@ const testAccHookSecretsUpdateAndRemoval = `
}
`

const testAccHookSecretsEmpty = `
dependencies = {}
secrets = {}
`

func TestHookNameRegexp(t *testing.T) {
for givenHookName, expectedError := range map[string]bool{
"my-hook-1": false,
Expand Down
Loading

0 comments on commit 14c0bdd

Please sign in to comment.