2016-01-08 23:50:01 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-01 18:40:30 +02:00
|
|
|
"log"
|
2016-01-08 23:50:01 +01:00
|
|
|
"net/http"
|
2017-06-01 18:40:30 +02:00
|
|
|
"strings"
|
2016-01-08 23:50:01 +01:00
|
|
|
"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-06-01 18:40:30 +02:00
|
|
|
config := testAccAzureRMSubnet_basic(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"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:40:30 +02:00
|
|
|
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)),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-29 18:14:20 +02:00
|
|
|
func TestAccAzureRMSubnet_disappears(t *testing.T) {
|
|
|
|
|
|
|
|
ri := acctest.RandInt()
|
2017-06-01 18:40:30 +02:00
|
|
|
config := testAccAzureRMSubnet_basic(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)
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:40:30 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:40:30 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:40:30 +02:00
|
|
|
func testAccAzureRMSubnet_basic(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-01-08 23:50:01 +01:00
|
|
|
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
|
|
|
}
|
2017-06-01 18:40:30 +02: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)
|
|
|
|
}
|