github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 testAccCheckDatadogMonitorDestroy(s *terraform.State) error {
   181  	client := testAccProvider.Meta().(*datadog.Client)
   182  
   183  	if err := destroyHelper(s, client); err != nil {
   184  		return err
   185  	}
   186  	return nil
   187  }
   188  
   189  func testAccCheckDatadogMonitorExists(n string) resource.TestCheckFunc {
   190  	return func(s *terraform.State) error {
   191  		client := testAccProvider.Meta().(*datadog.Client)
   192  		if err := existsHelper(s, client); err != nil {
   193  			return err
   194  		}
   195  		return nil
   196  	}
   197  }
   198  
   199  const testAccCheckDatadogMonitorConfig = `
   200  resource "datadog_monitor" "foo" {
   201    name = "name for monitor foo"
   202    type = "metric alert"
   203    message = "some message Notify: @hipchat-channel"
   204    escalation_message = "the situation has escalated @pagerduty"
   205  
   206    query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"
   207  
   208    thresholds {
   209  	warning = "1.0"
   210  	critical = "2.0"
   211    }
   212  
   213    notify_no_data = false
   214    renotify_interval = 60
   215  
   216    notify_audit = false
   217    timeout_h = 60
   218    include_tags = true
   219    require_full_window = true
   220    locked = false
   221    tags {
   222  	"foo" = "bar"
   223  	"bar" = "baz"
   224    }
   225  }
   226  `
   227  
   228  const testAccCheckDatadogMonitorConfigUpdated = `
   229  resource "datadog_monitor" "foo" {
   230    name = "name for monitor bar"
   231    type = "metric alert"
   232    message = "a different message Notify: @hipchat-channel"
   233    escalation_message = "the situation has escalated @pagerduty"
   234  
   235    query = "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"
   236  
   237    thresholds {
   238  	ok = "0.0"
   239  	warning = "1.0"
   240  	critical = "3.0"
   241    }
   242  
   243    notify_no_data = true
   244    no_data_timeframe = 20
   245    renotify_interval = 40
   246    escalation_message = "the situation has escalated! @pagerduty"
   247    notify_audit = true
   248    timeout_h = 70
   249    include_tags = false
   250    require_full_window = false
   251    locked = true
   252    silenced {
   253  	"*" = 0
   254    }
   255    tags {
   256  	"baz"  = "qux"
   257  	"quux" = "corge"
   258    }
   259  }
   260  `
   261  
   262  const testAccCheckDatadogMonitorConfigWhitespace = `
   263  resource "datadog_monitor" "foo" {
   264    name = "name for monitor foo"
   265    type = "metric alert"
   266    message = <<EOF
   267  some message Notify: @hipchat-channel
   268  EOF
   269    escalation_message = <<EOF
   270  the situation has escalated @pagerduty
   271  EOF
   272    query = <<EOF
   273  avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2
   274  EOF
   275    thresholds {
   276  	ok = "0.0"
   277  	warning = "1.0"
   278  	critical = "2.0"
   279    }
   280  
   281    notify_no_data = false
   282    renotify_interval = 60
   283  
   284    notify_audit = false
   285    timeout_h = 60
   286    include_tags = true
   287  }
   288  `
   289  
   290  func destroyHelper(s *terraform.State, client *datadog.Client) error {
   291  	for _, r := range s.RootModule().Resources {
   292  		i, _ := strconv.Atoi(r.Primary.ID)
   293  		if _, err := client.GetMonitor(i); err != nil {
   294  			if strings.Contains(err.Error(), "404 Not Found") {
   295  				continue
   296  			}
   297  			return fmt.Errorf("Received an error retrieving monitor %s", err)
   298  		}
   299  		return fmt.Errorf("Monitor still exists")
   300  	}
   301  	return nil
   302  }
   303  
   304  func existsHelper(s *terraform.State, client *datadog.Client) error {
   305  	for _, r := range s.RootModule().Resources {
   306  		i, _ := strconv.Atoi(r.Primary.ID)
   307  		if _, err := client.GetMonitor(i); err != nil {
   308  			return fmt.Errorf("Received an error retrieving monitor %s", err)
   309  		}
   310  	}
   311  	return nil
   312  }