From 7edbc92ee4b44616496e3e198a2c8fd819f5e658 Mon Sep 17 00:00:00 2001 From: Scott McAllister Date: Thu, 17 Dec 2020 18:29:56 -0800 Subject: [PATCH] add service.alert_grouping_params field --- service.go | 46 +++++++++++++-------- service_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 16 deletions(-) diff --git a/service.go b/service.go index dab17e3f..531283b0 100644 --- a/service.go +++ b/service.go @@ -58,22 +58,36 @@ type IncidentUrgencyRule struct { // Service represents something you monitor (like a web service, email service, or database service). type Service struct { APIObject - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - AutoResolveTimeout *uint `json:"auto_resolve_timeout"` - AcknowledgementTimeout *uint `json:"acknowledgement_timeout"` - CreateAt string `json:"created_at,omitempty"` - Status string `json:"status,omitempty"` - LastIncidentTimestamp string `json:"last_incident_timestamp,omitempty"` - Integrations []Integration `json:"integrations,omitempty"` - EscalationPolicy EscalationPolicy `json:"escalation_policy,omitempty"` - Teams []Team `json:"teams,omitempty"` - IncidentUrgencyRule *IncidentUrgencyRule `json:"incident_urgency_rule,omitempty"` - SupportHours *SupportHours `json:"support_hours,omitempty"` - ScheduledActions []ScheduledAction `json:"scheduled_actions"` - AlertCreation string `json:"alert_creation,omitempty"` - AlertGrouping string `json:"alert_grouping,omitempty"` - AlertGroupingTimeout *uint `json:"alert_grouping_timeout,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + AutoResolveTimeout *uint `json:"auto_resolve_timeout"` + AcknowledgementTimeout *uint `json:"acknowledgement_timeout"` + CreateAt string `json:"created_at,omitempty"` + Status string `json:"status,omitempty"` + LastIncidentTimestamp string `json:"last_incident_timestamp,omitempty"` + Integrations []Integration `json:"integrations,omitempty"` + EscalationPolicy EscalationPolicy `json:"escalation_policy,omitempty"` + Teams []Team `json:"teams,omitempty"` + IncidentUrgencyRule *IncidentUrgencyRule `json:"incident_urgency_rule,omitempty"` + SupportHours *SupportHours `json:"support_hours,omitempty"` + ScheduledActions []ScheduledAction `json:"scheduled_actions"` + AlertCreation string `json:"alert_creation,omitempty"` + AlertGrouping string `json:"alert_grouping,omitempty"` + AlertGroupingTimeout *uint `json:"alert_grouping_timeout,omitempty"` + AlertGroupingParameters *AlertGroupingParameters `json:"alert_grouping_parameters,omitempty"` +} + +// AlertGroupingParameters defines how alerts on the servicewill be automatically grouped into incidents +type AlertGroupingParameters struct { + Type string `json:"type"` + Config AlertGroupParamsConfig `json:"config"` +} + +// AlertGroupParamsConfig is the config object on alert_grouping_parameters +type AlertGroupParamsConfig struct { + Timeout uint `json:"timeout,omitempty"` + Aggregate string `json:"aggregate,omitempty"` + Fields []string `json:"fields,omitempty"` } // ListServiceOptions is the data structure used when calling the ListServices API endpoint. diff --git a/service_test.go b/service_test.go index 87927feb..bc2281a7 100644 --- a/service_test.go +++ b/service_test.go @@ -104,6 +104,109 @@ func TestService_Create(t *testing.T) { testEqual(t, want, res) } +// Create Service with AlertGroupingParameters of type time +func TestService_CreateWithAlertGroupParamsTime(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + w.Write([]byte(`{"service": {"id": "1","name":"foo"}}`)) + }) + + var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient} + input := Service{ + Name: "foo", + AlertGroupingParameters: &AlertGroupingParameters{ + Type: "time", + Config: AlertGroupParamsConfig{ + Timeout: 2, + }, + }, + } + res, err := client.CreateService(input) + + want := &Service{ + APIObject: APIObject{ + ID: "1", + }, + Name: "foo", + } + + if err != nil { + t.Fatal(err) + } + testEqual(t, want, res) +} + +// Create Service with AlertGroupingParameters of type content_based +func TestService_CreateWithAlertGroupParamsContentBased(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + w.Write([]byte(`{"service": {"id": "1","name":"foo"}}`)) + }) + + var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient} + input := Service{ + Name: "foo", + AlertGroupingParameters: &AlertGroupingParameters{ + Type: "content_based", + Config: AlertGroupParamsConfig{ + Aggregate: "any", + Fields: []string{"source", "component"}, + }, + }, + } + res, err := client.CreateService(input) + + want := &Service{ + APIObject: APIObject{ + ID: "1", + }, + Name: "foo", + } + + if err != nil { + t.Fatal(err) + } + testEqual(t, want, res) +} + +// Create Service with AlertGroupingParameters of type intelligent +func TestService_CreateWithAlertGroupParamsIntelligent(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + w.Write([]byte(`{"service": {"id": "1","name":"foo"}}`)) + }) + + var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient} + input := Service{ + Name: "foo", + AlertGroupingParameters: &AlertGroupingParameters{ + Type: "intelligent", + }, + } + res, err := client.CreateService(input) + + want := &Service{ + APIObject: APIObject{ + ID: "1", + }, + Name: "foo", + } + + if err != nil { + t.Fatal(err) + } + testEqual(t, want, res) +} + // Update Service func TestService_Update(t *testing.T) { setup()