github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_lb_monitor_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/monitors"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccLBV2Monitor_basic(t *testing.T) {
    13  	var monitor monitors.Monitor
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckLBV2MonitorDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: TestAccLBV2MonitorConfig_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckLBV2MonitorExists(t, "openstack_lb_monitor_v2.monitor_1", &monitor),
    24  				),
    25  			},
    26  			resource.TestStep{
    27  				Config: TestAccLBV2MonitorConfig_update,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					resource.TestCheckResourceAttr(
    30  						"openstack_lb_monitor_v2.monitor_1", "name", "monitor_1_updated"),
    31  					resource.TestCheckResourceAttr("openstack_lb_monitor_v2.monitor_1", "delay", "30"),
    32  					resource.TestCheckResourceAttr("openstack_lb_monitor_v2.monitor_1", "timeout", "15"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccLBV2Monitor_timeout(t *testing.T) {
    40  	var monitor monitors.Monitor
    41  
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testAccCheckLBV2MonitorDestroy,
    46  		Steps: []resource.TestStep{
    47  			resource.TestStep{
    48  				Config: TestAccLBV2MonitorConfig_timeout,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckLBV2MonitorExists(t, "openstack_lb_monitor_v2.monitor_1", &monitor),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func testAccCheckLBV2MonitorDestroy(s *terraform.State) error {
    58  	config := testAccProvider.Meta().(*Config)
    59  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    60  	if err != nil {
    61  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    62  	}
    63  
    64  	for _, rs := range s.RootModule().Resources {
    65  		if rs.Type != "openstack_lb_monitor_v2" {
    66  			continue
    67  		}
    68  
    69  		_, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
    70  		if err == nil {
    71  			return fmt.Errorf("Monitor still exists: %s", rs.Primary.ID)
    72  		}
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func testAccCheckLBV2MonitorExists(t *testing.T, n string, monitor *monitors.Monitor) resource.TestCheckFunc {
    79  	return func(s *terraform.State) error {
    80  		rs, ok := s.RootModule().Resources[n]
    81  		if !ok {
    82  			return fmt.Errorf("Not found: %s", n)
    83  		}
    84  
    85  		if rs.Primary.ID == "" {
    86  			return fmt.Errorf("No ID is set")
    87  		}
    88  
    89  		config := testAccProvider.Meta().(*Config)
    90  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    91  		if err != nil {
    92  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    93  		}
    94  
    95  		found, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
    96  		if err != nil {
    97  			return err
    98  		}
    99  
   100  		if found.ID != rs.Primary.ID {
   101  			return fmt.Errorf("Monitor not found")
   102  		}
   103  
   104  		*monitor = *found
   105  
   106  		return nil
   107  	}
   108  }
   109  
   110  const TestAccLBV2MonitorConfig_basic = `
   111  resource "openstack_networking_network_v2" "network_1" {
   112    name = "network_1"
   113    admin_state_up = "true"
   114  }
   115  
   116  resource "openstack_networking_subnet_v2" "subnet_1" {
   117    name = "subnet_1"
   118    cidr = "192.168.199.0/24"
   119    ip_version = 4
   120    network_id = "${openstack_networking_network_v2.network_1.id}"
   121  }
   122  
   123  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   124    name = "loadbalancer_1"
   125    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   126  }
   127  
   128  resource "openstack_lb_listener_v2" "listener_1" {
   129    name = "listener_1"
   130    protocol = "HTTP"
   131    protocol_port = 8080
   132    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   133  }
   134  
   135  resource "openstack_lb_pool_v2" "pool_1" {
   136    name = "pool_1"
   137    protocol = "HTTP"
   138    lb_method = "ROUND_ROBIN"
   139    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   140  }
   141  
   142  resource "openstack_lb_monitor_v2" "monitor_1" {
   143    name = "monitor_1"
   144    type = "PING"
   145    delay = 20
   146    timeout = 10
   147    max_retries = 5
   148    pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   149  }
   150  `
   151  
   152  const TestAccLBV2MonitorConfig_update = `
   153  resource "openstack_networking_network_v2" "network_1" {
   154    name = "network_1"
   155    admin_state_up = "true"
   156  }
   157  
   158  resource "openstack_networking_subnet_v2" "subnet_1" {
   159    name = "subnet_1"
   160    cidr = "192.168.199.0/24"
   161    ip_version = 4
   162    network_id = "${openstack_networking_network_v2.network_1.id}"
   163  }
   164  
   165  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   166    name = "loadbalancer_1"
   167    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   168  }
   169  
   170  resource "openstack_lb_listener_v2" "listener_1" {
   171    name = "listener_1"
   172    protocol = "HTTP"
   173    protocol_port = 8080
   174    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   175  }
   176  
   177  resource "openstack_lb_pool_v2" "pool_1" {
   178    name = "pool_1"
   179    protocol = "HTTP"
   180    lb_method = "ROUND_ROBIN"
   181    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   182  }
   183  
   184  resource "openstack_lb_monitor_v2" "monitor_1" {
   185    name = "monitor_1_updated"
   186    type = "PING"
   187    delay = 30
   188    timeout = 15
   189    max_retries = 10
   190    admin_state_up = "true"
   191    pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   192  }
   193  `
   194  
   195  const TestAccLBV2MonitorConfig_timeout = `
   196  resource "openstack_networking_network_v2" "network_1" {
   197    name = "network_1"
   198    admin_state_up = "true"
   199  }
   200  
   201  resource "openstack_networking_subnet_v2" "subnet_1" {
   202    name = "subnet_1"
   203    cidr = "192.168.199.0/24"
   204    ip_version = 4
   205    network_id = "${openstack_networking_network_v2.network_1.id}"
   206  }
   207  
   208  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   209    name = "loadbalancer_1"
   210    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   211  }
   212  
   213  resource "openstack_lb_listener_v2" "listener_1" {
   214    name = "listener_1"
   215    protocol = "HTTP"
   216    protocol_port = 8080
   217    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   218  }
   219  
   220  resource "openstack_lb_pool_v2" "pool_1" {
   221    name = "pool_1"
   222    protocol = "HTTP"
   223    lb_method = "ROUND_ROBIN"
   224    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   225  }
   226  
   227  resource "openstack_lb_monitor_v2" "monitor_1" {
   228    name = "monitor_1"
   229    type = "PING"
   230    delay = 20
   231    timeout = 10
   232    max_retries = 5
   233    pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   234  
   235    timeouts {
   236      create = "5m"
   237      delete = "5m"
   238    }
   239  }
   240  `