Skip to content

Commit

Permalink
EO - Support for incident custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
swbradshaw committed Sep 28, 2023
1 parent daed960 commit 1f0ba92
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
41 changes: 41 additions & 0 deletions pagerduty/event_orchestration_path_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ var eventOrchestrationAutomationActionObjectSchema = map[string]*schema.Schema{
},
}

var eventOrchestrationIncidentCustomFieldsObjectSchema = map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
}

var eventOrchestrationAutomationActionSchema = map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -235,6 +246,20 @@ func flattenEventOrchestrationPathVariables(v []*pagerduty.EventOrchestrationPat
return res
}

func expandEventOrchestrationPathIncidentCustomFields(v interface{}) []*pagerduty.EventOrchestrationPathIncidentCustomFieldUpdate {
res := []*pagerduty.EventOrchestrationPathIncidentCustomFieldUpdate{}

for _, eai := range v.([]interface{}) {
ea := eai.(map[string]interface{})
ext := &pagerduty.EventOrchestrationPathIncidentCustomFieldUpdate{
ID: ea["id"].(string),
Value: ea["value"].(string),
}
res = append(res, ext)
}
return res
}

func expandEventOrchestrationPathExtractions(v interface{}) []*pagerduty.EventOrchestrationPathActionExtractions {
res := []*pagerduty.EventOrchestrationPathActionExtractions{}

Expand Down Expand Up @@ -301,6 +326,22 @@ func expandEventOrchestrationAutomationActionObjects(v interface{}) []*pagerduty
return result
}


func flattenEventOrchestrationIncidentCustomFieldUpdates(v []*pagerduty.EventOrchestrationPathIncidentCustomFieldUpdate) []interface{} {
var result []interface{}

for _, i := range v {
pdaa := map[string]string{
"id": i.ID,
"value": i.Value,
}

result = append(result, pdaa)
}

return result
}

func flattenEventOrchestrationAutomationActions(v []*pagerduty.EventOrchestrationPathAutomationAction) []interface{} {
var result []interface{}

Expand Down
18 changes: 15 additions & 3 deletions pagerduty/resource_pagerduty_event_orchestration_path_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ var eventOrchestrationPathGlobalCatchAllActionsSchema = map[string]*schema.Schem
Schema: eventOrchestrationPathExtractionsSchema,
},
},
"incident_custom_field_update": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: eventOrchestrationIncidentCustomFieldsObjectSchema,
},
},
}

var eventOrchestrationPathGlobalRuleActionsSchema = buildEventOrchestrationPathGlobalRuleActionsSchema()
Expand Down Expand Up @@ -351,9 +358,10 @@ func expandGlobalPathCatchAll(v interface{}) *pagerduty.EventOrchestrationPathCa

func expandGlobalPathActions(v interface{}) *pagerduty.EventOrchestrationPathRuleActions {
var actions = &pagerduty.EventOrchestrationPathRuleActions{
AutomationActions: []*pagerduty.EventOrchestrationPathAutomationAction{},
Variables: []*pagerduty.EventOrchestrationPathActionVariables{},
Extractions: []*pagerduty.EventOrchestrationPathActionExtractions{},
AutomationActions: []*pagerduty.EventOrchestrationPathAutomationAction{},
Variables: []*pagerduty.EventOrchestrationPathActionVariables{},
Extractions: []*pagerduty.EventOrchestrationPathActionExtractions{},
IncidentCustomFieldUpdates: []*pagerduty.EventOrchestrationPathIncidentCustomFieldUpdate{},
}

for _, i := range v.([]interface{}) {
Expand All @@ -373,6 +381,7 @@ func expandGlobalPathActions(v interface{}) *pagerduty.EventOrchestrationPathRul
actions.AutomationActions = expandEventOrchestrationPathAutomationActions(a["automation_action"])
actions.Variables = expandEventOrchestrationPathVariables(a["variable"])
actions.Extractions = expandEventOrchestrationPathExtractions(a["extraction"])
actions.IncidentCustomFieldUpdates = expandEventOrchestrationPathIncidentCustomFields(a["incident_custom_field_update"])
}

return actions
Expand Down Expand Up @@ -450,6 +459,9 @@ func flattenGlobalPathActions(actions *pagerduty.EventOrchestrationPathRuleActio
if actions.AutomationActions != nil {
flattenedAction["automation_action"] = flattenEventOrchestrationAutomationActions(actions.AutomationActions)
}
if actions.IncidentCustomFieldUpdates != nil {
flattenedAction["incident_custom_field_update"] = flattenEventOrchestrationIncidentCustomFieldUpdates(actions.IncidentCustomFieldUpdates)
}

actionsMap = append(actionsMap, flattenedAction)

Expand Down
12 changes: 12 additions & 0 deletions pagerduty/resource_pagerduty_event_orchestration_path_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ var eventOrchestrationPathServiceCatchAllActionsSchema = map[string]*schema.Sche
Schema: eventOrchestrationPathExtractionsSchema,
},
},
"incident_custom_field_update": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: eventOrchestrationIncidentCustomFieldsObjectSchema,
},
},
}

var eventOrchestrationPathServiceRuleActionsSchema = buildEventOrchestrationPathServiceRuleActionsSchema()
Expand Down Expand Up @@ -447,6 +454,7 @@ func expandServicePathActions(v interface{}) *pagerduty.EventOrchestrationPathRu
PagerdutyAutomationActions: []*pagerduty.EventOrchestrationPathPagerdutyAutomationAction{},
Variables: []*pagerduty.EventOrchestrationPathActionVariables{},
Extractions: []*pagerduty.EventOrchestrationPathActionExtractions{},
IncidentCustomFieldUpdates: []*pagerduty.EventOrchestrationPathIncidentCustomFieldUpdate{},
}

for _, i := range v.([]interface{}) {
Expand All @@ -466,6 +474,7 @@ func expandServicePathActions(v interface{}) *pagerduty.EventOrchestrationPathRu
actions.AutomationActions = expandEventOrchestrationPathAutomationActions(a["automation_action"])
actions.Variables = expandEventOrchestrationPathVariables(a["variable"])
actions.Extractions = expandEventOrchestrationPathExtractions(a["extraction"])
actions.IncidentCustomFieldUpdates = expandEventOrchestrationPathIncidentCustomFields(a["incident_custom_field_update"])
}

return actions
Expand Down Expand Up @@ -560,6 +569,9 @@ func flattenServicePathActions(actions *pagerduty.EventOrchestrationPathRuleActi
if actions.AutomationActions != nil {
flattenedAction["automation_action"] = flattenEventOrchestrationAutomationActions(actions.AutomationActions)
}
if actions.IncidentCustomFieldUpdates != nil {
flattenedAction["incident_custom_field_update"] = flattenEventOrchestrationIncidentCustomFieldUpdates(actions.IncidentCustomFieldUpdates)
}

actionsMap = append(actionsMap, flattenedAction)

Expand Down
22 changes: 19 additions & 3 deletions website/docs/r/event_orchestration_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@ resource "pagerduty_team" "engineering" {
resource "pagerduty_user" "example" {
name = "Earline Greenholt"
email = "[email protected]"
teams = [pagerduty_team.engineering.id]
}
resource "pagerduty_escalation_policy" "foo" {
resource "pagerduty_team_membership" "foo" {
user_id = pagerduty_user.example.id
team_id = pagerduty_team.engineering.id
role = "manager"
}
resource "pagerduty_escalation_policy" "example" {
name = "Engineering Escalation Policy"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user"
type = "user_reference"
id = pagerduty_user.example.id
}
}
Expand All @@ -55,6 +60,12 @@ resource "pagerduty_service" "example" {
alert_creation = "create_alerts_and_incidents"
}
resource "pagerduty_incident_custom_field" "cs_impact" {
name = "impact"
data_type = "string"
field_type = "single_value"
}
data "pagerduty_priority" "p1" {
name = "P1"
}
Expand Down Expand Up @@ -99,6 +110,10 @@ resource "pagerduty_event_orchestration_service" "www" {
actions {
annotate = "Please use our P1 runbook: https://docs.test/p1-runbook"
priority = data.pagerduty_priority.p1.id
incident_custom_field_update {
id = pagerduty_incident_custom_field.cs_impact.id
value = "High Impact"
}
}
}
rule {
Expand Down Expand Up @@ -170,6 +185,7 @@ The following arguments are supported:
* `suspend` - (Optional) The number of seconds to suspend the resulting alert before triggering. This effectively pauses incident notifications. If a `resolve` event arrives before the alert triggers then PagerDuty won't create an incident for this alert.
* `priority` - (Optional) The ID of the priority you want to set on resulting incident. Consider using the [`pagerduty_priority`](https://registry.terraform.io/providers/PagerDuty/pagerduty/latest/docs/data-sources/priority) data source.
* `annotate` - (Optional) Add this text as a note on the resulting incident.
* `incident_custom_field_update` - (Optional) Assign a custom field to the resulting incident.
* `pagerduty_automation_action` - (Optional) Configure a [Process Automation](https://support.pagerduty.com/docs/event-orchestration#process-automation) associated with the resulting incident.
* `action_id` - (Required) Id of the Process Automation action to be triggered.
* `automation_action` - (Optional) Create a [Webhook](https://support.pagerduty.com/docs/event-orchestration#webhooks) associated with the resulting incident.
Expand Down

0 comments on commit 1f0ba92

Please sign in to comment.