Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MS] provider/azurerm: fixes for bug #8227 and bug #11226 #13877

Merged
merged 6 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions builtin/providers/azurerm/import_arm_subnet_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand All @@ -12,7 +11,31 @@ func TestAccAzureRMSubnet_importBasic(t *testing.T) {
resourceName := "azurerm_subnet.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMSubnet_basic, ri, ri, ri, ri, ri)
config := testAccAzureRMSubnet_basic(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSubnetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMSubnet_importWithRouteTable(t *testing.T) {
resourceName := "azurerm_subnet.test"

ri := acctest.RandInt()
config := testAccAzureRMSubnet_routeTable(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
205 changes: 201 additions & 4 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 All @@ -13,7 +15,7 @@ import (
func TestAccAzureRMSubnet_basic(t *testing.T) {

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMSubnet_basic, ri, ri, ri, ri, ri)
config := testAccAzureRMSubnet_basic(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -30,10 +32,38 @@ func TestAccAzureRMSubnet_basic(t *testing.T) {
})
}

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 := fmt.Sprintf(testAccAzureRMSubnet_basic, ri, ri, ri, ri, ri)
config := testAccAzureRMSubnet_basic(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -60,6 +90,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 +110,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 @@ -134,7 +220,8 @@ func testCheckAzureRMSubnetDestroy(s *terraform.State) error {
return nil
}

var testAccAzureRMSubnet_basic = `
func testAccAzureRMSubnet_basic(rInt int) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
Expand Down Expand Up @@ -170,4 +257,114 @@ resource "azurerm_route" "test" {
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = "10.10.1.1"
}
`
`, 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)
}
Loading