Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address: Service integration perm diff with Generic email and empty/omitted email_filter #625

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ the value of the environment variable is not relevant.
For example:
```sh
PAGERDUTY_ACC_INCIDENT_WORKFLOWS=1 make testacc TESTARGS="-run PagerDutyIncidentWorkflow"
PAGERDUTY_ACC_SERVICE_INTEGRATION_GENERIC_EMAIL_NO_FILTERS="user@<your_domain>.pagerduty.com" make testacc TESTARGS="-run TestAccPagerDutyServiceIntegration_GenericEmailNoFilters"
```

| Variable Name | Feature Set |
|------------------------------------|--------------------|
| `PAGERDUTY_ACC_INCIDENT_WORKFLOWS` | Incident Workflows |
| Variable Name | Feature Set |
| ----------------------------------------------------------- | ------------------- |
| `PAGERDUTY_ACC_INCIDENT_WORKFLOWS` | Incident Workflows |
| `PAGERDUTY_ACC_SERVICE_INTEGRATION_GENERIC_EMAIL_NO_FILTERS` | Service Integration |
92 changes: 81 additions & 11 deletions pagerduty/resource_pagerduty_service_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@ const (

func resourcePagerDutyServiceIntegration() *schema.Resource {
return &schema.Resource{
Create: resourcePagerDutyServiceIntegrationCreate,
Read: resourcePagerDutyServiceIntegrationRead,
Update: resourcePagerDutyServiceIntegrationUpdate,
Delete: resourcePagerDutyServiceIntegrationDelete,
CustomizeDiff: func(context context.Context, diff *schema.ResourceDiff, i interface{}) error {
t := diff.Get("type").(string)
if t == "generic_email_inbound_integration" && diff.Get("integration_email").(string) == "" && diff.NewValueKnown("integration_email") {
return errors.New(errEmailIntegrationMustHaveEmail)
}
return nil
},
Create: resourcePagerDutyServiceIntegrationCreate,
Read: resourcePagerDutyServiceIntegrationRead,
Update: resourcePagerDutyServiceIntegrationUpdate,
Delete: resourcePagerDutyServiceIntegrationDelete,
CustomizeDiff: customizeServiceIntegrationDiff(),
Importer: &schema.ResourceImporter{
State: resourcePagerDutyServiceIntegrationImport,
},
Expand Down Expand Up @@ -246,6 +240,7 @@ func resourcePagerDutyServiceIntegration() *schema.Resource {
"email_filter": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -299,6 +294,81 @@ func resourcePagerDutyServiceIntegration() *schema.Resource {
}
}

func customizeServiceIntegrationDiff() schema.CustomizeDiffFunc {
flattenEFConfigBlock := func(v interface{}) []map[string]interface{} {
var efConfigBlock []map[string]interface{}
if isNilFunc(v) {
return efConfigBlock
}
for _, ef := range v.([]interface{}) {
var efConfig map[string]interface{}
if !isNilFunc(ef) {
efConfig = ef.(map[string]interface{})
}
efConfigBlock = append(efConfigBlock, efConfig)
}
return efConfigBlock
}

isEFEmptyConfigBlock := func(ef map[string]interface{}) bool {
var isEmpty bool
if ef["body_mode"].(string) == "" &&
ef["body_regex"].(string) == "" &&
ef["from_email_mode"].(string) == "" &&
ef["from_email_regex"].(string) == "" &&
ef["subject_mode"].(string) == "" &&
ef["subject_regex"].(string) == "" {
isEmpty = true
}
return isEmpty
}

isEFDefaultConfigBlock := func(ef map[string]interface{}) bool {
var isDefault bool
if ef["body_mode"].(string) == "always" &&
ef["body_regex"].(string) == "" &&
ef["from_email_mode"].(string) == "always" &&
ef["from_email_regex"].(string) == "" &&
ef["subject_mode"].(string) == "always" &&
ef["subject_regex"].(string) == "" {
isDefault = true
}
return isDefault
}

return func(context context.Context, diff *schema.ResourceDiff, i interface{}) error {
t := diff.Get("type").(string)
if t == "generic_email_inbound_integration" && diff.Get("integration_email").(string) == "" && diff.NewValueKnown("integration_email") {
return errors.New(errEmailIntegrationMustHaveEmail)
}

// All this custom diff logic is needed because the email_filters API
// response returns a default value for its structure even when this
// configuration is sent empty, so it produces a permanent diff on each Read
// that has an empty configuration for email_filter attribute on HCL code.
vOldEF, vNewEF := diff.GetChange("email_filter")
oldEF := flattenEFConfigBlock(vOldEF)
newEF := flattenEFConfigBlock(vNewEF)
if len(oldEF) > 0 && len(newEF) > 0 && len(oldEF) == len(newEF) {
var updatedEF []map[string]interface{}
for idx, new := range newEF {
old := oldEF[idx]
isSameEFConfig := old["id"] == new["id"]

efConfig := new
if isSameEFConfig && isEFDefaultConfigBlock(old) && isEFEmptyConfigBlock(new) {
efConfig = old
}
updatedEF = append(updatedEF, efConfig)
}

diff.SetNew("email_filter", updatedEF)
}

return nil
}
}

func buildServiceIntegrationStruct(d *schema.ResourceData) (*pagerduty.Integration, error) {
serviceIntegration := &pagerduty.Integration{
Name: d.Get("name").(string),
Expand Down
167 changes: 167 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"
"os"
"regexp"
"testing"

Expand Down Expand Up @@ -370,6 +371,41 @@ func TestAccPagerDutyServiceIntegrationEmail_Filters(t *testing.T) {
})
}

func TestAccPagerDutyServiceIntegration_GenericEmailNoFilters(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))
serviceIntegration := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckServiceIntegrationGenericEmailNoFilters(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceIntegrationDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyServiceIntegrationGenericEmailNoFilters(username, email, escalationPolicy, service, serviceIntegration),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"pagerduty_service_integration.foo", "type", "generic_email_inbound_integration"),
resource.TestCheckResourceAttr(
"pagerduty_service_integration.bar", "type", "generic_email_inbound_integration"),
),
},
{
Config: testAccCheckPagerDutyServiceIntegrationMultipleGenericEmailNoFilters(username, email, escalationPolicy, service, serviceIntegration),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"pagerduty_service_integration.foo", "type", "generic_email_inbound_integration"),
),
},
},
})
}

func testAccCheckPagerDutyServiceIntegrationDestroy(s *terraform.State) error {
client, _ := testAccProvider.Meta().(*Config).Client()
for _, r := range s.RootModule().Resources {
Expand Down Expand Up @@ -958,3 +994,134 @@ resource "pagerduty_service_integration" "foo" {
}
`, username, email, escalationPolicy, service, serviceIntegration, accountDomain)
}

func testAccCheckPagerDutyServiceIntegrationGenericEmailNoFilters(username, email, escalationPolicy, service, serviceIntegration string) string {
integrationEmail := os.Getenv("PAGERDUTY_ACC_SERVICE_INTEGRATION_GENERIC_EMAIL_NO_FILTERS")

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 = "%[5]s"
service = pagerduty_service.foo.id
type = "generic_email_inbound_integration"
integration_email = "%[6]s"
}

resource "pagerduty_service_integration" "bar" {
name = "%[5]s"
service = pagerduty_service.foo.id
type = "generic_email_inbound_integration"
integration_email = "%[6]s"
email_filter {}
}
`, username, email, escalationPolicy, service, serviceIntegration, integrationEmail)
}

func testAccCheckPagerDutyServiceIntegrationMultipleGenericEmailNoFilters(username, email, escalationPolicy, service, serviceIntegration string) string {
integrationEmail := os.Getenv("PAGERDUTY_ACC_SERVICE_INTEGRATION_GENERIC_EMAIL_NO_FILTERS")

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 = "%[5]s"
service = pagerduty_service.foo.id
type = "generic_email_inbound_integration"
integration_email = "%[6]s"
email_filter {
body_mode = "always"
body_regex = null
from_email_mode = "match"
from_email_regex = "(@bar.com*)"
subject_mode = "match"
subject_regex = "(CRITICAL*)"
}
email_filter {}
email_filter {
body_mode = "always"
body_regex = null
from_email_mode = "match"
from_email_regex = "(@bar.com*)"
subject_mode = "match"
subject_regex = "(CRITICAL*)"
}
}
`, username, email, escalationPolicy, service, serviceIntegration, integrationEmail)
}

func testAccPreCheckServiceIntegrationGenericEmailNoFilters(t *testing.T) {
if v := os.Getenv("PAGERDUTY_ACC_SERVICE_INTEGRATION_GENERIC_EMAIL_NO_FILTERS"); v == "" {
t.Skip("PAGERDUTY_ACC_SERVICE_INTEGRATION_GENERIC_EMAIL_NO_FILTERS not set. Skipping Service Integration related test")
}
}