From 8406bfede5b7390e5efadedc7d2213d085b7459b Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 11 Oct 2015 15:20:58 -0400 Subject: [PATCH] jobspec: adding sugar for version constraint --- jobspec/parse.go | 7 +++++++ jobspec/parse_test.go | 20 ++++++++++++++++++++ jobspec/test-fixtures/version-constraint.hcl | 6 ++++++ 3 files changed, 33 insertions(+) create mode 100644 jobspec/test-fixtures/version-constraint.hcl diff --git a/jobspec/parse.go b/jobspec/parse.go index 1231b5ec0..20086d86e 100644 --- a/jobspec/parse.go +++ b/jobspec/parse.go @@ -242,6 +242,13 @@ func parseConstraints(result *[]*structs.Constraint, obj *hclobj.Object) error { m["hard"] = true } + // If "version" is provided, set the operand + // to "version" and the value to the "RTarget" + if constraint, ok := m["version"]; ok { + m["Operand"] = "version" + 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 dea59ec26..4336b9aac 100644 --- a/jobspec/parse_test.go +++ b/jobspec/parse_test.go @@ -152,6 +152,26 @@ func TestParse(t *testing.T) { false, }, + { + "version-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: "~> 3.2", + Operand: "version", + }, + }, + }, + false, + }, + { "specify-job.hcl", &structs.Job{ diff --git a/jobspec/test-fixtures/version-constraint.hcl b/jobspec/test-fixtures/version-constraint.hcl new file mode 100644 index 000000000..3ba755272 --- /dev/null +++ b/jobspec/test-fixtures/version-constraint.hcl @@ -0,0 +1,6 @@ +job "foo" { + constraint { + attribute = "$attr.kernel.version" + version = "~> 3.2" + } +}