github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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/gophercloud/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("openstack_networking_router_v2.router_1", &router),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccNetworkingV2Router_update,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					resource.TestCheckResourceAttr(
    31  						"openstack_networking_router_v2.router_1", "name", "router_2"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestAccNetworkingV2Router_update_external_gw(t *testing.T) {
    39  	var router routers.Router
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckNetworkingV2RouterDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: testAccNetworkingV2Router_update_external_gw_1,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckNetworkingV2RouterExists("openstack_networking_router_v2.router_1", &router),
    50  				),
    51  			},
    52  			resource.TestStep{
    53  				Config: testAccNetworkingV2Router_update_external_gw_2,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					resource.TestCheckResourceAttr(
    56  						"openstack_networking_router_v2.router_1", "external_gateway", OS_EXTGW_ID),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testAccCheckNetworkingV2RouterDestroy(s *terraform.State) error {
    64  	config := testAccProvider.Meta().(*Config)
    65  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    66  	if err != nil {
    67  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    68  	}
    69  
    70  	for _, rs := range s.RootModule().Resources {
    71  		if rs.Type != "openstack_networking_router_v2" {
    72  			continue
    73  		}
    74  
    75  		_, err := routers.Get(networkingClient, rs.Primary.ID).Extract()
    76  		if err == nil {
    77  			return fmt.Errorf("Router still exists")
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func testAccCheckNetworkingV2RouterExists(n string, router *routers.Router) resource.TestCheckFunc {
    85  	return func(s *terraform.State) error {
    86  		rs, ok := s.RootModule().Resources[n]
    87  		if !ok {
    88  			return fmt.Errorf("Not found: %s", n)
    89  		}
    90  
    91  		if rs.Primary.ID == "" {
    92  			return fmt.Errorf("No ID is set")
    93  		}
    94  
    95  		config := testAccProvider.Meta().(*Config)
    96  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    97  		if err != nil {
    98  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    99  		}
   100  
   101  		found, err := routers.Get(networkingClient, rs.Primary.ID).Extract()
   102  		if err != nil {
   103  			return err
   104  		}
   105  
   106  		if found.ID != rs.Primary.ID {
   107  			return fmt.Errorf("Router not found")
   108  		}
   109  
   110  		*router = *found
   111  
   112  		return nil
   113  	}
   114  }
   115  
   116  const testAccNetworkingV2Router_basic = `
   117  resource "openstack_networking_router_v2" "router_1" {
   118  	name = "router_1"
   119  	admin_state_up = "true"
   120  	distributed = "false"
   121  }
   122  `
   123  
   124  const testAccNetworkingV2Router_update = `
   125  resource "openstack_networking_router_v2" "router_1" {
   126  	name = "router_2"
   127  	admin_state_up = "true"
   128  	distributed = "false"
   129  }
   130  `
   131  
   132  const testAccNetworkingV2Router_update_external_gw_1 = `
   133  resource "openstack_networking_router_v2" "router_1" {
   134  	name = "router"
   135  	admin_state_up = "true"
   136  	distributed = "false"
   137  }
   138  `
   139  
   140  var testAccNetworkingV2Router_update_external_gw_2 = fmt.Sprintf(`
   141  resource "openstack_networking_router_v2" "router_1" {
   142  	name = "router"
   143  	admin_state_up = "true"
   144  	distributed = "false"
   145  	external_gateway = "%s"
   146  }
   147  `, OS_EXTGW_ID)