Repository: PagerDuty/go-pagerduty
Pull request #257 (2020-12-18 02:31:14)

add service.alert_grouping_params field
Adding support for new Alert Grouping Parameters fields on the Service object. 


Patch:
diff --git a/service.go b/service.go
index dab17e3..531283b 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.


Test patch:
diff --git a/service_test.go b/service_test.go
index 87927fe..bc2281a 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()