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

New Data Source: azurerm_route_table #1203

Merged
merged 2 commits into from
May 7, 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
138 changes: 138 additions & 0 deletions azurerm/data_source_route_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
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,
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"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
}
120 changes: 120 additions & 0 deletions azurerm/data_source_route_table_test.go
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)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_recovery_services_vault": dataSourceArmRecoveryServicesVault(),
"azurerm_resource_group": dataSourceArmResourceGroup(),
"azurerm_role_definition": dataSourceArmRoleDefinition(),
"azurerm_route_table": dataSourceArmRouteTable(),
"azurerm_scheduler_job_collection": dataSourceArmSchedulerJobCollection(),
"azurerm_snapshot": dataSourceArmSnapshot(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
Expand Down
6 changes: 5 additions & 1 deletion website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@
<a href="/docs/providers/azurerm/d/resource_group.html">azurerm_resource_group</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-role_definition") %>>
<li<%= sidebar_current("docs-azurerm-datasource-role-definition") %>>
<a href="/docs/providers/azurerm/d/role_definition.html">azurerm_role_definition</a>
</li>

<li<%= sidebar_current("docs-azurerm-data-source-route-table") %>>
<a href="/docs/providers/azurerm/d/route_table.html">azurerm_route_table</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-scheduler-job-collection") %>>
<a href="/docs/providers/azurerm/d/scheduler_job_collection.html">azurerm_scheduler_job_collection</a>
</li>
Expand Down
54 changes: 54 additions & 0 deletions website/docs/d/route_table.html.markdown
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some lines end with a ., some do not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 fixed


* `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.