More config options, markdown in incidents
- Specify method to ping with - Body regex match - Markdown in incident messages - Update existing incidents
This commit is contained in:
163
incident.go
163
incident.go
@@ -2,156 +2,113 @@ package cachet
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Incident Cachet data model
|
||||
type Incident struct {
|
||||
ID json.Number `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Message string `json:"message"`
|
||||
Status json.Number `json:"status"` // 4?
|
||||
HumanStatus string `json:"human_status"`
|
||||
Component *Component `json:"-"`
|
||||
ComponentID *json.Number `json:"component_id"`
|
||||
CreatedAt *string `json:"created_at"`
|
||||
UpdatedAt *string `json:"updated_at"`
|
||||
}
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Message string `json:"message"`
|
||||
Status int `json:"status"`
|
||||
Visible int `json"visible"`
|
||||
Notify bool `json:"notify"`
|
||||
|
||||
// GetIncidents - Get list of incidents
|
||||
func (monitor *CachetMonitor) GetIncidents() ([]Incident, error) {
|
||||
_, body, err := monitor.makeRequest("GET", "/incidents", nil)
|
||||
if err != nil {
|
||||
return []Incident{}, fmt.Errorf("Cannot get incidents: %v\n", err)
|
||||
}
|
||||
|
||||
var data struct {
|
||||
Incidents []Incident `json:"data"`
|
||||
}
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
return []Incident{}, fmt.Errorf("Cannot parse incidents: %v\n", err)
|
||||
}
|
||||
|
||||
return data.Incidents, nil
|
||||
ComponentID int `json:"component_id"`
|
||||
ComponentStatus int `json:"component_status"`
|
||||
}
|
||||
|
||||
// Send - Create or Update incident
|
||||
func (monitor *CachetMonitor) SendIncident(incident *Incident) error {
|
||||
jsonBytes, _ := json.Marshal(map[string]interface{}{
|
||||
"name": incident.Name,
|
||||
"message": incident.Message,
|
||||
"status": incident.Status,
|
||||
"component_id": incident.ComponentID,
|
||||
"notify": true,
|
||||
})
|
||||
func (incident *Incident) Send(cfg *CachetMonitor) error {
|
||||
switch incident.Status {
|
||||
case 1, 2, 3:
|
||||
// partial outage
|
||||
incident.ComponentStatus = 3
|
||||
|
||||
componentStatus, err := incident.GetComponentStatus(cfg)
|
||||
if componentStatus == 3 {
|
||||
// major outage
|
||||
incident.ComponentStatus = 4
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
cfg.Logger.Printf("cannot fetch component: %v", err)
|
||||
}
|
||||
case 4:
|
||||
// fixed
|
||||
incident.ComponentStatus = 1
|
||||
}
|
||||
|
||||
requestType := "POST"
|
||||
requestURL := "/incidents"
|
||||
if len(incident.ID) > 0 {
|
||||
if incident.ID > 0 {
|
||||
requestType = "PUT"
|
||||
requestURL += "/" + string(incident.ID)
|
||||
requestURL += "/" + strconv.Itoa(incident.ID)
|
||||
}
|
||||
|
||||
resp, body, err := monitor.makeRequest(requestType, requestURL, jsonBytes)
|
||||
jsonBytes, _ := json.Marshal(incident)
|
||||
|
||||
resp, body, err := cfg.makeRequest(requestType, requestURL, jsonBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var data struct {
|
||||
Incident Incident `json:"data"`
|
||||
Incident struct {
|
||||
ID int `json:"id"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &data); err != nil {
|
||||
return errors.New("Cannot parse incident body." + string(body))
|
||||
} else {
|
||||
incident.ID = data.Incident.ID
|
||||
incident.Component = data.Incident.Component
|
||||
return fmt.Errorf("Cannot parse incident body: %v, %v", err, string(body))
|
||||
}
|
||||
|
||||
incident.ID = data.Incident.ID
|
||||
if resp.StatusCode != 200 {
|
||||
return fmt.Errorf("Could not create/update incident!")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (incident *Incident) GetComponentStatus(cfg *CachetMonitor) (int, error) {
|
||||
resp, body, err := cfg.makeRequest("GET", "/components/"+strconv.Itoa(incident.ComponentID), nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return errors.New("Could not create/update incident!")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (monitor *CachetMonitor) fetchComponent(componentID string) (*Component, error) {
|
||||
_, body, err := monitor.makeRequest("GET", "/components/"+componentID, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return 0, fmt.Errorf("Invalid status code. Received %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var data struct {
|
||||
Component Component `json:"data"`
|
||||
Component struct {
|
||||
Status int `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &data); err != nil {
|
||||
return nil, errors.New("Cannot parse component body. " + string(body))
|
||||
return 0, fmt.Errorf("Cannot parse component body: %v. Err = %v", string(body), err)
|
||||
}
|
||||
|
||||
return &data.Component, nil
|
||||
}
|
||||
|
||||
func (monitor *CachetMonitor) UpdateComponent(incident *Incident) error {
|
||||
if incident.ComponentID == nil || len(*incident.ComponentID) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if incident.Component == nil {
|
||||
// fetch component
|
||||
component, err := monitor.fetchComponent(string(*incident.ComponentID))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot fetch component for incident. %v\n", err)
|
||||
}
|
||||
|
||||
incident.Component = component
|
||||
}
|
||||
|
||||
status, _ := strconv.Atoi(string(incident.Status))
|
||||
switch status {
|
||||
case 1, 2, 3:
|
||||
if incident.Component.Status == "3" {
|
||||
incident.Component.Status = "4"
|
||||
} else {
|
||||
incident.Component.Status = "3"
|
||||
}
|
||||
case 4:
|
||||
incident.Component.Status = "1"
|
||||
}
|
||||
|
||||
jsonBytes, _ := json.Marshal(map[string]interface{}{
|
||||
"status": incident.Component.Status,
|
||||
})
|
||||
|
||||
resp, _, err := monitor.makeRequest("PUT", "/components/"+string(incident.Component.ID), jsonBytes)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
return fmt.Errorf("Could not update component: (resp code %d) %v", resp.StatusCode, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return data.Component.Status, nil
|
||||
}
|
||||
|
||||
// SetInvestigating sets status to Investigating
|
||||
func (incident *Incident) SetInvestigating() {
|
||||
incident.Status = "1"
|
||||
incident.HumanStatus = "Investigating"
|
||||
incident.Status = 1
|
||||
}
|
||||
|
||||
// SetIdentified sets status to Identified
|
||||
func (incident *Incident) SetIdentified() {
|
||||
incident.Status = "2"
|
||||
incident.HumanStatus = "Identified"
|
||||
incident.Status = 2
|
||||
}
|
||||
|
||||
// SetWatching sets status to Watching
|
||||
func (incident *Incident) SetWatching() {
|
||||
incident.Status = "3"
|
||||
incident.HumanStatus = "Watching"
|
||||
incident.Status = 3
|
||||
}
|
||||
|
||||
// SetFixed sets status to Fixed
|
||||
func (incident *Incident) SetFixed() {
|
||||
incident.Status = "4"
|
||||
incident.HumanStatus = "Fixed"
|
||||
incident.Status = 4
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user