2015-02-09 23:37:39 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-10 05:28:25 +02:00
|
|
|
"os"
|
2015-02-09 23:37:39 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
|
|
|
|
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/routers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccNetworkingV2Router_basic(t *testing.T) {
|
|
|
|
var router routers.Router
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckNetworkingV2RouterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccNetworkingV2Router_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckNetworkingV2RouterExists(t, "openstack_networking_router_v2.foo", &router),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccNetworkingV2Router_update,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttr("openstack_networking_router_v2.foo", "name", "router_2"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-09 17:31:22 +02:00
|
|
|
func TestAccNetworkingV2Router_update_external_gw(t *testing.T) {
|
|
|
|
var router routers.Router
|
2016-08-10 05:28:25 +02:00
|
|
|
externalGateway := os.Getenv("OS_EXTGW_ID")
|
|
|
|
|
|
|
|
var testAccNetworkingV2Router_update_external_gw_1 = fmt.Sprintf(`
|
|
|
|
resource "openstack_networking_router_v2" "foo" {
|
|
|
|
name = "router"
|
|
|
|
admin_state_up = "true"
|
|
|
|
distributed = "false"
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var testAccNetworkingV2Router_update_external_gw_2 = fmt.Sprintf(`
|
|
|
|
resource "openstack_networking_router_v2" "foo" {
|
|
|
|
name = "router"
|
|
|
|
admin_state_up = "true"
|
|
|
|
distributed = "false"
|
|
|
|
external_gateway = "%s"
|
|
|
|
}`, externalGateway)
|
2016-08-09 17:31:22 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckNetworkingV2RouterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-08-10 05:28:25 +02:00
|
|
|
Config: testAccNetworkingV2Router_update_external_gw_1,
|
2016-08-09 17:31:22 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckNetworkingV2RouterExists(t, "openstack_networking_router_v2.foo", &router),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-08-10 05:28:25 +02:00
|
|
|
Config: testAccNetworkingV2Router_update_external_gw_2,
|
2016-08-09 17:31:22 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-08-10 05:28:25 +02:00
|
|
|
resource.TestCheckResourceAttr("openstack_networking_router_v2.foo", "external_gateway", externalGateway),
|
2016-08-09 17:31:22 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-09 23:37:39 +01:00
|
|
|
func testAccCheckNetworkingV2RouterDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("(testAccCheckNetworkingV2RouterDestroy) Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "openstack_networking_router_v2" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := routers.Get(networkingClient, rs.Primary.ID).Extract()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Router still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckNetworkingV2RouterExists(t *testing.T, n string, router *routers.Router) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("(testAccCheckNetworkingV2RouterExists) Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
found, err := routers.Get(networkingClient, rs.Primary.ID).Extract()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.ID != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Router not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*router = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccNetworkingV2Router_basic = fmt.Sprintf(`
|
2015-02-09 23:55:21 +01:00
|
|
|
resource "openstack_networking_router_v2" "foo" {
|
|
|
|
name = "router"
|
|
|
|
admin_state_up = "true"
|
2016-02-12 18:57:41 +01:00
|
|
|
distributed = "false"
|
2015-02-09 23:55:21 +01:00
|
|
|
}`)
|
2015-02-09 23:37:39 +01:00
|
|
|
|
|
|
|
var testAccNetworkingV2Router_update = fmt.Sprintf(`
|
2015-02-09 23:55:21 +01:00
|
|
|
resource "openstack_networking_router_v2" "foo" {
|
|
|
|
name = "router_2"
|
|
|
|
admin_state_up = "true"
|
2016-02-12 18:57:41 +01:00
|
|
|
distributed = "false"
|
2015-02-09 23:55:21 +01:00
|
|
|
}`)
|