Skip to content

Commit

Permalink
integration: Validate that email integration has email
Browse files Browse the repository at this point in the history
Add validation that iff email integration is used that an email
address is specified. Make a plan only change to trigger an error
and a diff.
  • Loading branch information
zecke committed Sep 6, 2021
1 parent 0490db7 commit f34014a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pagerduty/resource_pagerduty_service_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ func resourcePagerDutyServiceIntegration() *schema.Resource {
Read: resourcePagerDutyServiceIntegrationRead,
Update: resourcePagerDutyServiceIntegrationUpdate,
Delete: resourcePagerDutyServiceIntegrationDelete,
CustomizeDiff: func(diff *schema.ResourceDiff, i interface{}) error {
t := diff.Get("type").(string)
if t == "generic_email_inbound_integration" && diff.Get("integration_email").(string) == "" {
return fmt.Errorf("integration_email attribute must be set for an integration type generic_email_inbound_integration")
}
return nil
},
Importer: &schema.ResourceImporter{
State: resourcePagerDutyServiceIntegrationImport,
},
Expand Down
63 changes: 63 additions & 0 deletions pagerduty/resource_pagerduty_service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pagerduty

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand Down Expand Up @@ -86,6 +87,20 @@ func TestAccPagerDutyServiceIntegrationGeneric_Basic(t *testing.T) {
"pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"),
),
},
{
Config: testAccCheckPagerDutyServiceIntegrationGenericEmail(username, email, escalationPolicy, service, serviceIntegration, ""),
PlanOnly: true,
ExpectError: regexp.MustCompile("integration_email attribute must be set for an integration type generic_email_inbound_integration"),
},
{
Config: testAccCheckPagerDutyServiceIntegrationGenericEmail(username, email, escalationPolicy, service, serviceIntegration, "user@pagerduty.com"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"pagerduty_service_integration.foo", "type", "generic_email_inbound_integration"),
),
PlanOnly: true,
ExpectNonEmptyPlan: true,
},
},
})
}
Expand Down Expand Up @@ -322,3 +337,51 @@ resource "pagerduty_service_integration" "foo" {
}
`, username, email, escalationPolicy, service, serviceIntegration)
}

func testAccCheckPagerDutyServiceIntegrationGenericEmail(username, email, escalationPolicy, service, serviceIntegration, integrationEmail string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
name = "%s"
email = "%s"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "%s"
description = "bar"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = pagerduty_user.foo.id
}
}
}
resource "pagerduty_service" "foo" {
name = "%s"
description = "bar"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = pagerduty_escalation_policy.foo.id
incident_urgency_rule {
type = "constant"
urgency = "high"
}
}
resource "pagerduty_service_integration" "foo" {
name = "%s"
service = pagerduty_service.foo.id
type = "generic_email_inbound_integration"
integration_email = "%s"
}
`, username, email, escalationPolicy, service, serviceIntegration, integrationEmail)
}

0 comments on commit f34014a

Please sign in to comment.