aboutsummaryrefslogtreecommitdiffhomepage
path: root/mailgun
diff options
context:
space:
mode:
Diffstat (limited to 'mailgun')
-rw-r--r--mailgun/resource_mailgun_route.go133
1 files changed, 133 insertions, 0 deletions
diff --git a/mailgun/resource_mailgun_route.go b/mailgun/resource_mailgun_route.go
new file mode 100644
index 0000000..44fff8d
--- /dev/null
+++ b/mailgun/resource_mailgun_route.go
@@ -0,0 +1,133 @@
1package mailgun
2
3import (
4 "context"
5 "fmt"
6 "github.com/hashicorp/terraform/helper/schema"
7 "github.com/mailgun/mailgun-go"
8 "log"
9 "time"
10)
11
12func resourceMailgunRoute() *schema.Resource {
13 return &schema.Resource{
14 Create: CreateRoute,
15 Update: UpdateRoute,
16 Delete: DeleteRoute,
17 Read: ReadRoute,
18 Importer: &schema.ResourceImporter{
19 State: schema.ImportStatePassthrough,
20 },
21
22 Schema: map[string]*schema.Schema{
23 "id": &schema.Schema{
24 Type: schema.TypeString,
25 Computed: true,
26 },
27 "created_at": &schema.Schema{
28 Type: schema.TypeString,
29 Computed: true,
30 },
31
32 "priority": &schema.Schema{
33 Type: schema.TypeInt,
34 Required: true,
35 },
36
37 "expression": &schema.Schema{
38 Type: schema.TypeString,
39 Required: true,
40 },
41
42 "description": &schema.Schema{
43 Type: schema.TypeString,
44 Required: true,
45 },
46
47 "actions": &schema.Schema{
48 Type: schema.TypeList,
49 Required: true,
50 Elem: &schema.Schema{Type: schema.TypeString},
51 },
52 },
53 }
54}
55
56func CreateRoute(d *schema.ResourceData, meta interface{}) error {
57 mg := meta.(*mailgun.MailgunImpl)
58 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
59 defer cancel()
60
61 log.Printf("[DEBUG] creating mailgun route: %s", d.Id())
62
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")),
68 })
69
70 if err != nil {
71 return fmt.Errorf("Error creating mailgun route: %s", err.Error())
72 }
73
74 d.SetId(creationResponse.Id)
75
76 return ReadRoute(d, meta)
77}
78
79func UpdateRoute(d *schema.ResourceData, meta interface{}) error {
80 mg := meta.(*mailgun.MailgunImpl)
81 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
82 defer cancel()
83
84 log.Printf("[DEBUG] updating mailgun route: %s", d.Id())
85
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")),
91 })
92
93 if err != nil {
94 return fmt.Errorf("Error updating mailgun route: %s", err.Error())
95 }
96
97 return ReadDomain(d, meta)
98}
99
100func DeleteRoute(d *schema.ResourceData, meta interface{}) error {
101 mg := meta.(*mailgun.MailgunImpl)
102 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
103 defer cancel()
104
105 log.Printf("[DEBUG] Deleting mailgun route: %s", d.Id())
106
107 err := mg.DeleteRoute(ctx, d.Id())
108
109 return err
110}
111
112func ReadRoute(d *schema.ResourceData, meta interface{}) error {
113 mg := meta.(*mailgun.MailgunImpl)
114 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
115 defer cancel()
116
117 route, err := mg.GetRoute(ctx, d.Id())
118
119 if err != nil {
120 return fmt.Errorf("Error Getting mailgun route Details for %s: Error: %s", d.Id(), err)
121 }
122
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)
129
130 d.SetId(route.Id)
131
132 return nil
133}