-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source:
azurerm_route_table
Fixes #1155
- Loading branch information
1 parent
65aa4f3
commit ba39d10
Showing
5 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2017-09-01/network" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmRouteTable() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmRouteTableRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"resource_group_name": resourceGroupNameSchema(), | ||
|
||
"location": locationForDataSourceSchema(), | ||
|
||
"route": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"address_prefix": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"next_hop_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"next_hop_in_ip_address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"subnets": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"tags": tagsForDataSourceSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmRouteTableRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).routeTablesClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
resp, err := client.Get(ctx, resourceGroup, name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on Azure Route Table %q: %+v", name, err) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resourceGroup) | ||
if location := resp.Location; location != nil { | ||
d.Set("location", azureRMNormalizeLocation(*location)) | ||
} | ||
|
||
if props := resp.RouteTablePropertiesFormat; props != nil { | ||
if err := d.Set("route", flattenRouteTableDataSourceRoutes(props.Routes)); err != nil { | ||
return err | ||
} | ||
|
||
if err := d.Set("subnets", flattenRouteTableDataSourceSubnets(props.Subnets)); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
flattenAndSetTags(d, resp.Tags) | ||
|
||
return nil | ||
} | ||
|
||
func flattenRouteTableDataSourceRoutes(input *[]network.Route) []interface{} { | ||
results := make([]interface{}, 0) | ||
|
||
if routes := input; routes != nil { | ||
for _, route := range *routes { | ||
r := make(map[string]interface{}) | ||
|
||
r["name"] = *route.Name | ||
|
||
if props := route.RoutePropertiesFormat; props != nil { | ||
r["address_prefix"] = *props.AddressPrefix | ||
r["next_hop_type"] = string(props.NextHopType) | ||
if ip := props.NextHopIPAddress; ip != nil { | ||
r["next_hop_in_ip_address"] = *ip | ||
} | ||
} | ||
|
||
results = append(results, r) | ||
} | ||
} | ||
|
||
return results | ||
} | ||
|
||
func flattenRouteTableDataSourceSubnets(input *[]network.Subnet) []string { | ||
output := make([]string, 0) | ||
|
||
if subnets := input; subnets != nil { | ||
for _, subnet := range *subnets { | ||
output = append(output, *subnet.ID) | ||
} | ||
} | ||
|
||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAzureRMRouteTable_basic(t *testing.T) { | ||
dataSourceName := "data.azurerm_route_table.test" | ||
ri := acctest.RandInt() | ||
location := testLocation() | ||
config := testAccDataSourceAzureRMRouteTable_basic(ri, location) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMRouteTableDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMRouteTableExists(dataSourceName), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.#", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMRouteTable_singleRoute(t *testing.T) { | ||
dataSourceName := "data.azurerm_route_table.test" | ||
ri := acctest.RandInt() | ||
location := testLocation() | ||
config := testAccDataSourceAzureRMRouteTable_singleRoute(ri, location) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMRouteTableDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMRouteTableExists(dataSourceName), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.#", "1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.0.name", "route1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.0.address_prefix", "10.1.0.0/16"), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.0.next_hop_type", "VnetLocal"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMRouteTable_multipleRoutes(t *testing.T) { | ||
dataSourceName := "data.azurerm_route_table.test" | ||
ri := acctest.RandInt() | ||
location := testLocation() | ||
config := testAccDataSourceAzureRMRouteTable_multipleRoutes(ri, location) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMRouteTableDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMRouteTableExists(dataSourceName), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.#", "2"), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.0.name", "route1"), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.0.address_prefix", "10.1.0.0/16"), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.0.next_hop_type", "VnetLocal"), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.1.name", "route2"), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.1.address_prefix", "10.2.0.0/16"), | ||
resource.TestCheckResourceAttr(dataSourceName, "route.1.next_hop_type", "VnetLocal"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAzureRMRouteTable_basic(rInt int, location string) string { | ||
resource := testAccAzureRMRouteTable_basic(rInt, location) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_route_table" "test" { | ||
name = "${azurerm_route_table.test.name}" | ||
resource_group_name = "${azurerm_route_table.test.resource_group_name}" | ||
} | ||
`, resource) | ||
} | ||
|
||
func testAccDataSourceAzureRMRouteTable_singleRoute(rInt int, location string) string { | ||
resource := testAccAzureRMRouteTable_singleRoute(rInt, location) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_route_table" "test" { | ||
name = "${azurerm_route_table.test.name}" | ||
resource_group_name = "${azurerm_route_table.test.resource_group_name}" | ||
} | ||
`, resource) | ||
} | ||
|
||
func testAccDataSourceAzureRMRouteTable_multipleRoutes(rInt int, location string) string { | ||
resource := testAccAzureRMRouteTable_multipleRoutes(rInt, location) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_route_table" "test" { | ||
name = "${azurerm_route_table.test.name}" | ||
resource_group_name = "${azurerm_route_table.test.resource_group_name}" | ||
} | ||
`, resource) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: azurerm_route_table" | ||
sidebar_current: "docs-azurerm-data-source-route-table" | ||
description: |- | ||
Gets information about a Route Table | ||
--- | ||
|
||
# Data Source: azurerm_route_table | ||
|
||
Gets information about a Route Table | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "azurerm_route_table" "test" { | ||
name = "myroutetable" | ||
resource_group_name = "some-resource-group" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name of the Route Table | ||
|
||
* `resource_group_name` - (Required) The name of the Resource Group in which the Route Table exists | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - The Route Table ID. | ||
|
||
* `location` - The Azure Region in which the Route Table exists. | ||
|
||
* `route` - One or more `route` blocks as documented below | ||
|
||
* `subnets` - The collection of Subnets associated with this route table. | ||
|
||
* `tags` - A mapping of tags assigned to the Route Table | ||
|
||
The `route` block exports the following: | ||
|
||
* `name` - The name of the Route. | ||
|
||
* `address_prefix` - The destination CIDR to which the route applies. | ||
|
||
* `next_hop_type` - The type of Azure hop the packet should be sent to. | ||
|
||
* `next_hop_in_ip_address` - Contains the IP address packets should be forwarded to. | ||
|