github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/api/allocations.go (about) 1 package api 2 3 import ( 4 "sort" 5 "time" 6 ) 7 8 // Allocations is used to query the alloc-related endpoints. 9 type Allocations struct { 10 client *Client 11 } 12 13 // Allocations returns a handle on the allocs endpoints. 14 func (c *Client) Allocations() *Allocations { 15 return &Allocations{client: c} 16 } 17 18 // List returns a list of all of the allocations. 19 func (a *Allocations) List(q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) { 20 var resp []*AllocationListStub 21 qm, err := a.client.query("/v1/allocations", &resp, q) 22 if err != nil { 23 return nil, nil, err 24 } 25 sort.Sort(AllocIndexSort(resp)) 26 return resp, qm, nil 27 } 28 29 // Info is used to retrieve a single allocation. 30 func (a *Allocations) Info(allocID string, q *QueryOptions) (*Allocation, *QueryMeta, error) { 31 var resp Allocation 32 qm, err := a.client.query("/v1/allocation/"+allocID, &resp, q) 33 if err != nil { 34 return nil, nil, err 35 } 36 return &resp, qm, nil 37 } 38 39 // Allocation is used for serialization of allocations. 40 type Allocation struct { 41 ID string 42 EvalID string 43 Name string 44 NodeID string 45 JobID string 46 Job *Job 47 TaskGroup string 48 Resources *Resources 49 TaskResources map[string]*Resources 50 Metrics *AllocationMetric 51 DesiredStatus string 52 DesiredDescription string 53 ClientStatus string 54 ClientDescription string 55 CreateIndex uint64 56 ModifyIndex uint64 57 } 58 59 // AllocationMetric is used to deserialize allocation metrics. 60 type AllocationMetric struct { 61 NodesEvaluated int 62 NodesFiltered int 63 ClassFiltered map[string]int 64 ConstraintFiltered map[string]int 65 NodesExhausted int 66 ClassExhausted map[string]int 67 DimensionExhausted map[string]int 68 Scores map[string]float64 69 AllocationTime time.Duration 70 CoalescedFailures int 71 } 72 73 // AllocationListStub is used to return a subset of an allocation 74 // during list operations. 75 type AllocationListStub struct { 76 ID string 77 EvalID string 78 Name string 79 NodeID string 80 JobID string 81 TaskGroup string 82 DesiredStatus string 83 DesiredDescription string 84 ClientStatus string 85 ClientDescription string 86 CreateIndex uint64 87 ModifyIndex uint64 88 } 89 90 // AllocIndexSort reverse sorts allocs by CreateIndex. 91 type AllocIndexSort []*AllocationListStub 92 93 func (a AllocIndexSort) Len() int { 94 return len(a) 95 } 96 97 func (a AllocIndexSort) Less(i, j int) bool { 98 return a[i].CreateIndex > a[j].CreateIndex 99 } 100 101 func (a AllocIndexSort) Swap(i, j int) { 102 a[i], a[j] = a[j], a[i] 103 }