github.com/subuk/terraform@v0.6.14-0.20160317140351-de1567c2e732/builtin/providers/datadog/resource_datadog_monitor_test.go (about)

     1  package datadog
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/zorkian/go-datadog-api"
    12  )
    13  
    14  func TestAccDatadogMonitor_Basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckDatadogMonitorDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCheckDatadogMonitorConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
    24  					resource.TestCheckResourceAttr(
    25  						"datadog_monitor.foo", "name", "name for monitor foo"),
    26  					resource.TestCheckResourceAttr(
    27  						"datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"),
    28  					resource.TestCheckResourceAttr(
    29  						"datadog_monitor.foo", "type", "metric alert"),
    30  					resource.TestCheckResourceAttr(
    31  						"datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"),
    32  					resource.TestCheckResourceAttr(
    33  						"datadog_monitor.foo", "notify_no_data", "false"),
    34  					resource.TestCheckResourceAttr(
    35  						"datadog_monitor.foo", "renotify_interval", "60"),
    36  					resource.TestCheckResourceAttr(
    37  						"datadog_monitor.foo", "thresholds.ok", "0"),
    38  					resource.TestCheckResourceAttr(
    39  						"datadog_monitor.foo", "thresholds.warning", "1"),
    40  					resource.TestCheckResourceAttr(
    41  						"datadog_monitor.foo", "thresholds.critical", "2"),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func TestAccDatadogMonitor_Updated(t *testing.T) {
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testAccCheckDatadogMonitorDestroy,
    53  		Steps: []resource.TestStep{
    54  			resource.TestStep{
    55  				Config: testAccCheckDatadogMonitorConfig,
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
    58  					resource.TestCheckResourceAttr(
    59  						"datadog_monitor.foo", "name", "name for monitor foo"),
    60  					resource.TestCheckResourceAttr(
    61  						"datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"),
    62  					resource.TestCheckResourceAttr(
    63  						"datadog_monitor.foo", "escalation_message", "the situation has escalated @pagerduty"),
    64  					resource.TestCheckResourceAttr(
    65  						"datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"),
    66  					resource.TestCheckResourceAttr(
    67  						"datadog_monitor.foo", "type", "metric alert"),
    68  					resource.TestCheckResourceAttr(
    69  						"datadog_monitor.foo", "notify_no_data", "false"),
    70  					resource.TestCheckResourceAttr(
    71  						"datadog_monitor.foo", "renotify_interval", "60"),
    72  					resource.TestCheckResourceAttr(
    73  						"datadog_monitor.foo", "thresholds.ok", "0"),
    74  					resource.TestCheckResourceAttr(
    75  						"datadog_monitor.foo", "thresholds.warning", "1"),
    76  					resource.TestCheckResourceAttr(
    77  						"datadog_monitor.foo", "thresholds.critical", "2"),
    78  					resource.TestCheckResourceAttr(
    79  						"datadog_monitor.foo", "notify_audit", "false"),
    80  					resource.TestCheckResourceAttr(
    81  						"datadog_monitor.foo", "timeout_h", "60"),
    82  					resource.TestCheckResourceAttr(
    83  						"datadog_monitor.foo", "include_tags", "true"),
    84  				),
    85  			},
    86  			resource.TestStep{
    87  				Config: testAccCheckDatadogMonitorConfigUpdated,
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
    90  					resource.TestCheckResourceAttr(
    91  						"datadog_monitor.foo", "name", "name for monitor bar"),
    92  					resource.TestCheckResourceAttr(
    93  						"datadog_monitor.foo", "message", "a different message Notify: @hipchat-channel"),
    94  					resource.TestCheckResourceAttr(
    95  						"datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"),
    96  					resource.TestCheckResourceAttr(
    97  						"datadog_monitor.foo", "escalation_message", "the situation has escalated! @pagerduty"),
    98  					resource.TestCheckResourceAttr(
    99  						"datadog_monitor.foo", "type", "metric alert"),
   100  					resource.TestCheckResourceAttr(
   101  						"datadog_monitor.foo", "notify_no_data", "true"),
   102  					resource.TestCheckResourceAttr(
   103  						"datadog_monitor.foo", "renotify_interval", "40"),
   104  					resource.TestCheckResourceAttr(
   105  						"datadog_monitor.foo", "thresholds.ok", "0"),
   106  					resource.TestCheckResourceAttr(
   107  						"datadog_monitor.foo", "thresholds.warning", "1"),
   108  					resource.TestCheckResourceAttr(
   109  						"datadog_monitor.foo", "thresholds.critical", "3"),
   110  					resource.TestCheckResourceAttr(
   111  						"datadog_monitor.foo", "notify_audit", "true"),
   112  					resource.TestCheckResourceAttr(
   113  						"datadog_monitor.foo", "timeout_h", "70"),
   114  					resource.TestCheckResourceAttr(
   115  						"datadog_monitor.foo", "include_tags", "false"),
   116  					resource.TestCheckResourceAttr(
   117  						"datadog_monitor.foo", "silenced.*", "0"),
   118  				),
   119  			},
   120  		},
   121  	})
   122  }
   123  
   124  func testAccCheckDatadogMonitorDestroy(s *terraform.State) error {
   125  	client := testAccProvider.Meta().(*datadog.Client)
   126  
   127  	if err := destroyHelper(s, client); err != nil {
   128  		return err
   129  	}
   130  	return nil
   131  }
   132  
   133  func testAccCheckDatadogMonitorExists(n string) resource.TestCheckFunc {
   134  	return func(s *terraform.State) error {
   135  		client := testAccProvider.Meta().(*datadog.Client)
   136  		if err := existsHelper(s, client); err != nil {
   137  			return err
   138  		}
   139  		return nil
   140  	}
   141  }
   142  
   143  const testAccCheckDatadogMonitorConfig = `
   144  resource "datadog_monitor" "foo" {
   145    name = "name for monitor foo"
   146    type = "metric alert"
   147    message = "some message Notify: @hipchat-channel"
   148    escalation_message = "the situation has escalated @pagerduty"
   149  
   150    query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"
   151  
   152    thresholds {
   153  	ok = 0
   154  	warning = 1
   155  	critical = 2
   156    }
   157  
   158    notify_no_data = false
   159    renotify_interval = 60
   160  
   161    notify_audit = false
   162    timeout_h = 60
   163    include_tags = true
   164  }
   165  `
   166  
   167  const testAccCheckDatadogMonitorConfigUpdated = `
   168  resource "datadog_monitor" "foo" {
   169    name = "name for monitor bar"
   170    type = "metric alert"
   171    message = "a different message Notify: @hipchat-channel"
   172    escalation_message = "the situation has escalated @pagerduty"
   173  
   174    query = "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"
   175  
   176    thresholds {
   177  	ok = 0
   178  	warning = 1
   179  	critical = 3
   180    }
   181  
   182    notify_no_data = true
   183    renotify_interval = 40
   184    escalation_message = "the situation has escalated! @pagerduty"
   185    notify_audit = true
   186    timeout_h = 70
   187    include_tags = false
   188    silenced {
   189  	"*" = 0
   190    }
   191  }
   192  `
   193  
   194  func destroyHelper(s *terraform.State, client *datadog.Client) error {
   195  	for _, r := range s.RootModule().Resources {
   196  		i, _ := strconv.Atoi(r.Primary.ID)
   197  		if _, err := client.GetMonitor(i); err != nil {
   198  			if strings.Contains(err.Error(), "404 Not Found") {
   199  				continue
   200  			}
   201  			return fmt.Errorf("Received an error retrieving monitor %s", err)
   202  		}
   203  		return fmt.Errorf("Monitor still exists")
   204  	}
   205  	return nil
   206  }
   207  
   208  func existsHelper(s *terraform.State, client *datadog.Client) error {
   209  	for _, r := range s.RootModule().Resources {
   210  		i, _ := strconv.Atoi(r.Primary.ID)
   211  		if _, err := client.GetMonitor(i); err != nil {
   212  			return fmt.Errorf("Received an error retrieving monitor %s", err)
   213  		}
   214  	}
   215  	return nil
   216  }