mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
![Paddy Carver](/assets/img/avatar_default.png)
The GCP backend apparently lowercases the values, no matter what you enter, so we consider uppercase and lowercase values to be equivalent. This fixes #862.
342 lines
9.5 KiB
Go
342 lines
9.5 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAccDnsRecordSet_basic(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
zoneName := fmt.Sprintf("dnszone-test-%s", acctest.RandString(10))
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDnsRecordSetDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_basic(zoneName, "127.0.0.10", 300),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
resource.TestStep{
|
|
ResourceName: "google_dns_record_set.foobar",
|
|
ImportStateId: fmt.Sprintf("%s/test-record.hashicorptest.com./A", zoneName),
|
|
ImportState: true,
|
|
ImportStateVerify: true,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccDnsRecordSet_modify(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
zoneName := fmt.Sprintf("dnszone-test-%s", acctest.RandString(10))
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDnsRecordSetDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_basic(zoneName, "127.0.0.10", 300),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_basic(zoneName, "127.0.0.11", 300),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_basic(zoneName, "127.0.0.11", 600),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccDnsRecordSet_changeType(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
zoneName := fmt.Sprintf("dnszone-test-%s", acctest.RandString(10))
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDnsRecordSetDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_basic(zoneName, "127.0.0.10", 300),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_bigChange(zoneName, 600),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccDnsRecordSet_ns(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
zoneName := fmt.Sprintf("dnszone-test-ns-%s", acctest.RandString(10))
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDnsRecordSetDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_ns(zoneName, 300),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
resource.TestStep{
|
|
ResourceName: "google_dns_record_set.foobar",
|
|
ImportStateId: fmt.Sprintf("%s/hashicorptest.com./NS", zoneName),
|
|
ImportState: true,
|
|
ImportStateVerify: true,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccDnsRecordSet_nestedNS(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
zoneName := fmt.Sprintf("dnszone-test-ns-%s", acctest.RandString(10))
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDnsRecordSetDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_nestedNS(zoneName, 300),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccDnsRecordSet_quotedTXT(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
zoneName := fmt.Sprintf("dnszone-test-txt-%s", acctest.RandString(10))
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDnsRecordSetDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_quotedTXT(zoneName, 300),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccDnsRecordSet_uppercaseMX(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
zoneName := fmt.Sprintf("dnszone-test-txt-%s", acctest.RandString(10))
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDnsRecordSetDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDnsRecordSet_uppercaseMX(zoneName, 300),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDnsRecordSetExists(
|
|
"google_dns_record_set.foobar", zoneName),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func testAccCheckDnsRecordSetExists(resourceType, resourceName string) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[resourceType]
|
|
if !ok {
|
|
return fmt.Errorf("Not found: %s", resourceName)
|
|
}
|
|
|
|
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(
|
|
config.Project, resourceName).Name(dnsName).Type(dnsType).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error confirming DNS RecordSet existence: %#v", err)
|
|
}
|
|
switch len(resp.Rrsets) {
|
|
case 0:
|
|
// The resource doesn't exist anymore
|
|
return fmt.Errorf("DNS RecordSet not found")
|
|
case 1:
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("Only expected 1 record set, got %d", len(resp.Rrsets))
|
|
}
|
|
}
|
|
}
|
|
|
|
func testAccDnsRecordSet_basic(zoneName string, addr2 string, ttl int) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
name = "%s"
|
|
dns_name = "%s.hashicorptest.com."
|
|
description = "Test Description"
|
|
}
|
|
resource "google_dns_record_set" "foobar" {
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
|
name = "test-record.%s.hashicorptest.com."
|
|
type = "A"
|
|
rrdatas = ["127.0.0.1", "%s"]
|
|
ttl = %d
|
|
}
|
|
`, zoneName, zoneName, zoneName, addr2, ttl)
|
|
}
|
|
|
|
func testAccDnsRecordSet_ns(name string, ttl int) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
name = "%s"
|
|
dns_name = "%s.hashicorptest.com."
|
|
description = "Test Description"
|
|
}
|
|
resource "google_dns_record_set" "foobar" {
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
|
name = "%s.hashicorptest.com."
|
|
type = "NS"
|
|
rrdatas = ["ns.hashicorp.services.", "ns2.hashicorp.services."]
|
|
ttl = %d
|
|
}
|
|
`, name, name, name, ttl)
|
|
}
|
|
|
|
func testAccDnsRecordSet_nestedNS(name string, ttl int) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
name = "%s"
|
|
dns_name = "%s.hashicorptest.com."
|
|
description = "Test Description"
|
|
}
|
|
resource "google_dns_record_set" "foobar" {
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
|
name = "nested.%s.hashicorptest.com."
|
|
type = "NS"
|
|
rrdatas = ["ns.hashicorp.services.", "ns2.hashicorp.services."]
|
|
ttl = %d
|
|
}
|
|
`, name, name, name, ttl)
|
|
}
|
|
|
|
func testAccDnsRecordSet_bigChange(zoneName string, ttl int) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
name = "%s"
|
|
dns_name = "%s.hashicorptest.com."
|
|
description = "Test Description"
|
|
}
|
|
resource "google_dns_record_set" "foobar" {
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
|
name = "test-record.%s.hashicorptest.com."
|
|
type = "CNAME"
|
|
rrdatas = ["www.terraform.io."]
|
|
ttl = %d
|
|
}
|
|
`, zoneName, zoneName, zoneName, ttl)
|
|
}
|
|
|
|
func testAccDnsRecordSet_quotedTXT(name string, ttl int) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
name = "%s"
|
|
dns_name = "%s.hashicorptest.com."
|
|
description = "Test Description"
|
|
}
|
|
resource "google_dns_record_set" "foobar" {
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
|
name = "test-record.%s.hashicorptest.com."
|
|
type = "TXT"
|
|
rrdatas = ["test", "\"quoted test\""]
|
|
ttl = %d
|
|
}
|
|
`, name, name, name, ttl)
|
|
}
|
|
|
|
func testAccDnsRecordSet_uppercaseMX(name string, ttl int) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
name = "%s"
|
|
dns_name = "%s.hashicorptest.com."
|
|
description = "Test Description"
|
|
}
|
|
resource "google_dns_record_set" "foobar" {
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
|
name = "test-record.%s.hashicorptest.com."
|
|
type = "MX"
|
|
rrdatas = [
|
|
"1 ASPMX.L.GOOGLE.COM.",
|
|
"5 ALT1.ASPMX.L.GOOGLE.COM.",
|
|
"5 ALT2.ASPMX.L.GOOGLE.COM.",
|
|
"10 ASPMX2.GOOGLEMAIL.COM.",
|
|
"10 ASPMX3.GOOGLEMAIL.COM.",
|
|
]
|
|
ttl = %d
|
|
}
|
|
`, name, name, name, ttl)
|
|
}
|