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

Distinct Host constraint accepts bool or string. #501

Merged
merged 2 commits into from
Nov 25, 2015
Merged
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
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