api: add constraints generators

This commit is contained in:
Ryan Uber
2015-09-08 19:27:04 -07:00
parent 689fa2bb11
commit 33e7d0cd2d
2 changed files with 63 additions and 0 deletions

View File

@@ -82,6 +82,7 @@ type Job struct {
Priority int
AllAtOnce bool
Datacenters []string
Constraints []*Constraint
Meta map[string]string
Status string
StatusDescription string
@@ -96,3 +97,35 @@ type registerJobRequest struct {
type registerJobResponse struct {
EvalID string
}
// Constraint is used to serialize a job placement constraint.
type Constraint struct {
Hard bool
LTarget string
RTarget string
Operand string
Weight int
}
// HardConstraint is used to create a new hard constraint.
func HardConstraint(left, operand, right string) *Constraint {
return constraint(left, operand, right, true, 0)
}
// SoftConstraint is used to create a new soft constraint. It
// takes an additional weight parameter to allow balancing
// multiple soft constraints amongst eachother.
func SoftConstraint(left, operand, right string, weight int) *Constraint {
return constraint(left, operand, right, false, weight)
}
// constraint generates a new job placement constraint.
func constraint(left, operand, right string, hard bool, weight int) *Constraint {
return &Constraint{
Hard: hard,
LTarget: left,
RTarget: right,
Operand: operand,
Weight: weight,
}
}

View File

@@ -178,3 +178,33 @@ func TestJobs_Delete(t *testing.T) {
t.Fatalf("expected 0 jobs, got: %d", n)
}
}
func TestJobs_Constraints(t *testing.T) {
{
c := HardConstraint("kernel.name", "=", "darwin")
expect := &Constraint{
Hard: true,
LTarget: "kernel.name",
RTarget: "darwin",
Operand: "=",
Weight: 0,
}
if !reflect.DeepEqual(c, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, c)
}
}
{
c := SoftConstraint("memory.totalbytes", ">=", "250000000", 5)
expect := &Constraint{
Hard: false,
LTarget: "memory.totalbytes",
RTarget: "250000000",
Operand: ">=",
Weight: 5,
}
if !reflect.DeepEqual(c, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, c)
}
}
}