diff --git a/pagerduty/resource_pagerduty_service.go b/pagerduty/resource_pagerduty_service.go index 1473f3368..425c234a8 100644 --- a/pagerduty/resource_pagerduty_service.go +++ b/pagerduty/resource_pagerduty_service.go @@ -1,6 +1,7 @@ package pagerduty import ( + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" "log" "strconv" "time" @@ -68,8 +69,12 @@ func resourcePagerDutyService() *schema.Resource { Computed: true, }, "status": { - Type: schema.TypeString, - Computed: true, + Type: schema.TypeString, + Optional: true, + ForceNew: false, + Default: "active", + ValidateFunc: validation.StringInSlice([]string{"active", "disabled"}, false), + DiffSuppressFunc: suppressStatusDiffs, }, "acknowledgement_timeout": { Type: schema.TypeString, @@ -282,6 +287,8 @@ func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) er log.Printf("[INFO] Creating PagerDuty service %s", service.Name) + service.Status = d.Get("status").(string) + service, _, err = client.Services.Create(service) if err != nil { return err @@ -366,6 +373,20 @@ func resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) er return err } + if d.HasChange("status") { + oldStatusRaw, newStatusRaw := d.GetChange("status") + oldStatus := oldStatusRaw.(string) + newStatus := newStatusRaw.(string) + + if newStatus == "active" && oldStatus == "disabled" { + // Change of active status + service.Status = "active" + } else { + // New status has to be changed to disabled + service.Status = "disabled" + } + } + log.Printf("[INFO] Updating PagerDuty service %s", d.Id()) if _, _, err := client.Services.Update(d.Id(), service); err != nil { @@ -559,3 +580,13 @@ func flattenScheduledActionAt(v *pagerduty.At) []interface{} { at := map[string]interface{}{"type": v.Type, "name": v.Name} return []interface{}{at} } + +// allow status listed here even though we only allow 'active' and 'disabled' +// https://developer.pagerduty.com/api-reference/reference/REST/openapiv3.json/paths/~1services~1%7Bid%7D/put +func suppressStatusDiffs(k, old, new string, d *schema.ResourceData) bool { + if (old == "warning" || old == "critical" || old == "maintenance") && new == "active" { + return true + } + + return false +} diff --git a/pagerduty/resource_pagerduty_service_test.go b/pagerduty/resource_pagerduty_service_test.go index 9037fa7c7..02e04ced3 100644 --- a/pagerduty/resource_pagerduty_service_test.go +++ b/pagerduty/resource_pagerduty_service_test.go @@ -67,6 +67,8 @@ func TestAccPagerDutyService_Basic(t *testing.T) { "pagerduty_service.foo", "name", service), resource.TestCheckResourceAttr( "pagerduty_service.foo", "description", "foo"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "status", "active"), resource.TestCheckResourceAttr( "pagerduty_service.foo", "auto_resolve_timeout", "1800"), resource.TestCheckResourceAttr( @@ -95,6 +97,8 @@ func TestAccPagerDutyService_Basic(t *testing.T) { "pagerduty_service.foo", "name", serviceUpdated), resource.TestCheckResourceAttr( "pagerduty_service.foo", "description", "bar"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "status", "active"), resource.TestCheckResourceAttr( "pagerduty_service.foo", "auto_resolve_timeout", "3600"), resource.TestCheckResourceAttr( @@ -127,6 +131,99 @@ func TestAccPagerDutyService_Basic(t *testing.T) { }) } +func TestAccPagerDutyService_WithStatus(t *testing.T) { + username := fmt.Sprintf("tf-%s", acctest.RandString(5)) + email := fmt.Sprintf("%s@foo.com", username) + escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5)) + service := fmt.Sprintf("tf-%s", acctest.RandString(5)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPagerDutyServiceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, service), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "name", service), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "description", "foo"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "status", "active"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "auto_resolve_timeout", "1800"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "acknowledgement_timeout", "1800"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "alert_creation", "create_incidents"), + resource.TestCheckNoResourceAttr( + "pagerduty_service.foo", "alert_grouping"), + resource.TestCheckNoResourceAttr( + "pagerduty_service.foo", "alert_grouping_timeout"), + 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.TestCheckResourceAttrSet( + "pagerduty_service.foo", "html_url"), + ), + }, + { + Config: testAccCheckPagerDutyServiceWithStatus(username, email, escalationPolicy, service, "disabled"), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "name", service), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "description", "bar"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "status", "disabled"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "auto_resolve_timeout", "3600"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "acknowledgement_timeout", "3600"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "alert_creation", "create_incidents"), + 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"), + ), + }, + { + Config: testAccCheckPagerDutyServiceWithStatus(username, email, escalationPolicy, service, "active"), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "name", service), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "description", "bar"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "status", "active"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "auto_resolve_timeout", "3600"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "acknowledgement_timeout", "3600"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "alert_creation", "create_incidents"), + 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_AlertGrouping(t *testing.T) { username := fmt.Sprintf("tf-%s", acctest.RandString(5)) email := fmt.Sprintf("%s@foo.com", username) @@ -1018,3 +1115,44 @@ resource "pagerduty_service" "foo" { } `, username, email, escalationPolicy, service) } +func testAccCheckPagerDutyServiceWithStatus(username, email, escalationPolicy, service, status string) string { + return fmt.Sprintf(` +resource "pagerduty_user" "foo" { + name = "%s" + email = "%s" + color = "green" + role = "user" + job_title = "foo" + description = "foo" +} + +resource "pagerduty_escalation_policy" "foo" { + name = "%s" + 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 = "%s" + description = "foo" + auto_resolve_timeout = 1800 + acknowledgement_timeout = 1800 + escalation_policy = pagerduty_escalation_policy.foo.id + status = "%s" + + incident_urgency_rule { + type = "constant" + urgency = "high" + } + +} +`, username, email, escalationPolicy, service, status) +} diff --git a/website/docs/r/service.html.markdown b/website/docs/r/service.html.markdown index 30be1c263..5463ef602 100644 --- a/website/docs/r/service.html.markdown +++ b/website/docs/r/service.html.markdown @@ -53,6 +53,7 @@ The following arguments are supported: * `auto_resolve_timeout` - (Optional) Time in seconds that an incident is automatically resolved if left open for that long. Disabled if set to the `"null"` string. * `acknowledgement_timeout` - (Optional) Time in seconds that an incident changes to the Triggered State after being Acknowledged. Disabled if set to the `"null"` string. * `escalation_policy` - (Required) The escalation policy used by this service. + * `status` - (Optional) Whether the service is currently active. Can be either "active" or "disabled". Note that PagerDuty API has status "critical" and "maintenance" which are substates of "active", but not set-able here with Terraform. * `alert_creation` - (Optional) Must be one of two values. PagerDuty receives events from your monitoring systems and can then create incidents in different ways. Value "create_incidents" is default: events will create an incident that cannot be merged. Value "create_alerts_and_incidents" is the alternative: events will create an alert and then add it to a new incident, these incidents can be merged. This option is recommended. * `alert_grouping` - (Optional) Defines how alerts on this service will be automatically grouped into incidents. Note that the alert grouping features are available only on certain plans. If not set, each alert will create a separate incident; If value is set to `time`: All alerts within a specified duration will be grouped into the same incident. This duration is set in the `alert_grouping_timeout` setting (described below). Available on Standard, Enterprise, and Event Intelligence plans; If value is set to `intelligent` - Alerts will be intelligently grouped based on a machine learning model that looks at the alert summary, timing, and the history of grouped alerts. Available on Enterprise and Event Intelligence plan. * `alert_grouping_timeout` - (Optional) The duration in minutes within which to automatically group incoming alerts. This setting applies only when `alert_grouping` is set to `time`. To continue grouping alerts until the incident is resolved, set this value to `0`.