2014-07-24 19:47:22 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-27 06:06:51 +02:00
|
|
|
"strconv"
|
2014-07-24 19:47:22 +02:00
|
|
|
"testing"
|
|
|
|
|
2016-02-09 17:57:42 +01:00
|
|
|
"strings"
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
"github.com/digitalocean/godo"
|
2016-01-04 22:21:38 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2014-07-24 19:47:22 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2016-02-09 17:57:42 +01:00
|
|
|
func TestDigitalOceanRecordConstructFqdn(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input, Output string
|
|
|
|
}{
|
|
|
|
{"www", "www.nonexample.com"},
|
|
|
|
{"dev.www", "dev.www.nonexample.com"},
|
|
|
|
{"*", "*.nonexample.com"},
|
|
|
|
{"nonexample.com", "nonexample.com"},
|
|
|
|
{"test.nonexample.com", "test.nonexample.com"},
|
|
|
|
{"test.nonexample.com.", "test.nonexample.com"},
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := "nonexample.com"
|
|
|
|
for _, tc := range cases {
|
|
|
|
actual := constructFqdn(tc.Input, domain)
|
|
|
|
if actual != tc.Output {
|
|
|
|
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 19:47:22 +02:00
|
|
|
func TestAccDigitalOceanRecord_Basic(t *testing.T) {
|
2015-09-27 06:06:51 +02:00
|
|
|
var record godo.DomainRecord
|
2016-01-04 22:21:38 +01:00
|
|
|
domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
|
2014-07-24 19:47:22 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-20 18:47:54 +01:00
|
|
|
{
|
2016-01-04 22:21:38 +01:00
|
|
|
Config: fmt.Sprintf(testAccCheckDigitalOceanRecordConfig_basic, domain),
|
2014-07-24 19:47:22 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
|
|
|
|
testAccCheckDigitalOceanRecordAttributes(&record),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "name", "terraform"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-04 22:21:38 +01:00
|
|
|
"digitalocean_record.foobar", "domain", domain),
|
2014-07-24 19:47:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "value", "192.168.0.10"),
|
2016-02-09 17:57:42 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "fqdn", strings.Join([]string{"terraform", domain}, ".")),
|
2014-07-24 19:47:22 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccDigitalOceanRecord_Updated(t *testing.T) {
|
2015-09-27 06:06:51 +02:00
|
|
|
var record godo.DomainRecord
|
2016-01-04 22:21:38 +01:00
|
|
|
domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
|
2014-07-24 19:47:22 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-20 18:47:54 +01:00
|
|
|
{
|
2016-01-04 22:21:38 +01:00
|
|
|
Config: fmt.Sprintf(testAccCheckDigitalOceanRecordConfig_basic, domain),
|
2014-07-24 19:47:22 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
|
|
|
|
testAccCheckDigitalOceanRecordAttributes(&record),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "name", "terraform"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-04 22:21:38 +01:00
|
|
|
"digitalocean_record.foobar", "domain", domain),
|
2014-07-24 19:47:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "value", "192.168.0.10"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "type", "A"),
|
|
|
|
),
|
|
|
|
},
|
2017-01-20 18:47:54 +01:00
|
|
|
{
|
2016-01-04 22:21:38 +01:00
|
|
|
Config: fmt.Sprintf(
|
|
|
|
testAccCheckDigitalOceanRecordConfig_new_value, domain),
|
2014-07-24 19:47:22 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
|
|
|
|
testAccCheckDigitalOceanRecordAttributesUpdated(&record),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "name", "terraform"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-04 22:21:38 +01:00
|
|
|
"digitalocean_record.foobar", "domain", domain),
|
2014-07-24 19:47:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "value", "192.168.0.11"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "type", "A"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-25 05:30:09 +01:00
|
|
|
func TestAccDigitalOceanRecord_HostnameValue(t *testing.T) {
|
2015-09-27 06:06:51 +02:00
|
|
|
var record godo.DomainRecord
|
2016-01-04 22:21:38 +01:00
|
|
|
domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
|
2015-01-25 05:30:09 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-20 18:47:54 +01:00
|
|
|
{
|
2016-01-04 22:21:38 +01:00
|
|
|
Config: fmt.Sprintf(
|
|
|
|
testAccCheckDigitalOceanRecordConfig_cname, domain),
|
2015-01-25 05:30:09 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
|
2016-01-04 22:21:38 +01:00
|
|
|
testAccCheckDigitalOceanRecordAttributesHostname("a.foobar-test-terraform.com", &record),
|
2015-01-25 05:30:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "name", "terraform"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-04 22:21:38 +01:00
|
|
|
"digitalocean_record.foobar", "domain", domain),
|
2015-01-25 05:30:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-02-28 11:55:10 +01:00
|
|
|
"digitalocean_record.foobar", "value", "a.foobar-test-terraform.com."),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "type", "CNAME"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccDigitalOceanRecord_ExternalHostnameValue(t *testing.T) {
|
2015-09-27 06:06:51 +02:00
|
|
|
var record godo.DomainRecord
|
2016-01-04 22:21:38 +01:00
|
|
|
domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
|
2015-02-28 11:55:10 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-20 18:47:54 +01:00
|
|
|
{
|
2016-01-04 22:21:38 +01:00
|
|
|
Config: fmt.Sprintf(
|
|
|
|
testAccCheckDigitalOceanRecordConfig_external_cname, domain),
|
2015-02-28 11:55:10 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
|
|
|
|
testAccCheckDigitalOceanRecordAttributesHostname("a.foobar-test-terraform.net", &record),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "name", "terraform"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-04 22:21:38 +01:00
|
|
|
"digitalocean_record.foobar", "domain", domain),
|
2015-02-28 11:55:10 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "value", "a.foobar-test-terraform.net."),
|
2015-01-25 05:30:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_record.foobar", "type", "CNAME"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-24 19:47:22 +02:00
|
|
|
func testAccCheckDigitalOceanRecordDestroy(s *terraform.State) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := testAccProvider.Meta().(*godo.Client)
|
2014-07-24 19:47:22 +02:00
|
|
|
|
2014-09-17 02:28:22 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-24 19:47:22 +02:00
|
|
|
if rs.Type != "digitalocean_record" {
|
|
|
|
continue
|
|
|
|
}
|
2015-09-27 06:06:51 +02:00
|
|
|
domain := rs.Primary.Attributes["domain"]
|
|
|
|
id, err := strconv.Atoi(rs.Primary.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-24 19:47:22 +02:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
_, _, err = client.Domains.Record(domain, id)
|
2014-07-24 19:47:22 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Record still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
func testAccCheckDigitalOceanRecordAttributes(record *godo.DomainRecord) resource.TestCheckFunc {
|
2014-07-24 19:47:22 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if record.Data != "192.168.0.10" {
|
|
|
|
return fmt.Errorf("Bad value: %s", record.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
func testAccCheckDigitalOceanRecordAttributesUpdated(record *godo.DomainRecord) resource.TestCheckFunc {
|
2014-07-24 19:47:22 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if record.Data != "192.168.0.11" {
|
|
|
|
return fmt.Errorf("Bad value: %s", record.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
func testAccCheckDigitalOceanRecordExists(n string, record *godo.DomainRecord) resource.TestCheckFunc {
|
2014-07-24 19:47:22 +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:47:22 +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:47:22 +02:00
|
|
|
return fmt.Errorf("No Record ID is set")
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
client := testAccProvider.Meta().(*godo.Client)
|
|
|
|
|
|
|
|
domain := rs.Primary.Attributes["domain"]
|
|
|
|
id, err := strconv.Atoi(rs.Primary.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-24 19:47:22 +02:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
foundRecord, _, err := client.Domains.Record(domain, id)
|
2014-07-24 19:47:22 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
if strconv.Itoa(foundRecord.ID) != rs.Primary.ID {
|
2014-07-24 19:47:22 +02:00
|
|
|
return fmt.Errorf("Record not found")
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
*record = *foundRecord
|
2014-07-24 19:47:22 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
func testAccCheckDigitalOceanRecordAttributesHostname(data string, record *godo.DomainRecord) resource.TestCheckFunc {
|
2015-01-25 05:30:09 +01:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
2015-02-28 11:55:10 +01:00
|
|
|
if record.Data != data {
|
|
|
|
return fmt.Errorf("Bad value: expected %s, got %s", data, record.Data)
|
2015-01-25 05:30:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 19:47:22 +02:00
|
|
|
const testAccCheckDigitalOceanRecordConfig_basic = `
|
|
|
|
resource "digitalocean_domain" "foobar" {
|
2016-01-04 22:21:38 +01:00
|
|
|
name = "%s"
|
|
|
|
ip_address = "192.168.0.10"
|
2014-07-24 19:47:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "digitalocean_record" "foobar" {
|
2016-01-04 22:21:38 +01:00
|
|
|
domain = "${digitalocean_domain.foobar.name}"
|
2014-07-24 19:47:22 +02:00
|
|
|
|
2016-01-04 22:21:38 +01:00
|
|
|
name = "terraform"
|
|
|
|
value = "192.168.0.10"
|
|
|
|
type = "A"
|
2014-07-24 19:47:22 +02:00
|
|
|
}`
|
|
|
|
|
|
|
|
const testAccCheckDigitalOceanRecordConfig_new_value = `
|
|
|
|
resource "digitalocean_domain" "foobar" {
|
2016-01-04 22:21:38 +01:00
|
|
|
name = "%s"
|
|
|
|
ip_address = "192.168.0.10"
|
2014-07-24 19:47:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "digitalocean_record" "foobar" {
|
2016-01-04 22:21:38 +01:00
|
|
|
domain = "${digitalocean_domain.foobar.name}"
|
2014-07-24 19:47:22 +02:00
|
|
|
|
2016-01-04 22:21:38 +01:00
|
|
|
name = "terraform"
|
|
|
|
value = "192.168.0.11"
|
|
|
|
type = "A"
|
2014-07-24 19:47:22 +02:00
|
|
|
}`
|
2015-01-25 05:30:09 +01:00
|
|
|
|
|
|
|
const testAccCheckDigitalOceanRecordConfig_cname = `
|
|
|
|
resource "digitalocean_domain" "foobar" {
|
2016-01-04 22:21:38 +01:00
|
|
|
name = "%s"
|
|
|
|
ip_address = "192.168.0.10"
|
2015-01-25 05:30:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "digitalocean_record" "foobar" {
|
2016-01-04 22:21:38 +01:00
|
|
|
domain = "${digitalocean_domain.foobar.name}"
|
2015-01-25 05:30:09 +01:00
|
|
|
|
2016-01-04 22:21:38 +01:00
|
|
|
name = "terraform"
|
|
|
|
value = "a.foobar-test-terraform.com."
|
|
|
|
type = "CNAME"
|
2015-02-28 11:55:10 +01:00
|
|
|
}`
|
|
|
|
|
|
|
|
const testAccCheckDigitalOceanRecordConfig_external_cname = `
|
|
|
|
resource "digitalocean_domain" "foobar" {
|
2016-01-04 22:21:38 +01:00
|
|
|
name = "%s"
|
|
|
|
ip_address = "192.168.0.10"
|
2015-02-28 11:55:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "digitalocean_record" "foobar" {
|
2016-01-04 22:21:38 +01:00
|
|
|
domain = "${digitalocean_domain.foobar.name}"
|
2015-02-28 11:55:10 +01:00
|
|
|
|
2016-01-04 22:21:38 +01:00
|
|
|
name = "terraform"
|
|
|
|
value = "a.foobar-test-terraform.net."
|
|
|
|
type = "CNAME"
|
2015-01-25 05:30:09 +01:00
|
|
|
}`
|