2015-06-05 16:12:21 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureDnsServerBasic(t *testing.T) {
|
|
|
|
name := "azure_dns_server.foo"
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAzureDnsServerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureDnsServerBasic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureDnsServerExists(name),
|
|
|
|
resource.TestCheckResourceAttr(name, "name", "terraform-dns-server"),
|
|
|
|
resource.TestCheckResourceAttr(name, "dns_address", "8.8.8.8"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureDnsServerUpdate(t *testing.T) {
|
|
|
|
name := "azure_dns_server.foo"
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAzureDnsServerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureDnsServerBasic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureDnsServerExists(name),
|
|
|
|
resource.TestCheckResourceAttr(name, "name", "terraform-dns-server"),
|
|
|
|
resource.TestCheckResourceAttr(name, "dns_address", "8.8.8.8"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureDnsServerUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureDnsServerExists(name),
|
|
|
|
resource.TestCheckResourceAttr(name, "name", "terraform-dns-server"),
|
|
|
|
resource.TestCheckResourceAttr(name, "dns_address", "8.8.4.4"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureDnsServerExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
resource, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Resource not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resource.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No DNS Server ID set.")
|
|
|
|
}
|
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
vnetClient := testAccProvider.Meta().(*Client).vnetClient
|
|
|
|
netConf, err := vnetClient.GetVirtualNetworkConfiguration()
|
2015-06-05 16:12:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed fetching networking configuration: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dns := range netConf.Configuration.DNS.DNSServers {
|
|
|
|
if dns.Name == resource.Primary.ID {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Azure DNS Server not found.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureDnsServerDestroy(s *terraform.State) error {
|
2015-06-16 19:54:52 +02:00
|
|
|
vnetClient := testAccProvider.Meta().(*Client).vnetClient
|
2015-06-05 16:12:21 +02:00
|
|
|
|
|
|
|
for _, resource := range s.RootModule().Resources {
|
|
|
|
if resource.Type != "azure_dns_server" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if resource.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No DNS Server ID is set.")
|
|
|
|
}
|
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
netConf, err := vnetClient.GetVirtualNetworkConfiguration()
|
2015-06-05 16:12:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving networking configuration from Azure: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dns := range netConf.Configuration.DNS.DNSServers {
|
|
|
|
if dns.Name == resource.Primary.ID {
|
|
|
|
return fmt.Errorf("Azure DNS Server still exists.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccAzureDnsServerBasic = `
|
|
|
|
resource "azure_dns_server" "foo" {
|
|
|
|
name = "terraform-dns-server"
|
|
|
|
dns_address = "8.8.8.8"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccAzureDnsServerUpdate = `
|
|
|
|
resource "azure_dns_server" "foo" {
|
|
|
|
name = "terraform-dns-server"
|
|
|
|
dns_address = "8.8.4.4"
|
|
|
|
}
|
|
|
|
`
|