github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_virtual_network_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMVirtualNetwork_basic(t *testing.T) {
    14  
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccAzureRMVirtualNetwork_basic, ri, ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckAzureRMVirtualNetworkDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckAzureRMVirtualNetworkExists("azurerm_virtual_network.test"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccAzureRMVirtualNetwork_disappears(t *testing.T) {
    34  
    35  	ri := acctest.RandInt()
    36  	config := fmt.Sprintf(testAccAzureRMVirtualNetwork_basic, ri, ri)
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testCheckAzureRMVirtualNetworkDestroy,
    42  		Steps: []resource.TestStep{
    43  			resource.TestStep{
    44  				Config: config,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testCheckAzureRMVirtualNetworkExists("azurerm_virtual_network.test"),
    47  					testCheckAzureRMVirtualNetworkDisappears("azurerm_virtual_network.test"),
    48  				),
    49  				ExpectNonEmptyPlan: true,
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func TestAccAzureRMVirtualNetwork_withTags(t *testing.T) {
    56  
    57  	ri := acctest.RandInt()
    58  	preConfig := fmt.Sprintf(testAccAzureRMVirtualNetwork_withTags, ri, ri)
    59  	postConfig := fmt.Sprintf(testAccAzureRMVirtualNetwork_withTagsUpdated, ri, ri)
    60  
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testCheckAzureRMVirtualNetworkDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: preConfig,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testCheckAzureRMVirtualNetworkExists("azurerm_virtual_network.test"),
    70  					resource.TestCheckResourceAttr(
    71  						"azurerm_virtual_network.test", "tags.%", "2"),
    72  					resource.TestCheckResourceAttr(
    73  						"azurerm_virtual_network.test", "tags.environment", "Production"),
    74  					resource.TestCheckResourceAttr(
    75  						"azurerm_virtual_network.test", "tags.cost_center", "MSFT"),
    76  				),
    77  			},
    78  
    79  			resource.TestStep{
    80  				Config: postConfig,
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testCheckAzureRMVirtualNetworkExists("azurerm_virtual_network.test"),
    83  					resource.TestCheckResourceAttr(
    84  						"azurerm_virtual_network.test", "tags.%", "1"),
    85  					resource.TestCheckResourceAttr(
    86  						"azurerm_virtual_network.test", "tags.environment", "staging"),
    87  				),
    88  			},
    89  		},
    90  	})
    91  }
    92  
    93  func testCheckAzureRMVirtualNetworkExists(name string) resource.TestCheckFunc {
    94  	return func(s *terraform.State) error {
    95  		// Ensure we have enough information in state to look up in API
    96  		rs, ok := s.RootModule().Resources[name]
    97  		if !ok {
    98  			return fmt.Errorf("Not found: %s", name)
    99  		}
   100  
   101  		virtualNetworkName := rs.Primary.Attributes["name"]
   102  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   103  		if !hasResourceGroup {
   104  			return fmt.Errorf("Bad: no resource group found in state for virtual network: %s", virtualNetworkName)
   105  		}
   106  
   107  		// Ensure resource group/virtual network combination exists in API
   108  		conn := testAccProvider.Meta().(*ArmClient).vnetClient
   109  
   110  		resp, err := conn.Get(resourceGroup, virtualNetworkName, "")
   111  		if err != nil {
   112  			return fmt.Errorf("Bad: Get on vnetClient: %s", err)
   113  		}
   114  
   115  		if resp.StatusCode == http.StatusNotFound {
   116  			return fmt.Errorf("Bad: Virtual Network %q (resource group: %q) does not exist", name, resourceGroup)
   117  		}
   118  
   119  		return nil
   120  	}
   121  }
   122  
   123  func testCheckAzureRMVirtualNetworkDisappears(name string) resource.TestCheckFunc {
   124  	return func(s *terraform.State) error {
   125  		// Ensure we have enough information in state to look up in API
   126  		rs, ok := s.RootModule().Resources[name]
   127  		if !ok {
   128  			return fmt.Errorf("Not found: %s", name)
   129  		}
   130  
   131  		virtualNetworkName := rs.Primary.Attributes["name"]
   132  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   133  		if !hasResourceGroup {
   134  			return fmt.Errorf("Bad: no resource group found in state for virtual network: %s", virtualNetworkName)
   135  		}
   136  
   137  		// Ensure resource group/virtual network combination exists in API
   138  		conn := testAccProvider.Meta().(*ArmClient).vnetClient
   139  
   140  		_, err := conn.Delete(resourceGroup, virtualNetworkName, make(chan struct{}))
   141  		if err != nil {
   142  			return fmt.Errorf("Bad: Delete on vnetClient: %s", err)
   143  		}
   144  
   145  		return nil
   146  	}
   147  }
   148  
   149  func testCheckAzureRMVirtualNetworkDestroy(s *terraform.State) error {
   150  	conn := testAccProvider.Meta().(*ArmClient).vnetClient
   151  
   152  	for _, rs := range s.RootModule().Resources {
   153  		if rs.Type != "azurerm_virtual_network" {
   154  			continue
   155  		}
   156  
   157  		name := rs.Primary.Attributes["name"]
   158  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   159  
   160  		resp, err := conn.Get(resourceGroup, name, "")
   161  
   162  		if err != nil {
   163  			return nil
   164  		}
   165  
   166  		if resp.StatusCode != http.StatusNotFound {
   167  			return fmt.Errorf("Virtual Network sitll exists:\n%#v", resp.Properties)
   168  		}
   169  	}
   170  
   171  	return nil
   172  }
   173  
   174  var testAccAzureRMVirtualNetwork_basic = `
   175  resource "azurerm_resource_group" "test" {
   176      name = "acctestRG-%d"
   177      location = "West US"
   178  }
   179  
   180  resource "azurerm_virtual_network" "test" {
   181      name = "acctestvirtnet%d"
   182      address_space = ["10.0.0.0/16"]
   183      location = "West US"
   184      resource_group_name = "${azurerm_resource_group.test.name}"
   185  
   186      subnet {
   187          name = "subnet1"
   188          address_prefix = "10.0.1.0/24"
   189      }
   190  }
   191  `
   192  
   193  var testAccAzureRMVirtualNetwork_withTags = `
   194  resource "azurerm_resource_group" "test" {
   195      name = "acctestRG-%d"
   196      location = "West US"
   197  }
   198  
   199  resource "azurerm_virtual_network" "test" {
   200      name = "acctestvirtnet%d"
   201      address_space = ["10.0.0.0/16"]
   202      location = "West US"
   203      resource_group_name = "${azurerm_resource_group.test.name}"
   204  
   205      subnet {
   206          name = "subnet1"
   207          address_prefix = "10.0.1.0/24"
   208      }
   209  
   210      tags {
   211  	environment = "Production"
   212  	cost_center = "MSFT"
   213      }
   214  }
   215  `
   216  
   217  var testAccAzureRMVirtualNetwork_withTagsUpdated = `
   218  resource "azurerm_resource_group" "test" {
   219      name = "acctestRG-%d"
   220      location = "West US"
   221  }
   222  
   223  resource "azurerm_virtual_network" "test" {
   224      name = "acctestvirtnet%d"
   225      address_space = ["10.0.0.0/16"]
   226      location = "West US"
   227      resource_group_name = "${azurerm_resource_group.test.name}"
   228  
   229      subnet {
   230          name = "subnet1"
   231          address_prefix = "10.0.1.0/24"
   232      }
   233  
   234      tags {
   235  	environment = "staging"
   236      }
   237  }
   238  `