Add support for missing attributes for PagerDuty service resource (#11856)

* Add urgencies for PagerDuty services

* Improve naming, comments, handle unexpected urgency rules

* Document urgency rules for PagerDuty service
This commit is contained in:
Jahn Saito 2017-02-13 12:41:24 +01:00 committed by Paul Stack
parent 12925f5131
commit 9b2439b027
6 changed files with 859 additions and 45 deletions

View File

@ -26,3 +26,24 @@ func TestAccPagerDutyService_import(t *testing.T) {
},
})
}
func TestAccPagerDutyServiceWithIncidentUrgency_import(t *testing.T) {
resourceName := "pagerduty_service.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfig,
},
resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

View File

@ -50,6 +50,124 @@ func resourcePagerDutyService() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"incident_urgency_rule": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
"urgency": {
Type: schema.TypeString,
Optional: true,
},
"during_support_hours": {
Type: schema.TypeList,
MaxItems: 1,
MinItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
},
"urgency": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"outside_support_hours": {
Type: schema.TypeList,
MaxItems: 1,
MinItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
},
"urgency": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
"support_hours": &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 1,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
},
"time_zone": {
Type: schema.TypeString,
Optional: true,
},
"start_time": {
Type: schema.TypeString,
Optional: true,
},
"end_time": {
Type: schema.TypeString,
Optional: true,
},
"days_of_week": {
Type: schema.TypeList,
Optional: true,
MaxItems: 7,
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
},
},
"scheduled_actions": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
},
"to_urgency": {
Type: schema.TypeString,
Optional: true,
},
"at": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
},
}
}
@ -86,6 +204,18 @@ func buildServiceStruct(d *schema.ResourceData) *pagerduty.Service {
service.EscalationPolicy = *escalationPolicy
if attr, ok := d.GetOk("incident_urgency_rule"); ok {
if iur, ok := expandIncidentUrgencyRule(attr); ok {
service.IncidentUrgencyRule = iur
}
}
if attr, ok := d.GetOk("support_hours"); ok {
service.SupportHours = expandSupportHours(attr)
}
if attr, ok := d.GetOk("scheduled_actions"); ok {
service.ScheduledActions = expandScheduledActions(attr)
}
return &service
}
@ -133,6 +263,16 @@ func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) erro
d.Set("last_incident_timestamp", service.LastIncidentTimestamp)
d.Set("acknowledgement_timeout", service.AcknowledgementTimeout)
if incidentUrgencyRule, ok := flattenIncidentUrgencyRule(service); ok {
d.Set("incident_urgency_rule", incidentUrgencyRule)
}
supportHours := flattenSupportHours(service)
d.Set("support_hours", supportHours)
scheduledActions := flattenScheduledActions(service)
d.Set("scheduled_actions", scheduledActions)
return nil
}
@ -168,5 +308,6 @@ func resourcePagerDutyServiceImport(d *schema.ResourceData, meta interface{}) ([
if err := resourcePagerDutyServiceRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

View File

@ -117,6 +117,11 @@ resource "pagerduty_service" "foo" {
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
incident_urgency_rule {
type = "constant"
urgency = "high"
}
}
data "pagerduty_vendor" "datadog" {
@ -162,6 +167,11 @@ resource "pagerduty_service" "foo" {
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
incident_urgency_rule {
type = "constant"
urgency = "high"
}
}
data "pagerduty_vendor" "datadog" {

View File

@ -27,6 +27,12 @@ func TestAccPagerDutyService_Basic(t *testing.T) {
"pagerduty_service.foo", "auto_resolve_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
resource.TestStep{
@ -38,9 +44,241 @@ func TestAccPagerDutyService_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "description", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_resolve_timeout", "0"),
"pagerduty_service.foo", "auto_resolve_timeout", "3600"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "0"),
"pagerduty_service.foo", "acknowledgement_timeout", "3600"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
},
})
}
func TestAccPagerDutyService_BasicWithIncidentUrgencyRules(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "description", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_resolve_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.type", "constant"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.type", "constant"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.urgency", "low"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.type", "use_support_hours"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.at.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.at.0.name", "support_hours_start"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.to_urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.type", "urgency_change"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.#", "5"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.0", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.1", "2"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.2", "3"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.3", "4"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.4", "5"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.end_time", "17:00:00"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.start_time", "09:00:00"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.time_zone", "America/Lima"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.type", "fixed_time_per_day"),
),
},
resource.TestStep{
Config: testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfigUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "description", "bar bar bar"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_resolve_timeout", "3600"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "3600"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.type", "constant"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.type", "constant"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.urgency", "low"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.type", "use_support_hours"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.at.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.at.0.name", "support_hours_start"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.to_urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.type", "urgency_change"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.#", "5"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.0", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.1", "2"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.2", "3"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.3", "4"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.4", "5"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.end_time", "17:00:00"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.start_time", "09:00:00"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.time_zone", "America/Lima"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.type", "fixed_time_per_day"),
),
},
},
})
}
func TestAccPagerDutyService_FromBasicToCustomIncidentUrgencyRules(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPagerDutyServiceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "description", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_resolve_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
resource.TestStep{
Config: testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfigUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "description", "bar bar bar"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_resolve_timeout", "3600"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "3600"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.type", "constant"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.during_support_hours.0.urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.type", "constant"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.outside_support_hours.0.urgency", "low"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.type", "use_support_hours"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.at.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.at.0.name", "support_hours_start"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.to_urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "scheduled_actions.0.type", "urgency_change"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.#", "5"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.0", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.1", "2"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.2", "3"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.3", "4"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.days_of_week.4", "5"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.end_time", "17:00:00"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.start_time", "09:00:00"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.time_zone", "America/Lima"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "support_hours.0.type", "fixed_time_per_day"),
),
},
},
@ -70,6 +308,7 @@ func testAccCheckPagerDutyServiceExists(n string) resource.TestCheckFunc {
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Service ID is set")
}
@ -91,66 +330,199 @@ func testAccCheckPagerDutyServiceExists(n string) resource.TestCheckFunc {
const testAccCheckPagerDutyServiceConfig = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
name = "foo"
email = "foo@example.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
name = "bar"
description = "bar"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "foo"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
name = "foo"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
incident_urgency_rule {
type = "constant"
urgency = "high"
}
}
`
const testAccCheckPagerDutyServiceConfigUpdated = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
name = "foo"
email = "foo@example.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
name = "bar"
description = "bar"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "bar"
description = "bar"
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
name = "bar"
description = "bar"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
incident_urgency_rule {
type = "constant"
urgency = "high"
}
}
`
const testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfig = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@example.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "foo"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
incident_urgency_rule {
type = "use_support_hours"
during_support_hours {
type = "constant"
urgency = "high"
}
outside_support_hours {
type = "constant"
urgency = "low"
}
}
support_hours = [{
type = "fixed_time_per_day"
time_zone = "America/Lima"
start_time = "09:00:00"
end_time = "17:00:00"
days_of_week = [ 1, 2, 3, 4, 5 ]
}]
scheduled_actions {
type = "urgency_change"
to_urgency = "high"
at {
type = "named_time",
name = "support_hours_start"
}
}
}
`
const testAccCheckPagerDutyServiceWithIncidentUrgencyRulesConfigUpdated = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@example.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
}
resource "pagerduty_service" "foo" {
name = "bar"
description = "bar bar bar"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
incident_urgency_rule {
type = "use_support_hours"
during_support_hours {
type = "constant"
urgency = "high"
}
outside_support_hours {
type = "constant"
urgency = "low"
}
}
support_hours = [{
type = "fixed_time_per_day"
time_zone = "America/Lima"
start_time = "09:00:00"
end_time = "17:00:00"
days_of_week = [ 1, 2, 3, 4, 5 ]
}]
scheduled_actions {
type = "urgency_change"
to_urgency = "high"
at {
type = "named_time",
name = "support_hours_start"
}
}
}
`

View File

@ -184,3 +184,207 @@ func expandStringList(configured []interface{}) []string {
}
return vs
}
// Expands attribute slice to incident urgency rule, returns it and true if successful
func expandIncidentUrgencyRule(incidentUrgencyList interface{}) (*pagerduty.IncidentUrgencyRule, bool) {
i := incidentUrgencyList.([]interface{})
i, ok := incidentUrgencyList.([]interface{})
if !ok {
return nil, false
}
m, ok := i[0].(map[string]interface{})
if !ok || len(m) == 0 {
return nil, false
}
iur := pagerduty.IncidentUrgencyRule{}
if val, ok := m["type"]; ok {
iur.Type = val.(string)
}
if val, ok := m["urgency"]; ok {
iur.Urgency = val.(string)
}
if val, ok := m["during_support_hours"]; ok {
iur.DuringSupportHours = expandIncidentUrgencyType(val)
}
if val, ok := m["outside_support_hours"]; ok {
iur.OutsideSupportHours = expandIncidentUrgencyType(val)
}
return &iur, true
}
// Expands attribute to inline model
func expandActionInlineModel(inlineModelVal interface{}) *pagerduty.InlineModel {
inlineModel := pagerduty.InlineModel{}
if slice, ok := inlineModelVal.([]interface{}); ok && len(slice) == 1 {
m := slice[0].(map[string]interface{})
if val, ok := m["type"]; ok {
inlineModel.Type = val.(string)
}
if val, ok := m["name"]; ok {
inlineModel.Name = val.(string)
}
}
return &inlineModel
}
// Expands attribute into incident urgency type
func expandIncidentUrgencyType(attribute interface{}) *pagerduty.IncidentUrgencyType {
ict := pagerduty.IncidentUrgencyType{}
slice := attribute.([]interface{})
if len(slice) != 1 {
return &ict
}
m := slice[0].(map[string]interface{})
if val, ok := m["type"]; ok {
ict.Type = val.(string)
}
if val, ok := m["urgency"]; ok {
ict.Urgency = val.(string)
}
return &ict
}
// Returns service's incident urgency rule as slice of length one and bool indicating success
func flattenIncidentUrgencyRule(service *pagerduty.Service) ([]interface{}, bool) {
if service.IncidentUrgencyRule.Type == "" && service.IncidentUrgencyRule.Urgency == "" {
return nil, false
}
m := map[string]interface{}{
"type": service.IncidentUrgencyRule.Type,
"urgency": service.IncidentUrgencyRule.Urgency,
}
if dsh := service.IncidentUrgencyRule.DuringSupportHours; dsh != nil {
m["during_support_hours"] = flattenIncidentUrgencyType(dsh)
}
if osh := service.IncidentUrgencyRule.OutsideSupportHours; osh != nil {
m["outside_support_hours"] = flattenIncidentUrgencyType(osh)
}
return []interface{}{m}, true
}
func flattenIncidentUrgencyType(iut *pagerduty.IncidentUrgencyType) []interface{} {
incidenUrgencyType := map[string]interface{}{
"type": iut.Type,
"urgency": iut.Urgency,
}
return []interface{}{incidenUrgencyType}
}
// Expands attribute to support hours
func expandSupportHours(attribute interface{}) (sh *pagerduty.SupportHours) {
if slice, ok := attribute.([]interface{}); ok && len(slice) >= 1 {
m := slice[0].(map[string]interface{})
sh = &pagerduty.SupportHours{}
if val, ok := m["type"]; ok {
sh.Type = val.(string)
}
if val, ok := m["time_zone"]; ok {
sh.Timezone = val.(string)
}
if val, ok := m["start_time"]; ok {
sh.StartTime = val.(string)
}
if val, ok := m["end_time"]; ok {
sh.EndTime = val.(string)
}
if val, ok := m["days_of_week"]; ok {
daysOfWeekInt := val.([]interface{})
var daysOfWeek []uint
for _, i := range daysOfWeekInt {
daysOfWeek = append(daysOfWeek, uint(i.(int)))
}
sh.DaysOfWeek = daysOfWeek
}
}
return
}
// Returns service's support hours as slice of length one
func flattenSupportHours(service *pagerduty.Service) []interface{} {
if service.SupportHours == nil {
return nil
}
m := map[string]interface{}{}
if s := service.SupportHours; s != nil {
m["type"] = s.Type
m["time_zone"] = s.Timezone
m["start_time"] = s.StartTime
m["end_time"] = s.EndTime
m["days_of_week"] = s.DaysOfWeek
}
return []interface{}{m}
}
// Expands attribute to scheduled action
func expandScheduledActions(input interface{}) (scheduledActions []pagerduty.ScheduledAction) {
inputs := input.([]interface{})
for _, i := range inputs {
m := i.(map[string]interface{})
sa := pagerduty.ScheduledAction{}
if val, ok := m["type"]; ok {
sa.Type = val.(string)
}
if val, ok := m["to_urgency"]; ok {
sa.ToUrgency = val.(string)
}
if val, ok := m["at"]; ok {
sa.At = *expandActionInlineModel(val)
}
scheduledActions = append(scheduledActions, sa)
}
return scheduledActions
}
// Returns service's scheduled actions
func flattenScheduledActions(service *pagerduty.Service) []interface{} {
scheduledActions := []interface{}{}
if sas := service.ScheduledActions; sas != nil {
for _, sa := range sas {
m := map[string]interface{}{}
m["to_urgency"] = sa.ToUrgency
m["type"] = sa.Type
if at, ok := scheduledActionsAt(sa.At); ok {
m["at"] = at
}
scheduledActions = append(scheduledActions, m)
}
}
return scheduledActions
}
// Returns service's scheduled action's at attribute as slice of length one
func scheduledActionsAt(inlineModel pagerduty.InlineModel) ([]interface{}, bool) {
if inlineModel.Type == "" || inlineModel.Name == "" {
return nil, false
}
m := map[string]interface{}{"type": inlineModel.Type, "name": inlineModel.Name}
return []interface{}{m}, true
}

View File

@ -53,6 +53,72 @@ The following arguments are supported:
* `acknowledgement_timeout` - (Optional) Time in seconds that an incident changes to the Triggered State after being Acknowledged. Disabled if not set.
* `escalation_policy` - (Required) The escalation policy used by this service.
You may specify one optional `incident_urgency_rule` block configuring what urgencies to use.
Your PagerDuty account must have the `urgencies` ability to assign an incident urgency rule.
The block contains the following arguments.
* `type` - The type of incident urgency: `constant` or `use_support_hours` (when depending on specific suppor hours; see `support_hours`).
* `during_support_hours` - (Optional) Incidents' urgency during support hours.
* `outside_support_hours` - (Optional) Incidents' urgency outside of support hours.
When using `type = "use_support_hours"` in `incident_urgency_rule` you have to specify exactly one otherwise optional `support_hours` block.
Changes to `support_hours` necessitate re-creating the service resource. Account must have the `service_support_hours` ability to assign support hours.
The block contains the following arguments.
* `type` - The type of support hours. Can be `fixed_time_per_day`.
* `time_zone` - The time zone for the support hours.
* `days_of_week` - Array of days of week as integers.
* `start_time` - The support hours' starting time of day.
* `end_time` - The support hours' ending time of day.
When using `type = "use_support_hours"` in the `incident_urgency_rule` block you have to also specify `scheduled_actions` for the service. Otherwise `scheduled_actions` is optional. Changes necessitate re-createing the service resource.
* `type` - The type of scheduled action. Currently, this must be set to `urgency_change`.
* `at` - Represents when scheduled action will occur.
* `name` - Designates either the start or the end of the scheduled action. Can be `support_hours_start` or `support_hours_end`.
Below is an example for a `pagerduty_service` resource with `incident_urgency_rules` with `type = "use_support_hours"`, `support_hours` and a default `scheduled_action` as well.
```
resource "pagerduty_service" "foo" {
name = "bar"
description = "bar bar bar"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
incident_urgency_rule {
type = "use_support_hours"
during_support_hours {
type = "constant"
urgency = "high"
}
outside_support_hours {
type = "constant"
urgency = "low"
}
}
support_hours {
type = "fixed_time_per_day"
time_zone = "America/Lima"
start_time = "09:00:00"
end_time = "17:00:00"
days_of_week = [ 1, 2, 3, 4, 5 ]
}
scheduled_actions {
type = "urgency_change"
to_urgency = "high"
at {
type = "named_time",
name = "support_hours_start"
}
}
}
```
## Attributes Reference
The following attributes are exported: