2014-08-19 18:14:14 +02:00
|
|
|
package heroku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2014-08-27 16:50:37 +02:00
|
|
|
"github.com/cyberdelia/heroku-go/v3"
|
2014-08-19 18:14:14 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccHerokuDomain_Basic(t *testing.T) {
|
|
|
|
var domain heroku.Domain
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckHerokuDomainDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckHerokuDomainConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckHerokuDomainExists("heroku_domain.foobar", &domain),
|
|
|
|
testAccCheckHerokuDomainAttributes(&domain),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_domain.foobar", "hostname", "terraform.example.com"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_domain.foobar", "app", "terraform-test-app"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_domain.foobar", "cname", "terraform-test-app.herokuapp.com"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckHerokuDomainDestroy(s *terraform.State) error {
|
2014-08-27 16:50:37 +02:00
|
|
|
client := testAccProvider.Meta().(*heroku.Service)
|
2014-08-19 18:14:14 +02:00
|
|
|
|
|
|
|
for _, rs := range s.Resources {
|
|
|
|
if rs.Type != "heroku_domain" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.DomainInfo(rs.Attributes["app"], rs.ID)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Domain still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckHerokuDomainAttributes(Domain *heroku.Domain) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if Domain.Hostname != "terraform.example.com" {
|
|
|
|
return fmt.Errorf("Bad hostname: %s", Domain.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckHerokuDomainExists(n string, Domain *heroku.Domain) resource.TestCheckFunc {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2014-08-27 16:50:37 +02:00
|
|
|
client := testAccProvider.Meta().(*heroku.Service)
|
2014-08-19 18:14:14 +02:00
|
|
|
|
|
|
|
foundDomain, err := client.DomainInfo(rs.Attributes["app"], rs.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-27 16:50:37 +02:00
|
|
|
if foundDomain.ID != rs.ID {
|
2014-08-19 18:14:14 +02:00
|
|
|
return fmt.Errorf("Domain not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*Domain = *foundDomain
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckHerokuDomainConfig_basic = `
|
|
|
|
resource "heroku_app" "foobar" {
|
|
|
|
name = "terraform-test-app"
|
2014-08-28 00:50:38 +02:00
|
|
|
region = "us"
|
2014-08-19 18:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "heroku_domain" "foobar" {
|
|
|
|
app = "${heroku_app.foobar.name}"
|
|
|
|
hostname = "terraform.example.com"
|
|
|
|
}`
|