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
|
package mailgun
import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mailgun/mailgun-go/v3"
"log"
"time"
)
func resourceMailgunRoute() *schema.Resource {
return &schema.Resource{
Create: CreateRoute,
Update: UpdateRoute,
Delete: DeleteRoute,
Read: ReadRoute,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"route_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"priority": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"expression": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"actions": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func CreateRoute(d *schema.ResourceData, meta interface{}) error {
mg := meta.(*mailgun.MailgunImpl)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
log.Printf("[DEBUG] creating mailgun route: %s", d.Id())
creationResponse, err := mg.CreateRoute(ctx, mailgun.Route{
Priority: d.Get("priority").(int),
Description: d.Get("description").(string),
Expression: d.Get("expression").(string),
Actions: interfaceToStringTab(d.Get("actions")),
})
if err != nil {
return fmt.Errorf("Error creating mailgun route: %s", err.Error())
}
d.SetId(creationResponse.Id)
return ReadRoute(d, meta)
}
func UpdateRoute(d *schema.ResourceData, meta interface{}) error {
mg := meta.(*mailgun.MailgunImpl)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
log.Printf("[DEBUG] updating mailgun route: %s", d.Id())
_, err := mg.UpdateRoute(ctx, d.Id(), mailgun.Route{
Priority: d.Get("priority").(int),
Description: d.Get("description").(string),
Expression: d.Get("expression").(string),
Actions: interfaceToStringTab(d.Get("actions")),
})
if err != nil {
return fmt.Errorf("Error updating mailgun route: %s", err.Error())
}
return ReadRoute(d, meta)
}
func DeleteRoute(d *schema.ResourceData, meta interface{}) error {
mg := meta.(*mailgun.MailgunImpl)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
log.Printf("[DEBUG] Deleting mailgun route: %s", d.Id())
err := mg.DeleteRoute(ctx, d.Id())
return err
}
func ReadRoute(d *schema.ResourceData, meta interface{}) error {
mg := meta.(*mailgun.MailgunImpl)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
route, err := mg.GetRoute(ctx, d.Id())
if err != nil {
return fmt.Errorf("Error Getting mailgun route Details for %s: Error: %s", d.Id(), err)
}
d.Set("priority", route.Priority)
d.Set("description", route.Description)
d.Set("expression", route.Expression)
d.Set("actions", route.Actions)
d.Set("created_at", route.CreatedAt.String())
d.Set("route_id", route.Id)
d.SetId(route.Id)
return nil
}
|