Skip to content

Commit

Permalink
prevent iw triggers from trying to change type
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonw committed Nov 10, 2023
1 parent c6fd4ff commit befe99b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pagerduty/resource_pagerduty_incident_workflow_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func resourcePagerDutyIncidentWorkflowTriggerDelete(ctx context.Context, d *sche
}

func validateIncidentWorkflowTrigger(_ context.Context, d *schema.ResourceDiff, _ interface{}) error {
oldType, newType := d.GetChange("type")
if oldType != nil && oldType != "" && oldType != newType {
return fmt.Errorf("trigger type cannot be changed")
}

triggerType := d.Get("type").(string)
_, hadCondition := d.GetOk("condition")
if triggerType == "manual" && hadCondition {
Expand Down
59 changes: 51 additions & 8 deletions pagerduty/resource_pagerduty_incident_workflow_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,8 @@ func TestAccPagerDutyIncidentWorkflowTrigger_BasicConditionalAllServices(t *test
),
},
{
Config: testAccCheckPagerDutyIncidentWorkflowTriggerConfigConditionalAllServices(workflow, "incident.priority matches 'P2'"),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyIncidentWorkflowTriggerExists("pagerduty_incident_workflow_trigger.test"),
resource.TestCheckResourceAttr(
"pagerduty_incident_workflow_trigger.test", "type", "conditional"),
resource.TestCheckResourceAttr(
"pagerduty_incident_workflow_trigger.test", "condition", "incident.priority matches 'P2'"),
),
Config: testAccCheckPagerDutyIncidentWorkflowTriggerConfigManualAllServices(workflow),
ExpectError: regexp.MustCompile("trigger type cannot be changed"),
},
},
})
Expand All @@ -242,6 +236,55 @@ resource "pagerduty_incident_workflow_trigger" "test" {
`, testAccCheckPagerDutyIncidentWorkflowConfig(workflow), condition)
}

func testAccCheckPagerDutyIncidentWorkflowTriggerConfigManualAllServices(workflow string) string {
return fmt.Sprintf(`
%s
resource "pagerduty_incident_workflow_trigger" "test" {
type = "manual"
workflow = pagerduty_incident_workflow.test.id
services = []
subscribed_to_all_services = true
}
`, testAccCheckPagerDutyIncidentWorkflowConfig(workflow))
}

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

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckIncidentWorkflows(t)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckPagerDutyIncidentWorkflowTriggerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyIncidentWorkflowTriggerConfigConditionalAllServices(workflow, "incident.priority matches 'P1'"),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyIncidentWorkflowTriggerExists("pagerduty_incident_workflow_trigger.test"),
resource.TestCheckResourceAttr(
"pagerduty_incident_workflow_trigger.test", "type", "conditional"),
resource.TestCheckResourceAttr(
"pagerduty_incident_workflow_trigger.test", "condition", "incident.priority matches 'P1'"),
resource.TestCheckResourceAttr("pagerduty_incident_workflow_trigger.test", "subscribed_to_all_services", "true"),
),
},
{
Config: testAccCheckPagerDutyIncidentWorkflowTriggerConfigConditionalAllServices(workflow, "incident.priority matches 'P2'"),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyIncidentWorkflowTriggerExists("pagerduty_incident_workflow_trigger.test"),
resource.TestCheckResourceAttr(
"pagerduty_incident_workflow_trigger.test", "type", "conditional"),
resource.TestCheckResourceAttr(
"pagerduty_incident_workflow_trigger.test", "condition", "incident.priority matches 'P2'"),
),
},
},
})
}

func testAccCheckPagerDutyIncidentWorkflowTriggerDestroy(s *terraform.State) error {
client, _ := testAccProvider.Meta().(*Config).Client()
for _, r := range s.RootModule().Resources {
Expand Down

0 comments on commit befe99b

Please sign in to comment.