diff options
author | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-07-03 14:04:02 +0200 |
---|---|---|
committer | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-07-03 14:04:02 +0200 |
commit | 3430e3235c3428a00823a71c7de07bab801b9a67 (patch) | |
tree | 30f2274c9c07d91337762424c62d07ef9de9e403 | |
parent | 85b93a5d4f992c2b32204876c0b5f92f3fb4a85d (diff) | |
download | terraform-provider-mailgun-3430e3235c3428a00823a71c7de07bab801b9a67.tar.gz terraform-provider-mailgun-3430e3235c3428a00823a71c7de07bab801b9a67.tar.zst terraform-provider-mailgun-3430e3235c3428a00823a71c7de07bab801b9a67.zip |
bug fix and test for route
-rw-r--r-- | mailgun/provider.go | 1 | ||||
-rw-r--r-- | mailgun/resource_mailgun_domain_test.go | 2 | ||||
-rw-r--r-- | mailgun/resource_mailgun_route.go | 7 | ||||
-rw-r--r-- | mailgun/resource_mailgun_route_test.go | 165 |
4 files changed, 171 insertions, 4 deletions
diff --git a/mailgun/provider.go b/mailgun/provider.go index 32ec890..2133777 100644 --- a/mailgun/provider.go +++ b/mailgun/provider.go | |||
@@ -25,6 +25,7 @@ func Provider() terraform.ResourceProvider { | |||
25 | 25 | ||
26 | ResourcesMap: map[string]*schema.Resource{ | 26 | ResourcesMap: map[string]*schema.Resource{ |
27 | "mailgun_domain": resourceMailgunDomain(), | 27 | "mailgun_domain": resourceMailgunDomain(), |
28 | "mailgun_route": resourceMailgunRoute(), | ||
28 | }, | 29 | }, |
29 | 30 | ||
30 | ConfigureFunc: providerConfigure, | 31 | ConfigureFunc: providerConfigure, |
diff --git a/mailgun/resource_mailgun_domain_test.go b/mailgun/resource_mailgun_domain_test.go index c35df48..7ad9272 100644 --- a/mailgun/resource_mailgun_domain_test.go +++ b/mailgun/resource_mailgun_domain_test.go | |||
@@ -12,6 +12,7 @@ import ( | |||
12 | "time" | 12 | "time" |
13 | ) | 13 | ) |
14 | 14 | ||
15 | |||
15 | type fullDomain struct { | 16 | type fullDomain struct { |
16 | domainResponse mailgun.DomainResponse | 17 | domainResponse mailgun.DomainResponse |
17 | domainConnection mailgun.DomainConnection | 18 | domainConnection mailgun.DomainConnection |
@@ -226,3 +227,4 @@ resource "mailgun_domain" "exemple" { | |||
226 | 227 | ||
227 | } | 228 | } |
228 | ` | 229 | ` |
230 | |||
diff --git a/mailgun/resource_mailgun_route.go b/mailgun/resource_mailgun_route.go index 44fff8d..e19b67f 100644 --- a/mailgun/resource_mailgun_route.go +++ b/mailgun/resource_mailgun_route.go | |||
@@ -20,7 +20,7 @@ func resourceMailgunRoute() *schema.Resource { | |||
20 | }, | 20 | }, |
21 | 21 | ||
22 | Schema: map[string]*schema.Schema{ | 22 | Schema: map[string]*schema.Schema{ |
23 | "id": &schema.Schema{ | 23 | "route_id": &schema.Schema{ |
24 | Type: schema.TypeString, | 24 | Type: schema.TypeString, |
25 | Computed: true, | 25 | Computed: true, |
26 | }, | 26 | }, |
@@ -72,7 +72,6 @@ func CreateRoute(d *schema.ResourceData, meta interface{}) error { | |||
72 | } | 72 | } |
73 | 73 | ||
74 | d.SetId(creationResponse.Id) | 74 | d.SetId(creationResponse.Id) |
75 | |||
76 | return ReadRoute(d, meta) | 75 | return ReadRoute(d, meta) |
77 | } | 76 | } |
78 | 77 | ||
@@ -94,7 +93,7 @@ func UpdateRoute(d *schema.ResourceData, meta interface{}) error { | |||
94 | return fmt.Errorf("Error updating mailgun route: %s", err.Error()) | 93 | return fmt.Errorf("Error updating mailgun route: %s", err.Error()) |
95 | } | 94 | } |
96 | 95 | ||
97 | return ReadDomain(d, meta) | 96 | return ReadRoute(d, meta) |
98 | } | 97 | } |
99 | 98 | ||
100 | func DeleteRoute(d *schema.ResourceData, meta interface{}) error { | 99 | func DeleteRoute(d *schema.ResourceData, meta interface{}) error { |
@@ -125,7 +124,7 @@ func ReadRoute(d *schema.ResourceData, meta interface{}) error { | |||
125 | d.Set("expression", route.Expression) | 124 | d.Set("expression", route.Expression) |
126 | d.Set("actions", route.Actions) | 125 | d.Set("actions", route.Actions) |
127 | d.Set("created_at", route.CreatedAt) | 126 | d.Set("created_at", route.CreatedAt) |
128 | d.Set("id", route.Id) | 127 | d.Set("route_id", route.Id) |
129 | 128 | ||
130 | d.SetId(route.Id) | 129 | d.SetId(route.Id) |
131 | 130 | ||
diff --git a/mailgun/resource_mailgun_route_test.go b/mailgun/resource_mailgun_route_test.go new file mode 100644 index 0000000..b3806c2 --- /dev/null +++ b/mailgun/resource_mailgun_route_test.go | |||
@@ -0,0 +1,165 @@ | |||
1 | package mailgun | ||
2 | |||
3 | import ( | ||
4 | "context" | ||
5 | "fmt" | ||
6 | "github.com/hashicorp/terraform/helper/resource" | ||
7 | "github.com/hashicorp/terraform/terraform" | ||
8 | "github.com/mailgun/mailgun-go" | ||
9 | "strconv" | ||
10 | "testing" | ||
11 | "time" | ||
12 | ) | ||
13 | |||
14 | func TestAccMailgunRoute_basic(t *testing.T) { | ||
15 | var route mailgun.Route | ||
16 | |||
17 | resource.Test(t, resource.TestCase{ | ||
18 | PreCheck: func() { testAccPreCheck(t) }, | ||
19 | Providers: testAccProviders, | ||
20 | CheckDestroy: testAccRouteCheckDestroy(&route), | ||
21 | Steps: []resource.TestStep{ | ||
22 | { | ||
23 | Config: testAccRouteConfig_basic, | ||
24 | Check: resource.ComposeTestCheckFunc( | ||
25 | testAccRouteCheckExists("mailgun_route.exemple", &route), | ||
26 | testAccRouteCheckAttributes("mailgun_route.exemple", &route), | ||
27 | ), | ||
28 | }, | ||
29 | }, | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | func TestAccMailgunRoute_withUpdate(t *testing.T) { | ||
34 | var route mailgun.Route | ||
35 | |||
36 | resource.Test(t, resource.TestCase{ | ||
37 | PreCheck: func() { testAccPreCheck(t) }, | ||
38 | Providers: testAccProviders, | ||
39 | CheckDestroy: testAccRouteCheckDestroy(&route), | ||
40 | Steps: []resource.TestStep{ | ||
41 | { | ||
42 | Config: testAccRouteConfig_basic, | ||
43 | Check: resource.ComposeTestCheckFunc( | ||
44 | testAccRouteCheckExists("mailgun_route.exemple", &route), | ||
45 | testAccRouteCheckAttributes("mailgun_route.exemple", &route), | ||
46 | ), | ||
47 | }, | ||
48 | |||
49 | { | ||
50 | Config: testAccRouteConfig_update, | ||
51 | Check: resource.ComposeTestCheckFunc( | ||
52 | testAccRouteCheckExists("mailgun_route.exemple", &route), | ||
53 | testAccRouteCheckAttributes("mailgun_route.exemple", &route), | ||
54 | ), | ||
55 | }, | ||
56 | }, | ||
57 | }) | ||
58 | } | ||
59 | |||
60 | func testAccRouteCheckExists(rn string, route *mailgun.Route) resource.TestCheckFunc { | ||
61 | return func(s *terraform.State) error { | ||
62 | rs, ok := s.RootModule().Resources[rn] | ||
63 | if !ok { | ||
64 | return fmt.Errorf("resource not found: %s", rn) | ||
65 | } | ||
66 | |||
67 | if rs.Primary.ID == "" { | ||
68 | return fmt.Errorf("routeID not set") | ||
69 | } | ||
70 | |||
71 | mg := testAccProvider.Meta().(*mailgun.MailgunImpl) | ||
72 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) | ||
73 | defer cancel() | ||
74 | |||
75 | gotRoute, err := mg.GetRoute(ctx, rs.Primary.ID) | ||
76 | if err != nil { | ||
77 | return fmt.Errorf("error getting route: %s", err) | ||
78 | } | ||
79 | |||
80 | *route = gotRoute | ||
81 | |||
82 | return nil | ||
83 | } | ||
84 | } | ||
85 | |||
86 | func testAccRouteCheckAttributes(rn string, route *mailgun.Route) resource.TestCheckFunc { | ||
87 | return func(s *terraform.State) error { | ||
88 | attrs := s.RootModule().Resources[rn].Primary.Attributes | ||
89 | |||
90 | check := func(key, stateValue, routeValue string) error { | ||
91 | if routeValue != stateValue { | ||
92 | return fmt.Errorf("different values for %s in state (%s) and in mailgun (%s)", | ||
93 | key, stateValue, routeValue) | ||
94 | } | ||
95 | return nil | ||
96 | } | ||
97 | |||
98 | for key, value := range attrs { | ||
99 | var err error | ||
100 | |||
101 | switch key { | ||
102 | case "priority": | ||
103 | err = check(key, value, strconv.Itoa(route.Priority)) | ||
104 | case "description": | ||
105 | err = check(key, value, route.Description) | ||
106 | case "expression": | ||
107 | err = check(key, value, route.Expression) | ||
108 | case "created_at": | ||
109 | err = check(key, value, route.CreatedAt.String()) | ||
110 | case "route_id": | ||
111 | err = check(key, value, route.Id) | ||
112 | case "actions": | ||
113 | for _, k := range route.Actions { | ||
114 | err = check(key, value, k) | ||
115 | if err != nil { | ||
116 | return err | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | if err != nil { | ||
121 | return err | ||
122 | } | ||
123 | } | ||
124 | return nil | ||
125 | } | ||
126 | } | ||
127 | |||
128 | func testAccRouteCheckDestroy(route *mailgun.Route) resource.TestCheckFunc { | ||
129 | return func(s *terraform.State) error { | ||
130 | mg := testAccProvider.Meta().(*mailgun.MailgunImpl) | ||
131 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) | ||
132 | defer cancel() | ||
133 | |||
134 | _, err := mg.GetRoute(ctx, route.Id) | ||
135 | if err == nil { | ||
136 | return fmt.Errorf("route still exists") | ||
137 | } | ||
138 | |||
139 | return nil | ||
140 | } | ||
141 | } | ||
142 | |||
143 | const testAccRouteConfig_basic = ` | ||
144 | resource "mailgun_route" "exemple" { | ||
145 | priority=5 | ||
146 | description="ho ho hoh" | ||
147 | expression="match_recipient(\".*@samples.mailgun.org\")" | ||
148 | actions=[ | ||
149 | "forward(\"http://myhost.com/messages/\")", | ||
150 | "stop()" | ||
151 | ] | ||
152 | } | ||
153 | ` | ||
154 | |||
155 | const testAccRouteConfig_update = ` | ||
156 | resource "mailgun_route" "exemple" { | ||
157 | priority=4 | ||
158 | description="ho ho hohf" | ||
159 | expression="match_recipient(\".*@samples.mailgun.org\")" | ||
160 | actions=[ | ||
161 | "forward(\"http://myhost.com/messages/\")", | ||
162 | "stop()" | ||
163 | ] | ||
164 | } | ||
165 | ` | ||