github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/azure/resource_azure_dns_server_test.go (about)

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