github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			{
    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  			return fmt.Errorf("Error reading the state of local network gateway '%s'.", localNetName)
    60  		}
    61  
    62  		return nil
    63  	}
    64  }
    65  
    66  func testCheckAzureRMLocalNetworkGatewayDestroy(s *terraform.State) error {
    67  	for _, res := range s.RootModule().Resources {
    68  		if res.Type != "azurerm_local_network_gateway" {
    69  			continue
    70  		}
    71  
    72  		id, err := parseAzureResourceID(res.Primary.ID)
    73  		if err != nil {
    74  			return err
    75  		}
    76  		localNetName := id.Path["localNetworkGateways"]
    77  		resGrp := id.ResourceGroup
    78  
    79  		lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient
    80  		resp, err := lnetClient.Get(resGrp, localNetName)
    81  
    82  		if err != nil {
    83  			return nil
    84  		}
    85  
    86  		if resp.StatusCode != http.StatusNotFound {
    87  			return fmt.Errorf("Local network gateway still exists:\n%#v", resp.Properties)
    88  		}
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  var testAccAzureRMLocalNetworkGatewayConfig_basic = `
    95  resource "azurerm_resource_group" "test" {
    96      name = "tftestingResourceGroup"
    97      location = "West US"
    98  }
    99  
   100  resource "azurerm_local_network_gateway" "test" {
   101  	name = "tftestingLocalNetworkGateway"
   102  	location = "${azurerm_resource_group.test.location}"
   103  	resource_group_name = "${azurerm_resource_group.test.name}"
   104  	gateway_address = "127.0.0.1"
   105  	address_space = ["127.0.0.0/8"]
   106  }
   107  `