github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/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 } 64 65 // AllocationMetric is used to deserialize allocation metrics. 66 type AllocationMetric struct { 67 NodesEvaluated int 68 NodesFiltered int 69 NodesAvailable map[string]int 70 ClassFiltered map[string]int 71 ConstraintFiltered map[string]int 72 NodesExhausted int 73 ClassExhausted map[string]int 74 DimensionExhausted map[string]int 75 Scores map[string]float64 76 AllocationTime time.Duration 77 CoalescedFailures int 78 } 79 80 // AllocationListStub is used to return a subset of an allocation 81 // during list operations. 82 type AllocationListStub struct { 83 ID string 84 EvalID string 85 Name string 86 NodeID string 87 JobID string 88 TaskGroup string 89 DesiredStatus string 90 DesiredDescription string 91 ClientStatus string 92 ClientDescription string 93 TaskStates map[string]*TaskState 94 CreateIndex uint64 95 ModifyIndex uint64 96 } 97 98 // AllocIndexSort reverse sorts allocs by CreateIndex. 99 type AllocIndexSort []*AllocationListStub 100 101 func (a AllocIndexSort) Len() int { 102 return len(a) 103 } 104 105 func (a AllocIndexSort) Less(i, j int) bool { 106 return a[i].CreateIndex > a[j].CreateIndex 107 } 108 109 func (a AllocIndexSort) Swap(i, j int) { 110 a[i], a[j] = a[j], a[i] 111 }