]> git.immae.eu Git - github/fretlink/terraform-provider-mailgun.git/commitdiff
bug fix and test for route
authorAlexandre Garand <alexandre.garand@fretlink.com>
Wed, 3 Jul 2019 12:04:02 +0000 (14:04 +0200)
committerAlexandre Garand <alexandre.garand@fretlink.com>
Wed, 3 Jul 2019 12:04:02 +0000 (14:04 +0200)
mailgun/provider.go
mailgun/resource_mailgun_domain_test.go
mailgun/resource_mailgun_route.go
mailgun/resource_mailgun_route_test.go [new file with mode: 0644]

index 32ec8901c08b800c0321aff35827b40e733aef00..2133777bfa18232ce12b4947967e1b1f00cbb26c 100644 (file)
@@ -25,6 +25,7 @@ func Provider() terraform.ResourceProvider {
 
                ResourcesMap: map[string]*schema.Resource{
                        "mailgun_domain": resourceMailgunDomain(),
+                       "mailgun_route":  resourceMailgunRoute(),
                },
 
                ConfigureFunc: providerConfigure,
index c35df48ad32ce16d9bd3b4a7a1f4a6f82b00a5da..7ad92720d7cd93876f3ddce7d3fa1f3a22726f75 100644 (file)
@@ -12,6 +12,7 @@ import (
        "time"
 )
 
+
 type fullDomain struct {
        domainResponse   mailgun.DomainResponse
        domainConnection mailgun.DomainConnection
@@ -226,3 +227,4 @@ resource "mailgun_domain" "exemple" {
 
 }
 `
+
index 44fff8d9261cc930fd10f1087dcc692fb0ee9741..e19b67fd0c849717de3185a3d3fea97678f3bd93 100644 (file)
@@ -20,7 +20,7 @@ func resourceMailgunRoute() *schema.Resource {
                },
 
                Schema: map[string]*schema.Schema{
-                       "id": &schema.Schema{
+                       "route_id": &schema.Schema{
                                Type:     schema.TypeString,
                                Computed: true,
                        },
@@ -72,7 +72,6 @@ func CreateRoute(d *schema.ResourceData, meta interface{}) error {
        }
 
        d.SetId(creationResponse.Id)
-
        return ReadRoute(d, meta)
 }
 
@@ -94,7 +93,7 @@ func UpdateRoute(d *schema.ResourceData, meta interface{}) error {
                return fmt.Errorf("Error updating mailgun route: %s", err.Error())
        }
 
-       return ReadDomain(d, meta)
+       return ReadRoute(d, meta)
 }
 
 func DeleteRoute(d *schema.ResourceData, meta interface{}) error {
@@ -125,7 +124,7 @@ func ReadRoute(d *schema.ResourceData, meta interface{}) error {
        d.Set("expression", route.Expression)
        d.Set("actions", route.Actions)
        d.Set("created_at", route.CreatedAt)
-       d.Set("id", route.Id)
+       d.Set("route_id", route.Id)
 
        d.SetId(route.Id)
 
diff --git a/mailgun/resource_mailgun_route_test.go b/mailgun/resource_mailgun_route_test.go
new file mode 100644 (file)
index 0000000..b3806c2
--- /dev/null
@@ -0,0 +1,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()"
+        ]
+}
+`