Skip to content

Commit

Permalink
Merge pull request #402 from terraform-providers/routes
Browse files Browse the repository at this point in the history
`azurerm_route` / `azurerm_route_table` - additions
  • Loading branch information
tombuildsstuff authored Oct 6, 2017
2 parents 7e6affb + 5c10ed6 commit ce399f7
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 242 deletions.
46 changes: 46 additions & 0 deletions azurerm/import_arm_route_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,49 @@ func TestAccAzureRMRouteTable_importBasic(t *testing.T) {
},
})
}

func TestAccAzureRMRouteTable_importSingleRoute(t *testing.T) {
resourceName := "azurerm_route_table.test"

ri := acctest.RandInt()
config := testAccAzureRMRouteTable_singleRoute(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRouteTableDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMRouteTable_importMultipleRoutes(t *testing.T) {
resourceName := "azurerm_route_table.test"

ri := acctest.RandInt()
config := testAccAzureRMRouteTable_multipleRoutes(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRouteTableDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
59 changes: 32 additions & 27 deletions azurerm/resource_arm_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package azurerm

import (
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmRoute() *schema.Resource {
return &schema.Resource{
Create: resourceArmRouteCreate,
Create: resourceArmRouteCreateUpdate,
Read: resourceArmRouteRead,
Update: resourceArmRouteCreate,
Update: resourceArmRouteCreateUpdate,
Delete: resourceArmRouteDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -40,12 +40,16 @@ func resourceArmRoute() *schema.Resource {
},

"next_hop_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateRouteTableNextHopType,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(old) == strings.ToLower(new)
},
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(network.RouteNextHopTypeVirtualNetworkGateway),
string(network.RouteNextHopTypeVnetLocal),
string(network.RouteNextHopTypeInternet),
string(network.RouteNextHopTypeVirtualAppliance),
string(network.RouteNextHopTypeNone),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"next_hop_in_ip_address": {
Expand All @@ -57,9 +61,8 @@ func resourceArmRoute() *schema.Resource {
}
}

func resourceArmRouteCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
routesClient := client.routesClient
func resourceArmRouteCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).routesClient

name := d.Get("name").(string)
rtName := d.Get("route_table_name").(string)
Expand All @@ -86,26 +89,26 @@ func resourceArmRouteCreate(d *schema.ResourceData, meta interface{}) error {
RoutePropertiesFormat: &properties,
}

_, error := routesClient.CreateOrUpdate(resGroup, rtName, name, route, make(chan struct{}))
err := <-error
_, createErr := client.CreateOrUpdate(resGroup, rtName, name, route, make(chan struct{}))
err := <-createErr
if err != nil {
return err
}

read, err := routesClient.Get(resGroup, rtName, name)
read, err := client.Get(resGroup, rtName, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Route %s/%s (resource group %s) ID", rtName, name, resGroup)
return fmt.Errorf("Cannot read Route %q/%q (resource group %q) ID", rtName, name, resGroup)
}
d.SetId(*read.ID)

return resourceArmRouteRead(d, meta)
}

func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error {
routesClient := meta.(*ArmClient).routesClient
client := meta.(*ArmClient).routesClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -115,31 +118,33 @@ func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error {
rtName := id.Path["routeTables"]
routeName := id.Path["routes"]

resp, err := routesClient.Get(resGroup, rtName, routeName)
resp, err := client.Get(resGroup, rtName, routeName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err)
return fmt.Errorf("Error making Read request on Azure Route %q: %+v", routeName, err)
}

d.Set("name", routeName)
d.Set("resource_group_name", resGroup)
d.Set("route_table_name", rtName)
d.Set("address_prefix", resp.RoutePropertiesFormat.AddressPrefix)
d.Set("next_hop_type", string(resp.RoutePropertiesFormat.NextHopType))

if resp.RoutePropertiesFormat.NextHopIPAddress != nil {
d.Set("next_hop_in_ip_address", resp.RoutePropertiesFormat.NextHopIPAddress)
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)
}
}

return nil
}

func resourceArmRouteDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
routesClient := client.routesClient
client := meta.(*ArmClient).routesClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -152,8 +157,8 @@ func resourceArmRouteDelete(d *schema.ResourceData, meta interface{}) error {
azureRMLockByName(rtName, routeTableResourceName)
defer azureRMUnlockByName(rtName, routeTableResourceName)

_, error := routesClient.Delete(resGroup, rtName, routeName, make(chan struct{}))
err = <-error
_, deleteErr := client.Delete(resGroup, rtName, routeName, make(chan struct{}))
err = <-deleteErr

return err
}
Loading

0 comments on commit ce399f7

Please sign in to comment.