github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azure/resource_azure_local_network_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/management"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccAzureLocalNetworkConnectionBasic(t *testing.T) {
    13  	name := "azure_local_network_connection.foo"
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccAzureLocalNetworkConnectionDestroyed,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAzureLocalNetworkConnectionBasic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccAzureLocalNetworkConnectionExists(name),
    24  					resource.TestCheckResourceAttr(name, "name", "terraform-local-network-connection"),
    25  					resource.TestCheckResourceAttr(name, "vpn_gateway_address", "10.11.12.13"),
    26  					resource.TestCheckResourceAttr(name, "address_space_prefixes.0", "10.10.10.0/31"),
    27  					resource.TestCheckResourceAttr(name, "address_space_prefixes.1", "10.10.10.1/31"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAzureLocalNetworkConnectionUpdate(t *testing.T) {
    35  	name := "azure_local_network_connection.foo"
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccAzureLocalNetworkConnectionDestroyed,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccAzureLocalNetworkConnectionBasic,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccAzureLocalNetworkConnectionExists(name),
    46  					resource.TestCheckResourceAttr(name, "name", "terraform-local-network-connection"),
    47  					resource.TestCheckResourceAttr(name, "vpn_gateway_address", "10.11.12.13"),
    48  					resource.TestCheckResourceAttr(name, "address_space_prefixes.0", "10.10.10.0/31"),
    49  					resource.TestCheckResourceAttr(name, "address_space_prefixes.1", "10.10.10.1/31"),
    50  				),
    51  			},
    52  
    53  			resource.TestStep{
    54  				Config: testAccAzureLocalNetworkConnectionUpdate,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccAzureLocalNetworkConnectionExists(name),
    57  					resource.TestCheckResourceAttr(name, "name", "terraform-local-network-connection"),
    58  					resource.TestCheckResourceAttr(name, "vpn_gateway_address", "10.11.12.14"),
    59  					resource.TestCheckResourceAttr(name, "address_space_prefixes.0", "10.10.10.2/30"),
    60  					resource.TestCheckResourceAttr(name, "address_space_prefixes.1", "10.10.10.3/30"),
    61  				),
    62  			},
    63  		},
    64  	})
    65  }
    66  
    67  // testAccAzureLocalNetworkConnectionExists checks whether the given local network
    68  // connection exists on Azure.
    69  func testAccAzureLocalNetworkConnectionExists(name string) resource.TestCheckFunc {
    70  	return func(s *terraform.State) error {
    71  		resource, ok := s.RootModule().Resources[name]
    72  		if !ok {
    73  			return fmt.Errorf("Azure Local Network Connection not found: %s", name)
    74  		}
    75  
    76  		if resource.Primary.ID == "" {
    77  			return fmt.Errorf("Azure Local Network Connection ID not set.")
    78  		}
    79  
    80  		vnetClient := testAccProvider.Meta().(*Client).vnetClient
    81  		netConf, err := vnetClient.GetVirtualNetworkConfiguration()
    82  		if err != nil {
    83  			return err
    84  		}
    85  
    86  		for _, lnet := range netConf.Configuration.LocalNetworkSites {
    87  			if lnet.Name == resource.Primary.ID {
    88  				return nil
    89  			}
    90  			break
    91  		}
    92  
    93  		return fmt.Errorf("Local Network Connection not found: %s", name)
    94  	}
    95  }
    96  
    97  // testAccAzureLocalNetworkConnectionDestroyed checks whether the local network
    98  // connection has been destroyed on Azure or not.
    99  func testAccAzureLocalNetworkConnectionDestroyed(s *terraform.State) error {
   100  	vnetClient := testAccProvider.Meta().(*Client).vnetClient
   101  
   102  	for _, resource := range s.RootModule().Resources {
   103  		if resource.Type != "azure_local_network_connection" {
   104  			continue
   105  		}
   106  
   107  		if resource.Primary.ID == "" {
   108  			return fmt.Errorf("Azure Local Network Connection ID not set.")
   109  		}
   110  
   111  		netConf, err := vnetClient.GetVirtualNetworkConfiguration()
   112  		if err != nil {
   113  			// This is desirable - if there is no network config there can be no gateways
   114  			if management.IsResourceNotFoundError(err) {
   115  				continue
   116  			}
   117  			return err
   118  		}
   119  
   120  		for _, lnet := range netConf.Configuration.LocalNetworkSites {
   121  			if lnet.Name == resource.Primary.ID {
   122  				return fmt.Errorf("Azure Local Network Connection still exists.")
   123  			}
   124  		}
   125  	}
   126  
   127  	return nil
   128  }
   129  
   130  const testAccAzureLocalNetworkConnectionBasic = `
   131  resource "azure_local_network_connection" "foo" {
   132      name = "terraform-local-network-connection"
   133      vpn_gateway_address = "10.11.12.13"
   134      address_space_prefixes = ["10.10.10.0/31", "10.10.10.1/31"]
   135  }
   136  `
   137  
   138  const testAccAzureLocalNetworkConnectionUpdate = `
   139  resource "azure_local_network_connection" "foo" {
   140      name = "terraform-local-network-connection"
   141      vpn_gateway_address = "10.11.12.14"
   142      address_space_prefixes = ["10.10.10.2/30", "10.10.10.3/30"]
   143  }
   144  `