github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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("openstack_lb_monitor_v2.monitor_1", "name", "tf_test_monitor_updated"),
    30  					resource.TestCheckResourceAttr("openstack_lb_monitor_v2.monitor_1", "delay", "30"),
    31  					resource.TestCheckResourceAttr("openstack_lb_monitor_v2.monitor_1", "timeout", "15"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckLBV2MonitorDestroy(s *terraform.State) error {
    39  	config := testAccProvider.Meta().(*Config)
    40  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    41  	if err != nil {
    42  		return fmt.Errorf("(testAccCheckLBV2MonitorDestroy) Error creating OpenStack networking client: %s", err)
    43  	}
    44  
    45  	for _, rs := range s.RootModule().Resources {
    46  		if rs.Type != "openstack_lb_monitor_v2" {
    47  			continue
    48  		}
    49  
    50  		_, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
    51  		if err == nil {
    52  			return fmt.Errorf("Monitor still exists: %s", rs.Primary.ID)
    53  		}
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func testAccCheckLBV2MonitorExists(t *testing.T, n string, monitor *monitors.Monitor) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		rs, ok := s.RootModule().Resources[n]
    62  		if !ok {
    63  			return fmt.Errorf("Not found: %s", n)
    64  		}
    65  
    66  		if rs.Primary.ID == "" {
    67  			return fmt.Errorf("No ID is set")
    68  		}
    69  
    70  		config := testAccProvider.Meta().(*Config)
    71  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    72  		if err != nil {
    73  			return fmt.Errorf("(testAccCheckLBV2MonitorExists) Error creating OpenStack networking client: %s", err)
    74  		}
    75  
    76  		found, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		if found.ID != rs.Primary.ID {
    82  			return fmt.Errorf("Monitor not found")
    83  		}
    84  
    85  		*monitor = *found
    86  
    87  		return nil
    88  	}
    89  }
    90  
    91  var TestAccLBV2MonitorConfig_basic = fmt.Sprintf(`
    92  	resource "openstack_networking_network_v2" "network_1" {
    93  		name = "tf_test_network"
    94  		admin_state_up = "true"
    95  	}
    96  
    97  	resource "openstack_networking_subnet_v2" "subnet_1" {
    98  		network_id = "${openstack_networking_network_v2.network_1.id}"
    99  		cidr = "192.168.199.0/24"
   100  		ip_version = 4
   101  		name = "tf_test_subnet"
   102  	}
   103  
   104  	resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   105  		vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   106  		name = "tf_test_loadbalancer_v2"
   107  	}
   108  
   109  	resource "openstack_lb_listener_v2" "listener_1" {
   110  		protocol = "HTTP"
   111  		protocol_port = 8080
   112  		loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   113  		name = "tf_test_listener"
   114  	}
   115  
   116  	resource "openstack_lb_pool_v2" "pool_1" {
   117  		protocol = "HTTP"
   118  		lb_method = "ROUND_ROBIN"
   119  		listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   120  		name = "tf_test_pool"
   121  	}
   122  
   123  	resource "openstack_lb_monitor_v2" "monitor_1" {
   124  		pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   125  		type = "PING"
   126  		delay = 20
   127  		timeout = 10
   128  		max_retries = 5
   129  		name = "tf_test_monitor"
   130  	}`)
   131  
   132  var TestAccLBV2MonitorConfig_update = fmt.Sprintf(`
   133  	resource "openstack_networking_network_v2" "network_1" {
   134  		name = "tf_test_network"
   135  		admin_state_up = "true"
   136  	}
   137  
   138  	resource "openstack_networking_subnet_v2" "subnet_1" {
   139  		network_id = "${openstack_networking_network_v2.network_1.id}"
   140  		cidr = "192.168.199.0/24"
   141  		ip_version = 4
   142  		name = "tf_test_subnet"
   143  	}
   144  
   145  	resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   146  		vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   147  		name = "tf_test_loadbalancer_v2"
   148  	}
   149  
   150  	resource "openstack_lb_listener_v2" "listener_1" {
   151  		protocol = "HTTP"
   152  		protocol_port = 8080
   153  		loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   154  		name = "tf_test_listener"
   155  	}
   156  
   157  	resource "openstack_lb_pool_v2" "pool_1" {
   158  		protocol = "HTTP"
   159  		lb_method = "ROUND_ROBIN"
   160  		listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   161  		name = "tf_test_pool"
   162  	}
   163  
   164  	resource "openstack_lb_monitor_v2" "monitor_1" {
   165  		pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   166  		type = "PING"
   167  		delay = 30
   168  		timeout = 15
   169  		max_retries = 10
   170  		name = "tf_test_monitor_updated"
   171  		admin_state_up = "true"
   172  	}`)