6 "github.com/hashicorp/terraform/helper/schema"
7 "github.com/mailgun/mailgun-go/v3"
12 func resourceMailgunRoute() *schema.Resource {
13 return &schema.Resource{
18 Importer: &schema.ResourceImporter{
19 State: schema.ImportStatePassthrough,
22 Schema: map[string]*schema.Schema{
23 "route_id": &schema.Schema{
24 Type: schema.TypeString,
27 "created_at": &schema.Schema{
28 Type: schema.TypeString,
32 "priority": &schema.Schema{
37 "expression": &schema.Schema{
38 Type: schema.TypeString,
42 "description": &schema.Schema{
43 Type: schema.TypeString,
47 "actions": &schema.Schema{
48 Type: schema.TypeList,
50 Elem: &schema.Schema{Type: schema.TypeString},
56 func CreateRoute(d *schema.ResourceData, meta interface{}) error {
57 mg := meta.(*mailgun.MailgunImpl)
58 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
61 log.Printf("[DEBUG] creating mailgun route: %s", d.Id())
63 creationResponse, err := mg.CreateRoute(ctx, mailgun.Route{
64 Priority: d.Get("priority").(int),
65 Description: d.Get("description").(string),
66 Expression: d.Get("expression").(string),
67 Actions: interfaceToStringTab(d.Get("actions")),
71 return fmt.Errorf("Error creating mailgun route: %s", err.Error())
74 d.SetId(creationResponse.Id)
75 return ReadRoute(d, meta)
78 func UpdateRoute(d *schema.ResourceData, meta interface{}) error {
79 mg := meta.(*mailgun.MailgunImpl)
80 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
83 log.Printf("[DEBUG] updating mailgun route: %s", d.Id())
85 _, err := mg.UpdateRoute(ctx, d.Id(), mailgun.Route{
86 Priority: d.Get("priority").(int),
87 Description: d.Get("description").(string),
88 Expression: d.Get("expression").(string),
89 Actions: interfaceToStringTab(d.Get("actions")),
93 return fmt.Errorf("Error updating mailgun route: %s", err.Error())
96 return ReadRoute(d, meta)
99 func DeleteRoute(d *schema.ResourceData, meta interface{}) error {
100 mg := meta.(*mailgun.MailgunImpl)
101 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
104 log.Printf("[DEBUG] Deleting mailgun route: %s", d.Id())
106 err := mg.DeleteRoute(ctx, d.Id())
111 func ReadRoute(d *schema.ResourceData, meta interface{}) error {
112 mg := meta.(*mailgun.MailgunImpl)
113 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
116 route, err := mg.GetRoute(ctx, d.Id())
119 return fmt.Errorf("Error Getting mailgun route Details for %s: Error: %s", d.Id(), err)
122 d.Set("priority", route.Priority)
123 d.Set("description", route.Description)
124 d.Set("expression", route.Expression)
125 d.Set("actions", route.Actions)
126 d.Set("created_at", route.CreatedAt)
127 d.Set("route_id", route.Id)