2015-04-30 07:32:34 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-05 22:47:10 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-04-30 07:32:34 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccDnsRecordSet_basic(t *testing.T) {
|
2016-01-06 01:49:06 +01:00
|
|
|
zoneName := fmt.Sprintf("dnszone-test-%s", acctest.RandString(10))
|
2015-04-30 07:32:34 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDnsRecordSetDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-06 01:49:06 +01:00
|
|
|
Config: testAccDnsRecordSet_basic(zoneName),
|
2015-04-30 07:32:34 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDnsRecordSetExists(
|
2016-01-06 01:49:06 +01:00
|
|
|
"google_dns_record_set.foobar", zoneName),
|
2015-04-30 07:32:34 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckDnsRecordSetDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
// Deletion of the managed_zone implies everything is gone
|
|
|
|
if rs.Type == "google_dns_managed_zone" {
|
|
|
|
_, err := config.clientDns.ManagedZones.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("DNS ManagedZone still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-06 01:49:06 +01:00
|
|
|
func testAccCheckDnsRecordSetExists(resourceType, resourceName string) resource.TestCheckFunc {
|
2015-04-30 07:32:34 +02:00
|
|
|
return func(s *terraform.State) error {
|
2016-01-06 01:49:06 +01:00
|
|
|
rs, ok := s.RootModule().Resources[resourceType]
|
2015-04-30 07:32:34 +02:00
|
|
|
if !ok {
|
2016-01-06 01:49:06 +01:00
|
|
|
return fmt.Errorf("Not found: %s", resourceName)
|
2015-04-30 07:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dnsName := rs.Primary.Attributes["name"]
|
|
|
|
dnsType := rs.Primary.Attributes["type"]
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
resp, err := config.clientDns.ResourceRecordSets.List(
|
2016-01-06 01:49:06 +01:00
|
|
|
config.Project, resourceName).Name(dnsName).Type(dnsType).Do()
|
2015-04-30 07:32:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error confirming DNS RecordSet existence: %#v", err)
|
|
|
|
}
|
|
|
|
if len(resp.Rrsets) == 0 {
|
|
|
|
// The resource doesn't exist anymore
|
|
|
|
return fmt.Errorf("DNS RecordSet not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Rrsets) > 1 {
|
|
|
|
return fmt.Errorf("Only expected 1 record set, got %d", len(resp.Rrsets))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 01:49:06 +01:00
|
|
|
func testAccDnsRecordSet_basic(zoneName string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
|
|
name = "%s"
|
|
|
|
dns_name = "terraform.test."
|
|
|
|
description = "Test Description"
|
|
|
|
}
|
|
|
|
resource "google_dns_record_set" "foobar" {
|
|
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
|
|
|
name = "test-record.terraform.test."
|
|
|
|
type = "A"
|
|
|
|
rrdatas = ["127.0.0.1", "127.0.0.10"]
|
|
|
|
ttl = 600
|
|
|
|
}
|
|
|
|
`, zoneName)
|
2015-04-30 07:32:34 +02:00
|
|
|
}
|