github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/azurerm/resource_arm_local_network_gateway_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/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 resource.TestStep{ 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 // testCheckAzureRMLocalNetworkGatewayExists returns the resurce.TestCheckFunc 33 // which checks whether or not the expected local network gateway exists both 34 // in the schema, and on Azure. 35 func testCheckAzureRMLocalNetworkGatewayExists(name string) resource.TestCheckFunc { 36 return func(s *terraform.State) error { 37 // first check within the schema for the local network gateway: 38 res, ok := s.RootModule().Resources[name] 39 if !ok { 40 return fmt.Errorf("Local network gateway '%s' not found.", name) 41 } 42 43 // then, extract the name and the resource group: 44 id, err := parseAzureResourceID(res.Primary.ID) 45 if err != nil { 46 return err 47 } 48 localNetName := id.Path["localNetworkGateways"] 49 resGrp := id.ResourceGroup 50 51 // and finally, check that it exists on Azure: 52 lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient 53 54 resp, err := lnetClient.Get(resGrp, localNetName) 55 if err != nil { 56 if resp.StatusCode == http.StatusNotFound { 57 return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp) 58 } 59 60 return fmt.Errorf("Error reading the state of local network gateway '%s'.", localNetName) 61 } 62 63 return nil 64 } 65 } 66 67 func testCheckAzureRMLocalNetworkGatewayDestroy(s *terraform.State) error { 68 for _, res := range s.RootModule().Resources { 69 if res.Type != "azurerm_local_network_gateway" { 70 continue 71 } 72 73 id, err := parseAzureResourceID(res.Primary.ID) 74 if err != nil { 75 return err 76 } 77 localNetName := id.Path["localNetworkGateways"] 78 resGrp := id.ResourceGroup 79 80 lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient 81 resp, err := lnetClient.Get(resGrp, localNetName) 82 83 if err != nil { 84 return nil 85 } 86 87 if resp.StatusCode != http.StatusNotFound { 88 return fmt.Errorf("Local network gateway still exists:\n%#v", resp.Properties) 89 } 90 } 91 92 return nil 93 } 94 95 var testAccAzureRMLocalNetworkGatewayConfig_basic = ` 96 resource "azurerm_resource_group" "test" { 97 name = "tftestingResourceGroup" 98 location = "West US" 99 } 100 101 resource "azurerm_local_network_gateway" "test" { 102 name = "tftestingLocalNetworkGateway" 103 location = "${azurerm_resource_group.test.location}" 104 resource_group_name = "${azurerm_resource_group.test.name}" 105 gateway_address = "127.0.0.1" 106 address_space = ["127.0.0.0/8"] 107 } 108 `