2014-07-24 19:24:02 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
"github.com/digitalocean/godo"
|
2016-01-04 23:34:31 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2014-07-24 19:24:02 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccDigitalOceanDomain_Basic(t *testing.T) {
|
2015-09-27 06:06:51 +02:00
|
|
|
var domain godo.Domain
|
2016-01-04 23:34:31 +01:00
|
|
|
domainName := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
|
2014-07-24 19:24:02 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanDomainDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-04 23:34:31 +01:00
|
|
|
Config: fmt.Sprintf(testAccCheckDigitalOceanDomainConfig_basic, domainName),
|
2014-07-24 19:24:02 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanDomainExists("digitalocean_domain.foobar", &domain),
|
2016-01-04 23:34:31 +01:00
|
|
|
testAccCheckDigitalOceanDomainAttributes(&domain, domainName),
|
2014-07-24 19:24:02 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-04 23:34:31 +01:00
|
|
|
"digitalocean_domain.foobar", "name", domainName),
|
2014-07-24 19:24:02 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_domain.foobar", "ip_address", "192.168.0.10"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckDigitalOceanDomainDestroy(s *terraform.State) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := testAccProvider.Meta().(*godo.Client)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
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
|
2015-09-27 06:06:51 +02:00
|
|
|
_, _, err := client.Domains.Get(rs.Primary.ID)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
|
|
|
if err == nil {
|
2015-09-27 06:06:51 +02:00
|
|
|
return fmt.Errorf("Domain still exists")
|
2014-07-24 19:24:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:34:31 +01:00
|
|
|
func testAccCheckDigitalOceanDomainAttributes(domain *godo.Domain, name string) resource.TestCheckFunc {
|
2014-07-24 19:24:02 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
2016-01-04 23:34:31 +01:00
|
|
|
if domain.Name != name {
|
2014-07-24 19:24:02 +02:00
|
|
|
return fmt.Errorf("Bad name: %s", domain.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
func testAccCheckDigitalOceanDomainExists(n string, domain *godo.Domain) resource.TestCheckFunc {
|
2014-07-24 19:24:02 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
client := testAccProvider.Meta().(*godo.Client)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
foundDomain, _, err := client.Domains.Get(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")
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
*domain = *foundDomain
|
2014-07-24 19:24:02 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckDigitalOceanDomainConfig_basic = `
|
|
|
|
resource "digitalocean_domain" "foobar" {
|
2016-01-04 23:34:31 +01:00
|
|
|
name = "%s"
|
|
|
|
ip_address = "192.168.0.10"
|
2014-07-24 19:24:02 +02:00
|
|
|
}`
|