From: Alexandre Garand Date: Wed, 3 Jul 2019 12:04:02 +0000 (+0200) Subject: bug fix and test for route X-Git-Tag: v0.1.0~12 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=3430e3235c3428a00823a71c7de07bab801b9a67;p=github%2Ffretlink%2Fterraform-provider-mailgun.git bug fix and test for route --- 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 { ResourcesMap: map[string]*schema.Resource{ "mailgun_domain": resourceMailgunDomain(), + "mailgun_route": resourceMailgunRoute(), }, 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 ( "time" ) + type fullDomain struct { domainResponse mailgun.DomainResponse domainConnection mailgun.DomainConnection @@ -226,3 +227,4 @@ resource "mailgun_domain" "exemple" { } ` + 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 { }, 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 index 0000000..b3806c2 --- /dev/null +++ b/mailgun/resource_mailgun_route_test.go @@ -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()" + ] +} +`