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

Allow disabling service incident timeouts (take 2) #52

Merged
merged 3 commits into from
Dec 5, 2017
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: 40 additions & 11 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pagerduty

import (
"log"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
Expand Down Expand Up @@ -36,8 +37,9 @@ func resourcePagerDutyService() *schema.Resource {
}),
},
"auto_resolve_timeout": {
Type: schema.TypeInt,
Type: schema.TypeString,
Optional: true,
Default: "14400",
},
"last_incident_timestamp": {
Type: schema.TypeString,
Expand All @@ -52,8 +54,9 @@ func resourcePagerDutyService() *schema.Resource {
Computed: true,
},
"acknowledgement_timeout": {
Type: schema.TypeInt,
Type: schema.TypeString,
Optional: true,
Default: "1800",
},
"escalation_policy": {
Type: schema.TypeString,
Expand Down Expand Up @@ -182,7 +185,7 @@ func resourcePagerDutyService() *schema.Resource {
}
}

func buildServiceStruct(d *schema.ResourceData) *pagerduty.Service {
func buildServiceStruct(d *schema.ResourceData) (*pagerduty.Service, error) {
service := pagerduty.Service{
Name: d.Get("name").(string),
Status: d.Get("status").(string),
Expand All @@ -193,11 +196,23 @@ func buildServiceStruct(d *schema.ResourceData) *pagerduty.Service {
}

if attr, ok := d.GetOk("auto_resolve_timeout"); ok {
service.AutoResolveTimeout = attr.(int)
if attr.(string) != "null" {
if val, err := strconv.Atoi(attr.(string)); err == nil {
service.AutoResolveTimeout = &val
} else {
return nil, err
}
}
}

if attr, ok := d.GetOk("acknowledgement_timeout"); ok {
service.AcknowledgementTimeout = attr.(int)
if attr.(string) != "null" {
if val, err := strconv.Atoi(attr.(string)); err == nil {
service.AcknowledgementTimeout = &val
} else {
return nil, err
}
}
}

if attr, ok := d.GetOk("alert_creation"); ok {
Expand All @@ -223,17 +238,20 @@ func buildServiceStruct(d *schema.ResourceData) *pagerduty.Service {
service.ScheduledActions = expandScheduledActions(attr)
}

return &service
return &service, nil
}

func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

service := buildServiceStruct(d)
service, err := buildServiceStruct(d)
if err != nil {
return err
}

log.Printf("[INFO] Creating PagerDuty service %s", service.Name)

service, _, err := client.Services.Create(service)
service, _, err = client.Services.Create(service)
if err != nil {
return err
}
Expand All @@ -258,9 +276,17 @@ func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) erro
d.Set("created_at", service.CreatedAt)
d.Set("escalation_policy", service.EscalationPolicy.ID)
d.Set("description", service.Description)
d.Set("auto_resolve_timeout", service.AutoResolveTimeout)
if service.AutoResolveTimeout == nil {
d.Set("auto_resolve_timeout", "null")
} else {
d.Set("auto_resolve_timeout", strconv.Itoa(*service.AutoResolveTimeout))
}
d.Set("last_incident_timestamp", service.LastIncidentTimestamp)
d.Set("acknowledgement_timeout", service.AcknowledgementTimeout)
if service.AcknowledgementTimeout == nil {
d.Set("acknowledgement_timeout", "null")
} else {
d.Set("acknowledgement_timeout", strconv.Itoa(*service.AcknowledgementTimeout))
}
d.Set("alert_creation", service.AlertCreation)

if service.IncidentUrgencyRule != nil {
Expand All @@ -287,7 +313,10 @@ func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) erro
func resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

service := buildServiceStruct(d)
service, err := buildServiceStruct(d)
if err != nil {
return err
}

log.Printf("[INFO] Updating PagerDuty service %s", d.Id())

Expand Down
8 changes: 4 additions & 4 deletions pagerduty/resource_pagerduty_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ 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", "null"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "0"),
"pagerduty_service.foo", "acknowledgement_timeout", "null"),
),
},
},
Expand Down Expand Up @@ -518,8 +518,8 @@ resource "pagerduty_escalation_policy" "foo" {
resource "pagerduty_service" "foo" {
name = "%s"
description = "bar"
auto_resolve_timeout = 0
acknowledgement_timeout = 0
auto_resolve_timeout = "null"
acknowledgement_timeout = "null"

escalation_policy = "${pagerduty_escalation_policy.foo.id}"
incident_urgency_rule {
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/heimweh/go-pagerduty/pagerduty/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,10 @@
"revisionTime": "2016-07-20T23:31:40Z"
},
{
"checksumSHA1": "XFSyecgEgORKyxI8bfX8esMIBlE=",
"checksumSHA1": "ZQ0pchJreisfmNgJq387N9kqA+w=",
"path": "github.com/heimweh/go-pagerduty/pagerduty",
"revision": "4e8f8a6e375122d5ac801cd08bb0425ab2899549",
"revisionTime": "2017-10-20T17:28:17Z"
"revision": "64f5bd2f9c706f1b708fa7cc4e511611bc307890",
"revisionTime": "2017-11-24T16:45:11Z"
},
{
"checksumSHA1": "0ZrwvB6KoGPj2PoDNSEJwxQ6Mog=",
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ The following arguments are supported:
* `name` - (Required) The name of the service.
* `description` - (Optional) A human-friendly description of the escalation policy.
If not set, a placeholder of "Managed by Terraform" will be set.
* `auto_resolve_timeout` - (Optional) Time in seconds that an incident is automatically resolved if left open for that long. Disabled if not set.
* `acknowledgement_timeout` - (Optional) Time in seconds that an incident changes to the Triggered State after being Acknowledged. Disabled if not set.
* `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.
* `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.

Expand Down