forked from alexkappa/terraform-provider-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_auth0_hook.go
177 lines (160 loc) · 4.21 KB
/
resource_auth0_hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package auth0
import (
"net/http"
"regexp"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"gopkg.in/auth0.v5"
"gopkg.in/auth0.v5/management"
)
func newHook() *schema.Resource {
return &schema.Resource{
Create: createHook,
Read: readHook,
Update: updateHook,
Delete: deleteHook,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateHookNameFunc(),
Description: "Name of this hook",
},
"dependencies": {
Type: schema.TypeMap,
Elem: schema.TypeString,
Optional: true,
Description: "Dependencies of this hook used by webtask server",
},
"script": {
Type: schema.TypeString,
Required: true,
Description: "Code to be executed when this hook runs",
},
"trigger_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"credentials-exchange",
"pre-user-registration",
"post-user-registration",
"post-change-password",
"send-phone-message",
}, false),
Description: "Execution stage of this rule. Can be " +
"credentials-exchange, pre-user-registration, " +
"post-user-registration, post-change-password" +
", or send-phone-message",
},
"secrets": {
Type: schema.TypeMap,
Optional: true,
Description: "The secrets associated with the hook",
Elem: schema.TypeString,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Whether the hook is enabled, or disabled",
},
},
}
}
func createHook(d *schema.ResourceData, m interface{}) error {
c := buildHook(d)
api := m.(*management.Management)
if err := api.Hook.Create(c); err != nil {
return err
}
d.SetId(auth0.StringValue(c.ID))
if err := upsertHookSecrets(d, m); err != nil {
return err
}
return readHook(d, m)
}
func readHook(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
c, err := api.Hook.Read(d.Id())
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
}
return err
}
d.Set("name", c.Name)
d.Set("dependencies", c.Dependencies)
d.Set("script", c.Script)
d.Set("trigger_id", c.TriggerID)
d.Set("enabled", c.Enabled)
return nil
}
func updateHook(d *schema.ResourceData, m interface{}) error {
c := buildHook(d)
api := m.(*management.Management)
err := api.Hook.Update(d.Id(), c)
if err != nil {
return err
}
if err = upsertHookSecrets(d, m); err != nil {
return err
}
return readHook(d, m)
}
func upsertHookSecrets(d *schema.ResourceData, m interface{}) error {
if d.IsNewResource() || d.HasChange("secrets") {
secrets := Map(d, "secrets")
api := m.(*management.Management)
hookSecrets := toHookSecrets(secrets)
return api.Hook.ReplaceSecrets(d.Id(), hookSecrets)
}
return nil
}
func toHookSecrets(val map[string]interface{}) management.HookSecrets {
hookSecrets := management.HookSecrets{}
for key, value := range val {
if strVal, ok := value.(string); ok {
hookSecrets[key] = strVal
}
}
return hookSecrets
}
func deleteHook(d *schema.ResourceData, m interface{}) error {
api := m.(*management.Management)
err := api.Hook.Delete(d.Id())
if err != nil {
if mErr, ok := err.(management.Error); ok {
if mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}
}
return err
}
return err
}
func buildHook(d *schema.ResourceData) *management.Hook {
h := &management.Hook{
Name: String(d, "name"),
Script: String(d, "script"),
TriggerID: String(d, "trigger_id", IsNewResource()),
Enabled: Bool(d, "enabled"),
}
deps := Map(d, "dependencies")
if deps != nil {
h.Dependencies = &deps
}
return h
}
func validateHookNameFunc() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[^\\s-][\\w -]+[^\\s-]$"),
"Can only contain alphanumeric characters, spaces and '-'. Can neither start nor end with '-' or spaces.")
}