2016-01-10 16:02:48 +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-10 16:02:48 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureRMRoute_basic(t *testing.T) {
|
|
|
|
|
2016-01-28 22:00:43 +01:00
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMRoute_basic, ri, ri, ri)
|
|
|
|
|
2016-01-10 16:02:48 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMRouteDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: config,
|
2016-01-10 16:02:48 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMRouteExists("azurerm_route.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureRMRoute_multipleRoutes(t *testing.T) {
|
|
|
|
|
2016-01-28 22:00:43 +01:00
|
|
|
ri := acctest.RandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMRoute_basic, ri, ri, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMRoute_multipleRoutes, ri, ri, ri)
|
|
|
|
|
2016-01-10 16:02:48 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMRouteDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: preConfig,
|
2016-01-10 16:02:48 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMRouteExists("azurerm_route.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2016-01-28 22:00:43 +01:00
|
|
|
Config: postConfig,
|
2016-01-10 16:02:48 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMRouteExists("azurerm_route.test1"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMRouteExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
rtName := rs.Primary.Attributes["route_table_name"]
|
|
|
|
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
if !hasResourceGroup {
|
|
|
|
return fmt.Errorf("Bad: no resource group found in state for route: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).routesClient
|
|
|
|
|
|
|
|
resp, err := conn.Get(resourceGroup, rtName, name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Get on routesClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Bad: Route %q (resource group: %q) does not exist", name, resourceGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMRouteDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).routesClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_route" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
rtName := rs.Primary.Attributes["route_table_name"]
|
|
|
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
|
|
|
|
resp, err := conn.Get(resourceGroup, rtName, name)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Route still exists:\n%#v", resp.Properties)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureRMRoute_basic = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestrg-%d"
|
2016-01-10 16:02:48 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_route_table" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestrt%d"
|
2016-01-10 16:02:48 +01:00
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_route" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestroute%d"
|
2016-01-10 16:02:48 +01:00
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
route_table_name = "${azurerm_route_table.test.name}"
|
|
|
|
|
|
|
|
address_prefix = "10.1.0.0/16"
|
|
|
|
next_hop_type = "vnetlocal"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMRoute_multipleRoutes = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestrg-%d"
|
2016-01-10 16:02:48 +01:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_route_table" "test" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestrt%d"
|
2016-01-10 16:02:48 +01:00
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_route" "test1" {
|
2016-01-28 22:00:43 +01:00
|
|
|
name = "acctestroute%d"
|
2016-01-10 16:02:48 +01:00
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
route_table_name = "${azurerm_route_table.test.name}"
|
|
|
|
|
|
|
|
address_prefix = "10.2.0.0/16"
|
|
|
|
next_hop_type = "none"
|
|
|
|
}
|
|
|
|
`
|