github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_dns_zone_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/jen20/riviera/dns"
    11  )
    12  
    13  func TestAccAzureRMDnsZone_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMDnsZone_basic, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMDnsZoneDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMDnsZoneExists("azurerm_dns_zone.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAzureRMDnsZone_withTags(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	preConfig := fmt.Sprintf(testAccAzureRMDnsZone_withTags, ri, ri)
    35  	postConfig := fmt.Sprintf(testAccAzureRMDnsZone_withTagsUupdate, ri, ri)
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testCheckAzureRMDnsZoneDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: preConfig,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testCheckAzureRMDnsZoneExists("azurerm_dns_zone.test"),
    46  					resource.TestCheckResourceAttr(
    47  						"azurerm_dns_zone.test", "tags.%", "2"),
    48  				),
    49  			},
    50  
    51  			resource.TestStep{
    52  				Config: postConfig,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testCheckAzureRMDnsZoneExists("azurerm_dns_zone.test"),
    55  					resource.TestCheckResourceAttr(
    56  						"azurerm_dns_zone.test", "tags.%", "1"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testCheckAzureRMDnsZoneExists(name string) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		// Ensure we have enough information in state to look up in API
    66  		rs, ok := s.RootModule().Resources[name]
    67  		if !ok {
    68  			return fmt.Errorf("Not found: %s", name)
    69  		}
    70  
    71  		conn := testAccProvider.Meta().(*ArmClient).rivieraClient
    72  
    73  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
    74  		readRequest.Command = &dns.GetDNSZone{}
    75  
    76  		readResponse, err := readRequest.Execute()
    77  		if err != nil {
    78  			return fmt.Errorf("Bad: GetDNSZone: %s", err)
    79  		}
    80  		if !readResponse.IsSuccessful() {
    81  			return fmt.Errorf("Bad: GetDNSZone: %s", readResponse.Error)
    82  		}
    83  
    84  		return nil
    85  	}
    86  }
    87  
    88  func testCheckAzureRMDnsZoneDestroy(s *terraform.State) error {
    89  	conn := testAccProvider.Meta().(*ArmClient).rivieraClient
    90  
    91  	for _, rs := range s.RootModule().Resources {
    92  		if rs.Type != "azurerm_dns_zone" {
    93  			continue
    94  		}
    95  
    96  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
    97  		readRequest.Command = &dns.GetDNSZone{}
    98  
    99  		readResponse, err := readRequest.Execute()
   100  		if err != nil {
   101  			return fmt.Errorf("Bad: GetDNSZone: %s", err)
   102  		}
   103  
   104  		if readResponse.IsSuccessful() {
   105  			return fmt.Errorf("Bad: DNS zone still exists: %s", readResponse.Error)
   106  		}
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  var testAccAzureRMDnsZone_basic = `
   113  resource "azurerm_resource_group" "test" {
   114      name = "acctestRG_%d"
   115      location = "West US"
   116  }
   117  resource "azurerm_dns_zone" "test" {
   118      name = "acctestzone%d.com"
   119      resource_group_name = "${azurerm_resource_group.test.name}"
   120  }
   121  `
   122  
   123  var testAccAzureRMDnsZone_withTags = `
   124  resource "azurerm_resource_group" "test" {
   125      name = "acctestRG_%d"
   126      location = "West US"
   127  }
   128  resource "azurerm_dns_zone" "test" {
   129      name = "acctestzone%d.com"
   130      resource_group_name = "${azurerm_resource_group.test.name}"
   131  	tags {
   132  		environment = "Production"
   133  		cost_center = "MSFT"
   134      }
   135  }
   136  `
   137  
   138  var testAccAzureRMDnsZone_withTagsUupdate = `
   139  resource "azurerm_resource_group" "test" {
   140      name = "acctestRG_%d"
   141      location = "West US"
   142  }
   143  resource "azurerm_dns_zone" "test" {
   144      name = "acctestzone%d.com"
   145      resource_group_name = "${azurerm_resource_group.test.name}"
   146  	tags {
   147  		environment = "staging"
   148      }
   149  }
   150  `