-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #596 from jedelson-pagerduty/issue/incident-workflows
implementation of incident workflow and triggers
- Loading branch information
Showing
25 changed files
with
2,361 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
pagerduty/import_pagerduty_incident_workflow_trigger_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.