Simplify role check for user

This commit is contained in:
Alexander Hellbom 2016-10-17 20:55:02 +02:00
parent 0951adab3b
commit ee20c11907
3 changed files with 27 additions and 24 deletions

View File

@ -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
}

View File

@ -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))

View File

@ -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
}
}