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