2015-04-30 05:32:34 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-05 21:47:10 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-04-30 05:32:34 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2018-05-29 21:43:39 +00:00
|
|
|
func TestIpv6AddressDiffSuppress(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Old, New string
|
|
|
|
ShouldSuppress bool
|
|
|
|
}{
|
|
|
|
"compact form should suppress diff": {
|
|
|
|
Old: "2a03:b0c0:1:e0::29b:8001",
|
|
|
|
New: "2a03:b0c0:0001:00e0:0000:0000:029b:8001",
|
|
|
|
ShouldSuppress: true,
|
|
|
|
},
|
|
|
|
"different address should not suppress diff": {
|
|
|
|
Old: "2a03:b0c0:1:e00::29b:8001",
|
|
|
|
New: "2a03:b0c0:0001:00e0:0000:0000:029b:8001",
|
|
|
|
ShouldSuppress: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for tn, tc := range cases {
|
|
|
|
shouldSuppress := ipv6AddressDiffSuppress("", tc.Old, tc.New, nil)
|
|
|
|
if shouldSuppress != tc.ShouldSuppress {
|
|
|
|
t.Errorf("%s: expected %t", tn, tc.ShouldSuppress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 05:32:34 +00:00
|
|
|
func TestAccDnsRecordSet_basic(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-01-06 00:49:06 +00:00
|
|
|
zoneName := fmt.Sprintf("dnszone-test-%s", acctest.RandString(10))
|
2015-04-30 05:32:34 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDnsRecordSetDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-08-08 00:36:27 +00:00
|
|
|
Config: testAccDnsRecordSet_basic(zoneName, "127.0.0.10", 300),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDnsRecordSetExists(
|
|
|
|
"google_dns_record_set.foobar", zoneName),
|
|
|
|
),
|
|
|
|
},
|
2018-01-17 19:19:55 +00:00
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_dns_record_set.foobar",
|
2018-05-10 16:26:02 +00:00
|
|
|
ImportStateId: fmt.Sprintf("%s/test-record.%s.hashicorptest.com./A", zoneName, zoneName),
|
2018-01-17 19:19:55 +00:00
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2016-08-08 00:36:27 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccDnsRecordSet_modify(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-08-08 00:36:27 +00:00
|
|
|
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) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-08-08 00:36:27 +00:00
|
|
|
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),
|
2015-04-30 05:32:34 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDnsRecordSetExists(
|
2016-01-06 00:49:06 +00:00
|
|
|
"google_dns_record_set.foobar", zoneName),
|
2015-04-30 05:32:34 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-25 09:08:35 +00:00
|
|
|
func TestAccDnsRecordSet_ns(t *testing.T) {
|
2018-01-17 19:19:55 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-08-25 09:08:35 +00:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
},
|
2018-01-17 19:19:55 +00:00
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_dns_record_set.foobar",
|
2018-05-10 16:26:02 +00:00
|
|
|
ImportStateId: fmt.Sprintf("%s/%s.hashicorptest.com./NS", zoneName, zoneName),
|
2018-01-17 19:19:55 +00:00
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2017-08-25 09:08:35 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-30 00:21:01 +00:00
|
|
|
func TestAccDnsRecordSet_nestedNS(t *testing.T) {
|
2018-01-17 19:19:55 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-11-30 00:21:01 +00:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-01 20:50:47 +00:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-01 21:01:30 +00:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-30 05:32:34 +00: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 00:49:06 +00:00
|
|
|
func testAccCheckDnsRecordSetExists(resourceType, resourceName string) resource.TestCheckFunc {
|
2015-04-30 05:32:34 +00:00
|
|
|
return func(s *terraform.State) error {
|
2016-01-06 00:49:06 +00:00
|
|
|
rs, ok := s.RootModule().Resources[resourceType]
|
2015-04-30 05:32:34 +00:00
|
|
|
if !ok {
|
2016-01-06 00:49:06 +00:00
|
|
|
return fmt.Errorf("Not found: %s", resourceName)
|
2015-04-30 05:32:34 +00: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 00:49:06 +00:00
|
|
|
config.Project, resourceName).Name(dnsName).Type(dnsType).Do()
|
2015-04-30 05:32:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error confirming DNS RecordSet existence: %#v", err)
|
|
|
|
}
|
2016-08-08 00:36:27 +00:00
|
|
|
switch len(resp.Rrsets) {
|
|
|
|
case 0:
|
2015-04-30 05:32:34 +00:00
|
|
|
// The resource doesn't exist anymore
|
|
|
|
return fmt.Errorf("DNS RecordSet not found")
|
2016-08-08 00:36:27 +00:00
|
|
|
case 1:
|
|
|
|
return nil
|
|
|
|
default:
|
2015-04-30 05:32:34 +00:00
|
|
|
return fmt.Errorf("Only expected 1 record set, got %d", len(resp.Rrsets))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 00:36:27 +00:00
|
|
|
func testAccDnsRecordSet_basic(zoneName string, addr2 string, ttl int) string {
|
2016-01-06 00:49:06 +00:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
|
|
name = "%s"
|
2018-03-01 20:50:47 +00:00
|
|
|
dns_name = "%s.hashicorptest.com."
|
2016-01-06 00:49:06 +00:00
|
|
|
description = "Test Description"
|
|
|
|
}
|
|
|
|
resource "google_dns_record_set" "foobar" {
|
|
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
2018-03-01 20:50:47 +00:00
|
|
|
name = "test-record.%s.hashicorptest.com."
|
2016-01-06 00:49:06 +00:00
|
|
|
type = "A"
|
2016-08-08 00:36:27 +00:00
|
|
|
rrdatas = ["127.0.0.1", "%s"]
|
|
|
|
ttl = %d
|
|
|
|
}
|
2018-03-01 20:50:47 +00:00
|
|
|
`, zoneName, zoneName, zoneName, addr2, ttl)
|
2016-08-08 00:36:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 09:08:35 +00:00
|
|
|
func testAccDnsRecordSet_ns(name string, ttl int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
|
|
name = "%s"
|
2018-03-01 20:50:47 +00:00
|
|
|
dns_name = "%s.hashicorptest.com."
|
2017-08-25 09:08:35 +00:00
|
|
|
description = "Test Description"
|
|
|
|
}
|
|
|
|
resource "google_dns_record_set" "foobar" {
|
|
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
2018-03-01 20:50:47 +00:00
|
|
|
name = "%s.hashicorptest.com."
|
2017-08-25 09:08:35 +00:00
|
|
|
type = "NS"
|
|
|
|
rrdatas = ["ns.hashicorp.services.", "ns2.hashicorp.services."]
|
|
|
|
ttl = %d
|
|
|
|
}
|
2018-03-01 20:50:47 +00:00
|
|
|
`, name, name, name, ttl)
|
2017-08-25 09:08:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 00:21:01 +00:00
|
|
|
func testAccDnsRecordSet_nestedNS(name string, ttl int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
|
|
name = "%s"
|
2018-03-01 20:50:47 +00:00
|
|
|
dns_name = "%s.hashicorptest.com."
|
2017-11-30 00:21:01 +00:00
|
|
|
description = "Test Description"
|
|
|
|
}
|
|
|
|
resource "google_dns_record_set" "foobar" {
|
|
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
2018-03-01 20:50:47 +00:00
|
|
|
name = "nested.%s.hashicorptest.com."
|
2017-11-30 00:21:01 +00:00
|
|
|
type = "NS"
|
|
|
|
rrdatas = ["ns.hashicorp.services.", "ns2.hashicorp.services."]
|
|
|
|
ttl = %d
|
|
|
|
}
|
2018-03-01 20:50:47 +00:00
|
|
|
`, name, name, name, ttl)
|
2017-11-30 00:21:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 00:36:27 +00:00
|
|
|
func testAccDnsRecordSet_bigChange(zoneName string, ttl int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_dns_managed_zone" "parent-zone" {
|
|
|
|
name = "%s"
|
2018-03-01 20:50:47 +00:00
|
|
|
dns_name = "%s.hashicorptest.com."
|
2016-08-08 00:36:27 +00:00
|
|
|
description = "Test Description"
|
|
|
|
}
|
|
|
|
resource "google_dns_record_set" "foobar" {
|
|
|
|
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
|
2018-03-01 20:50:47 +00:00
|
|
|
name = "test-record.%s.hashicorptest.com."
|
2016-08-08 00:36:27 +00:00
|
|
|
type = "CNAME"
|
|
|
|
rrdatas = ["www.terraform.io."]
|
|
|
|
ttl = %d
|
2016-01-06 00:49:06 +00:00
|
|
|
}
|
2018-03-01 20:50:47 +00:00
|
|
|
`, 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)
|
2015-04-30 05:32:34 +00:00
|
|
|
}
|
2018-03-01 21:01:30 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|