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"),
|
2014-08-29 20:39:06 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"mailgun_domain.foobar", "receiving_records.0.priority", "10"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"mailgun_domain.foobar", "sending_records.0.name", "terraform.example.com"),
|
2014-08-25 00:40:54 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckMailgunDomainDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*mailgun.Client)
|
|
|
|
|
2014-09-17 02:21:49 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-08-25 00:40:54 +02:00
|
|
|
if rs.Type != "mailgun_domain" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:51:08 +01:00
|
|
|
resp, err := client.RetrieveDomain(rs.Primary.ID)
|
2014-08-25 00:40:54 +02:00
|
|
|
|
|
|
|
if err == nil {
|
2016-01-21 20:02:26 +01:00
|
|
|
return fmt.Errorf("Domain still exists: %#v", resp)
|
2014-08-25 00:40:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2014-12-17 12:59:07 +01:00
|
|
|
return fmt.Errorf("Bad wildcard: %t", 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
|
|
|
}
|
|
|
|
|
2014-08-29 20:39:06 +02:00
|
|
|
if DomainResp.ReceivingRecords[0].Priority == "" {
|
|
|
|
return fmt.Errorf("Bad receiving_records: %s", DomainResp.ReceivingRecords)
|
|
|
|
}
|
|
|
|
|
|
|
|
if DomainResp.SendingRecords[0].Name == "" {
|
|
|
|
return fmt.Errorf("Bad sending_records: %s", DomainResp.SendingRecords)
|
|
|
|
}
|
|
|
|
|
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 {
|
2014-09-17 02:21:49 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-08-25 00:40:54 +02:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:21:49 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-08-25 00:40:54 +02:00
|
|
|
return fmt.Errorf("No Domain ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*mailgun.Client)
|
|
|
|
|
2014-09-17 02:21:49 +02:00
|
|
|
resp, err := client.RetrieveDomain(rs.Primary.ID)
|
2014-08-25 00:40:54 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:21:49 +02:00
|
|
|
if resp.Domain.Name != rs.Primary.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
|
|
|
|
}`
|