github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/rackspace/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(t, "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 testAccCheckLBV1MonitorDestroy(s *terraform.State) error {
    38  	config := testAccProvider.Meta().(*Config)
    39  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    40  	if err != nil {
    41  		return fmt.Errorf("(testAccCheckLBV1MonitorDestroy) Error creating OpenStack networking client: %s", err)
    42  	}
    43  
    44  	for _, rs := range s.RootModule().Resources {
    45  		if rs.Type != "openstack_lb_monitor_v1" {
    46  			continue
    47  		}
    48  
    49  		_, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
    50  		if err == nil {
    51  			return fmt.Errorf("LB monitor still exists")
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func testAccCheckLBV1MonitorExists(t *testing.T, n string, monitor *monitors.Monitor) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources[n]
    61  		if !ok {
    62  			return fmt.Errorf("Not found: %s", n)
    63  		}
    64  
    65  		if rs.Primary.ID == "" {
    66  			return fmt.Errorf("No ID is set")
    67  		}
    68  
    69  		config := testAccProvider.Meta().(*Config)
    70  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    71  		if err != nil {
    72  			return fmt.Errorf("(testAccCheckLBV1MonitorExists) Error creating OpenStack networking client: %s", err)
    73  		}
    74  
    75  		found, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		if found.ID != rs.Primary.ID {
    81  			return fmt.Errorf("Monitor not found")
    82  		}
    83  
    84  		*monitor = *found
    85  
    86  		return nil
    87  	}
    88  }
    89  
    90  var testAccLBV1Monitor_basic = fmt.Sprintf(`
    91    resource "openstack_lb_monitor_v1" "monitor_1" {
    92      region = "%s"
    93      type = "PING"
    94      delay = 30
    95      timeout = 5
    96      max_retries = 3
    97      admin_state_up = "true"
    98    }`,
    99  	OS_REGION_NAME)
   100  
   101  var testAccLBV1Monitor_update = fmt.Sprintf(`
   102    resource "openstack_lb_monitor_v1" "monitor_1" {
   103      region = "%s"
   104      type = "PING"
   105      delay = 20
   106      timeout = 5
   107      max_retries = 3
   108      admin_state_up = "true"
   109    }`,
   110  	OS_REGION_NAME)