provider/datadog: Add datadog_downtime resource (#10994)
* provider/datadog: Initial datadog_downtime resource * provider/datadog: Update datadog_downtime resource to v2 library, fix recurrence handling * provider/datadog: Fix datadog_downtime import test
This commit is contained in:
parent
1917646ec3
commit
3e817e019f
|
@ -0,0 +1,37 @@
|
|||
package datadog
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestDatadogDowntime_import(t *testing.T) {
|
||||
resourceName := "datadog_downtime.foo"
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDatadogDowntimeDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfigImported,
|
||||
},
|
||||
resource.TestStep{
|
||||
ResourceName: resourceName,
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const testAccCheckDatadogDowntimeConfigImported = `
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["host:X", "host:Y"]
|
||||
start = 1735707600
|
||||
end = 1735765200
|
||||
|
||||
message = "Example Datadog downtime message."
|
||||
}
|
||||
`
|
|
@ -25,6 +25,7 @@ func Provider() terraform.ResourceProvider {
|
|||
},
|
||||
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"datadog_downtime": resourceDatadogDowntime(),
|
||||
"datadog_monitor": resourceDatadogMonitor(),
|
||||
"datadog_timeboard": resourceDatadogTimeboard(),
|
||||
"datadog_user": resourceDatadogUser(),
|
||||
|
|
|
@ -0,0 +1,339 @@
|
|||
package datadog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"gopkg.in/zorkian/go-datadog-api.v2"
|
||||
)
|
||||
|
||||
func resourceDatadogDowntime() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceDatadogDowntimeCreate,
|
||||
Read: resourceDatadogDowntimeRead,
|
||||
Update: resourceDatadogDowntimeUpdate,
|
||||
Delete: resourceDatadogDowntimeDelete,
|
||||
Exists: resourceDatadogDowntimeExists,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: resourceDatadogDowntimeImport,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"active": {
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
},
|
||||
"disabled": {
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
},
|
||||
"end": {
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
},
|
||||
"message": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
StateFunc: func(val interface{}) string {
|
||||
return strings.TrimSpace(val.(string))
|
||||
},
|
||||
},
|
||||
"recurrence": {
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
MaxItems: 1,
|
||||
Elem: &schema.Resource{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"period": {
|
||||
Type: schema.TypeInt,
|
||||
Required: true,
|
||||
},
|
||||
"type": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ValidateFunc: validateDatadogDowntimeRecurrenceType,
|
||||
},
|
||||
"until_date": {
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
ConflictsWith: []string{"recurrence.until_occurrences"},
|
||||
},
|
||||
"until_occurrences": {
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
ConflictsWith: []string{"recurrence.until_date"},
|
||||
},
|
||||
"week_days": {
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
ValidateFunc: validateDatadogDowntimeRecurrenceWeekDays,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"scope": {
|
||||
Type: schema.TypeList,
|
||||
Required: true,
|
||||
Elem: &schema.Schema{Type: schema.TypeString},
|
||||
},
|
||||
"start": {
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func buildDowntimeStruct(d *schema.ResourceData) *datadog.Downtime {
|
||||
var dt datadog.Downtime
|
||||
|
||||
if attr, ok := d.GetOk("active"); ok {
|
||||
dt.SetActive(attr.(bool))
|
||||
}
|
||||
if attr, ok := d.GetOk("disabled"); ok {
|
||||
dt.SetDisabled(attr.(bool))
|
||||
}
|
||||
if attr, ok := d.GetOk("end"); ok {
|
||||
dt.SetEnd(attr.(int))
|
||||
}
|
||||
if attr, ok := d.GetOk("message"); ok {
|
||||
dt.SetMessage(strings.TrimSpace(attr.(string)))
|
||||
}
|
||||
if _, ok := d.GetOk("recurrence"); ok {
|
||||
var recurrence datadog.Recurrence
|
||||
|
||||
if attr, ok := d.GetOk("recurrence.0.period"); ok {
|
||||
recurrence.SetPeriod(attr.(int))
|
||||
}
|
||||
if attr, ok := d.GetOk("recurrence.0.type"); ok {
|
||||
recurrence.SetType(attr.(string))
|
||||
}
|
||||
if attr, ok := d.GetOk("recurrence.0.until_date"); ok {
|
||||
recurrence.SetUntilDate(attr.(int))
|
||||
}
|
||||
if attr, ok := d.GetOk("recurrence.0.until_occurrences"); ok {
|
||||
recurrence.SetUntilOccurrences(attr.(int))
|
||||
}
|
||||
if attr, ok := d.GetOk("recurrence.0.week_days"); ok {
|
||||
weekDays := make([]string, 0, len(attr.([]interface{})))
|
||||
for _, weekDay := range attr.([]interface{}) {
|
||||
weekDays = append(weekDays, weekDay.(string))
|
||||
}
|
||||
recurrence.WeekDays = weekDays
|
||||
}
|
||||
|
||||
dt.SetRecurrence(recurrence)
|
||||
}
|
||||
scope := []string{}
|
||||
for _, s := range d.Get("scope").([]interface{}) {
|
||||
scope = append(scope, s.(string))
|
||||
}
|
||||
dt.Scope = scope
|
||||
if attr, ok := d.GetOk("start"); ok {
|
||||
dt.SetStart(attr.(int))
|
||||
}
|
||||
|
||||
return &dt
|
||||
}
|
||||
|
||||
func resourceDatadogDowntimeExists(d *schema.ResourceData, meta interface{}) (b bool, e error) {
|
||||
// Exists - This is called to verify a resource still exists. It is called prior to Read,
|
||||
// and lowers the burden of Read to be able to assume the resource exists.
|
||||
client := meta.(*datadog.Client)
|
||||
|
||||
id, err := strconv.Atoi(d.Id())
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if _, err = client.GetDowntime(id); err != nil {
|
||||
if strings.Contains(err.Error(), "404 Not Found") {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func resourceDatadogDowntimeCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*datadog.Client)
|
||||
|
||||
dts := buildDowntimeStruct(d)
|
||||
dt, err := client.CreateDowntime(dts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error updating downtime: %s", err.Error())
|
||||
}
|
||||
|
||||
d.SetId(strconv.Itoa(dt.GetId()))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceDatadogDowntimeRead(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*datadog.Client)
|
||||
|
||||
id, err := strconv.Atoi(d.Id())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dt, err := client.GetDowntime(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] downtime: %v", dt)
|
||||
d.Set("active", dt.GetActive())
|
||||
d.Set("disabled", dt.GetDisabled())
|
||||
d.Set("end", dt.GetEnd())
|
||||
d.Set("message", dt.GetMessage())
|
||||
if r, ok := dt.GetRecurrenceOk(); ok {
|
||||
recurrence := make(map[string]interface{})
|
||||
recurrenceList := make([]map[string]interface{}, 0, 1)
|
||||
|
||||
if attr, ok := r.GetPeriodOk(); ok {
|
||||
recurrence["period"] = strconv.Itoa(attr)
|
||||
}
|
||||
if attr, ok := r.GetTypeOk(); ok {
|
||||
recurrence["type"] = attr
|
||||
}
|
||||
if attr, ok := r.GetUntilDateOk(); ok {
|
||||
recurrence["until_date"] = strconv.Itoa(attr)
|
||||
}
|
||||
if attr, ok := r.GetUntilOccurrencesOk(); ok {
|
||||
recurrence["until_occurrences"] = strconv.Itoa(attr)
|
||||
}
|
||||
if r.WeekDays != nil {
|
||||
weekDays := make([]string, 0, len(r.WeekDays))
|
||||
for _, weekDay := range r.WeekDays {
|
||||
weekDays = append(weekDays, weekDay)
|
||||
}
|
||||
recurrence["week_days"] = weekDays
|
||||
}
|
||||
recurrenceList = append(recurrenceList, recurrence)
|
||||
d.Set("recurrence", recurrenceList)
|
||||
}
|
||||
d.Set("scope", dt.Scope)
|
||||
d.Set("start", dt.GetStart())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceDatadogDowntimeUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*datadog.Client)
|
||||
|
||||
var dt datadog.Downtime
|
||||
|
||||
id, err := strconv.Atoi(d.Id())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dt.SetId(id)
|
||||
if attr, ok := d.GetOk("active"); ok {
|
||||
dt.SetActive(attr.(bool))
|
||||
}
|
||||
if attr, ok := d.GetOk("disabled"); ok {
|
||||
dt.SetDisabled(attr.(bool))
|
||||
}
|
||||
if attr, ok := d.GetOk("end"); ok {
|
||||
dt.SetEnd(attr.(int))
|
||||
}
|
||||
if attr, ok := d.GetOk("message"); ok {
|
||||
dt.SetMessage(attr.(string))
|
||||
}
|
||||
|
||||
if _, ok := d.GetOk("recurrence"); ok {
|
||||
var recurrence datadog.Recurrence
|
||||
|
||||
if attr, ok := d.GetOk("recurrence.0.period"); ok {
|
||||
recurrence.SetPeriod(attr.(int))
|
||||
}
|
||||
if attr, ok := d.GetOk("recurrence.0.type"); ok {
|
||||
recurrence.SetType(attr.(string))
|
||||
}
|
||||
if attr, ok := d.GetOk("recurrence.0.until_date"); ok {
|
||||
recurrence.SetUntilDate(attr.(int))
|
||||
}
|
||||
if attr, ok := d.GetOk("recurrence.0.until_occurrences"); ok {
|
||||
recurrence.SetUntilOccurrences(attr.(int))
|
||||
}
|
||||
if attr, ok := d.GetOk("recurrence.0.week_days"); ok {
|
||||
weekDays := make([]string, 0, len(attr.([]interface{})))
|
||||
for _, weekDay := range attr.([]interface{}) {
|
||||
weekDays = append(weekDays, weekDay.(string))
|
||||
}
|
||||
recurrence.WeekDays = weekDays
|
||||
}
|
||||
|
||||
dt.SetRecurrence(recurrence)
|
||||
}
|
||||
|
||||
scope := make([]string, 0)
|
||||
for _, v := range d.Get("scope").([]interface{}) {
|
||||
scope = append(scope, v.(string))
|
||||
}
|
||||
dt.Scope = scope
|
||||
if attr, ok := d.GetOk("start"); ok {
|
||||
dt.SetStart(attr.(int))
|
||||
}
|
||||
|
||||
if err = client.UpdateDowntime(&dt); err != nil {
|
||||
return fmt.Errorf("error updating downtime: %s", err.Error())
|
||||
}
|
||||
|
||||
return resourceDatadogDowntimeRead(d, meta)
|
||||
}
|
||||
|
||||
func resourceDatadogDowntimeDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*datadog.Client)
|
||||
|
||||
id, err := strconv.Atoi(d.Id())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = client.DeleteDowntime(id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceDatadogDowntimeImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
||||
if err := resourceDatadogDowntimeRead(d, meta); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []*schema.ResourceData{d}, nil
|
||||
}
|
||||
|
||||
func validateDatadogDowntimeRecurrenceType(v interface{}, k string) (ws []string, errors []error) {
|
||||
value := v.(string)
|
||||
switch value {
|
||||
case "days", "months", "weeks", "years":
|
||||
break
|
||||
default:
|
||||
errors = append(errors, fmt.Errorf(
|
||||
"%q contains an invalid recurrence type parameter %q. Valid parameters are days, months, weeks, or years", k, value))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func validateDatadogDowntimeRecurrenceWeekDays(v interface{}, k string) (ws []string, errors []error) {
|
||||
value := v.(string)
|
||||
switch value {
|
||||
case "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun":
|
||||
break
|
||||
default:
|
||||
errors = append(errors, fmt.Errorf(
|
||||
"%q contains an invalid recurrence week day parameter %q. Valid parameters are Mon, Tue, Wed, Thu, Fri, Sat, or Sun", k, value))
|
||||
}
|
||||
return
|
||||
}
|
|
@ -0,0 +1,527 @@
|
|||
package datadog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"gopkg.in/zorkian/go-datadog-api.v2"
|
||||
)
|
||||
|
||||
func TestAccDatadogDowntime_Basic(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDatadogDowntimeDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.0", "*"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "start", "1735707600"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "end", "1735765200"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.type", "days"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.period", "1"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "message", "Example Datadog downtime message."),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccDatadogDowntime_BasicMultiScope(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDatadogDowntimeDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfigMultiScope,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.0", "host:A"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.1", "host:B"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "start", "1735707600"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "end", "1735765200"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.type", "days"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.period", "1"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "message", "Example Datadog downtime message."),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccDatadogDowntime_BasicNoRecurrence(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDatadogDowntimeDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfigNoRecurrence,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.0", "host:NoRecurrence"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "start", "1735707600"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "end", "1735765200"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "message", "Example Datadog downtime message."),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccDatadogDowntime_BasicUntilDateRecurrence(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDatadogDowntimeDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfigUntilDateRecurrence,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.0", "host:UntilDateRecurrence"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "start", "1735707600"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "end", "1735765200"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.type", "days"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.period", "1"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.until_date", "1736226000"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "message", "Example Datadog downtime message."),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccDatadogDowntime_BasicUntilOccurrencesRecurrence(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDatadogDowntimeDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfigUntilOccurrencesRecurrence,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.0", "host:UntilOccurrencesRecurrence"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "start", "1735707600"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "end", "1735765200"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.type", "days"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.period", "1"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.until_occurrences", "5"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "message", "Example Datadog downtime message."),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccDatadogDowntime_WeekDayRecurring(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDatadogDowntimeDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfigWeekDaysRecurrence,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.0", "WeekDaysRecurrence"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "start", "1735646400"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "end", "1735732799"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.type", "weeks"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.period", "1"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.week_days.0", "Sat"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.week_days.1", "Sun"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "message", "Example Datadog downtime message."),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccDatadogDowntime_Updated(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDatadogDowntimeDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.0", "*"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "start", "1735707600"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "end", "1735765200"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.type", "days"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.period", "1"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "message", "Example Datadog downtime message."),
|
||||
),
|
||||
},
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfigUpdated,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.0", "Updated"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "start", "1735707600"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "end", "1735765200"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.type", "days"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.period", "3"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "message", "Example Datadog downtime message."),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccDatadogDowntime_TrimWhitespace(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDatadogDowntimeDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDatadogDowntimeConfigWhitespace,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDatadogDowntimeExists("datadog_downtime.foo"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "scope.0", "host:Whitespace"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "start", "1735707600"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "end", "1735765200"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.type", "days"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "recurrence.0.period", "1"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"datadog_downtime.foo", "message", "Example Datadog downtime message."),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckDatadogDowntimeDestroy(s *terraform.State) error {
|
||||
client := testAccProvider.Meta().(*datadog.Client)
|
||||
|
||||
if err := datadogDowntimeDestroyHelper(s, client); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccCheckDatadogDowntimeExists(n string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
client := testAccProvider.Meta().(*datadog.Client)
|
||||
if err := datadogDowntimeExistsHelper(s, client); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
const testAccCheckDatadogDowntimeConfig = `
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["*"]
|
||||
start = 1735707600
|
||||
end = 1735765200
|
||||
|
||||
recurrence {
|
||||
type = "days"
|
||||
period = 1
|
||||
}
|
||||
|
||||
message = "Example Datadog downtime message."
|
||||
}
|
||||
`
|
||||
|
||||
const testAccCheckDatadogDowntimeConfigMultiScope = `
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["host:A", "host:B"]
|
||||
start = 1735707600
|
||||
end = 1735765200
|
||||
|
||||
recurrence {
|
||||
type = "days"
|
||||
period = 1
|
||||
}
|
||||
|
||||
message = "Example Datadog downtime message."
|
||||
}
|
||||
`
|
||||
|
||||
const testAccCheckDatadogDowntimeConfigNoRecurrence = `
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["host:NoRecurrence"]
|
||||
start = 1735707600
|
||||
end = 1735765200
|
||||
message = "Example Datadog downtime message."
|
||||
}
|
||||
`
|
||||
|
||||
const testAccCheckDatadogDowntimeConfigUntilDateRecurrence = `
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["host:UntilDateRecurrence"]
|
||||
start = 1735707600
|
||||
end = 1735765200
|
||||
|
||||
recurrence {
|
||||
type = "days"
|
||||
period = 1
|
||||
until_date = 1736226000
|
||||
}
|
||||
|
||||
message = "Example Datadog downtime message."
|
||||
}
|
||||
`
|
||||
|
||||
const testAccCheckDatadogDowntimeConfigUntilOccurrencesRecurrence = `
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["host:UntilOccurrencesRecurrence"]
|
||||
start = 1735707600
|
||||
end = 1735765200
|
||||
|
||||
recurrence {
|
||||
type = "days"
|
||||
period = 1
|
||||
until_occurrences = 5
|
||||
}
|
||||
|
||||
message = "Example Datadog downtime message."
|
||||
}
|
||||
`
|
||||
|
||||
const testAccCheckDatadogDowntimeConfigWeekDaysRecurrence = `
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["WeekDaysRecurrence"]
|
||||
start = 1735646400
|
||||
end = 1735732799
|
||||
|
||||
recurrence {
|
||||
period = 1
|
||||
type = "weeks"
|
||||
week_days = ["Sat", "Sun"]
|
||||
}
|
||||
|
||||
message = "Example Datadog downtime message."
|
||||
}
|
||||
`
|
||||
|
||||
const testAccCheckDatadogDowntimeConfigUpdated = `
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["Updated"]
|
||||
start = 1735707600
|
||||
end = 1735765200
|
||||
|
||||
recurrence {
|
||||
type = "days"
|
||||
period = 3
|
||||
}
|
||||
|
||||
message = "Example Datadog downtime message."
|
||||
}
|
||||
`
|
||||
|
||||
const testAccCheckDatadogDowntimeConfigWhitespace = `
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["host:Whitespace"]
|
||||
start = 1735707600
|
||||
end = 1735765200
|
||||
|
||||
recurrence {
|
||||
type = "days"
|
||||
period = 1
|
||||
}
|
||||
|
||||
message = <<EOF
|
||||
Example Datadog downtime message.
|
||||
EOF
|
||||
}
|
||||
`
|
||||
|
||||
func TestResourceDatadogDowntimeRecurrenceTypeValidation(t *testing.T) {
|
||||
cases := []struct {
|
||||
Value string
|
||||
ErrCount int
|
||||
}{
|
||||
{
|
||||
Value: "daily",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "days",
|
||||
ErrCount: 0,
|
||||
},
|
||||
{
|
||||
Value: "days,weeks",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "months",
|
||||
ErrCount: 0,
|
||||
},
|
||||
{
|
||||
Value: "years",
|
||||
ErrCount: 0,
|
||||
},
|
||||
{
|
||||
Value: "weeks",
|
||||
ErrCount: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
_, errors := validateDatadogDowntimeRecurrenceType(tc.Value, "datadog_downtime_recurrence_type")
|
||||
|
||||
if len(errors) != tc.ErrCount {
|
||||
t.Fatalf("Expected Datadog Downtime Recurrence Type validation to trigger %d error(s) for value %q - instead saw %d",
|
||||
tc.ErrCount, tc.Value, len(errors))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceDatadogDowntimeRecurrenceWeekDaysValidation(t *testing.T) {
|
||||
cases := []struct {
|
||||
Value string
|
||||
ErrCount int
|
||||
}{
|
||||
{
|
||||
Value: "Mon",
|
||||
ErrCount: 0,
|
||||
},
|
||||
{
|
||||
Value: "Mon,",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "Monday",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "mon",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "mon,",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "monday",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "mon,Tue",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "Mon,tue",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "Mon,Tue",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "Mon, Tue",
|
||||
ErrCount: 1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
_, errors := validateDatadogDowntimeRecurrenceWeekDays(tc.Value, "datadog_downtime_recurrence_week_days")
|
||||
|
||||
if len(errors) != tc.ErrCount {
|
||||
t.Fatalf("Expected Datadog Downtime Recurrence Week Days validation to trigger %d error(s) for value %q - instead saw %d",
|
||||
tc.ErrCount, tc.Value, len(errors))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func datadogDowntimeDestroyHelper(s *terraform.State, client *datadog.Client) error {
|
||||
for _, r := range s.RootModule().Resources {
|
||||
id, _ := strconv.Atoi(r.Primary.ID)
|
||||
dt, err := client.GetDowntime(id)
|
||||
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "404 Not Found") {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("Received an error retrieving downtime %s", err)
|
||||
}
|
||||
|
||||
// Datadog only cancels downtime on DELETE
|
||||
if !dt.GetActive() {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("Downtime still exists")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func datadogDowntimeExistsHelper(s *terraform.State, client *datadog.Client) error {
|
||||
for _, r := range s.RootModule().Resources {
|
||||
id, _ := strconv.Atoi(r.Primary.ID)
|
||||
if _, err := client.GetDowntime(id); err != nil {
|
||||
return fmt.Errorf("Received an error retrieving downtime %s", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
---
|
||||
layout: "datadog"
|
||||
page_title: "Datadog: datadog_downtime"
|
||||
sidebar_current: "docs-datadog-resource-downtime"
|
||||
description: |-
|
||||
Provides a Datadog downtime resource. This can be used to create and manage downtimes.
|
||||
---
|
||||
|
||||
# datadog\_downtime
|
||||
|
||||
Provides a Datadog downtime resource. This can be used to create and manage Datadog downtimes.
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
# Create a new daily 1700-0900 Datadog downtime
|
||||
resource "datadog_downtime" "foo" {
|
||||
scope = ["*"]
|
||||
start = 1483308000
|
||||
end = 1483365600
|
||||
|
||||
recurrence {
|
||||
type = "days"
|
||||
period = 1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Argument Reference
|
||||
|
||||
The following arguments are supported:
|
||||
|
||||
* `scope` - (Required) A list of items to apply the downtime to, e.g. host:X
|
||||
* `start` - (Optional) POSIX timestamp to start the downtime.
|
||||
* `end` - (Optional) POSIX timestamp to end the downtime.
|
||||
* `recurrence` - (Optional) A dictionary to configure the downtime to be recurring.
|
||||
* `type` - days, weeks, months, or years
|
||||
* `period` - How often to repeat as an integer. For example to repeat every 3 days, select a type of days and a period of 3.
|
||||
* `week_days` - (Optional) A list of week days to repeat on. Choose from: Mon, Tue, Wed, Thu, Fri, Sat or Sun. Only applicable when type is weeks. First letter must be capitalized.
|
||||
* `until_occurrences` - (Optional) How many times the downtime will be rescheduled. `until_occurrences` and `until_date` are mutually exclusive.
|
||||
* `until_date` - (Optional) The date at which the recurrence should end as a POSIX timestamp. `until_occurrences` and `until_date` are mutually exclusive.
|
||||
* `message` - (Optional) A message to include with notifications for this downtime.
|
||||
|
||||
## Attributes Reference
|
||||
|
||||
The following attributes are exported:
|
||||
|
||||
* `id` - ID of the Datadog downtime
|
||||
|
||||
## Import
|
||||
|
||||
Downtimes can be imported using their numeric ID, e.g.
|
||||
|
||||
```
|
||||
$ terraform import datadog_downtime.bytes_received_localhost 2081
|
||||
```
|
|
@ -13,6 +13,9 @@
|
|||
<li<%= sidebar_current(/^docs-datadog-resource/) %>>
|
||||
<a href="#">Resources</a>
|
||||
<ul class="nav nav-visible">
|
||||
<li<%= sidebar_current("docs-datadog-resource-downtime") %>>
|
||||
<a href="/docs/providers/datadog/r/downtime.html">datadog_downtime</a>
|
||||
</li>
|
||||
<li<%= sidebar_current("docs-datadog-resource-monitor") %>>
|
||||
<a href="/docs/providers/datadog/r/monitor.html">datadog_monitor</a>
|
||||
</li>
|
||||
|
|
Loading…
Reference in New Issue