Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.

Commit 0bbadc8

Browse files
authored
Merge pull request #481 from alexkappa/add-auth0_trigger_binding
Add `auth0_trigger_binding`
2 parents 5fbae06 + 61f12d7 commit 0bbadc8

6 files changed

+333
-0
lines changed

.github/labeler.yml

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ resource/auth0_tenant:
7474
- '**/*tenant.go'
7575
- '**/*tenant_test.go'
7676

77+
resource/auth0_trigger_binding:
78+
- '**/*trigger_binding.go'
79+
- '**/*trigger_binding_test.go'
80+
7781
resource/auth0_user:
7882
- '**/*user.go'
7983
- '**/*user_test.go'

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ENHANCEMENTS:
44

55
* **New Resource:** `auth0_trigger_binding` a.k.a Action Flow ([#481](https://github.com/alexkappa/terraform-provider-auth0/pull/481))
66
* resource/auth0_connection: Add `entity_id` field for SAMLP connections ([#468](https://github.com/alexkappa/terraform-provider-auth0/pull/468))
7+
* resource/auth0_client_grant: Update import documentation ([#471](https://github.com/alexkappa/terraform-provider-auth0/pull/471))
78

89
## 0.24.3
910

auth0/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func init() {
6666
"auth0_guardian": newGuardian(),
6767
"auth0_organization": newOrganization(),
6868
"auth0_action": newAction(),
69+
"auth0_trigger_binding": newTriggerBinding(),
6970
},
7071
ConfigureFunc: Configure,
7172
}
+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package auth0
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
8+
9+
"gopkg.in/auth0.v5"
10+
"gopkg.in/auth0.v5/management"
11+
)
12+
13+
func newTriggerBinding() *schema.Resource {
14+
return &schema.Resource{
15+
16+
Create: createTriggerBinding,
17+
Read: readTriggerBinding,
18+
Update: updateTriggerBinding,
19+
Delete: deleteTriggerBinding,
20+
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
24+
25+
Schema: map[string]*schema.Schema{
26+
"trigger": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
ForceNew: true,
30+
ValidateFunc: validation.StringInSlice([]string{
31+
"post-login",
32+
"credentials-exchange",
33+
"pre-user-registration",
34+
"post-user-registration",
35+
"post-change-password",
36+
"send-phone-message",
37+
"iga-approval",
38+
"iga-certification",
39+
"iga-fulfillment-assignment",
40+
"iga-fulfillment-execution",
41+
}, false),
42+
Description: "The id of the trigger to bind with",
43+
},
44+
"actions": {
45+
Type: schema.TypeList,
46+
Required: true,
47+
Elem: &schema.Resource{
48+
Schema: map[string]*schema.Schema{
49+
"id": {
50+
Type: schema.TypeString,
51+
Required: true,
52+
Description: "Trigger ID",
53+
},
54+
"display_name": {
55+
Type: schema.TypeString,
56+
Required: true,
57+
Description: "The name of an action",
58+
},
59+
},
60+
},
61+
Description: "The actions bound to this trigger",
62+
},
63+
},
64+
}
65+
}
66+
67+
func createTriggerBinding(d *schema.ResourceData, m interface{}) error {
68+
api := m.(*management.Management)
69+
id := d.Get("trigger").(string)
70+
b := expandTriggerBindings(d)
71+
err := api.Action.UpdateBindings(id, b)
72+
if err != nil {
73+
return err
74+
}
75+
d.SetId(id)
76+
return readTriggerBinding(d, m)
77+
}
78+
79+
func readTriggerBinding(d *schema.ResourceData, m interface{}) error {
80+
api := m.(*management.Management)
81+
b, err := api.Action.Bindings(d.Id())
82+
if err != nil {
83+
if mErr, ok := err.(management.Error); ok {
84+
if mErr.Status() == http.StatusNotFound {
85+
d.SetId("")
86+
return nil
87+
}
88+
}
89+
return err
90+
}
91+
92+
d.Set("actions", flattenTriggerBindingActions(b.Bindings))
93+
94+
return nil
95+
}
96+
97+
func updateTriggerBinding(d *schema.ResourceData, m interface{}) error {
98+
b := expandTriggerBindings(d)
99+
api := m.(*management.Management)
100+
err := api.Action.UpdateBindings(d.Id(), b)
101+
if err != nil {
102+
return err
103+
}
104+
return readTriggerBinding(d, m)
105+
}
106+
107+
func deleteTriggerBinding(d *schema.ResourceData, m interface{}) error {
108+
api := m.(*management.Management)
109+
if err := api.Action.UpdateBindings(d.Id(), []*management.ActionBinding{}); err != nil {
110+
if mErr, ok := err.(management.Error); ok {
111+
if mErr.Status() == http.StatusNotFound {
112+
d.SetId("")
113+
return nil
114+
}
115+
}
116+
return err
117+
}
118+
return nil
119+
}
120+
121+
func expandTriggerBindings(d *schema.ResourceData) (b []*management.ActionBinding) {
122+
List(d, "actions").Elem(func(d ResourceData) {
123+
b = append(b, &management.ActionBinding{
124+
Ref: &management.ActionBindingReference{
125+
Type: auth0.String("action_id"),
126+
Value: String(d, "id"),
127+
},
128+
DisplayName: String(d, "display_name"),
129+
})
130+
})
131+
return
132+
}
133+
134+
func flattenTriggerBindingActions(bindings []*management.ActionBinding) (r []interface{}) {
135+
for _, b := range bindings {
136+
r = append(r, map[string]interface{}{
137+
"id": b.Action.GetID(),
138+
"display_name": b.GetDisplayName(),
139+
})
140+
}
141+
return
142+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package auth0
2+
3+
import (
4+
"testing"
5+
6+
"github.com/alexkappa/terraform-provider-auth0/auth0/internal/random"
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
9+
)
10+
11+
func TestAccTriggerBinding(t *testing.T) {
12+
13+
rand := random.String(6)
14+
15+
resource.Test(t, resource.TestCase{
16+
Providers: map[string]terraform.ResourceProvider{
17+
"auth0": Provider(),
18+
},
19+
Steps: []resource.TestStep{
20+
{
21+
Config: random.Template(testAccTriggerBindingConfigCreate, rand),
22+
Check: resource.ComposeTestCheckFunc(
23+
random.TestCheckResourceAttr("auth0_action.action_foo", "name", "Test Trigger Binding Foo {{.random}}", rand),
24+
random.TestCheckResourceAttr("auth0_action.action_bar", "name", "Test Trigger Binding Bar {{.random}}", rand),
25+
resource.TestCheckResourceAttr("auth0_trigger_binding.login_flow", "actions.#", "2"),
26+
random.TestCheckResourceAttr("auth0_trigger_binding.login_flow", "actions.0.display_name", "Test Trigger Binding Foo {{.random}}", rand),
27+
random.TestCheckResourceAttr("auth0_trigger_binding.login_flow", "actions.1.display_name", "Test Trigger Binding Bar {{.random}}", rand),
28+
),
29+
},
30+
{
31+
Config: random.Template(testAccTriggerBindingConfigUpdate, rand),
32+
Check: resource.ComposeTestCheckFunc(
33+
random.TestCheckResourceAttr("auth0_action.action_foo", "name", "Test Trigger Binding Foo {{.random}}", rand),
34+
random.TestCheckResourceAttr("auth0_action.action_bar", "name", "Test Trigger Binding Bar {{.random}}", rand),
35+
resource.TestCheckResourceAttr("auth0_trigger_binding.login_flow", "actions.#", "2"),
36+
random.TestCheckResourceAttr("auth0_trigger_binding.login_flow", "actions.0.display_name", "Test Trigger Binding Bar {{.random}}", rand),
37+
random.TestCheckResourceAttr("auth0_trigger_binding.login_flow", "actions.1.display_name", "Test Trigger Binding Foo {{.random}}", rand),
38+
),
39+
},
40+
},
41+
})
42+
}
43+
44+
const testAccTriggerBindingAction = `
45+
46+
resource auth0_action action_foo {
47+
name = "Test Trigger Binding Foo {{.random}}"
48+
supported_triggers {
49+
id = "post-login"
50+
version = "v2"
51+
}
52+
code = <<-EOT
53+
exports.onContinuePostLogin = async (event, api) => {
54+
console.log("foo")
55+
};"
56+
EOT
57+
deploy = true
58+
}
59+
60+
resource auth0_action action_bar {
61+
name = "Test Trigger Binding Bar {{.random}}"
62+
supported_triggers {
63+
id = "post-login"
64+
version = "v2"
65+
}
66+
code = <<-EOT
67+
exports.onContinuePostLogin = async (event, api) => {
68+
console.log("bar")
69+
};"
70+
EOT
71+
deploy = true
72+
}
73+
`
74+
75+
const testAccTriggerBindingConfigCreate = testAccTriggerBindingAction + `
76+
77+
resource auth0_trigger_binding login_flow {
78+
trigger = "post-login"
79+
actions {
80+
id = auth0_action.action_foo.id
81+
display_name = auth0_action.action_foo.name
82+
}
83+
actions {
84+
id = auth0_action.action_bar.id
85+
display_name = auth0_action.action_bar.name
86+
}
87+
}
88+
`
89+
90+
const testAccTriggerBindingConfigUpdate = testAccTriggerBindingAction + `
91+
92+
resource auth0_trigger_binding login_flow {
93+
trigger = "post-login"
94+
actions {
95+
id = auth0_action.action_bar.id # <----- change the order of the actions
96+
display_name = auth0_action.action_bar.name
97+
}
98+
actions {
99+
id = auth0_action.action_foo.id
100+
display_name = auth0_action.action_foo.name
101+
}
102+
}
103+
`

docs/resources/trigger_binding.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
layout: "auth0"
3+
page_title: "Auth0: auth0_trigger_binding"
4+
description: |-
5+
With this resource, you can bind an action to a trigger. Once an action is
6+
created and deployed, it can be attached (i.e. bound) to a trigger so that it
7+
will be executed as part of a flow.
8+
---
9+
10+
# auth0_trigger_binding
11+
12+
With this resource, you can bind an action to a trigger. Once an action is
13+
created and deployed, it can be attached (i.e. bound) to a trigger so that it
14+
will be executed as part of a flow.
15+
16+
The list of actions reflects the order in which they will be executed during the
17+
appropriate flow.
18+
19+
## Example Usage
20+
21+
```hcl
22+
resource auth0_action action_foo {
23+
name = "Test Trigger Binding Foo {{.random}}"
24+
supported_triggers {
25+
id = "post-login"
26+
version = "v2"
27+
}
28+
code = <<-EOT
29+
exports.onContinuePostLogin = async (event, api) => {
30+
console.log("foo")
31+
};"
32+
EOT
33+
deploy = true
34+
}
35+
36+
resource auth0_action action_bar {
37+
name = "Test Trigger Binding Bar {{.random}}"
38+
supported_triggers {
39+
id = "post-login"
40+
version = "v2"
41+
}
42+
code = <<-EOT
43+
exports.onContinuePostLogin = async (event, api) => {
44+
console.log("bar")
45+
};"
46+
EOT
47+
deploy = true
48+
}
49+
50+
resource auth0_trigger_binding login_flow {
51+
trigger = "post-login"
52+
actions {
53+
id = auth0_action.action_foo.id
54+
display_name = auth0_action.action_foo.name
55+
}
56+
actions {
57+
id = auth0_action.action_bar.id
58+
display_name = auth0_action.action_bar.name
59+
}
60+
}
61+
```
62+
63+
## Argument Reference
64+
65+
The following arguments are supported:
66+
67+
* `trigger` - (Required) The id of the trigger to bind with
68+
* `actions` - (Required) The actions bound to this trigger. For details, see
69+
[Actions](#actions).
70+
71+
### Actions
72+
73+
* `id` - (Required) Trigger ID.
74+
* `display_name` - (Required) The name of an action.
75+
76+
## Import
77+
78+
auth0_trigger_binding can be imported using the bindings trigger ID, e.g.
79+
80+
```
81+
$ terraform import auth0_trigger_binding.example "post-login"
82+
```

0 commit comments

Comments
 (0)