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