Skip to content

Commit

Permalink
add name format validation for EP
Browse files Browse the repository at this point in the history
Addiontally refactor name format validation for Service into a reusable util, which is being used and tested for EP and Service
  • Loading branch information
imjaroiswebdev committed Jul 12, 2023
1 parent 655e601 commit 81cec41
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
5 changes: 3 additions & 2 deletions pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ func resourcePagerDutyEscalationPolicy() *schema.Resource {
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateCantBeBlankOrNotPrintableChars,
},
"description": {
Type: schema.TypeString,
Expand Down
26 changes: 26 additions & 0 deletions pagerduty/resource_pagerduty_escalation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"fmt"
"log"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -109,6 +110,31 @@ func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) {
})
}

func TestAccPagerDutyEscalationPolicy_FormatValidation(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyEscalationPolicyDestroy,
Steps: []resource.TestStep{
// Blank Name
{
Config: testAccCheckPagerDutyEscalationPolicyConfig(username, email, ""),
PlanOnly: true,
ExpectError: regexp.MustCompile("Name can't be blank neither contain.*, nor non-printable characters."),
},
// Name with & in it
{
Config: testAccCheckPagerDutyEscalationPolicyConfig(username, email, "this name has an ampersand (&)"),
PlanOnly: true,
ExpectError: regexp.MustCompile("Name can't be blank neither contain.*, nor non-printable characters."),
},
},
})
}

func TestAccPagerDutyEscalationPolicyWithTeams_Basic(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)
Expand Down
7 changes: 3 additions & 4 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"regexp"
"strconv"
"time"

Expand Down Expand Up @@ -35,9 +34,9 @@ func resourcePagerDutyService() *schema.Resource {
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringDoesNotMatch(regexp.MustCompile(`^$|^[ ]+$|[/\\<>&]`), "Service name can't be blank or contain '\\', '/', '&', '<', '>' or non-printable characters. "),
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateCantBeBlankOrNotPrintableChars,
},
"html_url": {
Type: schema.TypeString,
Expand Down
26 changes: 26 additions & 0 deletions pagerduty/resource_pagerduty_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,32 @@ func TestAccPagerDutyService_Basic(t *testing.T) {
})
}

func TestAccPagerDutyService_FormatValidation(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))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDestroy,
Steps: []resource.TestStep{
// Blank Name
{
Config: testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, ""),
PlanOnly: true,
ExpectError: regexp.MustCompile("Name can't be blank neither contain.*, nor non-printable characters."),
},
// Name with & in it
{
Config: testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, "this name has an ampersand (&)"),
PlanOnly: true,
ExpectError: regexp.MustCompile("Name can't be blank neither contain.*, nor non-printable characters."),
},
},
})
}

func TestAccPagerDutyService_AlertGrouping(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)
Expand Down
19 changes: 19 additions & 0 deletions pagerduty/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"math"
"reflect"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -113,6 +114,24 @@ func validateValueDiagFunc(values []string) schema.SchemaValidateDiagFunc {
}
}

func validateCantBeBlankOrNotPrintableChars(v interface{}, p cty.Path) diag.Diagnostics {
var diags diag.Diagnostics

value := v.(string)
matcher := regexp.MustCompile(`^$|^[ ]+$|[/\\<>&]`)
matches := matcher.MatchString(value)

if matches {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Name can't be blank neither contain '\\', '/', '&', '<', '>', nor non-printable characters.",
AttributePath: p,
})
}

return diags
}

// Takes the result of flatmap.Expand for an array of strings
// and returns a []string
func expandStringList(configured []interface{}) []string {
Expand Down

0 comments on commit 81cec41

Please sign in to comment.