2015-06-05 16:12:21 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-06 01:56:39 +01:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management"
|
2016-04-27 19:06:04 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-06-05 16:12:21 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureDnsServerBasic(t *testing.T) {
|
|
|
|
name := "azure_dns_server.foo"
|
|
|
|
|
2016-04-27 19:06:04 +02:00
|
|
|
random := acctest.RandInt()
|
|
|
|
config := testAccAzureDnsServerBasic(random)
|
|
|
|
serverName := fmt.Sprintf("tf-dns-server-%d", random)
|
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAzureDnsServerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-04-27 19:06:04 +02:00
|
|
|
Config: config,
|
2015-06-05 16:12:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureDnsServerExists(name),
|
2016-04-27 19:06:04 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "name", serverName),
|
2015-06-05 16:12:21 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "dns_address", "8.8.8.8"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureDnsServerUpdate(t *testing.T) {
|
|
|
|
name := "azure_dns_server.foo"
|
|
|
|
|
2016-04-27 19:06:04 +02:00
|
|
|
random := acctest.RandInt()
|
|
|
|
basicConfig := testAccAzureDnsServerBasic(random)
|
|
|
|
updateConfig := testAccAzureDnsServerUpdate(random)
|
|
|
|
serverName := fmt.Sprintf("tf-dns-server-%d", random)
|
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAzureDnsServerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-04-27 19:06:04 +02:00
|
|
|
Config: basicConfig,
|
2015-06-05 16:12:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureDnsServerExists(name),
|
2016-04-27 19:06:04 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "name", serverName),
|
2015-06-05 16:12:21 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "dns_address", "8.8.8.8"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2016-04-27 19:06:04 +02:00
|
|
|
Config: updateConfig,
|
2015-06-05 16:12:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureDnsServerExists(name),
|
2016-04-27 19:06:04 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "name", serverName),
|
2015-06-05 16:12:21 +02:00
|
|
|
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 {
|
2016-01-06 01:56:39 +01:00
|
|
|
// This is desirable - if there is no network config there can't be any DNS Servers
|
|
|
|
if management.IsResourceNotFoundError(err) {
|
|
|
|
continue
|
|
|
|
}
|
2015-06-05 16:12:21 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-27 19:06:04 +02:00
|
|
|
func testAccAzureDnsServerBasic(random int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-06-05 16:12:21 +02:00
|
|
|
resource "azure_dns_server" "foo" {
|
2016-04-27 19:06:04 +02:00
|
|
|
name = "tf-dns-server-%d"
|
2015-06-05 16:12:21 +02:00
|
|
|
dns_address = "8.8.8.8"
|
|
|
|
}
|
2016-04-27 19:06:04 +02:00
|
|
|
`, random)
|
|
|
|
}
|
2015-06-05 16:12:21 +02:00
|
|
|
|
2016-04-27 19:06:04 +02:00
|
|
|
func testAccAzureDnsServerUpdate(random int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-06-05 16:12:21 +02:00
|
|
|
resource "azure_dns_server" "foo" {
|
2016-04-27 19:06:04 +02:00
|
|
|
name = "tf-dns-server-%d"
|
2015-06-05 16:12:21 +02:00
|
|
|
dns_address = "8.8.4.4"
|
|
|
|
}
|
2016-04-27 19:06:04 +02:00
|
|
|
`, random)
|
|
|
|
}
|