2014-08-19 18:14:14 +02:00
|
|
|
package heroku
|
|
|
|
|
|
|
|
import (
|
2017-03-10 13:00:03 +01:00
|
|
|
"context"
|
2014-08-19 18:14:14 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2014-08-27 16:50:37 +02:00
|
|
|
"github.com/cyberdelia/heroku-go/v3"
|
2016-01-22 16:26:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
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) {
|
2017-03-10 13:00:03 +01:00
|
|
|
var domain heroku.DomainInfoResult
|
2016-01-22 16:26:34 +01:00
|
|
|
appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
|
2014-08-19 18:14:14 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckHerokuDomainDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-10 13:00:03 +01:00
|
|
|
{
|
2016-01-22 16:26:34 +01:00
|
|
|
Config: testAccCheckHerokuDomainConfig_basic(appName),
|
2014-08-19 18:14:14 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckHerokuDomainExists("heroku_domain.foobar", &domain),
|
|
|
|
testAccCheckHerokuDomainAttributes(&domain),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"heroku_domain.foobar", "hostname", "terraform.example.com"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-22 16:26:34 +01:00
|
|
|
"heroku_domain.foobar", "app", appName),
|
2014-08-19 18:14:14 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-22 16:26:34 +01:00
|
|
|
"heroku_domain.foobar", "cname",
|
|
|
|
fmt.Sprintf("%s.herokuapp.com", appName)),
|
2014-08-19 18:14:14 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-09-17 02:20:47 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-08-19 18:14:14 +02:00
|
|
|
if rs.Type != "heroku_domain" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
_, err := client.DomainInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID)
|
2014-08-19 18:14:14 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Domain still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
func testAccCheckHerokuDomainAttributes(Domain *heroku.DomainInfoResult) resource.TestCheckFunc {
|
2014-08-19 18:14:14 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if Domain.Hostname != "terraform.example.com" {
|
|
|
|
return fmt.Errorf("Bad hostname: %s", Domain.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
func testAccCheckHerokuDomainExists(n string, Domain *heroku.DomainInfoResult) resource.TestCheckFunc {
|
2014-08-19 18:14:14 +02:00
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:20:47 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-08-19 18:14:14 +02:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:20:47 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-08-19 18:14:14 +02:00
|
|
|
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
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
foundDomain, err := client.DomainInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID)
|
2014-08-19 18:14:14 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:20:47 +02:00
|
|
|
if foundDomain.ID != rs.Primary.ID {
|
2014-08-19 18:14:14 +02:00
|
|
|
return fmt.Errorf("Domain not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*Domain = *foundDomain
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 16:26:34 +01:00
|
|
|
func testAccCheckHerokuDomainConfig_basic(appName string) string {
|
|
|
|
return fmt.Sprintf(`resource "heroku_app" "foobar" {
|
|
|
|
name = "%s"
|
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"
|
2016-01-22 16:26:34 +01:00
|
|
|
}`, appName)
|
|
|
|
}
|