Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove invalid alert_grouping_parameters fields after strict check #888

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func resourcePagerDutyService() *schema.Resource {
Delete: resourcePagerDutyServiceDelete,
CustomizeDiff: customizePagerDutyServiceDiff,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -419,12 +419,14 @@ func buildServiceStruct(d *schema.ResourceData) (*pagerduty.Service, error) {
ag := attr.(string)
service.AlertGrouping = &ag
}

if attr, ok := d.GetOk("alert_grouping_parameters"); ok {
service.AlertGroupingParameters = expandAlertGroupingParameters(attr)
} else {
// Clear AlertGroupingParameters as it takes precedence over AlertGrouping and AlertGroupingTimeout which are apparently deprecated (that's not explicitly documented in the API)
service.AlertGroupingParameters = nil
}

if attr, ok := d.GetOk("alert_grouping_timeout"); ok {
if attr.(string) != "null" {
if val, err := strconv.Atoi(attr.(string)); err == nil {
Expand Down Expand Up @@ -598,11 +600,15 @@ func flattenService(d *schema.ResourceData, service *pagerduty.Service) error {
} else {
d.Set("alert_grouping_timeout", strconv.Itoa(*service.AlertGroupingTimeout))
}
if service.AlertGroupingParameters != nil {

_, hasGrouping := d.GetOk("alert_grouping")
_, hasGroupingParams := d.GetOk("alert_grouping_parameters")
if service.AlertGroupingParameters != nil && (!hasGrouping && hasGroupingParams) {
if err := d.Set("alert_grouping_parameters", flattenAlertGroupingParameters(service.AlertGroupingParameters)); err != nil {
return err
}
}

if service.AutoPauseNotificationsParameters != nil {
if err := d.Set("auto_pause_notifications_parameters", flattenAutoPauseNotificationsParameters(service.AutoPauseNotificationsParameters)); err != nil {
return err
Expand Down Expand Up @@ -649,7 +655,7 @@ func expandAlertGroupingParameters(v interface{}) *pagerduty.AlertGroupingParame
alertGroupingParameters.Type = &groupingType
}

alertGroupingParameters.Config = expandAlertGroupingConfig(ragpVal["config"])
alertGroupingParameters.Config = expandAlertGroupingConfig(groupingType, ragpVal["config"])
if groupingType == "content_based" && alertGroupingParameters.Config != nil {
alertGroupingParameters.Config.Timeout = nil
}
Expand All @@ -675,32 +681,41 @@ func expandAutoPauseNotificationsParameters(v interface{}) *pagerduty.AutoPauseN
return autoPauseNotificationsParameters
}

func expandAlertGroupingConfig(v interface{}) *pagerduty.AlertGroupingConfig {
func expandAlertGroupingConfig(groupingType string, v interface{}) *pagerduty.AlertGroupingConfig {
alertGroupingConfig := &pagerduty.AlertGroupingConfig{}
rconfig := v.([]interface{})
if len(rconfig) == 0 || rconfig[0] == nil {
return nil
}
config := rconfig[0].(map[string]interface{})

alertGroupingConfig.Fields = []string{}
if val, ok := config["fields"]; ok {
for _, field := range val.([]interface{}) {
alertGroupingConfig.Fields = append(alertGroupingConfig.Fields, field.(string))
if groupingType == "time" {
if val, ok := config["timeout"]; ok {
to := val.(int)
alertGroupingConfig.Timeout = &to
}
}
if val, ok := config["aggregate"]; ok {
agg := val.(string)
alertGroupingConfig.Aggregate = &agg
}
if val, ok := config["timeout"]; ok {
to := val.(int)
alertGroupingConfig.Timeout = &to

if groupingType == "intelligent" || groupingType == "content_based" {
if val, ok := config["time_window"]; ok {
to := val.(int)
alertGroupingConfig.TimeWindow = &to
}
}
if val, ok := config["time_window"]; ok {
to := val.(int)
alertGroupingConfig.TimeWindow = &to

if groupingType == "content_based" {
alertGroupingConfig.Fields = []string{}
if val, ok := config["fields"]; ok {
for _, field := range val.([]interface{}) {
alertGroupingConfig.Fields = append(alertGroupingConfig.Fields, field.(string))
}
}
if val, ok := config["aggregate"]; ok {
agg := val.(string)
alertGroupingConfig.Aggregate = &agg
}
}

return alertGroupingConfig
}

Expand Down
60 changes: 30 additions & 30 deletions pagerduty/resource_pagerduty_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDestroy,
Steps: []resource.TestStep{
{
{ // 1
Config: testAccCheckPagerDutyServiceConfigWithAlertContentGrouping(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
Expand Down Expand Up @@ -509,11 +509,11 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
{
{ // 2
Config: testAccCheckPagerDutyServiceConfigWithAlertContentGrouping(username, email, escalationPolicy, service),
PlanOnly: true,
},
{
{ // 3
Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingUpdated(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
Expand All @@ -529,8 +529,8 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "alert_creation", "create_alerts_and_incidents"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.type", "intelligent"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
Expand All @@ -539,7 +539,7 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
{
{ // 4
Config: testAccCheckPagerDutyServiceConfigWithAlertContentGroupingUpdated(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
Expand All @@ -553,10 +553,10 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "acknowledgement_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_creation", "create_alerts_and_incidents"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.type"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.config"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.type"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
Expand All @@ -565,7 +565,7 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
{
{ // 5
Config: testAccCheckPagerDutyServiceConfigWithAlertTimeGroupingUpdated(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
Expand All @@ -583,8 +583,8 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "alert_grouping_parameters.0.type", "time"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0.timeout", "5"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
Expand All @@ -593,7 +593,7 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
{
{ // 6
Config: testAccCheckPagerDutyServiceConfigWithAlertTimeGroupingTimeoutZeroUpdated(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
Expand All @@ -611,8 +611,8 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "alert_grouping_parameters.0.type", "time"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0.timeout", "0"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
Expand All @@ -621,7 +621,7 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
{
{ // 7
Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingUpdated(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
Expand All @@ -637,8 +637,8 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "alert_creation", "create_alerts_and_incidents"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.type", "intelligent"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
Expand All @@ -647,7 +647,7 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
{
{ // 8
Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingDescriptionUpdated(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
Expand All @@ -663,8 +663,8 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "alert_creation", "create_alerts_and_incidents"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.type", "intelligent"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
Expand All @@ -673,28 +673,28 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
{
{ // 9
Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingOmittingConfig(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", service),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.type", "intelligent"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
),
},
{
{ // 10
Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingTypeNullEmptyConfigConfig(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", service),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.type"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.type"),
// resource.TestCheckNoResourceAttr(
// "pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
),
},
},
Expand Down
Loading