github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_virtual_network_peering_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/Azure/azure-sdk-for-go/core/http" 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_update(t *testing.T) { 38 ri := acctest.RandInt() 39 preConfig := fmt.Sprintf(testAccAzureRMVirtualNetworkPeering_basic, ri, ri, ri, ri, ri) 40 postConfig := fmt.Sprintf(testAccAzureRMVirtualNetworkPeering_basicUpdate, ri, ri, ri, ri, ri) 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testCheckAzureRMVirtualNetworkPeeringDestroy, 46 Steps: []resource.TestStep{ 47 resource.TestStep{ 48 Config: preConfig, 49 Check: resource.ComposeTestCheckFunc( 50 testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test1"), 51 testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test2"), 52 resource.TestCheckResourceAttr( 53 "azurerm_virtual_network_peering.test1", "allow_virtual_network_access", "true"), 54 resource.TestCheckResourceAttr( 55 "azurerm_virtual_network_peering.test2", "allow_virtual_network_access", "true"), 56 resource.TestCheckResourceAttr( 57 "azurerm_virtual_network_peering.test1", "allow_forwarded_traffic", "false"), 58 resource.TestCheckResourceAttr( 59 "azurerm_virtual_network_peering.test2", "allow_forwarded_traffic", "false"), 60 ), 61 }, 62 63 resource.TestStep{ 64 Config: postConfig, 65 Check: resource.ComposeTestCheckFunc( 66 testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test1"), 67 testCheckAzureRMVirtualNetworkPeeringExists("azurerm_virtual_network_peering.test2"), 68 resource.TestCheckResourceAttr( 69 "azurerm_virtual_network_peering.test1", "allow_virtual_network_access", "true"), 70 resource.TestCheckResourceAttr( 71 "azurerm_virtual_network_peering.test2", "allow_virtual_network_access", "true"), 72 resource.TestCheckResourceAttr( 73 "azurerm_virtual_network_peering.test1", "allow_forwarded_traffic", "true"), 74 resource.TestCheckResourceAttr( 75 "azurerm_virtual_network_peering.test2", "allow_forwarded_traffic", "true"), 76 ), 77 }, 78 }, 79 }) 80 } 81 82 func testCheckAzureRMVirtualNetworkPeeringExists(name string) resource.TestCheckFunc { 83 return func(s *terraform.State) error { 84 // Ensure we have enough information in state to look up in API 85 rs, ok := s.RootModule().Resources[name] 86 if !ok { 87 return fmt.Errorf("Not found: %s", name) 88 } 89 90 name := rs.Primary.Attributes["name"] 91 vnetName := rs.Primary.Attributes["virtual_network_name"] 92 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 93 if !hasResourceGroup { 94 return fmt.Errorf("Bad: no resource group found in state for virtual network peering: %s", name) 95 } 96 97 // Ensure resource group/virtual network peering combination exists in API 98 conn := testAccProvider.Meta().(*ArmClient).vnetPeeringsClient 99 100 resp, err := conn.Get(resourceGroup, vnetName, name) 101 if err != nil { 102 return fmt.Errorf("Bad: Get on vnetPeeringsClient: %s", err) 103 } 104 105 if resp.StatusCode == http.StatusNotFound { 106 return fmt.Errorf("Bad: Virtual Network Peering %q (resource group: %q) does not exist", name, resourceGroup) 107 } 108 109 return nil 110 } 111 } 112 113 func testCheckAzureRMVirtualNetworkPeeringDestroy(s *terraform.State) error { 114 conn := testAccProvider.Meta().(*ArmClient).vnetPeeringsClient 115 116 for _, rs := range s.RootModule().Resources { 117 if rs.Type != "azurerm_virtual_network_peering" { 118 continue 119 } 120 121 name := rs.Primary.Attributes["name"] 122 vnetName := rs.Primary.Attributes["virtual_network_name"] 123 resourceGroup := rs.Primary.Attributes["resource_group_name"] 124 125 resp, err := conn.Get(resourceGroup, vnetName, name) 126 if err != nil { 127 return nil 128 } 129 130 if resp.StatusCode != http.StatusNotFound { 131 return fmt.Errorf("Virtual Network Peering sitll exists:\n%#v", resp.Properties) 132 } 133 } 134 135 return nil 136 } 137 138 var testAccAzureRMVirtualNetworkPeering_basic = ` 139 resource "azurerm_resource_group" "test" { 140 name = "acctestrg-%d" 141 location = "West US" 142 } 143 144 resource "azurerm_virtual_network" "test1" { 145 name = "acctestvirtnet-1-%d" 146 resource_group_name = "${azurerm_resource_group.test.name}" 147 address_space = ["10.0.1.0/24"] 148 location = "${azurerm_resource_group.test.location}" 149 } 150 151 resource "azurerm_virtual_network" "test2" { 152 name = "acctestvirtnet-2-%d" 153 resource_group_name = "${azurerm_resource_group.test.name}" 154 address_space = ["10.0.2.0/24"] 155 location = "${azurerm_resource_group.test.location}" 156 } 157 158 resource "azurerm_virtual_network_peering" "test1" { 159 name = "acctestpeer-1-%d" 160 resource_group_name = "${azurerm_resource_group.test.name}" 161 virtual_network_name = "${azurerm_virtual_network.test1.name}" 162 remote_virtual_network_id = "${azurerm_virtual_network.test2.id}" 163 allow_virtual_network_access = true 164 } 165 166 resource "azurerm_virtual_network_peering" "test2" { 167 name = "acctestpeer-2-%d" 168 resource_group_name = "${azurerm_resource_group.test.name}" 169 virtual_network_name = "${azurerm_virtual_network.test2.name}" 170 remote_virtual_network_id = "${azurerm_virtual_network.test1.id}" 171 allow_virtual_network_access = true 172 } 173 ` 174 175 var testAccAzureRMVirtualNetworkPeering_basicUpdate = ` 176 resource "azurerm_resource_group" "test" { 177 name = "acctestrg-%d" 178 location = "West US" 179 } 180 181 resource "azurerm_virtual_network" "test1" { 182 name = "acctestvirtnet-1-%d" 183 resource_group_name = "${azurerm_resource_group.test.name}" 184 address_space = ["10.0.1.0/24"] 185 location = "${azurerm_resource_group.test.location}" 186 } 187 188 resource "azurerm_virtual_network" "test2" { 189 name = "acctestvirtnet-2-%d" 190 resource_group_name = "${azurerm_resource_group.test.name}" 191 address_space = ["10.0.2.0/24"] 192 location = "${azurerm_resource_group.test.location}" 193 } 194 195 resource "azurerm_virtual_network_peering" "test1" { 196 name = "acctestpeer-1-%d" 197 resource_group_name = "${azurerm_resource_group.test.name}" 198 virtual_network_name = "${azurerm_virtual_network.test1.name}" 199 remote_virtual_network_id = "${azurerm_virtual_network.test2.id}" 200 allow_forwarded_traffic = true 201 allow_virtual_network_access = true 202 } 203 204 resource "azurerm_virtual_network_peering" "test2" { 205 name = "acctestpeer-2-%d" 206 resource_group_name = "${azurerm_resource_group.test.name}" 207 virtual_network_name = "${azurerm_virtual_network.test2.name}" 208 remote_virtual_network_id = "${azurerm_virtual_network.test1.id}" 209 allow_forwarded_traffic = true 210 allow_virtual_network_access = true 211 } 212 `