1/* 2 * Datadog API for Go 3 * 4 * Please see the included LICENSE file for licensing information. 5 * 6 * Copyright 2013 by authors and contributors. 7 */ 8 9package datadog 10 11import ( 12 "fmt" 13 "strings" 14) 15 16// DowntimeType are a classification of a given downtime scope 17type DowntimeType int 18 19// The three downtime type classifications. 20const ( 21 StarDowntimeType DowntimeType = 0 22 HostDowntimeType DowntimeType = 1 23 OtherDowntimeType DowntimeType = 2 24) 25 26type Recurrence struct { 27 Period *int `json:"period,omitempty"` 28 Type *string `json:"type,omitempty"` 29 UntilDate *int `json:"until_date,omitempty"` 30 UntilOccurrences *int `json:"until_occurrences,omitempty"` 31 WeekDays []string `json:"week_days,omitempty"` 32} 33 34type Downtime struct { 35 Active *bool `json:"active,omitempty"` 36 Canceled *int `json:"canceled,omitempty"` 37 Disabled *bool `json:"disabled,omitempty"` 38 End *int `json:"end,omitempty"` 39 Id *int `json:"id,omitempty"` 40 Message *string `json:"message,omitempty"` 41 MonitorId *int `json:"monitor_id,omitempty"` 42 MonitorTags []string `json:"monitor_tags,omitempty"` 43 ParentId *int `json:"parent_id,omitempty"` 44 Timezone *string `json:"timezone,omitempty"` 45 Recurrence *Recurrence `json:"recurrence,omitempty"` 46 Scope []string `json:"scope,omitempty"` 47 Start *int `json:"start,omitempty"` 48 CreatorID *int `json:"creator_id,omitempty"` 49 UpdaterID *int `json:"updater_id,omitempty"` 50 Type *int `json:"downtime_type,omitempty"` 51} 52 53// DowntimeType returns the canonical downtime type classification. 54// This is calculated based on the provided server response, but the logic is copied down here to calculate locally. 55func (d *Downtime) DowntimeType() DowntimeType { 56 if d.Type != nil { 57 switch *d.Type { 58 case 0: 59 return StarDowntimeType 60 case 1: 61 return HostDowntimeType 62 default: 63 return OtherDowntimeType 64 } 65 } 66 if len(d.Scope) == 1 { 67 if d.Scope[0] == "*" { 68 return StarDowntimeType 69 } 70 if strings.HasPrefix(d.Scope[0], "host:") { 71 return HostDowntimeType 72 } 73 } 74 return OtherDowntimeType 75} 76 77// reqDowntimes retrieves a slice of all Downtimes. 78type reqDowntimes struct { 79 Downtimes []Downtime `json:"downtimes,omitempty"` 80} 81 82// CreateDowntime adds a new downtme to the system. This returns a pointer 83// to a Downtime so you can pass that to UpdateDowntime or CancelDowntime 84// later if needed. 85func (client *Client) CreateDowntime(downtime *Downtime) (*Downtime, error) { 86 var out Downtime 87 if err := client.doJsonRequest("POST", "/v1/downtime", downtime, &out); err != nil { 88 return nil, err 89 } 90 return &out, nil 91} 92 93// UpdateDowntime takes a downtime that was previously retrieved through some method 94// and sends it back to the server. 95func (client *Client) UpdateDowntime(downtime *Downtime) error { 96 return client.doJsonRequest("PUT", fmt.Sprintf("/v1/downtime/%d", *downtime.Id), 97 downtime, downtime) 98} 99 100// Getdowntime retrieves an downtime by identifier. 101func (client *Client) GetDowntime(id int) (*Downtime, error) { 102 var out Downtime 103 if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/downtime/%d", id), nil, &out); err != nil { 104 return nil, err 105 } 106 return &out, nil 107} 108 109// DeleteDowntime removes an downtime from the system. 110func (client *Client) DeleteDowntime(id int) error { 111 return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/downtime/%d", id), 112 nil, nil) 113} 114 115// GetDowntimes returns a slice of all downtimes. 116func (client *Client) GetDowntimes() ([]Downtime, error) { 117 var out reqDowntimes 118 if err := client.doJsonRequest("GET", "/v1/downtime", nil, &out.Downtimes); err != nil { 119 return nil, err 120 } 121 return out.Downtimes, nil 122} 123