github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccAzureRMLocalNetworkGateway_basic(t *testing.T) { 13 name := "azurerm_local_network_gateway.test" 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testCheckAzureRMLocalNetworkGatewayDestroy, 19 Steps: []resource.TestStep{ 20 { 21 Config: testAccAzureRMLocalNetworkGatewayConfig_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testCheckAzureRMLocalNetworkGatewayExists(name), 24 resource.TestCheckResourceAttr(name, "gateway_address", "127.0.0.1"), 25 resource.TestCheckResourceAttr(name, "address_space.0", "127.0.0.0/8"), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccAzureRMLocalNetworkGateway_disappears(t *testing.T) { 33 name := "azurerm_local_network_gateway.test" 34 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testCheckAzureRMLocalNetworkGatewayDestroy, 39 Steps: []resource.TestStep{ 40 { 41 Config: testAccAzureRMLocalNetworkGatewayConfig_basic, 42 Check: resource.ComposeTestCheckFunc( 43 testCheckAzureRMLocalNetworkGatewayExists(name), 44 resource.TestCheckResourceAttr(name, "gateway_address", "127.0.0.1"), 45 resource.TestCheckResourceAttr(name, "address_space.0", "127.0.0.0/8"), 46 testCheckAzureRMLocalNetworkGatewayDisappears(name), 47 ), 48 ExpectNonEmptyPlan: true, 49 }, 50 }, 51 }) 52 } 53 54 // testCheckAzureRMLocalNetworkGatewayExists returns the resurce.TestCheckFunc 55 // which checks whether or not the expected local network gateway exists both 56 // in the schema, and on Azure. 57 func testCheckAzureRMLocalNetworkGatewayExists(name string) resource.TestCheckFunc { 58 return func(s *terraform.State) error { 59 // first check within the schema for the local network gateway: 60 res, ok := s.RootModule().Resources[name] 61 if !ok { 62 return fmt.Errorf("Local network gateway '%s' not found.", name) 63 } 64 65 // then, extract the name and the resource group: 66 id, err := parseAzureResourceID(res.Primary.ID) 67 if err != nil { 68 return err 69 } 70 localNetName := id.Path["localNetworkGateways"] 71 resGrp := id.ResourceGroup 72 73 // and finally, check that it exists on Azure: 74 lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient 75 76 resp, err := lnetClient.Get(resGrp, localNetName) 77 if err != nil { 78 if resp.StatusCode == http.StatusNotFound { 79 return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp) 80 } 81 return fmt.Errorf("Error reading the state of local network gateway '%s'.", localNetName) 82 } 83 84 return nil 85 } 86 } 87 88 func testCheckAzureRMLocalNetworkGatewayDisappears(name string) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 // first check within the schema for the local network gateway: 91 res, ok := s.RootModule().Resources[name] 92 if !ok { 93 return fmt.Errorf("Local network gateway '%s' not found.", name) 94 } 95 96 // then, extract the name and the resource group: 97 id, err := parseAzureResourceID(res.Primary.ID) 98 if err != nil { 99 return err 100 } 101 localNetName := id.Path["localNetworkGateways"] 102 resGrp := id.ResourceGroup 103 104 // and finally, check that it exists on Azure: 105 lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient 106 107 resp, err := lnetClient.Delete(resGrp, localNetName, make(chan struct{})) 108 if err != nil { 109 if resp.StatusCode == http.StatusNotFound { 110 return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp) 111 } 112 return fmt.Errorf("Error deleting the state of local network gateway '%s'.", localNetName) 113 } 114 115 return nil 116 } 117 } 118 119 func testCheckAzureRMLocalNetworkGatewayDestroy(s *terraform.State) error { 120 for _, res := range s.RootModule().Resources { 121 if res.Type != "azurerm_local_network_gateway" { 122 continue 123 } 124 125 id, err := parseAzureResourceID(res.Primary.ID) 126 if err != nil { 127 return err 128 } 129 localNetName := id.Path["localNetworkGateways"] 130 resGrp := id.ResourceGroup 131 132 lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient 133 resp, err := lnetClient.Get(resGrp, localNetName) 134 135 if err != nil { 136 return nil 137 } 138 139 if resp.StatusCode != http.StatusNotFound { 140 return fmt.Errorf("Local network gateway still exists:\n%#v", resp.LocalNetworkGatewayPropertiesFormat) 141 } 142 } 143 144 return nil 145 } 146 147 var testAccAzureRMLocalNetworkGatewayConfig_basic = ` 148 resource "azurerm_resource_group" "test" { 149 name = "tftestingResourceGroup" 150 location = "West US" 151 } 152 153 resource "azurerm_local_network_gateway" "test" { 154 name = "tftestingLocalNetworkGateway" 155 location = "${azurerm_resource_group.test.location}" 156 resource_group_name = "${azurerm_resource_group.test.name}" 157 gateway_address = "127.0.0.1" 158 address_space = ["127.0.0.0/8"] 159 } 160 `