github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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.warning", "1.0"),
    38  					resource.TestCheckResourceAttr(
    39  						"datadog_monitor.foo", "thresholds.critical", "2.0"),
    40  					resource.TestCheckResourceAttr(
    41  						"datadog_monitor.foo", "require_full_window", "true"),
    42  					resource.TestCheckResourceAttr(
    43  						"datadog_monitor.foo", "locked", "false"),
    44  					resource.TestCheckResourceAttr(
    45  						"datadog_monitor.foo", "tags.foo", "bar"),
    46  					resource.TestCheckResourceAttr(
    47  						"datadog_monitor.foo", "tags.bar", "baz"),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func TestAccDatadogMonitor_Updated(t *testing.T) {
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:     func() { testAccPreCheck(t) },
    57  		Providers:    testAccProviders,
    58  		CheckDestroy: testAccCheckDatadogMonitorDestroy,
    59  		Steps: []resource.TestStep{
    60  			resource.TestStep{
    61  				Config: testAccCheckDatadogMonitorConfig,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
    64  					resource.TestCheckResourceAttr(
    65  						"datadog_monitor.foo", "name", "name for monitor foo"),
    66  					resource.TestCheckResourceAttr(
    67  						"datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"),
    68  					resource.TestCheckResourceAttr(
    69  						"datadog_monitor.foo", "escalation_message", "the situation has escalated @pagerduty"),
    70  					resource.TestCheckResourceAttr(
    71  						"datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"),
    72  					resource.TestCheckResourceAttr(
    73  						"datadog_monitor.foo", "type", "metric alert"),
    74  					resource.TestCheckResourceAttr(
    75  						"datadog_monitor.foo", "notify_no_data", "false"),
    76  					resource.TestCheckResourceAttr(
    77  						"datadog_monitor.foo", "renotify_interval", "60"),
    78  					resource.TestCheckResourceAttr(
    79  						"datadog_monitor.foo", "thresholds.warning", "1.0"),
    80  					resource.TestCheckResourceAttr(
    81  						"datadog_monitor.foo", "thresholds.critical", "2.0"),
    82  					resource.TestCheckResourceAttr(
    83  						"datadog_monitor.foo", "notify_audit", "false"),
    84  					resource.TestCheckResourceAttr(
    85  						"datadog_monitor.foo", "timeout_h", "60"),
    86  					resource.TestCheckResourceAttr(
    87  						"datadog_monitor.foo", "include_tags", "true"),
    88  					resource.TestCheckResourceAttr(
    89  						"datadog_monitor.foo", "require_full_window", "true"),
    90  					resource.TestCheckResourceAttr(
    91  						"datadog_monitor.foo", "locked", "false"),
    92  					resource.TestCheckResourceAttr(
    93  						"datadog_monitor.foo", "tags.foo", "bar"),
    94  					resource.TestCheckResourceAttr(
    95  						"datadog_monitor.foo", "tags.bar", "baz"),
    96  				),
    97  			},
    98  			resource.TestStep{
    99  				Config: testAccCheckDatadogMonitorConfigUpdated,
   100  				Check: resource.ComposeTestCheckFunc(
   101  					testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
   102  					resource.TestCheckResourceAttr(
   103  						"datadog_monitor.foo", "name", "name for monitor bar"),
   104  					resource.TestCheckResourceAttr(
   105  						"datadog_monitor.foo", "message", "a different message Notify: @hipchat-channel"),
   106  					resource.TestCheckResourceAttr(
   107  						"datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"),
   108  					resource.TestCheckResourceAttr(
   109  						"datadog_monitor.foo", "escalation_message", "the situation has escalated! @pagerduty"),
   110  					resource.TestCheckResourceAttr(
   111  						"datadog_monitor.foo", "type", "metric alert"),
   112  					resource.TestCheckResourceAttr(
   113  						"datadog_monitor.foo", "notify_no_data", "true"),
   114  					resource.TestCheckResourceAttr(
   115  						"datadog_monitor.foo", "no_data_timeframe", "20"),
   116  					resource.TestCheckResourceAttr(
   117  						"datadog_monitor.foo", "renotify_interval", "40"),
   118  					resource.TestCheckResourceAttr(
   119  						"datadog_monitor.foo", "thresholds.ok", "0.0"),
   120  					resource.TestCheckResourceAttr(
   121  						"datadog_monitor.foo", "thresholds.warning", "1.0"),
   122  					resource.TestCheckResourceAttr(
   123  						"datadog_monitor.foo", "thresholds.critical", "3.0"),
   124  					resource.TestCheckResourceAttr(
   125  						"datadog_monitor.foo", "notify_audit", "true"),
   126  					resource.TestCheckResourceAttr(
   127  						"datadog_monitor.foo", "timeout_h", "70"),
   128  					resource.TestCheckResourceAttr(
   129  						"datadog_monitor.foo", "include_tags", "false"),
   130  					resource.TestCheckResourceAttr(
   131  						"datadog_monitor.foo", "silenced.*", "0"),
   132  					resource.TestCheckResourceAttr(
   133  						"datadog_monitor.foo", "require_full_window", "false"),
   134  					resource.TestCheckResourceAttr(
   135  						"datadog_monitor.foo", "locked", "true"),
   136  					resource.TestCheckResourceAttr(
   137  						"datadog_monitor.foo", "tags.baz", "qux"),
   138  					resource.TestCheckResourceAttr(
   139  						"datadog_monitor.foo", "tags.quux", "corge"),
   140  				),
   141  			},
   142  		},
   143  	})
   144  }
   145  
   146  func TestAccDatadogMonitor_TrimWhitespace(t *testing.T) {
   147  	resource.Test(t, resource.TestCase{
   148  		PreCheck:     func() { testAccPreCheck(t) },
   149  		Providers:    testAccProviders,
   150  		CheckDestroy: testAccCheckDatadogMonitorDestroy,
   151  		Steps: []resource.TestStep{
   152  			resource.TestStep{
   153  				Config: testAccCheckDatadogMonitorConfigWhitespace,
   154  				Check: resource.ComposeTestCheckFunc(
   155  					testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
   156  					resource.TestCheckResourceAttr(
   157  						"datadog_monitor.foo", "name", "name for monitor foo"),
   158  					resource.TestCheckResourceAttr(
   159  						"datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"),
   160  					resource.TestCheckResourceAttr(
   161  						"datadog_monitor.foo", "type", "metric alert"),
   162  					resource.TestCheckResourceAttr(
   163  						"datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"),
   164  					resource.TestCheckResourceAttr(
   165  						"datadog_monitor.foo", "notify_no_data", "false"),
   166  					resource.TestCheckResourceAttr(
   167  						"datadog_monitor.foo", "renotify_interval", "60"),
   168  					resource.TestCheckResourceAttr(
   169  						"datadog_monitor.foo", "thresholds.ok", "0.0"),
   170  					resource.TestCheckResourceAttr(
   171  						"datadog_monitor.foo", "thresholds.warning", "1.0"),
   172  					resource.TestCheckResourceAttr(
   173  						"datadog_monitor.foo", "thresholds.critical", "2.0"),
   174  				),
   175  			},
   176  		},
   177  	})
   178  }
   179  
   180  func TestAccDatadogMonitor_Basic_float_int(t *testing.T) {
   181  	resource.Test(t, resource.TestCase{
   182  		PreCheck:     func() { testAccPreCheck(t) },
   183  		Providers:    testAccProviders,
   184  		CheckDestroy: testAccCheckDatadogMonitorDestroy,
   185  		Steps: []resource.TestStep{
   186  			resource.TestStep{
   187  				Config: testAccCheckDatadogMonitorConfig_ints,
   188  				Check: resource.ComposeTestCheckFunc(
   189  					testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
   190  					resource.TestCheckResourceAttr(
   191  						"datadog_monitor.foo", "thresholds.warning", "1"),
   192  					resource.TestCheckResourceAttr(
   193  						"datadog_monitor.foo", "thresholds.critical", "2"),
   194  				),
   195  			},
   196  
   197  			resource.TestStep{
   198  				Config: testAccCheckDatadogMonitorConfig_ints_mixed,
   199  				Check: resource.ComposeTestCheckFunc(
   200  					testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
   201  					resource.TestCheckResourceAttr(
   202  						"datadog_monitor.foo", "thresholds.warning", "1.0"),
   203  					resource.TestCheckResourceAttr(
   204  						"datadog_monitor.foo", "thresholds.critical", "3.0"),
   205  				),
   206  			},
   207  		},
   208  	})
   209  }
   210  
   211  func testAccCheckDatadogMonitorDestroy(s *terraform.State) error {
   212  	client := testAccProvider.Meta().(*datadog.Client)
   213  
   214  	if err := destroyHelper(s, client); err != nil {
   215  		return err
   216  	}
   217  	return nil
   218  }
   219  
   220  func testAccCheckDatadogMonitorExists(n string) resource.TestCheckFunc {
   221  	return func(s *terraform.State) error {
   222  		client := testAccProvider.Meta().(*datadog.Client)
   223  		if err := existsHelper(s, client); err != nil {
   224  			return err
   225  		}
   226  		return nil
   227  	}
   228  }
   229  
   230  const testAccCheckDatadogMonitorConfig = `
   231  resource "datadog_monitor" "foo" {
   232    name = "name for monitor foo"
   233    type = "metric alert"
   234    message = "some message Notify: @hipchat-channel"
   235    escalation_message = "the situation has escalated @pagerduty"
   236  
   237    query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"
   238  
   239    thresholds {
   240  	warning = "1.0"
   241  	critical = "2.0"
   242    }
   243  
   244    notify_no_data = false
   245    renotify_interval = 60
   246  
   247    notify_audit = false
   248    timeout_h = 60
   249    include_tags = true
   250    require_full_window = true
   251    locked = false
   252    tags {
   253  	"foo" = "bar"
   254  	"bar" = "baz"
   255    }
   256  }
   257  `
   258  
   259  const testAccCheckDatadogMonitorConfig_ints = `
   260  resource "datadog_monitor" "foo" {
   261    name               = "name for monitor foo"
   262    type               = "metric alert"
   263    message            = "some message Notify: @hipchat-channel"
   264    escalation_message = "the situation has escalated @pagerduty"
   265  
   266    query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"
   267  
   268    thresholds {
   269      warning  = 1
   270      critical = 2
   271    }
   272  
   273    notify_no_data    = false
   274    renotify_interval = 60
   275  
   276    notify_audit        = false
   277    timeout_h           = 60
   278    include_tags        = true
   279    require_full_window = true
   280    locked              = false
   281  
   282    tags {
   283      "foo" = "bar"
   284      "bar" = "baz"
   285    }
   286  }
   287  `
   288  
   289  const testAccCheckDatadogMonitorConfig_ints_mixed = `
   290  resource "datadog_monitor" "foo" {
   291    name               = "name for monitor foo"
   292    type               = "metric alert"
   293    message            = "some message Notify: @hipchat-channel"
   294    escalation_message = "the situation has escalated @pagerduty"
   295  
   296    query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 3"
   297  
   298    thresholds {
   299      warning  = 1
   300      critical = 3.0 
   301    }
   302  
   303    notify_no_data    = false
   304    renotify_interval = 60
   305  
   306    notify_audit        = false
   307    timeout_h           = 60
   308    include_tags        = true
   309    require_full_window = true
   310    locked              = false
   311  
   312    tags {
   313      "foo" = "bar"
   314      "bar" = "baz"
   315    }
   316  }
   317  `
   318  
   319  const testAccCheckDatadogMonitorConfigUpdated = `
   320  resource "datadog_monitor" "foo" {
   321    name = "name for monitor bar"
   322    type = "metric alert"
   323    message = "a different message Notify: @hipchat-channel"
   324    escalation_message = "the situation has escalated @pagerduty"
   325  
   326    query = "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"
   327  
   328    thresholds {
   329  	ok = "0.0"
   330  	warning = "1.0"
   331  	critical = "3.0"
   332    }
   333  
   334    notify_no_data = true
   335    no_data_timeframe = 20
   336    renotify_interval = 40
   337    escalation_message = "the situation has escalated! @pagerduty"
   338    notify_audit = true
   339    timeout_h = 70
   340    include_tags = false
   341    require_full_window = false
   342    locked = true
   343    silenced {
   344  	"*" = 0
   345    }
   346    tags {
   347  	"baz"  = "qux"
   348  	"quux" = "corge"
   349    }
   350  }
   351  `
   352  
   353  const testAccCheckDatadogMonitorConfigWhitespace = `
   354  resource "datadog_monitor" "foo" {
   355    name = "name for monitor foo"
   356    type = "metric alert"
   357    message = <<EOF
   358  some message Notify: @hipchat-channel
   359  EOF
   360    escalation_message = <<EOF
   361  the situation has escalated @pagerduty
   362  EOF
   363    query = <<EOF
   364  avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2
   365  EOF
   366    thresholds {
   367  	ok = "0.0"
   368  	warning = "1.0"
   369  	critical = "2.0"
   370    }
   371  
   372    notify_no_data = false
   373    renotify_interval = 60
   374  
   375    notify_audit = false
   376    timeout_h = 60
   377    include_tags = true
   378  }
   379  `
   380  
   381  func destroyHelper(s *terraform.State, client *datadog.Client) error {
   382  	for _, r := range s.RootModule().Resources {
   383  		i, _ := strconv.Atoi(r.Primary.ID)
   384  		if _, err := client.GetMonitor(i); err != nil {
   385  			if strings.Contains(err.Error(), "404 Not Found") {
   386  				continue
   387  			}
   388  			return fmt.Errorf("Received an error retrieving monitor %s", err)
   389  		}
   390  		return fmt.Errorf("Monitor still exists")
   391  	}
   392  	return nil
   393  }
   394  
   395  func existsHelper(s *terraform.State, client *datadog.Client) error {
   396  	for _, r := range s.RootModule().Resources {
   397  		i, _ := strconv.Atoi(r.Primary.ID)
   398  		if _, err := client.GetMonitor(i); err != nil {
   399  			return fmt.Errorf("Received an error retrieving monitor %s", err)
   400  		}
   401  	}
   402  	return nil
   403  }