2014-07-24 19:24:02 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/pearkes/digitalocean"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccDigitalOceanDomain_Basic(t *testing.T) {
|
|
|
|
var domain digitalocean.Domain
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanDomainDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckDigitalOceanDomainConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanDomainExists("digitalocean_domain.foobar", &domain),
|
|
|
|
testAccCheckDigitalOceanDomainAttributes(&domain),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_domain.foobar", "name", "foobar-test-terraform.com"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_domain.foobar", "ip_address", "192.168.0.10"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckDigitalOceanDomainDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.client
|
|
|
|
|
2014-09-17 02:28:22 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-24 19:24:02 +02:00
|
|
|
if rs.Type != "digitalocean_domain" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the domain
|
2014-09-17 02:28:22 +02:00
|
|
|
_, err := client.RetrieveDomain(rs.Primary.ID)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
fmt.Errorf("Domain still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckDigitalOceanDomainAttributes(domain *digitalocean.Domain) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if domain.Name != "foobar-test-terraform.com" {
|
|
|
|
return fmt.Errorf("Bad name: %s", domain.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckDigitalOceanDomainExists(n string, domain *digitalocean.Domain) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:28:22 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-24 19:24:02 +02:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:28:22 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-24 19:24:02 +02:00
|
|
|
return fmt.Errorf("No Record ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.client
|
|
|
|
|
2014-09-17 02:28:22 +02:00
|
|
|
foundDomain, err := client.RetrieveDomain(rs.Primary.ID)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:28:22 +02:00
|
|
|
if foundDomain.Name != rs.Primary.ID {
|
2014-07-24 19:24:02 +02:00
|
|
|
return fmt.Errorf("Record not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*domain = foundDomain
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckDigitalOceanDomainConfig_basic = `
|
|
|
|
resource "digitalocean_domain" "foobar" {
|
|
|
|
name = "foobar-test-terraform.com"
|
|
|
|
ip_address = "192.168.0.10"
|
|
|
|
}`
|