Skip to content

Commit

Permalink
Merge pull request #501 from hashicorp/b-distinct-host-panic
Browse files Browse the repository at this point in the history
Distinct Host constraint accepts bool or string.
  • Loading branch information
dadgar committed Nov 25, 2015
2 parents 857ecac + 3049507 commit 5babe23
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions jobspec/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ func parseConstraints(result *[]*structs.Constraint, list *ast.ObjectList) error
}

if value, ok := m[structs.ConstraintDistinctHosts]; ok {
enabled, err := strconv.ParseBool(value.(string))
enabled, err := parseBool(value)
if err != nil {
return err
return fmt.Errorf("distinct_hosts should be set to true or false; %v", err)
}

// If it is not enabled, skip the constraint.
Expand All @@ -346,6 +346,23 @@ func parseConstraints(result *[]*structs.Constraint, list *ast.ObjectList) error
return nil
}

// parseBool takes an interface value and tries to convert it to a boolean and
// returns an error if the type can't be converted.
func parseBool(value interface{}) (bool, error) {
var enabled bool
var err error
switch value.(type) {
case string:
enabled, err = strconv.ParseBool(value.(string))
case bool:
enabled = value.(bool)
default:
err = fmt.Errorf("%v couldn't be converted to boolean value", value)
}

return enabled, err
}

func parseTasks(jobName string, taskGroupName string, result *[]*structs.Task, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
Expand Down

0 comments on commit 5babe23

Please sign in to comment.