Skip to content

Commit

Permalink
Merge pull request #596 from jedelson-pagerduty/issue/incident-workflows
Browse files Browse the repository at this point in the history
implementation of incident workflow and triggers
  • Loading branch information
imjaroiswebdev authored Dec 23, 2022
2 parents 90bed94 + 5bb78ee commit ced15e4
Show file tree
Hide file tree
Showing 25 changed files with 2,361 additions and 63 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,16 @@ Run a specific subset of tests by name use the `TESTARGS="-run TestName"` option
```sh
$ make testacc TESTARGS="-run TestAccPagerDutyTeam"
```

Some tests require additional environment variables to be set to enable them due to account restrictions on certain
features. Similarly to [`TF_ACC`](https://developer.hashicorp.com/terraform/plugin/sdkv2/testing/acceptance-tests#environment-variables),
the value of the environment variable is not relevant.

For example:
```sh
PAGERDUTY_ACC_INCIDENT_WORKFLOWS=1 make testacc TESTARGS="-run PagerDutyIncidentWorkflow"
```

| Variable Name | Feature Set |
|------------------------------------|--------------------|
| `PAGERDUTY_ACC_INCIDENT_WORKFLOWS` | Incident Workflows |
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ 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/heimweh/go-pagerduty v0.0.0-20221222221341-c1c27ca3744a
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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/heimweh/go-pagerduty v0.0.0-20221219143240-8b7c456b23c6 h1:aK7yqQASNtcxrTy4L3Gidh0EUBoDLjDVWTcZ7bzcXJY=
github.com/heimweh/go-pagerduty v0.0.0-20221219143240-8b7c456b23c6/go.mod h1:t9vftsO1IjYHGdgJXeemZtomCWnxi2SRgu0PRcRb2oY=
github.com/heimweh/go-pagerduty v0.0.0-20221222221341-c1c27ca3744a h1:U2RXTaVK7ZwWKnmaF/any3PQnBfuuZxYGQ57DP8Fvjc=
github.com/heimweh/go-pagerduty v0.0.0-20221222221341-c1c27ca3744a/go.mod h1:t9vftsO1IjYHGdgJXeemZtomCWnxi2SRgu0PRcRb2oY=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down
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 ced15e4

Please sign in to comment.