github.com/tam7t/terraform@v0.7.0-rc2.0.20160705125922-be2469a05c5e/builtin/providers/openstack/resource_openstack_networking_router_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/routers"
    11  )
    12  
    13  func TestAccNetworkingV2Router_basic(t *testing.T) {
    14  	var router routers.Router
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckNetworkingV2RouterDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccNetworkingV2Router_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckNetworkingV2RouterExists(t, "openstack_networking_router_v2.foo", &router),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccNetworkingV2Router_update,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					resource.TestCheckResourceAttr("openstack_networking_router_v2.foo", "name", "router_2"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccCheckNetworkingV2RouterDestroy(s *terraform.State) error {
    38  	config := testAccProvider.Meta().(*Config)
    39  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    40  	if err != nil {
    41  		return fmt.Errorf("(testAccCheckNetworkingV2RouterDestroy) Error creating OpenStack networking client: %s", err)
    42  	}
    43  
    44  	for _, rs := range s.RootModule().Resources {
    45  		if rs.Type != "openstack_networking_router_v2" {
    46  			continue
    47  		}
    48  
    49  		_, err := routers.Get(networkingClient, rs.Primary.ID).Extract()
    50  		if err == nil {
    51  			return fmt.Errorf("Router still exists")
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func testAccCheckNetworkingV2RouterExists(t *testing.T, n string, router *routers.Router) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources[n]
    61  		if !ok {
    62  			return fmt.Errorf("Not found: %s", n)
    63  		}
    64  
    65  		if rs.Primary.ID == "" {
    66  			return fmt.Errorf("No ID is set")
    67  		}
    68  
    69  		config := testAccProvider.Meta().(*Config)
    70  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    71  		if err != nil {
    72  			return fmt.Errorf("(testAccCheckNetworkingV2RouterExists) Error creating OpenStack networking client: %s", err)
    73  		}
    74  
    75  		found, err := routers.Get(networkingClient, rs.Primary.ID).Extract()
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		if found.ID != rs.Primary.ID {
    81  			return fmt.Errorf("Router not found")
    82  		}
    83  
    84  		*router = *found
    85  
    86  		return nil
    87  	}
    88  }
    89  
    90  var testAccNetworkingV2Router_basic = fmt.Sprintf(`
    91    resource "openstack_networking_router_v2" "foo" {
    92      name = "router"
    93      admin_state_up = "true"
    94      distributed = "false"
    95    }`)
    96  
    97  var testAccNetworkingV2Router_update = fmt.Sprintf(`
    98    resource "openstack_networking_router_v2" "foo" {
    99      name = "router_2"
   100      admin_state_up = "true"
   101      distributed = "false"
   102    }`)