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

Some more AzureStack changes for route table resource and datasource #1807

Merged
merged 3 commits into from
Aug 23, 2018
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
10 changes: 6 additions & 4 deletions azurerm/data_source_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-04-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -14,8 +15,9 @@ func dataSourceArmRouteTable() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),
Expand Down Expand Up @@ -124,10 +126,10 @@ func flattenRouteTableDataSourceRoutes(input *[]network.Route) []interface{} {
return results
}

func flattenRouteTableDataSourceSubnets(input *[]network.Subnet) []string {
func flattenRouteTableDataSourceSubnets(subnets *[]network.Subnet) []string {
output := make([]string, 0)

if subnets := input; subnets != nil {
if subnets != nil {
for _, subnet := range *subnets {
output = append(output, *subnet.ID)
}
Expand Down
50 changes: 19 additions & 31 deletions azurerm/resource_arm_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

var routeTableResourceName = "azurerm_route_table"

func resourceArmRouteTable() *schema.Resource {
return &schema.Resource{
Create: resourceArmRouteTableCreate,
Create: resourceArmRouteTableCreateUpdate,
Read: resourceArmRouteTableRead,
Update: resourceArmRouteTableCreate,
Update: resourceArmRouteTableCreateUpdate,
Delete: resourceArmRouteTableDelete,

Importer: &schema.ResourceImporter{
Expand Down Expand Up @@ -64,7 +65,7 @@ func resourceArmRouteTable() *schema.Resource {
string(network.RouteNextHopTypeVirtualAppliance),
string(network.RouteNextHopTypeNone),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
DiffSuppressFunc: suppress.CaseDifference,
},

"next_hop_in_ip_address": {
Expand Down Expand Up @@ -95,7 +96,7 @@ func resourceArmRouteTable() *schema.Resource {
}
}

func resourceArmRouteTableCreate(d *schema.ResourceData, meta interface{}) error {
func resourceArmRouteTableCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).routeTablesClient
ctx := meta.(*ArmClient).StopContext

Expand All @@ -106,16 +107,11 @@ func resourceArmRouteTableCreate(d *schema.ResourceData, meta interface{}) error
resGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})

routes, err := expandRouteTableRoutes(d)
if err != nil {
return fmt.Errorf("Error Expanding list of Route Table Routes: %+v", err)
}

routeSet := network.RouteTable{
Name: &name,
Location: &location,
RouteTablePropertiesFormat: &network.RouteTablePropertiesFormat{
Routes: &routes,
Routes: expandRouteTableRoutes(d),
DisableBgpRoutePropagation: utils.Bool(d.Get("disable_bgp_route_propagation").(bool)),
},
Tags: expandTags(tags),
Expand All @@ -126,8 +122,7 @@ func resourceArmRouteTableCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error Creating/Updating Route Table %q (Resource Group %q): %+v", name, resGroup, err)
}

err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Route Table %q (Resource Group %q): %+v", name, resGroup, err)
}

Expand Down Expand Up @@ -204,43 +199,36 @@ func resourceArmRouteTableDelete(d *schema.ResourceData, meta interface{}) error
}
}

err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of Route Table %q (Resource Group %q): %+v", name, resGroup, err)
}

return nil
}

func expandRouteTableRoutes(d *schema.ResourceData) ([]network.Route, error) {
func expandRouteTableRoutes(d *schema.ResourceData) *[]network.Route {
configs := d.Get("route").([]interface{})
routes := make([]network.Route, 0, len(configs))

for _, configRaw := range configs {
data := configRaw.(map[string]interface{})

addressPrefix := data["address_prefix"].(string)
nextHopType := data["next_hop_type"].(string)

properties := network.RoutePropertiesFormat{
AddressPrefix: &addressPrefix,
NextHopType: network.RouteNextHopType(nextHopType),
route := network.Route{
Name: utils.String(data["name"].(string)),
RoutePropertiesFormat: &network.RoutePropertiesFormat{
AddressPrefix: utils.String(data["address_prefix"].(string)),
NextHopType: network.RouteNextHopType(data["next_hop_type"].(string)),
},
}

if v := data["next_hop_in_ip_address"].(string); v != "" {
properties.NextHopIPAddress = &v
}

name := data["name"].(string)
route := network.Route{
Name: &name,
RoutePropertiesFormat: &properties,
route.RoutePropertiesFormat.NextHopIPAddress = &v
}

routes = append(routes, route)
}

return routes, nil
return &routes
}

func flattenRouteTableRoutes(input *[]network.Route) []interface{} {
Expand All @@ -267,10 +255,10 @@ func flattenRouteTableRoutes(input *[]network.Route) []interface{} {
return results
}

func flattenRouteTableSubnets(input *[]network.Subnet) []string {
func flattenRouteTableSubnets(subnets *[]network.Subnet) []string {
output := []string{}

if subnets := input; subnets != nil {
if subnets != nil {
for _, subnet := range *subnets {
output = append(output, *subnet.ID)
}
Expand Down
52 changes: 26 additions & 26 deletions azurerm/resource_arm_route_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,19 @@ resource "azurerm_route_table" "test" {
func testAccAzureRMRouteTable_singleRoute(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_route_table" "test" {
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

route {
name = "route1"
name = "route1"
address_prefix = "10.1.0.0/16"
next_hop_type = "vnetlocal"
next_hop_type = "vnetlocal"
}
}
`, rInt, location, rInt)
Expand All @@ -403,41 +403,41 @@ resource "azurerm_route_table" "test" {
func testAccAzureRMRouteTable_singleRouteRemoved(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_route_table" "test" {
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
route = []
route = []
}
`, rInt, location, rInt)
}

func testAccAzureRMRouteTable_multipleRoutes(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_route_table" "test" {
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

route {
name = "route1"
name = "route1"
address_prefix = "10.1.0.0/16"
next_hop_type = "vnetlocal"
next_hop_type = "vnetlocal"
}

route {
name = "route2"
name = "route2"
address_prefix = "10.2.0.0/16"
next_hop_type = "vnetlocal"
next_hop_type = "vnetlocal"
}
}
`, rInt, location, rInt)
Expand All @@ -446,19 +446,19 @@ resource "azurerm_route_table" "test" {
func testAccAzureRMRouteTable_withTags(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_route_table" "test" {
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

route {
name = "route1"
name = "route1"
address_prefix = "10.1.0.0/16"
next_hop_type = "vnetlocal"
next_hop_type = "vnetlocal"
}

tags {
Expand All @@ -472,19 +472,19 @@ resource "azurerm_route_table" "test" {
func testAccAzureRMRouteTable_withTagsUpdate(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_route_table" "test" {
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
name = "acctestrt%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

route {
name = "route1"
name = "route1"
address_prefix = "10.1.0.0/16"
next_hop_type = "vnetlocal"
next_hop_type = "vnetlocal"
}

tags {
Expand Down
3 changes: 1 addition & 2 deletions azurerm/resource_arm_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ func resourceArmSubnetDelete(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error deleting Subnet %q (VN %q / Resource Group %q): %+v", name, vnetName, resGroup, err)
}

err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion for Subnet %q (VN %q / Resource Group %q): %+v", name, vnetName, resGroup, err)
}

Expand Down