github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_virtual_network_peering_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 TestAccAzureRMVirtualNetworkPeering_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMVirtualNetworkPeering_basic, ri, ri, ri, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMVirtualNetworkPeeringDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test1"),
    26  					testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test2"),
    27  					resource.TestCheckResourceAttr(
    28  						"azurerm_virtual_network_peering.test1", "allow_virtual_network_access", "true"),
    29  					resource.TestCheckResourceAttr(
    30  						"azurerm_virtual_network_peering.test2", "allow_virtual_network_access", "true"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAzureRMVirtualNetworkPeering_disappears(t *testing.T) {
    38  	ri := acctest.RandInt()
    39  	config := fmt.Sprintf(testAccAzureRMVirtualNetworkPeering_basic, ri, ri, ri, ri, ri)
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testCheckAzureRMVirtualNetworkPeeringDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: config,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test1"),
    50  					testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test2"),
    51  					resource.TestCheckResourceAttr(
    52  						"azurerm_virtual_network_peering.test1", "allow_virtual_network_access", "true"),
    53  					resource.TestCheckResourceAttr(
    54  						"azurerm_virtual_network_peering.test2", "allow_virtual_network_access", "true"),
    55  					testCheckAzureRMVirtualNetworkPeeringDisappears("azurerm_virtual_network_peering.test1"),
    56  				),
    57  				ExpectNonEmptyPlan: true,
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccAzureRMVirtualNetworkPeering_update(t *testing.T) {
    64  	ri := acctest.RandInt()
    65  	preConfig := fmt.Sprintf(testAccAzureRMVirtualNetworkPeering_basic, ri, ri, ri, ri, ri)
    66  	postConfig := fmt.Sprintf(testAccAzureRMVirtualNetworkPeering_basicUpdate, ri, ri, ri, ri, ri)
    67  
    68  	resource.Test(t, resource.TestCase{
    69  		PreCheck:     func() { testAccPreCheck(t) },
    70  		Providers:    testAccProviders,
    71  		CheckDestroy: testCheckAzureRMVirtualNetworkPeeringDestroy,
    72  		Steps: []resource.TestStep{
    73  			resource.TestStep{
    74  				Config: preConfig,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test1"),
    77  					testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test2"),
    78  					resource.TestCheckResourceAttr(
    79  						"azurerm_virtual_network_peering.test1", "allow_virtual_network_access", "true"),
    80  					resource.TestCheckResourceAttr(
    81  						"azurerm_virtual_network_peering.test2", "allow_virtual_network_access", "true"),
    82  					resource.TestCheckResourceAttr(
    83  						"azurerm_virtual_network_peering.test1", "allow_forwarded_traffic", "false"),
    84  					resource.TestCheckResourceAttr(
    85  						"azurerm_virtual_network_peering.test2", "allow_forwarded_traffic", "false"),
    86  				),
    87  			},
    88  
    89  			resource.TestStep{
    90  				Config: postConfig,
    91  				Check: resource.ComposeTestCheckFunc(
    92  					testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test1"),
    93  					testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test2"),
    94  					resource.TestCheckResourceAttr(
    95  						"azurerm_virtual_network_peering.test1", "allow_virtual_network_access", "true"),
    96  					resource.TestCheckResourceAttr(
    97  						"azurerm_virtual_network_peering.test2", "allow_virtual_network_access", "true"),
    98  					resource.TestCheckResourceAttr(
    99  						"azurerm_virtual_network_peering.test1", "allow_forwarded_traffic", "true"),
   100  					resource.TestCheckResourceAttr(
   101  						"azurerm_virtual_network_peering.test2", "allow_forwarded_traffic", "true"),
   102  				),
   103  			},
   104  		},
   105  	})
   106  }
   107  
   108  func testCheckAzureRMVirtualNetworkPeeringExists(name string) resource.TestCheckFunc {
   109  	return func(s *terraform.State) error {
   110  		// Ensure we have enough information in state to look up in API
   111  		rs, ok := s.RootModule().Resources[name]
   112  		if !ok {
   113  			return fmt.Errorf("Not found: %s", name)
   114  		}
   115  
   116  		name := rs.Primary.Attributes["name"]
   117  		vnetName := rs.Primary.Attributes["virtual_network_name"]
   118  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   119  		if !hasResourceGroup {
   120  			return fmt.Errorf("Bad: no resource group found in state for virtual network peering: %s", name)
   121  		}
   122  
   123  		// Ensure resource group/virtual network peering combination exists in API
   124  		conn := testAccProvider.Meta().(*ArmClient).vnetPeeringsClient
   125  
   126  		resp, err := conn.Get(resourceGroup, vnetName, name)
   127  		if err != nil {
   128  			return fmt.Errorf("Bad: Get on vnetPeeringsClient: %s", err)
   129  		}
   130  
   131  		if resp.StatusCode == http.StatusNotFound {
   132  			return fmt.Errorf("Bad: Virtual Network Peering %q (resource group: %q) does not exist", name, resourceGroup)
   133  		}
   134  
   135  		return nil
   136  	}
   137  }
   138  
   139  func testCheckAzureRMVirtualNetworkPeeringDisappears(name string) resource.TestCheckFunc {
   140  	return func(s *terraform.State) error {
   141  		// Ensure we have enough information in state to look up in API
   142  		rs, ok := s.RootModule().Resources[name]
   143  		if !ok {
   144  			return fmt.Errorf("Not found: %s", name)
   145  		}
   146  
   147  		name := rs.Primary.Attributes["name"]
   148  		vnetName := rs.Primary.Attributes["virtual_network_name"]
   149  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   150  		if !hasResourceGroup {
   151  			return fmt.Errorf("Bad: no resource group found in state for virtual network peering: %s", name)
   152  		}
   153  
   154  		// Ensure resource group/virtual network peering combination exists in API
   155  		conn := testAccProvider.Meta().(*ArmClient).vnetPeeringsClient
   156  
   157  		_, err := conn.Delete(resourceGroup, vnetName, name, make(chan struct{}))
   158  		if err != nil {
   159  			return fmt.Errorf("Bad: Delete on vnetPeeringsClient: %s", err)
   160  		}
   161  
   162  		return nil
   163  	}
   164  }
   165  
   166  func testCheckAzureRMVirtualNetworkPeeringDestroy(s *terraform.State) error {
   167  	conn := testAccProvider.Meta().(*ArmClient).vnetPeeringsClient
   168  
   169  	for _, rs := range s.RootModule().Resources {
   170  		if rs.Type != "azurerm_virtual_network_peering" {
   171  			continue
   172  		}
   173  
   174  		name := rs.Primary.Attributes["name"]
   175  		vnetName := rs.Primary.Attributes["virtual_network_name"]
   176  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   177  
   178  		resp, err := conn.Get(resourceGroup, vnetName, name)
   179  		if err != nil {
   180  			return nil
   181  		}
   182  
   183  		if resp.StatusCode != http.StatusNotFound {
   184  			return fmt.Errorf("Virtual Network Peering sitll exists:\n%#v", resp.VirtualNetworkPeeringPropertiesFormat)
   185  		}
   186  	}
   187  
   188  	return nil
   189  }
   190  
   191  var testAccAzureRMVirtualNetworkPeering_basic = `
   192  resource "azurerm_resource_group" "test" {
   193    name     = "acctestRG-%d"
   194    location = "West US"
   195  }
   196  
   197  resource "azurerm_virtual_network" "test1" {
   198    name                = "acctestvirtnet-1-%d"
   199    resource_group_name = "${azurerm_resource_group.test.name}"
   200    address_space       = ["10.0.1.0/24"]
   201    location            = "${azurerm_resource_group.test.location}"
   202  }
   203  
   204  resource "azurerm_virtual_network" "test2" {
   205    name                = "acctestvirtnet-2-%d"
   206    resource_group_name = "${azurerm_resource_group.test.name}"
   207    address_space       = ["10.0.2.0/24"]
   208    location            = "${azurerm_resource_group.test.location}"
   209  }
   210  
   211  resource "azurerm_virtual_network_peering" "test1" {
   212      name = "acctestpeer-1-%d"
   213      resource_group_name = "${azurerm_resource_group.test.name}"
   214      virtual_network_name = "${azurerm_virtual_network.test1.name}"
   215      remote_virtual_network_id = "${azurerm_virtual_network.test2.id}"
   216      allow_virtual_network_access = true
   217  }
   218  
   219  resource "azurerm_virtual_network_peering" "test2" {
   220      name = "acctestpeer-2-%d"
   221      resource_group_name = "${azurerm_resource_group.test.name}"
   222      virtual_network_name = "${azurerm_virtual_network.test2.name}"
   223      remote_virtual_network_id = "${azurerm_virtual_network.test1.id}"
   224      allow_virtual_network_access = true
   225  }
   226  `
   227  
   228  var testAccAzureRMVirtualNetworkPeering_basicUpdate = `
   229  resource "azurerm_resource_group" "test" {
   230    name     = "acctestRG-%d"
   231    location = "West US"
   232  }
   233  
   234  resource "azurerm_virtual_network" "test1" {
   235    name                = "acctestvirtnet-1-%d"
   236    resource_group_name = "${azurerm_resource_group.test.name}"
   237    address_space       = ["10.0.1.0/24"]
   238    location            = "${azurerm_resource_group.test.location}"
   239  }
   240  
   241  resource "azurerm_virtual_network" "test2" {
   242    name                = "acctestvirtnet-2-%d"
   243    resource_group_name = "${azurerm_resource_group.test.name}"
   244    address_space       = ["10.0.2.0/24"]
   245    location            = "${azurerm_resource_group.test.location}"
   246  }
   247  
   248  resource "azurerm_virtual_network_peering" "test1" {
   249      name = "acctestpeer-1-%d"
   250      resource_group_name = "${azurerm_resource_group.test.name}"
   251      virtual_network_name = "${azurerm_virtual_network.test1.name}"
   252      remote_virtual_network_id = "${azurerm_virtual_network.test2.id}"
   253      allow_forwarded_traffic = true
   254      allow_virtual_network_access = true
   255  }
   256  
   257  resource "azurerm_virtual_network_peering" "test2" {
   258      name = "acctestpeer-2-%d"
   259      resource_group_name = "${azurerm_resource_group.test.name}"
   260      virtual_network_name = "${azurerm_virtual_network.test2.name}"
   261      remote_virtual_network_id = "${azurerm_virtual_network.test1.id}"
   262      allow_forwarded_traffic = true
   263      allow_virtual_network_access = true
   264  }
   265  `