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