github.com/dkerwin/nomad@v0.3.3-0.20160525181927-74554135514b/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 func (a *Allocations) PrefixList(prefix string) ([]*AllocationListStub, *QueryMeta, error) { 30 return a.List(&QueryOptions{Prefix: prefix}) 31 } 32 33 // Info is used to retrieve a single allocation. 34 func (a *Allocations) Info(allocID string, q *QueryOptions) (*Allocation, *QueryMeta, error) { 35 var resp Allocation 36 qm, err := a.client.query("/v1/allocation/"+allocID, &resp, q) 37 if err != nil { 38 return nil, nil, err 39 } 40 return &resp, qm, nil 41 } 42 43 // Allocation is used for serialization of allocations. 44 type Allocation struct { 45 ID string 46 EvalID string 47 Name string 48 NodeID string 49 JobID string 50 Job *Job 51 TaskGroup string 52 Resources *Resources 53 TaskResources map[string]*Resources 54 Services map[string]string 55 Metrics *AllocationMetric 56 DesiredStatus string 57 DesiredDescription string 58 ClientStatus string 59 ClientDescription string 60 TaskStates map[string]*TaskState 61 CreateIndex uint64 62 ModifyIndex uint64 63 CreateTime int64 64 } 65 66 // AllocationMetric is used to deserialize allocation metrics. 67 type AllocationMetric struct { 68 NodesEvaluated int 69 NodesFiltered int 70 NodesAvailable map[string]int 71 ClassFiltered map[string]int 72 ConstraintFiltered map[string]int 73 NodesExhausted int 74 ClassExhausted map[string]int 75 DimensionExhausted map[string]int 76 Scores map[string]float64 77 AllocationTime time.Duration 78 CoalescedFailures int 79 } 80 81 // AllocationListStub is used to return a subset of an allocation 82 // during list operations. 83 type AllocationListStub struct { 84 ID string 85 EvalID string 86 Name string 87 NodeID string 88 JobID string 89 TaskGroup string 90 DesiredStatus string 91 DesiredDescription string 92 ClientStatus string 93 ClientDescription string 94 TaskStates map[string]*TaskState 95 CreateIndex uint64 96 ModifyIndex uint64 97 CreateTime int64 98 } 99 100 // AllocIndexSort reverse sorts allocs by CreateIndex. 101 type AllocIndexSort []*AllocationListStub 102 103 func (a AllocIndexSort) Len() int { 104 return len(a) 105 } 106 107 func (a AllocIndexSort) Less(i, j int) bool { 108 return a[i].CreateIndex > a[j].CreateIndex 109 } 110 111 func (a AllocIndexSort) Swap(i, j int) { 112 a[i], a[j] = a[j], a[i] 113 }