github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_lb_monitor_v1_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/lbaas/monitors"
    11  )
    12  
    13  func TestAccLBV1Monitor_basic(t *testing.T) {
    14  	var monitor monitors.Monitor
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckLBV1MonitorDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccLBV1Monitor_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_1", &monitor),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccLBV1Monitor_update,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					resource.TestCheckResourceAttr("openstack_lb_monitor_v1.monitor_1", "delay", "20"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccLBV1Monitor_timeout(t *testing.T) {
    38  	var monitor monitors.Monitor
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckLBV1MonitorDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccLBV1Monitor_timeout,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_1", &monitor),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccCheckLBV1MonitorDestroy(s *terraform.State) error {
    56  	config := testAccProvider.Meta().(*Config)
    57  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    58  	if err != nil {
    59  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    60  	}
    61  
    62  	for _, rs := range s.RootModule().Resources {
    63  		if rs.Type != "openstack_lb_monitor_v1" {
    64  			continue
    65  		}
    66  
    67  		_, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
    68  		if err == nil {
    69  			return fmt.Errorf("LB monitor still exists")
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  func testAccCheckLBV1MonitorExists(n string, monitor *monitors.Monitor) resource.TestCheckFunc {
    77  	return func(s *terraform.State) error {
    78  		rs, ok := s.RootModule().Resources[n]
    79  		if !ok {
    80  			return fmt.Errorf("Not found: %s", n)
    81  		}
    82  
    83  		if rs.Primary.ID == "" {
    84  			return fmt.Errorf("No ID is set")
    85  		}
    86  
    87  		config := testAccProvider.Meta().(*Config)
    88  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    89  		if err != nil {
    90  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    91  		}
    92  
    93  		found, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
    94  		if err != nil {
    95  			return err
    96  		}
    97  
    98  		if found.ID != rs.Primary.ID {
    99  			return fmt.Errorf("Monitor not found")
   100  		}
   101  
   102  		*monitor = *found
   103  
   104  		return nil
   105  	}
   106  }
   107  
   108  const testAccLBV1Monitor_basic = `
   109  resource "openstack_lb_monitor_v1" "monitor_1" {
   110    type = "PING"
   111    delay = 30
   112    timeout = 5
   113    max_retries = 3
   114    admin_state_up = "true"
   115  }
   116  `
   117  
   118  const testAccLBV1Monitor_update = `
   119  resource "openstack_lb_monitor_v1" "monitor_1" {
   120    type = "PING"
   121    delay = 20
   122    timeout = 5
   123    max_retries = 3
   124    admin_state_up = "true"
   125  }
   126  `
   127  
   128  const testAccLBV1Monitor_timeout = `
   129  resource "openstack_lb_monitor_v1" "monitor_1" {
   130    type = "PING"
   131    delay = 30
   132    timeout = 5
   133    max_retries = 3
   134    admin_state_up = "true"
   135  
   136    timeouts {
   137      create = "5m"
   138      delete = "5m"
   139    }
   140  }
   141  `