github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azure/resource_azure_dns_server_test.go (about) 1 package azure 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/Azure/azure-sdk-for-go/management" 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAzureDnsServerBasic(t *testing.T) { 14 name := "azure_dns_server.foo" 15 16 random := acctest.RandInt() 17 config := testAccAzureDnsServerBasic(random) 18 serverName := fmt.Sprintf("tf-dns-server-%d", random) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckAzureDnsServerDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: config, 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckAzureDnsServerExists(name), 29 resource.TestCheckResourceAttr(name, "name", serverName), 30 resource.TestCheckResourceAttr(name, "dns_address", "8.8.8.8"), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAzureDnsServerUpdate(t *testing.T) { 38 name := "azure_dns_server.foo" 39 40 random := acctest.RandInt() 41 basicConfig := testAccAzureDnsServerBasic(random) 42 updateConfig := testAccAzureDnsServerUpdate(random) 43 serverName := fmt.Sprintf("tf-dns-server-%d", random) 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckAzureDnsServerDestroy, 49 Steps: []resource.TestStep{ 50 resource.TestStep{ 51 Config: basicConfig, 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckAzureDnsServerExists(name), 54 resource.TestCheckResourceAttr(name, "name", serverName), 55 resource.TestCheckResourceAttr(name, "dns_address", "8.8.8.8"), 56 ), 57 }, 58 59 resource.TestStep{ 60 Config: updateConfig, 61 Check: resource.ComposeTestCheckFunc( 62 testAccCheckAzureDnsServerExists(name), 63 resource.TestCheckResourceAttr(name, "name", serverName), 64 resource.TestCheckResourceAttr(name, "dns_address", "8.8.4.4"), 65 ), 66 }, 67 }, 68 }) 69 } 70 71 func testAccCheckAzureDnsServerExists(name string) resource.TestCheckFunc { 72 return func(s *terraform.State) error { 73 resource, ok := s.RootModule().Resources[name] 74 if !ok { 75 return fmt.Errorf("Resource not found: %s", name) 76 } 77 78 if resource.Primary.ID == "" { 79 return fmt.Errorf("No DNS Server ID set.") 80 } 81 82 vnetClient := testAccProvider.Meta().(*Client).vnetClient 83 netConf, err := vnetClient.GetVirtualNetworkConfiguration() 84 if err != nil { 85 return fmt.Errorf("Failed fetching networking configuration: %s", err) 86 } 87 88 for _, dns := range netConf.Configuration.DNS.DNSServers { 89 if dns.Name == resource.Primary.ID { 90 return nil 91 } 92 } 93 94 return fmt.Errorf("Azure DNS Server not found.") 95 } 96 } 97 98 func testAccCheckAzureDnsServerDestroy(s *terraform.State) error { 99 vnetClient := testAccProvider.Meta().(*Client).vnetClient 100 101 for _, resource := range s.RootModule().Resources { 102 if resource.Type != "azure_dns_server" { 103 continue 104 } 105 106 if resource.Primary.ID == "" { 107 return fmt.Errorf("No DNS Server ID is set.") 108 } 109 110 netConf, err := vnetClient.GetVirtualNetworkConfiguration() 111 if err != nil { 112 // This is desirable - if there is no network config there can't be any DNS Servers 113 if management.IsResourceNotFoundError(err) { 114 continue 115 } 116 return fmt.Errorf("Error retrieving networking configuration from Azure: %s", err) 117 } 118 119 for _, dns := range netConf.Configuration.DNS.DNSServers { 120 if dns.Name == resource.Primary.ID { 121 return fmt.Errorf("Azure DNS Server still exists.") 122 } 123 } 124 } 125 126 return nil 127 } 128 129 func testAccAzureDnsServerBasic(random int) string { 130 return fmt.Sprintf(` 131 resource "azure_dns_server" "foo" { 132 name = "tf-dns-server-%d" 133 dns_address = "8.8.8.8" 134 } 135 `, random) 136 } 137 138 func testAccAzureDnsServerUpdate(random int) string { 139 return fmt.Sprintf(` 140 resource "azure_dns_server" "foo" { 141 name = "tf-dns-server-%d" 142 dns_address = "8.8.4.4" 143 } 144 `, random) 145 }