]>
Commit | Line | Data |
---|---|---|
85b93a5d AG |
1 | package mailgun |
2 | ||
3 | import ( | |
4 | "context" | |
5 | "fmt" | |
6 | "github.com/hashicorp/terraform/helper/schema" | |
7c735cfa | 7 | "github.com/mailgun/mailgun-go/v3" |
85b93a5d AG |
8 | "log" |
9 | "time" | |
10 | ) | |
11 | ||
12 | func 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{ | |
3430e323 | 23 | "route_id": &schema.Schema{ |
85b93a5d AG |
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 | ||
56 | func 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) | |
85b93a5d AG |
75 | return ReadRoute(d, meta) |
76 | } | |
77 | ||
78 | func UpdateRoute(d *schema.ResourceData, meta interface{}) error { | |
79 | mg := meta.(*mailgun.MailgunImpl) | |
80 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) | |
81 | defer cancel() | |
82 | ||
83 | log.Printf("[DEBUG] updating mailgun route: %s", d.Id()) | |
84 | ||
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")), | |
90 | }) | |
91 | ||
92 | if err != nil { | |
93 | return fmt.Errorf("Error updating mailgun route: %s", err.Error()) | |
94 | } | |
95 | ||
3430e323 | 96 | return ReadRoute(d, meta) |
85b93a5d AG |
97 | } |
98 | ||
99 | func DeleteRoute(d *schema.ResourceData, meta interface{}) error { | |
100 | mg := meta.(*mailgun.MailgunImpl) | |
101 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) | |
102 | defer cancel() | |
103 | ||
104 | log.Printf("[DEBUG] Deleting mailgun route: %s", d.Id()) | |
105 | ||
106 | err := mg.DeleteRoute(ctx, d.Id()) | |
107 | ||
108 | return err | |
109 | } | |
110 | ||
111 | func ReadRoute(d *schema.ResourceData, meta interface{}) error { | |
112 | mg := meta.(*mailgun.MailgunImpl) | |
113 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) | |
114 | defer cancel() | |
115 | ||
116 | route, err := mg.GetRoute(ctx, d.Id()) | |
117 | ||
118 | if err != nil { | |
119 | return fmt.Errorf("Error Getting mailgun route Details for %s: Error: %s", d.Id(), err) | |
120 | } | |
121 | ||
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) | |
3430e323 | 127 | d.Set("route_id", route.Id) |
85b93a5d AG |
128 | |
129 | d.SetId(route.Id) | |
130 | ||
131 | return nil | |
132 | } |