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

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

Merged
merged 2 commits into from
Oct 7, 2022
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
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