github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/datadog/resource_datadog_downtime_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 TestAccDatadogDowntime_Basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckDatadogDowntimeDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCheckDatadogDowntimeConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
    24  					resource.TestCheckResourceAttr(
    25  						"datadog_downtime.foo", "scope.0", "*"),
    26  					resource.TestCheckResourceAttr(
    27  						"datadog_downtime.foo", "start", "1735707600"),
    28  					resource.TestCheckResourceAttr(
    29  						"datadog_downtime.foo", "end", "1735765200"),
    30  					resource.TestCheckResourceAttr(
    31  						"datadog_downtime.foo", "recurrence.0.type", "days"),
    32  					resource.TestCheckResourceAttr(
    33  						"datadog_downtime.foo", "recurrence.0.period", "1"),
    34  					resource.TestCheckResourceAttr(
    35  						"datadog_downtime.foo", "message", "Example Datadog downtime message."),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccDatadogDowntime_BasicMultiScope(t *testing.T) {
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckDatadogDowntimeDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: testAccCheckDatadogDowntimeConfigMultiScope,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
    52  					resource.TestCheckResourceAttr(
    53  						"datadog_downtime.foo", "scope.0", "host:A"),
    54  					resource.TestCheckResourceAttr(
    55  						"datadog_downtime.foo", "scope.1", "host:B"),
    56  					resource.TestCheckResourceAttr(
    57  						"datadog_downtime.foo", "start", "1735707600"),
    58  					resource.TestCheckResourceAttr(
    59  						"datadog_downtime.foo", "end", "1735765200"),
    60  					resource.TestCheckResourceAttr(
    61  						"datadog_downtime.foo", "recurrence.0.type", "days"),
    62  					resource.TestCheckResourceAttr(
    63  						"datadog_downtime.foo", "recurrence.0.period", "1"),
    64  					resource.TestCheckResourceAttr(
    65  						"datadog_downtime.foo", "message", "Example Datadog downtime message."),
    66  				),
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  func TestAccDatadogDowntime_BasicNoRecurrence(t *testing.T) {
    73  	resource.Test(t, resource.TestCase{
    74  		PreCheck:     func() { testAccPreCheck(t) },
    75  		Providers:    testAccProviders,
    76  		CheckDestroy: testAccCheckDatadogDowntimeDestroy,
    77  		Steps: []resource.TestStep{
    78  			resource.TestStep{
    79  				Config: testAccCheckDatadogDowntimeConfigNoRecurrence,
    80  				Check: resource.ComposeTestCheckFunc(
    81  					testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
    82  					resource.TestCheckResourceAttr(
    83  						"datadog_downtime.foo", "scope.0", "host:NoRecurrence"),
    84  					resource.TestCheckResourceAttr(
    85  						"datadog_downtime.foo", "start", "1735707600"),
    86  					resource.TestCheckResourceAttr(
    87  						"datadog_downtime.foo", "end", "1735765200"),
    88  					resource.TestCheckResourceAttr(
    89  						"datadog_downtime.foo", "message", "Example Datadog downtime message."),
    90  				),
    91  			},
    92  		},
    93  	})
    94  }
    95  
    96  func TestAccDatadogDowntime_BasicUntilDateRecurrence(t *testing.T) {
    97  	resource.Test(t, resource.TestCase{
    98  		PreCheck:     func() { testAccPreCheck(t) },
    99  		Providers:    testAccProviders,
   100  		CheckDestroy: testAccCheckDatadogDowntimeDestroy,
   101  		Steps: []resource.TestStep{
   102  			resource.TestStep{
   103  				Config: testAccCheckDatadogDowntimeConfigUntilDateRecurrence,
   104  				Check: resource.ComposeTestCheckFunc(
   105  					testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
   106  					resource.TestCheckResourceAttr(
   107  						"datadog_downtime.foo", "scope.0", "host:UntilDateRecurrence"),
   108  					resource.TestCheckResourceAttr(
   109  						"datadog_downtime.foo", "start", "1735707600"),
   110  					resource.TestCheckResourceAttr(
   111  						"datadog_downtime.foo", "end", "1735765200"),
   112  					resource.TestCheckResourceAttr(
   113  						"datadog_downtime.foo", "recurrence.0.type", "days"),
   114  					resource.TestCheckResourceAttr(
   115  						"datadog_downtime.foo", "recurrence.0.period", "1"),
   116  					resource.TestCheckResourceAttr(
   117  						"datadog_downtime.foo", "recurrence.0.until_date", "1736226000"),
   118  					resource.TestCheckResourceAttr(
   119  						"datadog_downtime.foo", "message", "Example Datadog downtime message."),
   120  				),
   121  			},
   122  		},
   123  	})
   124  }
   125  
   126  func TestAccDatadogDowntime_BasicUntilOccurrencesRecurrence(t *testing.T) {
   127  	resource.Test(t, resource.TestCase{
   128  		PreCheck:     func() { testAccPreCheck(t) },
   129  		Providers:    testAccProviders,
   130  		CheckDestroy: testAccCheckDatadogDowntimeDestroy,
   131  		Steps: []resource.TestStep{
   132  			resource.TestStep{
   133  				Config: testAccCheckDatadogDowntimeConfigUntilOccurrencesRecurrence,
   134  				Check: resource.ComposeTestCheckFunc(
   135  					testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
   136  					resource.TestCheckResourceAttr(
   137  						"datadog_downtime.foo", "scope.0", "host:UntilOccurrencesRecurrence"),
   138  					resource.TestCheckResourceAttr(
   139  						"datadog_downtime.foo", "start", "1735707600"),
   140  					resource.TestCheckResourceAttr(
   141  						"datadog_downtime.foo", "end", "1735765200"),
   142  					resource.TestCheckResourceAttr(
   143  						"datadog_downtime.foo", "recurrence.0.type", "days"),
   144  					resource.TestCheckResourceAttr(
   145  						"datadog_downtime.foo", "recurrence.0.period", "1"),
   146  					resource.TestCheckResourceAttr(
   147  						"datadog_downtime.foo", "recurrence.0.until_occurrences", "5"),
   148  					resource.TestCheckResourceAttr(
   149  						"datadog_downtime.foo", "message", "Example Datadog downtime message."),
   150  				),
   151  			},
   152  		},
   153  	})
   154  }
   155  
   156  func TestAccDatadogDowntime_WeekDayRecurring(t *testing.T) {
   157  	resource.Test(t, resource.TestCase{
   158  		PreCheck:     func() { testAccPreCheck(t) },
   159  		Providers:    testAccProviders,
   160  		CheckDestroy: testAccCheckDatadogDowntimeDestroy,
   161  		Steps: []resource.TestStep{
   162  			resource.TestStep{
   163  				Config: testAccCheckDatadogDowntimeConfigWeekDaysRecurrence,
   164  				Check: resource.ComposeTestCheckFunc(
   165  					testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
   166  					resource.TestCheckResourceAttr(
   167  						"datadog_downtime.foo", "scope.0", "WeekDaysRecurrence"),
   168  					resource.TestCheckResourceAttr(
   169  						"datadog_downtime.foo", "start", "1735646400"),
   170  					resource.TestCheckResourceAttr(
   171  						"datadog_downtime.foo", "end", "1735732799"),
   172  					resource.TestCheckResourceAttr(
   173  						"datadog_downtime.foo", "recurrence.0.type", "weeks"),
   174  					resource.TestCheckResourceAttr(
   175  						"datadog_downtime.foo", "recurrence.0.period", "1"),
   176  					resource.TestCheckResourceAttr(
   177  						"datadog_downtime.foo", "recurrence.0.week_days.0", "Sat"),
   178  					resource.TestCheckResourceAttr(
   179  						"datadog_downtime.foo", "recurrence.0.week_days.1", "Sun"),
   180  					resource.TestCheckResourceAttr(
   181  						"datadog_downtime.foo", "message", "Example Datadog downtime message."),
   182  				),
   183  			},
   184  		},
   185  	})
   186  }
   187  
   188  func TestAccDatadogDowntime_Updated(t *testing.T) {
   189  	resource.Test(t, resource.TestCase{
   190  		PreCheck:     func() { testAccPreCheck(t) },
   191  		Providers:    testAccProviders,
   192  		CheckDestroy: testAccCheckDatadogDowntimeDestroy,
   193  		Steps: []resource.TestStep{
   194  			resource.TestStep{
   195  				Config: testAccCheckDatadogDowntimeConfig,
   196  				Check: resource.ComposeTestCheckFunc(
   197  					testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
   198  					resource.TestCheckResourceAttr(
   199  						"datadog_downtime.foo", "scope.0", "*"),
   200  					resource.TestCheckResourceAttr(
   201  						"datadog_downtime.foo", "start", "1735707600"),
   202  					resource.TestCheckResourceAttr(
   203  						"datadog_downtime.foo", "end", "1735765200"),
   204  					resource.TestCheckResourceAttr(
   205  						"datadog_downtime.foo", "recurrence.0.type", "days"),
   206  					resource.TestCheckResourceAttr(
   207  						"datadog_downtime.foo", "recurrence.0.period", "1"),
   208  					resource.TestCheckResourceAttr(
   209  						"datadog_downtime.foo", "message", "Example Datadog downtime message."),
   210  				),
   211  			},
   212  			resource.TestStep{
   213  				Config: testAccCheckDatadogDowntimeConfigUpdated,
   214  				Check: resource.ComposeTestCheckFunc(
   215  					testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
   216  					resource.TestCheckResourceAttr(
   217  						"datadog_downtime.foo", "scope.0", "Updated"),
   218  					resource.TestCheckResourceAttr(
   219  						"datadog_downtime.foo", "start", "1735707600"),
   220  					resource.TestCheckResourceAttr(
   221  						"datadog_downtime.foo", "end", "1735765200"),
   222  					resource.TestCheckResourceAttr(
   223  						"datadog_downtime.foo", "recurrence.0.type", "days"),
   224  					resource.TestCheckResourceAttr(
   225  						"datadog_downtime.foo", "recurrence.0.period", "3"),
   226  					resource.TestCheckResourceAttr(
   227  						"datadog_downtime.foo", "message", "Example Datadog downtime message."),
   228  				),
   229  			},
   230  		},
   231  	})
   232  }
   233  
   234  func TestAccDatadogDowntime_TrimWhitespace(t *testing.T) {
   235  	resource.Test(t, resource.TestCase{
   236  		PreCheck:     func() { testAccPreCheck(t) },
   237  		Providers:    testAccProviders,
   238  		CheckDestroy: testAccCheckDatadogDowntimeDestroy,
   239  		Steps: []resource.TestStep{
   240  			resource.TestStep{
   241  				Config: testAccCheckDatadogDowntimeConfigWhitespace,
   242  				Check: resource.ComposeTestCheckFunc(
   243  					testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
   244  					resource.TestCheckResourceAttr(
   245  						"datadog_downtime.foo", "scope.0", "host:Whitespace"),
   246  					resource.TestCheckResourceAttr(
   247  						"datadog_downtime.foo", "start", "1735707600"),
   248  					resource.TestCheckResourceAttr(
   249  						"datadog_downtime.foo", "end", "1735765200"),
   250  					resource.TestCheckResourceAttr(
   251  						"datadog_downtime.foo", "recurrence.0.type", "days"),
   252  					resource.TestCheckResourceAttr(
   253  						"datadog_downtime.foo", "recurrence.0.period", "1"),
   254  					resource.TestCheckResourceAttr(
   255  						"datadog_downtime.foo", "message", "Example Datadog downtime message."),
   256  				),
   257  			},
   258  		},
   259  	})
   260  }
   261  
   262  func testAccCheckDatadogDowntimeDestroy(s *terraform.State) error {
   263  	client := testAccProvider.Meta().(*datadog.Client)
   264  
   265  	if err := datadogDowntimeDestroyHelper(s, client); err != nil {
   266  		return err
   267  	}
   268  	return nil
   269  }
   270  
   271  func testAccCheckDatadogDowntimeExists(n string) resource.TestCheckFunc {
   272  	return func(s *terraform.State) error {
   273  		client := testAccProvider.Meta().(*datadog.Client)
   274  		if err := datadogDowntimeExistsHelper(s, client); err != nil {
   275  			return err
   276  		}
   277  		return nil
   278  	}
   279  }
   280  
   281  const testAccCheckDatadogDowntimeConfig = `
   282  resource "datadog_downtime" "foo" {
   283    scope = ["*"]
   284    start = 1735707600
   285    end   = 1735765200
   286  
   287    recurrence {
   288      type   = "days"
   289      period = 1
   290    }
   291  
   292    message = "Example Datadog downtime message."
   293  }
   294  `
   295  
   296  const testAccCheckDatadogDowntimeConfigMultiScope = `
   297  resource "datadog_downtime" "foo" {
   298    scope = ["host:A", "host:B"]
   299    start = 1735707600
   300    end   = 1735765200
   301  
   302    recurrence {
   303      type   = "days"
   304      period = 1
   305    }
   306  
   307    message = "Example Datadog downtime message."
   308  }
   309  `
   310  
   311  const testAccCheckDatadogDowntimeConfigNoRecurrence = `
   312  resource "datadog_downtime" "foo" {
   313    scope = ["host:NoRecurrence"]
   314    start = 1735707600
   315    end   = 1735765200
   316    message = "Example Datadog downtime message."
   317  }
   318  `
   319  
   320  const testAccCheckDatadogDowntimeConfigUntilDateRecurrence = `
   321  resource "datadog_downtime" "foo" {
   322    scope = ["host:UntilDateRecurrence"]
   323    start = 1735707600
   324    end   = 1735765200
   325  
   326    recurrence {
   327      type       = "days"
   328      period     = 1
   329  	until_date = 1736226000
   330    }
   331  
   332    message = "Example Datadog downtime message."
   333  }
   334  `
   335  
   336  const testAccCheckDatadogDowntimeConfigUntilOccurrencesRecurrence = `
   337  resource "datadog_downtime" "foo" {
   338    scope = ["host:UntilOccurrencesRecurrence"]
   339    start = 1735707600
   340    end   = 1735765200
   341  
   342    recurrence {
   343      type              = "days"
   344      period            = 1
   345  	until_occurrences = 5
   346    }
   347  
   348    message = "Example Datadog downtime message."
   349  }
   350  `
   351  
   352  const testAccCheckDatadogDowntimeConfigWeekDaysRecurrence = `
   353  resource "datadog_downtime" "foo" {
   354    scope = ["WeekDaysRecurrence"]
   355    start = 1735646400
   356    end   = 1735732799
   357  
   358    recurrence {
   359      period    = 1
   360  	type      = "weeks"
   361  	week_days = ["Sat", "Sun"]
   362    }
   363  
   364    message = "Example Datadog downtime message."
   365  }
   366  `
   367  
   368  const testAccCheckDatadogDowntimeConfigUpdated = `
   369  resource "datadog_downtime" "foo" {
   370    scope = ["Updated"]
   371    start = 1735707600
   372    end   = 1735765200
   373  
   374    recurrence {
   375      type   = "days"
   376      period = 3
   377    }
   378  
   379    message = "Example Datadog downtime message."
   380  }
   381  `
   382  
   383  const testAccCheckDatadogDowntimeConfigWhitespace = `
   384  resource "datadog_downtime" "foo" {
   385    scope = ["host:Whitespace"]
   386    start = 1735707600
   387    end   = 1735765200
   388  
   389    recurrence {
   390      type   = "days"
   391      period = 1
   392    }
   393  
   394    message = <<EOF
   395  Example Datadog downtime message.
   396  EOF
   397  }
   398  `
   399  
   400  func TestResourceDatadogDowntimeRecurrenceTypeValidation(t *testing.T) {
   401  	cases := []struct {
   402  		Value    string
   403  		ErrCount int
   404  	}{
   405  		{
   406  			Value:    "daily",
   407  			ErrCount: 1,
   408  		},
   409  		{
   410  			Value:    "days",
   411  			ErrCount: 0,
   412  		},
   413  		{
   414  			Value:    "days,weeks",
   415  			ErrCount: 1,
   416  		},
   417  		{
   418  			Value:    "months",
   419  			ErrCount: 0,
   420  		},
   421  		{
   422  			Value:    "years",
   423  			ErrCount: 0,
   424  		},
   425  		{
   426  			Value:    "weeks",
   427  			ErrCount: 0,
   428  		},
   429  	}
   430  
   431  	for _, tc := range cases {
   432  		_, errors := validateDatadogDowntimeRecurrenceType(tc.Value, "datadog_downtime_recurrence_type")
   433  
   434  		if len(errors) != tc.ErrCount {
   435  			t.Fatalf("Expected Datadog Downtime Recurrence Type validation to trigger %d error(s) for value %q - instead saw %d",
   436  				tc.ErrCount, tc.Value, len(errors))
   437  		}
   438  	}
   439  }
   440  
   441  func TestResourceDatadogDowntimeRecurrenceWeekDaysValidation(t *testing.T) {
   442  	cases := []struct {
   443  		Value    string
   444  		ErrCount int
   445  	}{
   446  		{
   447  			Value:    "Mon",
   448  			ErrCount: 0,
   449  		},
   450  		{
   451  			Value:    "Mon,",
   452  			ErrCount: 1,
   453  		},
   454  		{
   455  			Value:    "Monday",
   456  			ErrCount: 1,
   457  		},
   458  		{
   459  			Value:    "mon",
   460  			ErrCount: 1,
   461  		},
   462  		{
   463  			Value:    "mon,",
   464  			ErrCount: 1,
   465  		},
   466  		{
   467  			Value:    "monday",
   468  			ErrCount: 1,
   469  		},
   470  		{
   471  			Value:    "mon,Tue",
   472  			ErrCount: 1,
   473  		},
   474  		{
   475  			Value:    "Mon,tue",
   476  			ErrCount: 1,
   477  		},
   478  		{
   479  			Value:    "Mon,Tue",
   480  			ErrCount: 1,
   481  		},
   482  		{
   483  			Value:    "Mon, Tue",
   484  			ErrCount: 1,
   485  		},
   486  	}
   487  
   488  	for _, tc := range cases {
   489  		_, errors := validateDatadogDowntimeRecurrenceWeekDays(tc.Value, "datadog_downtime_recurrence_week_days")
   490  
   491  		if len(errors) != tc.ErrCount {
   492  			t.Fatalf("Expected Datadog Downtime Recurrence Week Days validation to trigger %d error(s) for value %q - instead saw %d",
   493  				tc.ErrCount, tc.Value, len(errors))
   494  		}
   495  	}
   496  }
   497  
   498  func datadogDowntimeDestroyHelper(s *terraform.State, client *datadog.Client) error {
   499  	for _, r := range s.RootModule().Resources {
   500  		id, _ := strconv.Atoi(r.Primary.ID)
   501  		dt, err := client.GetDowntime(id)
   502  
   503  		if err != nil {
   504  			if strings.Contains(err.Error(), "404 Not Found") {
   505  				continue
   506  			}
   507  			return fmt.Errorf("Received an error retrieving downtime %s", err)
   508  		}
   509  
   510  		// Datadog only cancels downtime on DELETE
   511  		if !dt.GetActive() {
   512  			continue
   513  		}
   514  		return fmt.Errorf("Downtime still exists")
   515  	}
   516  	return nil
   517  }
   518  
   519  func datadogDowntimeExistsHelper(s *terraform.State, client *datadog.Client) error {
   520  	for _, r := range s.RootModule().Resources {
   521  		id, _ := strconv.Atoi(r.Primary.ID)
   522  		if _, err := client.GetDowntime(id); err != nil {
   523  			return fmt.Errorf("Received an error retrieving downtime %s", err)
   524  		}
   525  	}
   526  	return nil
   527  }