-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some lines end with a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
resourceGroupNameForDataSourceSchema()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 fixed