github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 TestAccNetworkingV2Router_timeout(t *testing.T) {
    64  	var router routers.Router
    65  
    66  	resource.Test(t, resource.TestCase{
    67  		PreCheck:     func() { testAccPreCheck(t) },
    68  		Providers:    testAccProviders,
    69  		CheckDestroy: testAccCheckNetworkingV2RouterDestroy,
    70  		Steps: []resource.TestStep{
    71  			resource.TestStep{
    72  				Config: testAccNetworkingV2Router_timeout,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckNetworkingV2RouterExists("openstack_networking_router_v2.router_1", &router),
    75  				),
    76  			},
    77  		},
    78  	})
    79  }
    80  
    81  func testAccCheckNetworkingV2RouterDestroy(s *terraform.State) error {
    82  	config := testAccProvider.Meta().(*Config)
    83  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    84  	if err != nil {
    85  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    86  	}
    87  
    88  	for _, rs := range s.RootModule().Resources {
    89  		if rs.Type != "openstack_networking_router_v2" {
    90  			continue
    91  		}
    92  
    93  		_, err := routers.Get(networkingClient, rs.Primary.ID).Extract()
    94  		if err == nil {
    95  			return fmt.Errorf("Router still exists")
    96  		}
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  func testAccCheckNetworkingV2RouterExists(n string, router *routers.Router) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  		rs, ok := s.RootModule().Resources[n]
   105  		if !ok {
   106  			return fmt.Errorf("Not found: %s", n)
   107  		}
   108  
   109  		if rs.Primary.ID == "" {
   110  			return fmt.Errorf("No ID is set")
   111  		}
   112  
   113  		config := testAccProvider.Meta().(*Config)
   114  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   115  		if err != nil {
   116  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
   117  		}
   118  
   119  		found, err := routers.Get(networkingClient, rs.Primary.ID).Extract()
   120  		if err != nil {
   121  			return err
   122  		}
   123  
   124  		if found.ID != rs.Primary.ID {
   125  			return fmt.Errorf("Router not found")
   126  		}
   127  
   128  		*router = *found
   129  
   130  		return nil
   131  	}
   132  }
   133  
   134  const testAccNetworkingV2Router_basic = `
   135  resource "openstack_networking_router_v2" "router_1" {
   136  	name = "router_1"
   137  	admin_state_up = "true"
   138  	distributed = "false"
   139  }
   140  `
   141  
   142  const testAccNetworkingV2Router_update = `
   143  resource "openstack_networking_router_v2" "router_1" {
   144  	name = "router_2"
   145  	admin_state_up = "true"
   146  	distributed = "false"
   147  }
   148  `
   149  
   150  const testAccNetworkingV2Router_update_external_gw_1 = `
   151  resource "openstack_networking_router_v2" "router_1" {
   152  	name = "router"
   153  	admin_state_up = "true"
   154  	distributed = "false"
   155  }
   156  `
   157  
   158  var testAccNetworkingV2Router_update_external_gw_2 = fmt.Sprintf(`
   159  resource "openstack_networking_router_v2" "router_1" {
   160  	name = "router"
   161  	admin_state_up = "true"
   162  	distributed = "false"
   163  	external_gateway = "%s"
   164  }
   165  `, OS_EXTGW_ID)
   166  
   167  const testAccNetworkingV2Router_timeout = `
   168  resource "openstack_networking_router_v2" "router_1" {
   169  	name = "router_1"
   170  	admin_state_up = "true"
   171  	distributed = "false"
   172  
   173    timeouts {
   174      create = "5m"
   175      delete = "5m"
   176    }
   177  }
   178  `