diff --git a/builtin/providers/pagerduty/resource_pagerduty_user.go b/builtin/providers/pagerduty/resource_pagerduty_user.go index 02948283c..33b7e80c8 100644 --- a/builtin/providers/pagerduty/resource_pagerduty_user.go +++ b/builtin/providers/pagerduty/resource_pagerduty_user.go @@ -1,7 +1,6 @@ package pagerduty import ( - "fmt" "log" "github.com/PagerDuty/go-pagerduty" @@ -35,7 +34,7 @@ func resourcePagerDutyUser() *schema.Resource { Type: schema.TypeString, Optional: true, Default: "user", - ValidateFunc: validatePagerDutyUserRole, + ValidateFunc: validateValueFunc([]string{"admin", "limited_user", "owner", "read_only_user", "user"}), }, "job_title": &schema.Schema{ Type: schema.TypeString, @@ -223,14 +222,3 @@ func resourcePagerDutyUserImport(d *schema.ResourceData, meta interface{}) ([]*s } return []*schema.ResourceData{d}, nil } - -func validatePagerDutyUserRole(v interface{}, k string) (ws []string, errors []error) { - validRoles := []string{"admin", "limited_user", "owner", "read_only_user", "user"} - role := v.(string) - - if !contains(validRoles, role) { - errors = append(errors, fmt.Errorf("%q must be one of %v", k, validRoles)) - } - - return -} diff --git a/builtin/providers/pagerduty/structure.go b/builtin/providers/pagerduty/structure.go index b7a9d26ef..54601171c 100644 --- a/builtin/providers/pagerduty/structure.go +++ b/builtin/providers/pagerduty/structure.go @@ -2,17 +2,6 @@ package pagerduty import pagerduty "github.com/PagerDuty/go-pagerduty" -// Checks if a slice contains a string -func contains(slice []string, item string) bool { - set := make(map[string]struct{}, len(slice)) - for _, s := range slice { - set[s] = struct{}{} - } - - _, ok := set[item] - return ok -} - // Expands an array of escalation rules into []pagerduty.EscalationRules func expandRules(list []interface{}) []pagerduty.EscalationRule { result := make([]pagerduty.EscalationRule, 0, len(list)) diff --git a/builtin/providers/pagerduty/util.go b/builtin/providers/pagerduty/util.go new file mode 100644 index 000000000..661b7f9e6 --- /dev/null +++ b/builtin/providers/pagerduty/util.go @@ -0,0 +1,26 @@ +package pagerduty + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" +) + +// Validate a value against a set of possible values +func validateValueFunc(values []string) schema.SchemaValidateFunc { + return func(v interface{}, k string) (we []string, errors []error) { + value := v.(string) + valid := false + for _, role := range values { + if value == role { + valid = true + break + } + } + + if !valid { + errors = append(errors, fmt.Errorf("%s is an invalid value for argument %s", value, k)) + } + return + } +}