2014-08-25 00:40:54 +02:00
|
|
|
package mailgun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/pearkes/mailgun"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccMailgunDomain_Basic(t *testing.T) {
|
2014-08-29 20:15:14 +02:00
|
|
|
var resp mailgun.DomainResponse
|
2014-08-25 00:40:54 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckMailgunDomainDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckMailgunDomainConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2014-08-29 20:15:14 +02:00
|
|
|
testAccCheckMailgunDomainExists("mailgun_domain.foobar", &resp),
|
|
|
|
testAccCheckMailgunDomainAttributes(&resp),
|
2014-08-25 00:40:54 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"mailgun_domain.foobar", "name", "terraform.example.com"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"mailgun_domain.foobar", "spam_action", "disabled"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"mailgun_domain.foobar", "smtp_password", "foobar"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"mailgun_domain.foobar", "wildcard", "true"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckMailgunDomainDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*mailgun.Client)
|
|
|
|
|
|
|
|
for _, rs := range s.Resources {
|
|
|
|
if rs.Type != "mailgun_domain" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.RetrieveDomain(rs.ID)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Domain still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
func testAccCheckMailgunDomainAttributes(DomainResp *mailgun.DomainResponse) resource.TestCheckFunc {
|
2014-08-25 00:40:54 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
if DomainResp.Domain.Name != "terraform.example.com" {
|
|
|
|
return fmt.Errorf("Bad name: %s", DomainResp.Domain.Name)
|
2014-08-25 00:40:54 +02:00
|
|
|
}
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
if DomainResp.Domain.SpamAction != "disabled" {
|
|
|
|
return fmt.Errorf("Bad spam_action: %s", DomainResp.Domain.SpamAction)
|
2014-08-25 00:40:54 +02:00
|
|
|
}
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
if DomainResp.Domain.Wildcard != true {
|
|
|
|
return fmt.Errorf("Bad wildcard: %s", DomainResp.Domain.Wildcard)
|
2014-08-25 00:40:54 +02:00
|
|
|
}
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
if DomainResp.Domain.SmtpPassword != "foobar" {
|
|
|
|
return fmt.Errorf("Bad smtp_password: %s", DomainResp.Domain.SmtpPassword)
|
2014-08-25 00:40:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
func testAccCheckMailgunDomainExists(n string, DomainResp *mailgun.DomainResponse) resource.TestCheckFunc {
|
2014-08-25 00:40:54 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.Resources[n]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.ID == "" {
|
|
|
|
return fmt.Errorf("No Domain ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*mailgun.Client)
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
resp, err := client.RetrieveDomain(rs.ID)
|
2014-08-25 00:40:54 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
if resp.Domain.Name != rs.ID {
|
2014-08-25 00:40:54 +02:00
|
|
|
return fmt.Errorf("Domain not found")
|
|
|
|
}
|
|
|
|
|
2014-08-29 20:15:14 +02:00
|
|
|
*DomainResp = resp
|
2014-08-25 00:40:54 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckMailgunDomainConfig_basic = `
|
|
|
|
resource "mailgun_domain" "foobar" {
|
|
|
|
name = "terraform.example.com"
|
|
|
|
spam_action = "disabled"
|
|
|
|
smtp_password = "foobar"
|
|
|
|
wildcard = true
|
|
|
|
}`
|