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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
package mailgun
import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/mailgun/mailgun-go"
"strconv"
"testing"
"time"
)
func TestAccMailgunRoute_basic(t *testing.T) {
var route mailgun.Route
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccRouteCheckDestroy(&route),
Steps: []resource.TestStep{
{
Config: testAccRouteConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccRouteCheckExists("mailgun_route.exemple", &route),
testAccRouteCheckAttributes("mailgun_route.exemple", &route),
),
},
},
})
}
func TestAccMailgunRoute_withUpdate(t *testing.T) {
var route mailgun.Route
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccRouteCheckDestroy(&route),
Steps: []resource.TestStep{
{
Config: testAccRouteConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccRouteCheckExists("mailgun_route.exemple", &route),
testAccRouteCheckAttributes("mailgun_route.exemple", &route),
),
},
{
Config: testAccRouteConfig_update,
Check: resource.ComposeTestCheckFunc(
testAccRouteCheckExists("mailgun_route.exemple", &route),
testAccRouteCheckAttributes("mailgun_route.exemple", &route),
),
},
},
})
}
func testAccRouteCheckExists(rn string, route *mailgun.Route) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
if !ok {
return fmt.Errorf("resource not found: %s", rn)
}
if rs.Primary.ID == "" {
return fmt.Errorf("routeID not set")
}
mg := testAccProvider.Meta().(*mailgun.MailgunImpl)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
gotRoute, err := mg.GetRoute(ctx, rs.Primary.ID)
if err != nil {
return fmt.Errorf("error getting route: %s", err)
}
*route = gotRoute
return nil
}
}
func testAccRouteCheckAttributes(rn string, route *mailgun.Route) resource.TestCheckFunc {
return func(s *terraform.State) error {
attrs := s.RootModule().Resources[rn].Primary.Attributes
check := func(key, stateValue, routeValue string) error {
if routeValue != stateValue {
return fmt.Errorf("different values for %s in state (%s) and in mailgun (%s)",
key, stateValue, routeValue)
}
return nil
}
for key, value := range attrs {
var err error
switch key {
case "priority":
err = check(key, value, strconv.Itoa(route.Priority))
case "description":
err = check(key, value, route.Description)
case "expression":
err = check(key, value, route.Expression)
case "created_at":
err = check(key, value, route.CreatedAt.String())
case "route_id":
err = check(key, value, route.Id)
case "actions":
for _, k := range route.Actions {
err = check(key, value, k)
if err != nil {
return err
}
}
}
if err != nil {
return err
}
}
return nil
}
}
func testAccRouteCheckDestroy(route *mailgun.Route) resource.TestCheckFunc {
return func(s *terraform.State) error {
mg := testAccProvider.Meta().(*mailgun.MailgunImpl)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
_, err := mg.GetRoute(ctx, route.Id)
if err == nil {
return fmt.Errorf("route still exists")
}
return nil
}
}
const testAccRouteConfig_basic = `
resource "mailgun_route" "exemple" {
priority=5
description="ho ho hoh"
expression="match_recipient(\".*@samples.mailgun.org\")"
actions=[
"forward(\"http://myhost.com/messages/\")",
"stop()"
]
}
`
const testAccRouteConfig_update = `
resource "mailgun_route" "exemple" {
priority=4
description="ho ho hohf"
expression="match_recipient(\".*@samples.mailgun.org\")"
actions=[
"forward(\"http://myhost.com/messages/\")",
"stop()"
]
}
`
|