From e2093e41891aedf7f0f6047a33e337f11207dae7 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 11 Oct 2015 15:37:50 -0400 Subject: [PATCH] jobspec: adding sugar for regexp constraint --- jobspec/parse.go | 7 +++++++ jobspec/parse_test.go | 20 ++++++++++++++++++++ jobspec/test-fixtures/regexp-constraint.hcl | 6 ++++++ 3 files changed, 33 insertions(+) create mode 100644 jobspec/test-fixtures/regexp-constraint.hcl diff --git a/jobspec/parse.go b/jobspec/parse.go index 20086d86e..b7ca31c54 100644 --- a/jobspec/parse.go +++ b/jobspec/parse.go @@ -249,6 +249,13 @@ func parseConstraints(result *[]*structs.Constraint, obj *hclobj.Object) error { m["RTarget"] = constraint } + // If "regexp" is provided, set the operand + // to "regexp" and the value to the "RTarget" + if constraint, ok := m["regexp"]; ok { + m["Operand"] = "regexp" + m["RTarget"] = constraint + } + // Build the constraint var c structs.Constraint if err := mapstructure.WeakDecode(m, &c); err != nil { diff --git a/jobspec/parse_test.go b/jobspec/parse_test.go index 4336b9aac..60c5f1f09 100644 --- a/jobspec/parse_test.go +++ b/jobspec/parse_test.go @@ -172,6 +172,26 @@ func TestParse(t *testing.T) { false, }, + { + "regexp-constraint.hcl", + &structs.Job{ + ID: "foo", + Name: "foo", + Priority: 50, + Region: "global", + Type: "service", + Constraints: []*structs.Constraint{ + &structs.Constraint{ + Hard: true, + LTarget: "$attr.kernel.version", + RTarget: "[0-9.]+", + Operand: "regexp", + }, + }, + }, + false, + }, + { "specify-job.hcl", &structs.Job{ diff --git a/jobspec/test-fixtures/regexp-constraint.hcl b/jobspec/test-fixtures/regexp-constraint.hcl new file mode 100644 index 000000000..dfdb4ce20 --- /dev/null +++ b/jobspec/test-fixtures/regexp-constraint.hcl @@ -0,0 +1,6 @@ +job "foo" { + constraint { + attribute = "$attr.kernel.version" + regexp = "[0-9.]+" + } +}