Skip to content

Commit

Permalink
Changes to vNet and subnet files to allow subnet updates without losi…
Browse files Browse the repository at this point in the history
…ng routing table references

Minor cleanup of bug hashicorp#8227-related work
  • Loading branch information
echuvyrov committed Apr 12, 2017
1 parent be80f8a commit 60f84cc
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 15 deletions.
2 changes: 1 addition & 1 deletion backend/local/counthookaction_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/operationtype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

249 changes: 249 additions & 0 deletions builtin/providers/azurerm/resource_arm_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package azurerm

import (
"fmt"
"log"
"net/http"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -30,6 +32,55 @@ func TestAccAzureRMSubnet_basic(t *testing.T) {
})
}

func TestAccAzureRMSubnet_routeTable(t *testing.T) {

ri := acctest.RandInt()
initConfig := fmt.Sprintf(testAccAzureRMSubnet_routeTable, ri, ri, ri)
updatedConfig := fmt.Sprintf(testAccAzureRMSubnet_updatedRouteTable, ri, ri, 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.rtTable2"),
),
},

resource.TestStep{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSubnetRouteTableExists("azurerm_subnet.rtTable2", "rtTable2-RT"),
),
},
},
})
}

func TestAccAzureRMSubnet_routeTable_subnetInline(t *testing.T) {

ri := acctest.RandInt()
//initConfig := fmt.Sprintf(testAccAzureRMSubnet_routeTable_subnetInline, ri, ri, ri)
updatedConfig := fmt.Sprintf(testAccAzureRMSubnet_updatedRouteTable, ri, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSubnetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSubnetRouteTableExists("azurerm_subnet.rtTable2", "rtTable2-RT"),
),
},
},
})
}

func TestAccAzureRMSubnet_disappears(t *testing.T) {

ri := acctest.RandInt()
Expand Down Expand Up @@ -60,6 +111,8 @@ func testCheckAzureRMSubnetExists(name string) resource.TestCheckFunc {
return fmt.Errorf("Not found: %s", name)
}

log.Printf("[INFO] Checking Subnet addition.")

name := rs.Primary.Attributes["name"]
vnetName := rs.Primary.Attributes["virtual_network_name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
Expand All @@ -78,6 +131,60 @@ func testCheckAzureRMSubnetExists(name string) resource.TestCheckFunc {
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)
}

return nil
}
}
Expand Down Expand Up @@ -154,3 +261,145 @@ resource "azurerm_subnet" "test" {
address_prefix = "10.0.2.0/24"
}
`

var testAccAzureRMSubnet_routeTable = `
resource "azurerm_resource_group" "rtTable2" {
name = "acctestRG-%d"
location = "West US"
}
resource "azurerm_virtual_network" "rtTable2" {
name = "acctestvirtnet%d"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
}
resource "azurerm_subnet" "rtTable2" {
name = "acctestsubnet%d"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
virtual_network_name = "${azurerm_virtual_network.rtTable2.name}"
address_prefix = "10.0.2.0/24"
route_table_id = "${azurerm_route_table.rtTable2.id}"
}
resource "azurerm_route_table" "rtTable2" {
name = "rtTable2-RT"
location = "West US"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
}
resource "azurerm_route" "route_a" {
name = "TestRouteA"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
route_table_name = "${azurerm_route_table.rtTable2.name}"
address_prefix = "10.100.0.0/14"
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = "10.10.1.1"
}`

var testAccAzureRMSubnet_routeTable_subnetInline = `
resource "azurerm_resource_group" "rtTable2" {
name = "acctestRG-%d"
location = "West US"
}
resource "azurerm_virtual_network" "rtTable2" {
name = "acctestvirtnet%d"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
subnet {
name = "rtTable2"
address_prefix = "10.0.3.0/24"
route_table = "${azurerm_route_table.rtTable2.id}"
}
tags {
environment = "Testing"
}
}
resource "azurerm_route_table" "rtTable2" {
name = "rtTable2-RT"
location = "West US"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
}
resource "azurerm_route" "route_a" {
name = "TestRouteA"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
route_table_name = "${azurerm_route_table.rtTable2.name}"
address_prefix = "10.100.0.0/14"
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = "10.10.1.1"
}`

var testAccAzureRMSubnet_updatedRouteTable = `
resource "azurerm_resource_group" "rtTable2" {
name = "acctestRG-%d"
location = "West US"
tags {
environment = "Testing"
}
}
resource "azurerm_network_security_group" "rtTable2_secgroup" {
name = "acceptanceTestSecurityGroup1"
location = "West US"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
security_rule {
name = "test123"
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" "rtTable2" {
name = "acctestvirtnet%d"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
tags {
environment = "Testing"
}
}
resource "azurerm_subnet" "rtTable2" {
name = "acctestsubnet%d"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
virtual_network_name = "${azurerm_virtual_network.rtTable2.name}"
address_prefix = "10.0.2.0/24"
route_table_id = "${azurerm_route_table.rtTable2.id}"
}
resource "azurerm_route_table" "rtTable2" {
name = "rtTable2-RT"
location = "West US"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
tags {
environment = "Testing"
}
}
resource "azurerm_route" "route_a" {
name = "TestRouteA"
resource_group_name = "${azurerm_resource_group.rtTable2.name}"
route_table_name = "${azurerm_route_table.rtTable2.name}"
address_prefix = "10.100.0.0/14"
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = "10.10.1.1"
}`
Loading

0 comments on commit 60f84cc

Please sign in to comment.