6 "github.com/hashicorp/terraform/helper/schema"
7 "github.com/mailgun/mailgun-go"
12 func resourceMailgunRoute() *schema.Resource {
13 return &schema.Resource{
18 Importer: &schema.ResourceImporter{
19 State: schema.ImportStatePassthrough,
22 Schema: map[string]*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)
76 return ReadRoute(d, meta)
79 func UpdateRoute(d *schema.ResourceData, meta interface{}) error {
80 mg := meta.(*mailgun.MailgunImpl)
81 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
84 log.Printf("[DEBUG] updating mailgun route: %s", d.Id())
86 _, err := mg.UpdateRoute(ctx, d.Id(), mailgun.Route{
87 Priority: d.Get("priority").(int),
88 Description: d.Get("description").(string),
89 Expression: d.Get("expression").(string),
90 Actions: interfaceToStringTab(d.Get("actions")),
94 return fmt.Errorf("Error updating mailgun route: %s", err.Error())
97 return ReadDomain(d, meta)
100 func DeleteRoute(d *schema.ResourceData, meta interface{}) error {
101 mg := meta.(*mailgun.MailgunImpl)
102 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
105 log.Printf("[DEBUG] Deleting mailgun route: %s", d.Id())
107 err := mg.DeleteRoute(ctx, d.Id())
112 func ReadRoute(d *schema.ResourceData, meta interface{}) error {
113 mg := meta.(*mailgun.MailgunImpl)
114 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
117 route, err := mg.GetRoute(ctx, d.Id())
120 return fmt.Errorf("Error Getting mailgun route Details for %s: Error: %s", d.Id(), err)
123 d.Set("priority", route.Priority)
124 d.Set("description", route.Description)
125 d.Set("expression", route.Expression)
126 d.Set("actions", route.Actions)
127 d.Set("created_at", route.CreatedAt)
128 d.Set("id", route.Id)