From b7c50a010e3898ac842b07b23750c28be263d829 Mon Sep 17 00:00:00 2001 From: Tony Fortes Ramos Date: Wed, 7 Nov 2018 14:32:27 +0100 Subject: [PATCH] Fix error when changing route from virtual appliance to any other type (#2184) * Route table now allows removing next-hop * Route now allows removing next hop and is now more similar to route_table * run gofmt 1.10.4 against resource_arm_route_table * merge from master upstream and fix linting errors * add route test for next_hop_type change * add route table test for next_hop_type change * Update route test to verify empty next hop ip Co-Authored-By: draggeta * Update route test to verify empty next hop ip Co-Authored-By: draggeta * ensuring the `next_hop_in_ip_address` field is always set ``` $ acctests azurerm TestAccAzureRMRoute_update === RUN TestAccAzureRMRoute_update --- PASS: TestAccAzureRMRoute_update (151.30s) PASS ok github.com/terraform-providers/terraform-provider-azurerm/azurerm 153.050s ``` --- azurerm/resource_arm_route.go | 16 +++--- azurerm/resource_arm_route_table.go | 1 - azurerm/resource_arm_route_table_test.go | 30 ++++++++++++ azurerm/resource_arm_route_test.go | 62 ++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 10 deletions(-) diff --git a/azurerm/resource_arm_route.go b/azurerm/resource_arm_route.go index 818a2f6075d1..3a53f0c2ff37 100644 --- a/azurerm/resource_arm_route.go +++ b/azurerm/resource_arm_route.go @@ -39,8 +39,9 @@ func resourceArmRoute() *schema.Resource { }, "address_prefix": { - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, }, "next_hop_type": { @@ -57,9 +58,9 @@ func resourceArmRoute() *schema.Resource { }, "next_hop_in_ip_address": { - Type: schema.TypeString, - Optional: true, - Computed: true, + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.NoZeroValues, }, }, } @@ -140,10 +141,7 @@ func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error { if props := resp.RoutePropertiesFormat; props != nil { d.Set("address_prefix", props.AddressPrefix) d.Set("next_hop_type", string(props.NextHopType)) - - if ip := props.NextHopIPAddress; ip != nil { - d.Set("next_hop_in_ip_address", props.NextHopIPAddress) - } + d.Set("next_hop_in_ip_address", props.NextHopIPAddress) } return nil diff --git a/azurerm/resource_arm_route_table.go b/azurerm/resource_arm_route_table.go index 6db00ff3719c..e6dceae3013e 100644 --- a/azurerm/resource_arm_route_table.go +++ b/azurerm/resource_arm_route_table.go @@ -71,7 +71,6 @@ func resourceArmRouteTable() *schema.Resource { "next_hop_in_ip_address": { Type: schema.TypeString, Optional: true, - Computed: true, ValidateFunc: validation.NoZeroValues, }, }, diff --git a/azurerm/resource_arm_route_table_test.go b/azurerm/resource_arm_route_table_test.go index 5f5de26899de..b1848f4d09c6 100644 --- a/azurerm/resource_arm_route_table_test.go +++ b/azurerm/resource_arm_route_table_test.go @@ -70,6 +70,14 @@ func TestAccAzureRMRouteTable_update(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "route.#", "0"), ), }, + { + Config: testAccAzureRMRouteTable_basicAppliance(ri, testLocation()), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMRouteTableExists("azurerm_route_table.test"), + resource.TestCheckResourceAttr(resourceName, "disable_bgp_route_propagation", "false"), + resource.TestCheckResourceAttr(resourceName, "route.#", "1"), + ), + }, { Config: testAccAzureRMRouteTable_complete(ri, testLocation()), Check: resource.ComposeTestCheckFunc( @@ -367,6 +375,28 @@ resource "azurerm_route_table" "test" { `, rInt, location, rInt) } +func testAccAzureRMRouteTable_basicAppliance(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_route_table" "test" { + name = "acctestrt%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + route { + name = "route1" + address_prefix = "10.1.0.0/16" + next_hop_type = "VirtualAppliance" + next_hop_in_ip_address = "192.168.0.1" + } +} +`, rInt, location, rInt) +} + func testAccAzureRMRouteTable_complete(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { diff --git a/azurerm/resource_arm_route_test.go b/azurerm/resource_arm_route_test.go index ae9340095f2c..472fd6e7f646 100644 --- a/azurerm/resource_arm_route_test.go +++ b/azurerm/resource_arm_route_test.go @@ -35,6 +35,43 @@ func TestAccAzureRMRoute_basic(t *testing.T) { }) } +func TestAccAzureRMRoute_update(t *testing.T) { + resourceName := "azurerm_route.test" + ri := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMRouteDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMRoute_basic(ri, testLocation()), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMRouteExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "next_hop_type", "VnetLocal"), + resource.TestCheckResourceAttr(resourceName, "next_hop_in_ip_address", ""), + ), + }, + { + Config: testAccAzureRMRoute_basicAppliance(ri, testLocation()), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMRouteExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "next_hop_type", "VirtualAppliance"), + resource.TestCheckResourceAttr(resourceName, "next_hop_in_ip_address", "192.168.0.1"), + ), + }, + { + Config: testAccAzureRMRoute_basic(ri, testLocation()), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMRouteExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "next_hop_type", "VnetLocal"), + resource.TestCheckResourceAttr(resourceName, "next_hop_in_ip_address", ""), + ), + }, + }, + }) +} + func TestAccAzureRMRoute_disappears(t *testing.T) { ri := acctest.RandInt() config := testAccAzureRMRoute_basic(ri, testLocation()) @@ -197,6 +234,31 @@ resource "azurerm_route" "test" { `, rInt, location, rInt, rInt) } +func testAccAzureRMRoute_basicAppliance(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_route_table" "test" { + name = "acctestrt%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" +} + +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.1.0.0/16" + next_hop_type = "VirtualAppliance" + next_hop_in_ip_address = "192.168.0.1" +} +`, rInt, location, rInt, rInt) +} + func testAccAzureRMRoute_multipleRoutes(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" {