jobspec: adding sugar for regexp constraint

This commit is contained in:
Armon Dadgar
2015-10-11 15:37:50 -04:00
parent 0a2e874245
commit e2093e4189
3 changed files with 33 additions and 0 deletions

View File

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

View File

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

View File

@@ -0,0 +1,6 @@
job "foo" {
constraint {
attribute = "$attr.kernel.version"
regexp = "[0-9.]+"
}
}