2016-01-08 23:50:01 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-28 22:00:43 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-01-08 23:50:01 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureRMSubnet_basic(t *testing.T) {
|
|
|
|
|
2016-01-28 22:00:43 +01:00
|
|
|
ri := acctest.RandInt()
|
2017-04-15 00:19:22 +02:00
|
|
|
config := fmt.Sprintf(testAccAzureRMSubnet_basic, ri, ri, ri, ri, ri)
|
2016-01-28 22:00:43 +01:00
|
|
|
|
2016-01-08 23:50:01 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMSubnetDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: config,
|
2016-01-08 23:50:01 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMSubnetExists("azurerm_subnet.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-29 18:14:20 +02:00
|
|
|
func TestAccAzureRMSubnet_disappears(t *testing.T) {
|
|
|
|
|
|
|
|
ri := acctest.RandInt()
|
2017-04-15 00:19:22 +02:00
|
|
|
config := fmt.Sprintf(testAccAzureRMSubnet_basic, ri, ri, ri, ri, ri)
|
2016-09-29 18:14:20 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMSubnetDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMSubnetExists("azurerm_subnet.test"),
|
|
|
|
testCheckAzureRMSubnetDisappears("azurerm_subnet.test"),
|
|
|
|
),
|
|
|
|
ExpectNonEmptyPlan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-08 23:50:01 +01:00
|
|
|
func testCheckAzureRMSubnetExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
// Ensure we have enough information in state to look up in API
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
vnetName := rs.Primary.Attributes["virtual_network_name"]
|
|
|
|
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
if !hasResourceGroup {
|
|
|
|
return fmt.Errorf("Bad: no resource group found in state for subnet: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).subnetClient
|
|
|
|
|
|
|
|
resp, err := conn.Get(resourceGroup, vnetName, name, "")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Get on subnetClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Bad: Subnet %q (resource group: %q) does not exist", name, resourceGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 18:14:20 +02:00
|
|
|
func testCheckAzureRMSubnetDisappears(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
// Ensure we have enough information in state to look up in API
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
vnetName := rs.Primary.Attributes["virtual_network_name"]
|
|
|
|
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
if !hasResourceGroup {
|
|
|
|
return fmt.Errorf("Bad: no resource group found in state for subnet: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).subnetClient
|
|
|
|
|
2017-06-01 11:18:22 +02:00
|
|
|
_, error := conn.Delete(resourceGroup, vnetName, name, make(chan struct{}))
|
|
|
|
err := <-error
|
2016-09-29 18:14:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Delete on subnetClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 23:50:01 +01:00
|
|
|
func testCheckAzureRMSubnetDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).subnetClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_subnet" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
vnetName := rs.Primary.Attributes["virtual_network_name"]
|
|
|
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
|
|
|
|
resp, err := conn.Get(resourceGroup, vnetName, name, "")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
2016-12-06 09:39:47 +01:00
|
|
|
return fmt.Errorf("Subnet still exists:\n%#v", resp.SubnetPropertiesFormat)
|
2016-01-08 23:50:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureRMSubnet_basic = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG-%d"
|
2016-01-08 23:50:01 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_virtual_network" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestvirtnet%d"
|
2016-01-08 23:50:01 +01:00
|
|
|
address_space = ["10.0.0.0/16"]
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_subnet" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestsubnet%d"
|
2016-01-08 23:50:01 +01:00
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
virtual_network_name = "${azurerm_virtual_network.test.name}"
|
|
|
|
address_prefix = "10.0.2.0/24"
|
2017-04-15 00:19:22 +02:00
|
|
|
route_table_id = "${azurerm_route_table.test.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_route_table" "test" {
|
|
|
|
name = "acctestroutetable%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_route" "test" {
|
|
|
|
name = "acctestroute%d"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
route_table_name = "${azurerm_route_table.test.name}"
|
|
|
|
|
|
|
|
address_prefix = "10.100.0.0/14"
|
|
|
|
next_hop_type = "VirtualAppliance"
|
|
|
|
next_hop_in_ip_address = "10.10.1.1"
|
2016-01-08 23:50:01 +01:00
|
|
|
}
|
|
|
|
`
|