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