Skip to content

Commit

Permalink
implementation of incident workflow and triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelson-pagerduty committed Dec 22, 2022
1 parent 90bed94 commit 5adadf1
Show file tree
Hide file tree
Showing 24 changed files with 2,394 additions and 60 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ go 1.16

require (
cloud.google.com/go v0.71.0 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/heimweh/go-pagerduty v0.0.0-20221219143240-8b7c456b23c6
github.com/klauspost/compress v1.15.9 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
go.mongodb.org/mongo-driver v1.10.2 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sync v0.0.0-20220907140024-f12130a52804 // indirect
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd // indirect
google.golang.org/api v0.35.0 // indirect
google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb // indirect
google.golang.org/grpc v1.33.2 // indirect
)

replace github.com/heimweh/go-pagerduty => ../go-pagerduty
79 changes: 79 additions & 0 deletions pagerduty/data_source_pagerduty_incident_workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package pagerduty

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)

func dataSourcePagerDutyIncidentWorkflow() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePagerDutyIncidentWorkflowRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourcePagerDutyIncidentWorkflowRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := meta.(*Config).Client()
if err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] Reading PagerDuty incident workflow")

searchName := d.Get("name").(string)

err = resource.RetryContext(ctx, 5*time.Minute, func() *resource.RetryError {
resp, _, err := client.IncidentWorkflows.ListContext(ctx, &pagerduty.ListIncidentWorkflowOptions{})
if err != nil {
// Delaying retry by 30s as recommended by PagerDuty
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit
time.Sleep(30 * time.Second)
return resource.RetryableError(err)
}

var found *pagerduty.IncidentWorkflow

for _, iw := range resp.IncidentWorkflows {
if iw.Name == searchName {
found = iw
break
}
}

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("unable to locate any incident workflow with name: %s", searchName),
)
}

err = flattenIncidentWorkflow(d, found, false, nil)
if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if err != nil {
return diag.FromErr(err)
}
return nil

}
78 changes: 78 additions & 0 deletions pagerduty/data_source_pagerduty_incident_workflow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package pagerduty

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourcePagerDutyIncidentWorkflow(t *testing.T) {
name := fmt.Sprintf("tf-%s", acctest.RandString(5))
dataSourceName := fmt.Sprintf("data.pagerduty_incident_workflow.%s", name)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckIncidentWorkflows(t)
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyIncidentWorkflowConfig(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
resource.TestCheckResourceAttr(dataSourceName, "name", name),
),
},
},
})
}

func testAccDataSourcePagerDutyIncidentWorkflowConfig(name string) string {
return fmt.Sprintf(`
resource "pagerduty_incident_workflow" "input" {
name = "%[1]s"
}
data "pagerduty_incident_workflow" "%[1]s" {
depends_on = [
pagerduty_incident_workflow.input
]
name = "%[1]s"
}
`, name)
}

func TestAccDataSourcePagerDutyIncidentWorkflow_Missing(t *testing.T) {
name := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckIncidentWorkflows(t)
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyIncidentWorkflowConfigBad(name),
ExpectError: regexp.MustCompile(fmt.Sprintf("unable to locate any incident workflow with name: %s-incorrect", name)),
},
},
})
}

func testAccDataSourcePagerDutyIncidentWorkflowConfigBad(name string) string {
return fmt.Sprintf(`
resource "pagerduty_incident_workflow" "input" {
name = "%[1]s"
}
data "pagerduty_incident_workflow" "%[1]s" {
name = "%[1]s-incorrect"
}
`, name)

}
42 changes: 42 additions & 0 deletions pagerduty/import_pagerduty_incident_workflow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pagerduty

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccPagerDutyIncidentWorkflow_import(t *testing.T) {
workflowName := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckIncidentWorkflows(t)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckPagerDutyIncidentWorkflowDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyIncidentWorkflowConfigNoSteps(workflowName),
},

{
ResourceName: "pagerduty_incident_workflow.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckPagerDutyIncidentWorkflowConfigNoSteps(name string) string {
return fmt.Sprintf(`
resource "pagerduty_incident_workflow" "test" {
name = "%s"
description = "some description"
}
`, name)
}
37 changes: 37 additions & 0 deletions pagerduty/import_pagerduty_incident_workflow_trigger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pagerduty

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccPagerDutyIncidentWorkflowTrigger_import(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
service := fmt.Sprintf("tf-%s", acctest.RandString(5))
workflow := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckIncidentWorkflows(t)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckPagerDutyIncidentWorkflowDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyIncidentWorkflowTriggerConfigManualSingleService(username, email, escalationPolicy, service, workflow),
},

{
ResourceName: "pagerduty_incident_workflow_trigger.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
3 changes: 3 additions & 0 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func Provider() *schema.Provider {
"pagerduty_tag": dataSourcePagerDutyTag(),
"pagerduty_event_orchestration": dataSourcePagerDutyEventOrchestration(),
"pagerduty_automation_actions_runner": dataSourcePagerDutyAutomationActionsRunner(),
"pagerduty_incident_workflow": dataSourcePagerDutyIncidentWorkflow(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -97,6 +98,8 @@ func Provider() *schema.Provider {
"pagerduty_automation_actions_runner": resourcePagerDutyAutomationActionsRunner(),
"pagerduty_automation_actions_action": resourcePagerDutyAutomationActionsAction(),
"pagerduty_automation_actions_action_team_association": resourcePagerDutyAutomationActionsActionTeamAssociation(),
"pagerduty_incident_workflow": resourcePagerDutyIncidentWorkflow(),
"pagerduty_incident_workflow_trigger": resourcePagerDutyIncidentWorkflowTrigger(),
},
}

Expand Down
6 changes: 6 additions & 0 deletions pagerduty/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ import (

var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider
var testAccProviderFactories map[string]func() (*schema.Provider, error)

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"pagerduty": testAccProvider,
}
testAccProviderFactories = map[string]func() (*schema.Provider, error){
"pagerduty": func() (*schema.Provider, error) {
return testAccProvider, nil
},
}
}

func TestProvider(t *testing.T) {
Expand Down
Loading

0 comments on commit 5adadf1

Please sign in to comment.