Skip to content

Commit

Permalink
wip: handling possible inputs for Alert Grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
imjaroiswebdev committed Nov 24, 2023
1 parent 1223039 commit 5840657
Showing 1 changed file with 69 additions and 29 deletions.
98 changes: 69 additions & 29 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,11 @@ import (

func resourcePagerDutyService() *schema.Resource {
return &schema.Resource{
Create: resourcePagerDutyServiceCreate,
Read: resourcePagerDutyServiceRead,
Update: resourcePagerDutyServiceUpdate,
Delete: resourcePagerDutyServiceDelete,
CustomizeDiff: func(context context.Context, diff *schema.ResourceDiff, i interface{}) error {
in := diff.Get("incident_urgency_rule.#").(int)
for i := 0; i <= in; i++ {
t := diff.Get(fmt.Sprintf("incident_urgency_rule.%d.type", i)).(string)
if t == "use_support_hours" && diff.Get(fmt.Sprintf("incident_urgency_rule.%d.urgency", i)).(string) != "" {
return fmt.Errorf("general urgency cannot be set for a use_support_hours incident urgency rule type")
}
}

// Due to alert_grouping_parameters.type = null is a valid configuration
// for disabling Service's Alert Grouping configuration and having an
// empty alert_grouping_parameters.config block is also valid, API ignore
// this input fields, and turns out that API response for Service
// configuration doesn't bring a representation of this HCL, which leads
// to a permadiff, described in
// https://github.com/PagerDuty/terraform-provider-pagerduty/issues/700
//
// So, bellow is the formated representation alert_grouping_parameters
// value when this permadiff appears and must be ignored.
ignoreThisAlertGroupingParamsConfigDiff := `[]interface {}{map[string]interface {}{"config":[]interface {}{interface {}(nil)}, "type":""}}`
if agpdiff, ok := diff.Get("alert_grouping_parameters").([]interface{}); ok && diff.NewValueKnown("alert_grouping_parameters") && fmt.Sprintf("%#v", agpdiff) == ignoreThisAlertGroupingParamsConfigDiff {
diff.Clear("alert_grouping_parameters")
}
return nil
},
Create: resourcePagerDutyServiceCreate,
Read: resourcePagerDutyServiceRead,
Update: resourcePagerDutyServiceUpdate,
Delete: resourcePagerDutyServiceDelete,
CustomizeDiff: customizePagerDutyServiceDiff,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -122,6 +98,7 @@ func resourcePagerDutyService() *schema.Resource {
"time_window": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"fields": {
Type: schema.TypeList,
Expand Down Expand Up @@ -331,6 +308,69 @@ func resourcePagerDutyService() *schema.Resource {
}
}

func customizePagerDutyServiceDiff(context context.Context, diff *schema.ResourceDiff, i interface{}) error {
in := diff.Get("incident_urgency_rule.#").(int)
for i := 0; i <= in; i++ {
t := diff.Get(fmt.Sprintf("incident_urgency_rule.%d.type", i)).(string)
if t == "use_support_hours" && diff.Get(fmt.Sprintf("incident_urgency_rule.%d.urgency", i)).(string) != "" {
return fmt.Errorf("general urgency cannot be set for a use_support_hours incident urgency rule type")
}
}

// Due to alert_grouping_parameters.type = null is a valid configuration
// for disabling Service's Alert Grouping configuration and having an
// empty alert_grouping_parameters.config block is also valid, API ignore
// this input fields, and turns out that API response for Service
// configuration doesn't bring a representation of this HCL, which leads
// to a permadiff, described in
// https://github.com/PagerDuty/terraform-provider-pagerduty/issues/700
//
// So, bellow is the formated representation alert_grouping_parameters
// value when this permadiff appears and must be ignored.
ignoreThisAlertGroupingParamsConfigDiff := `[]interface {}{map[string]interface {}{"config":[]interface {}{interface {}(nil)}, "type":""}}`
if agpdiff, ok := diff.Get("alert_grouping_parameters").([]interface{}); ok && diff.NewValueKnown("alert_grouping_parameters") && fmt.Sprintf("%#v", agpdiff) == ignoreThisAlertGroupingParamsConfigDiff {
diff.Clear("alert_grouping_parameters")
}

if agpType, ok := diff.Get("alert_grouping_parameters.0.type").(string); ok {
_, isTimeoutSet := diff.GetOkExists("alert_grouping_parameters.0.config.0.timeout")
aggregateVal, isAggregateSet := diff.GetOkExists("alert_grouping_parameters.0.config.0.aggregate")
fieldsVal, isFieldsSet := diff.GetOkExists("alert_grouping_parameters.0.config.0.fields")
_, isTimeWindowSet := diff.GetOkExists("alert_grouping_parameters.0.config.0.time_window")

// alert_grouping_parameters.type = time
// Required -> config.timeout
if agpType == "time" {
if isAggregateSet || isFieldsSet || isTimeWindowSet {
return fmt.Errorf("Alert grouping parameters configuration of type \"time\" only supports setting \"timeout\" attribute")
}
if !isTimeoutSet {
return fmt.Errorf("When using Alert grouping parameters configuration of type \"time\" is in use, attribute \"timeout\" is required")
}
}
// alert_grouping_parameters.type = content_based
// Required -> config.aggregate
// Required -> config.fields
if agpType == "content_based" {
if isTimeoutSet || isTimeWindowSet {
return fmt.Errorf("Alert grouping parameters configuration of type \"content_based\" only supports setting \"aggregate\" and \"fields\" attributes")
}
if !isAggregateSet || !isFieldsSet || aggregateVal == "" || len(fieldsVal.([]interface{})) == 0 {
return fmt.Errorf("When using Alert grouping parameters configuration of type \"content_based\" is in use, attributes \"aggregate\" and \"fields\" are required")
}
}
// alert_grouping_parameters.type = intelligent
// Optional -> config.time_window
if agpType == "intelligent" {
if isAggregateSet || isFieldsSet || isTimeoutSet {
return fmt.Errorf("Alert grouping parameters configuration of type \"intelligent\" only supports setting the optional \"time_window\" attribute")
}
}
}

return nil
}

func buildServiceStruct(d *schema.ResourceData) (*pagerduty.Service, error) {
service := pagerduty.Service{
Name: d.Get("name").(string),
Expand Down

0 comments on commit 5840657

Please sign in to comment.