github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/resource_openstack_networking_router_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 11 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers" 12 ) 13 14 func TestAccNetworkingV2Router_basic(t *testing.T) { 15 var router routers.Router 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckNetworkingV2RouterDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccNetworkingV2Router_basic, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckNetworkingV2RouterExists(t, "openstack_networking_router_v2.foo", &router), 26 ), 27 }, 28 resource.TestStep{ 29 Config: testAccNetworkingV2Router_update, 30 Check: resource.ComposeTestCheckFunc( 31 resource.TestCheckResourceAttr("openstack_networking_router_v2.foo", "name", "router_2"), 32 ), 33 }, 34 }, 35 }) 36 } 37 38 func TestAccNetworkingV2Router_update_external_gw(t *testing.T) { 39 var router routers.Router 40 externalGateway := os.Getenv("OS_EXTGW_ID") 41 42 var testAccNetworkingV2Router_update_external_gw_1 = fmt.Sprintf(` 43 resource "openstack_networking_router_v2" "foo" { 44 name = "router" 45 admin_state_up = "true" 46 distributed = "false" 47 }`) 48 49 var testAccNetworkingV2Router_update_external_gw_2 = fmt.Sprintf(` 50 resource "openstack_networking_router_v2" "foo" { 51 name = "router" 52 admin_state_up = "true" 53 distributed = "false" 54 external_gateway = "%s" 55 }`, externalGateway) 56 57 resource.Test(t, resource.TestCase{ 58 PreCheck: func() { testAccPreCheck(t) }, 59 Providers: testAccProviders, 60 CheckDestroy: testAccCheckNetworkingV2RouterDestroy, 61 Steps: []resource.TestStep{ 62 resource.TestStep{ 63 Config: testAccNetworkingV2Router_update_external_gw_1, 64 Check: resource.ComposeTestCheckFunc( 65 testAccCheckNetworkingV2RouterExists(t, "openstack_networking_router_v2.foo", &router), 66 ), 67 }, 68 resource.TestStep{ 69 Config: testAccNetworkingV2Router_update_external_gw_2, 70 Check: resource.ComposeTestCheckFunc( 71 resource.TestCheckResourceAttr("openstack_networking_router_v2.foo", "external_gateway", externalGateway), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 func testAccCheckNetworkingV2RouterDestroy(s *terraform.State) error { 79 config := testAccProvider.Meta().(*Config) 80 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 81 if err != nil { 82 return fmt.Errorf("(testAccCheckNetworkingV2RouterDestroy) Error creating OpenStack networking client: %s", err) 83 } 84 85 for _, rs := range s.RootModule().Resources { 86 if rs.Type != "openstack_networking_router_v2" { 87 continue 88 } 89 90 _, err := routers.Get(networkingClient, rs.Primary.ID).Extract() 91 if err == nil { 92 return fmt.Errorf("Router still exists") 93 } 94 } 95 96 return nil 97 } 98 99 func testAccCheckNetworkingV2RouterExists(t *testing.T, n string, router *routers.Router) resource.TestCheckFunc { 100 return func(s *terraform.State) error { 101 rs, ok := s.RootModule().Resources[n] 102 if !ok { 103 return fmt.Errorf("Not found: %s", n) 104 } 105 106 if rs.Primary.ID == "" { 107 return fmt.Errorf("No ID is set") 108 } 109 110 config := testAccProvider.Meta().(*Config) 111 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 112 if err != nil { 113 return fmt.Errorf("(testAccCheckNetworkingV2RouterExists) Error creating OpenStack networking client: %s", err) 114 } 115 116 found, err := routers.Get(networkingClient, rs.Primary.ID).Extract() 117 if err != nil { 118 return err 119 } 120 121 if found.ID != rs.Primary.ID { 122 return fmt.Errorf("Router not found") 123 } 124 125 *router = *found 126 127 return nil 128 } 129 } 130 131 var testAccNetworkingV2Router_basic = fmt.Sprintf(` 132 resource "openstack_networking_router_v2" "foo" { 133 name = "router" 134 admin_state_up = "true" 135 distributed = "false" 136 }`) 137 138 var testAccNetworkingV2Router_update = fmt.Sprintf(` 139 resource "openstack_networking_router_v2" "foo" { 140 name = "router_2" 141 admin_state_up = "true" 142 distributed = "false" 143 }`)