Skip to content

Commit

Permalink
provider/openstack: Add network_id to Network data source (#12615)
Browse files Browse the repository at this point in the history
This commit adds the ability to search for a network by its ID. This
is useful for doing ID-to-name translations.
  • Loading branch information
jtopjian authored and stack72 committed Mar 13, 2017
1 parent 3cd3cb0 commit 87e0119
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func dataSourceNetworkingNetworkV2() *schema.Resource {
Read: dataSourceNetworkingNetworkV2Read,

Schema: map[string]*schema.Schema{
"network_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -56,6 +60,7 @@ func dataSourceNetworkingNetworkV2Read(d *schema.ResourceData, meta interface{})
networkingClient, err := config.networkingV2Client(GetRegion(d))

listOpts := networks.ListOpts{
ID: d.Get("network_id").(string),
Name: d.Get("name").(string),
TenantID: d.Get("tenant_id").(string),
Status: "ACTIVE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ func TestAccOpenStackNetworkingNetworkV2DataSource_subnet(t *testing.T) {
})
}

func TestAccOpenStackNetworkingNetworkV2DataSource_networkID(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccOpenStackNetworkingNetworkV2DataSource_network,
},
resource.TestStep{
Config: testAccOpenStackNetworkingNetworkV2DataSource_networkID,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingNetworkV2DataSourceID("data.openstack_networking_network_v2.net"),
resource.TestCheckResourceAttr(
"data.openstack_networking_network_v2.net", "name", "tf_test_network"),
resource.TestCheckResourceAttr(
"data.openstack_networking_network_v2.net", "admin_state_up", "true"),
),
},
},
})
}

func testAccCheckNetworkingNetworkV2DataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -96,3 +118,11 @@ data "openstack_networking_network_v2" "net" {
matching_subnet_cidr = "${openstack_networking_subnet_v2.subnet.cidr}"
}
`, testAccOpenStackNetworkingNetworkV2DataSource_network)

var testAccOpenStackNetworkingNetworkV2DataSource_networkID = fmt.Sprintf(`
%s
data "openstack_networking_network_v2" "net" {
network_id = "${openstack_networking_network_v2.net.id}"
}
`, testAccOpenStackNetworkingNetworkV2DataSource_network)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ data "openstack_networking_network_v2" "network" {
A Neutron client is needed to retrieve networks ids. If omitted, the
`OS_REGION_NAME` environment variable is used.

* `network_id` - (Optional) The ID of the network.

* `name` - (Optional) The name of the network.

* `matching_subnet_cidr` - (Optional) The CIDR of a subnet within the network.
Expand Down

0 comments on commit 87e0119

Please sign in to comment.