From befe99b2b5ac71a626739c85fc3e9e90af86987f Mon Sep 17 00:00:00 2001 From: Brandon Waskiewicz Date: Fri, 10 Nov 2023 15:33:36 -0500 Subject: [PATCH] prevent iw triggers from trying to change type --- ...rce_pagerduty_incident_workflow_trigger.go | 5 ++ ...agerduty_incident_workflow_trigger_test.go | 59 ++++++++++++++++--- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/pagerduty/resource_pagerduty_incident_workflow_trigger.go b/pagerduty/resource_pagerduty_incident_workflow_trigger.go index 0eaa50e98..891bbcd60 100644 --- a/pagerduty/resource_pagerduty_incident_workflow_trigger.go +++ b/pagerduty/resource_pagerduty_incident_workflow_trigger.go @@ -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 { diff --git a/pagerduty/resource_pagerduty_incident_workflow_trigger_test.go b/pagerduty/resource_pagerduty_incident_workflow_trigger_test.go index 3c2c444e8..ca64d5007 100644 --- a/pagerduty/resource_pagerduty_incident_workflow_trigger_test.go +++ b/pagerduty/resource_pagerduty_incident_workflow_trigger_test.go @@ -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"), }, }, }) @@ -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 {