terraform/builtin/providers/azurerm/resource_arm_subnet_test.go

372 lines
10 KiB
Go
Raw Normal View History

2016-01-08 23:50:01 +01:00
package azurerm
import (
"fmt"
"log"
2016-01-08 23:50:01 +01:00
"net/http"
"strings"
2016-01-08 23:50:01 +01:00
"testing"
"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) {
ri := acctest.RandInt()
config := testAccAzureRMSubnet_basic(ri)
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{
Config: config,
2016-01-08 23:50:01 +01:00
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSubnetExists("azurerm_subnet.test"),
),
},
},
})
}
func TestAccAzureRMSubnet_routeTableUpdate(t *testing.T) {
ri := acctest.RandInt()
initConfig := testAccAzureRMSubnet_routeTable(ri)
updatedConfig := testAccAzureRMSubnet_updatedRouteTable(ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSubnetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: initConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSubnetExists("azurerm_subnet.test"),
),
},
resource.TestStep{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSubnetRouteTableExists("azurerm_subnet.test", fmt.Sprintf("acctest-%d", ri)),
),
},
},
})
}
func TestAccAzureRMSubnet_disappears(t *testing.T) {
ri := acctest.RandInt()
config := testAccAzureRMSubnet_basic(ri)
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)
}
log.Printf("[INFO] Checking Subnet addition.")
2016-01-08 23:50:01 +01:00
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)
}
if resp.RouteTable == nil {
return fmt.Errorf("Bad: Subnet %q (resource group: %q) does not contain route tables after add", name, resourceGroup)
}
return nil
}
}
func testCheckAzureRMSubnetRouteTableExists(subnetName string, routeTableId 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[subnetName]
if !ok {
return fmt.Errorf("Not found: %s", subnetName)
}
log.Printf("[INFO] Checking Subnet update.")
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)
}
vnetConn := testAccProvider.Meta().(*ArmClient).vnetClient
vnetResp, vnetErr := vnetConn.Get(resourceGroup, vnetName, "")
if vnetErr != nil {
return fmt.Errorf("Bad: Get on vnetClient: %s", vnetErr)
}
if vnetResp.Subnets == nil {
return fmt.Errorf("Bad: Vnet %q (resource group: %q) does not have subnets after update", vnetName, resourceGroup)
}
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", subnetName, resourceGroup)
}
if resp.RouteTable == nil {
return fmt.Errorf("Bad: Subnet %q (resource group: %q) does not contain route tables after update", subnetName, resourceGroup)
}
if !strings.Contains(*resp.RouteTable.ID, routeTableId) {
return fmt.Errorf("Bad: Subnet %q (resource group: %q) does not have route table %q", subnetName, resourceGroup, routeTableId)
}
2016-01-08 23:50:01 +01:00
return nil
}
}
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
_, error := conn.Delete(resourceGroup, vnetName, name, make(chan struct{}))
err := <-error
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 {
return fmt.Errorf("Subnet still exists:\n%#v", resp.SubnetPropertiesFormat)
2016-01-08 23:50:01 +01:00
}
}
return nil
}
func testAccAzureRMSubnet_basic(rInt int) string {
return fmt.Sprintf(`
2016-01-08 23:50:01 +01:00
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
2016-01-08 23:50:01 +01:00
location = "West US"
}
resource "azurerm_virtual_network" "test" {
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" {
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
}
`, rInt, rInt, rInt, rInt, rInt)
}
func testAccAzureRMSubnet_routeTable(rInt int) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
}
resource "azurerm_virtual_network" "test" {
name = "acctestvirtnet%d"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "acctestsubnet%d"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
route_table_id = "${azurerm_route_table.test.id}"
}
resource "azurerm_route_table" "test" {
name = "acctest-%d"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_route" "route_a" {
name = "acctest-%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"
}`, rInt, rInt, rInt, rInt, rInt)
}
func testAccAzureRMSubnet_updatedRouteTable(rInt int) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
tags {
environment = "Testing"
}
}
resource "azurerm_network_security_group" "test_secgroup" {
name = "acctest-%d"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
security_rule {
name = "acctest-%d"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags {
environment = "Testing"
}
}
resource "azurerm_virtual_network" "test" {
name = "acctestvirtnet%d"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
environment = "Testing"
}
}
resource "azurerm_subnet" "test" {
name = "acctestsubnet%d"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.2.0/24"
route_table_id = "${azurerm_route_table.test.id}"
}
resource "azurerm_route_table" "test" {
name = "acctest-%d"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
environment = "Testing"
}
}
resource "azurerm_route" "route_a" {
name = "acctest-%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"
}`, rInt, rInt, rInt, rInt, rInt, rInt, rInt)
}