2016-02-03 01:48:09 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/jen20/riviera/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureRMDnsZone_basic(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMDnsZone_basic, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMDnsZoneDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMDnsZoneExists("azurerm_dns_zone.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:14:53 +01:00
|
|
|
func TestAccAzureRMDnsZone_withTags(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMDnsZone_withTags, ri, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMDnsZone_withTagsUupdate, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMDnsZoneDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: preConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMDnsZoneExists("azurerm_dns_zone.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_dns_zone.test", "tags.%", "2"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: postConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMDnsZoneExists("azurerm_dns_zone.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_dns_zone.test", "tags.%", "1"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-03 01:48:09 +01:00
|
|
|
func testCheckAzureRMDnsZoneExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
// Ensure we have enough information in state to look up in API
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
|
|
|
|
|
|
|
|
readRequest := conn.NewRequestForURI(rs.Primary.ID)
|
|
|
|
readRequest.Command = &dns.GetDNSZone{}
|
|
|
|
|
|
|
|
readResponse, err := readRequest.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: GetDNSZone: %s", err)
|
2016-02-03 19:16:36 +01:00
|
|
|
}
|
|
|
|
if !readResponse.IsSuccessful() {
|
2016-02-03 01:48:09 +01:00
|
|
|
return fmt.Errorf("Bad: GetDNSZone: %s", readResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMDnsZoneDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_dns_zone" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
readRequest := conn.NewRequestForURI(rs.Primary.ID)
|
|
|
|
readRequest.Command = &dns.GetDNSZone{}
|
|
|
|
|
|
|
|
readResponse, err := readRequest.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: GetDNSZone: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if readResponse.IsSuccessful() {
|
|
|
|
return fmt.Errorf("Bad: DNS zone still exists: %s", readResponse.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureRMDnsZone_basic = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG_%d"
|
2016-02-03 01:48:09 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_dns_zone" "test" {
|
|
|
|
name = "acctestzone%d.com"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
}
|
|
|
|
`
|
2016-12-15 15:14:53 +01:00
|
|
|
|
|
|
|
var testAccAzureRMDnsZone_withTags = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
|
|
name = "acctestRG_%d"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_dns_zone" "test" {
|
|
|
|
name = "acctestzone%d.com"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
tags {
|
|
|
|
environment = "Production"
|
|
|
|
cost_center = "MSFT"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMDnsZone_withTagsUupdate = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
|
|
name = "acctestRG_%d"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_dns_zone" "test" {
|
|
|
|
name = "acctestzone%d.com"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
tags {
|
|
|
|
environment = "staging"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|