terraform/builtin/providers/digitalocean/resource_digitalocean_domai...

103 lines
2.5 KiB
Go
Raw Normal View History

2014-07-24 19:24:02 +02:00
package digitalocean
import (
"context"
2014-07-24 19:24:02 +02:00
"fmt"
"testing"
"github.com/digitalocean/godo"
"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) {
var domain godo.Domain
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{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDomainConfig_basic, domainName),
2014-07-24 19:24:02 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanDomainExists("digitalocean_domain.foobar", &domain),
testAccCheckDigitalOceanDomainAttributes(&domain, domainName),
2014-07-24 19:24:02 +02:00
resource.TestCheckResourceAttr(
"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 {
client := testAccProvider.Meta().(*godo.Client)
2014-07-24 19:24:02 +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
_, _, err := client.Domains.Get(context.Background(), rs.Primary.ID)
2014-07-24 19:24:02 +02:00
if err == nil {
return fmt.Errorf("Domain still exists")
2014-07-24 19:24:02 +02:00
}
}
return nil
}
func testAccCheckDigitalOceanDomainAttributes(domain *godo.Domain, name string) resource.TestCheckFunc {
2014-07-24 19:24:02 +02:00
return func(s *terraform.State) error {
if domain.Name != name {
2014-07-24 19:24:02 +02:00
return fmt.Errorf("Bad name: %s", domain.Name)
}
return nil
}
}
func testAccCheckDigitalOceanDomainExists(n string, domain *godo.Domain) resource.TestCheckFunc {
2014-07-24 19:24:02 +02:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
2014-07-24 19:24:02 +02:00
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
2014-07-24 19:24:02 +02:00
return fmt.Errorf("No Record ID is set")
}
client := testAccProvider.Meta().(*godo.Client)
2014-07-24 19:24:02 +02:00
foundDomain, _, err := client.Domains.Get(context.Background(), rs.Primary.ID)
2014-07-24 19:24:02 +02:00
if err != nil {
return err
}
if foundDomain.Name != rs.Primary.ID {
2014-07-24 19:24:02 +02:00
return fmt.Errorf("Record not found")
}
*domain = *foundDomain
2014-07-24 19:24:02 +02:00
return nil
}
}
const testAccCheckDigitalOceanDomainConfig_basic = `
resource "digitalocean_domain" "foobar" {
name = "%s"
ip_address = "192.168.0.10"
2014-07-24 19:24:02 +02:00
}`