|
| 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 | +} |
0 commit comments