github.com/anuvu/nomad@v0.8.7-atom1/api/quota.go (about) 1 package api 2 3 import ( 4 "fmt" 5 "sort" 6 ) 7 8 // Quotas is used to query the quotas endpoints. 9 type Quotas struct { 10 client *Client 11 } 12 13 // Quotas returns a new handle on the quotas. 14 func (c *Client) Quotas() *Quotas { 15 return &Quotas{client: c} 16 } 17 18 // List is used to dump all of the quota specs 19 func (q *Quotas) List(qo *QueryOptions) ([]*QuotaSpec, *QueryMeta, error) { 20 var resp []*QuotaSpec 21 qm, err := q.client.query("/v1/quotas", &resp, qo) 22 if err != nil { 23 return nil, nil, err 24 } 25 sort.Sort(QuotaSpecIndexSort(resp)) 26 return resp, qm, nil 27 } 28 29 // PrefixList is used to do a PrefixList search over quota specs 30 func (q *Quotas) PrefixList(prefix string, qo *QueryOptions) ([]*QuotaSpec, *QueryMeta, error) { 31 if qo == nil { 32 qo = &QueryOptions{Prefix: prefix} 33 } else { 34 qo.Prefix = prefix 35 } 36 37 return q.List(qo) 38 } 39 40 // ListUsage is used to dump all of the quota usages 41 func (q *Quotas) ListUsage(qo *QueryOptions) ([]*QuotaUsage, *QueryMeta, error) { 42 var resp []*QuotaUsage 43 qm, err := q.client.query("/v1/quota-usages", &resp, qo) 44 if err != nil { 45 return nil, nil, err 46 } 47 sort.Sort(QuotaUsageIndexSort(resp)) 48 return resp, qm, nil 49 } 50 51 // PrefixList is used to do a PrefixList search over quota usages 52 func (q *Quotas) PrefixListUsage(prefix string, qo *QueryOptions) ([]*QuotaUsage, *QueryMeta, error) { 53 if qo == nil { 54 qo = &QueryOptions{Prefix: prefix} 55 } else { 56 qo.Prefix = prefix 57 } 58 59 return q.ListUsage(qo) 60 } 61 62 // Info is used to query a single quota spec by its name. 63 func (q *Quotas) Info(name string, qo *QueryOptions) (*QuotaSpec, *QueryMeta, error) { 64 var resp QuotaSpec 65 qm, err := q.client.query("/v1/quota/"+name, &resp, qo) 66 if err != nil { 67 return nil, nil, err 68 } 69 return &resp, qm, nil 70 } 71 72 // Usage is used to query a single quota usage by its name. 73 func (q *Quotas) Usage(name string, qo *QueryOptions) (*QuotaUsage, *QueryMeta, error) { 74 var resp QuotaUsage 75 qm, err := q.client.query("/v1/quota/usage/"+name, &resp, qo) 76 if err != nil { 77 return nil, nil, err 78 } 79 return &resp, qm, nil 80 } 81 82 // Register is used to register a quota spec. 83 func (q *Quotas) Register(spec *QuotaSpec, qo *WriteOptions) (*WriteMeta, error) { 84 wm, err := q.client.write("/v1/quota", spec, nil, qo) 85 if err != nil { 86 return nil, err 87 } 88 return wm, nil 89 } 90 91 // Delete is used to delete a quota spec 92 func (q *Quotas) Delete(quota string, qo *WriteOptions) (*WriteMeta, error) { 93 wm, err := q.client.delete(fmt.Sprintf("/v1/quota/%s", quota), nil, qo) 94 if err != nil { 95 return nil, err 96 } 97 return wm, nil 98 } 99 100 // QuotaSpec specifies the allowed resource usage across regions. 101 type QuotaSpec struct { 102 // Name is the name for the quota object 103 Name string 104 105 // Description is an optional description for the quota object 106 Description string 107 108 // Limits is the set of quota limits encapsulated by this quota object. Each 109 // limit applies quota in a particular region and in the future over a 110 // particular priority range and datacenter set. 111 Limits []*QuotaLimit 112 113 // Raft indexes to track creation and modification 114 CreateIndex uint64 115 ModifyIndex uint64 116 } 117 118 // QuotaLimit describes the resource limit in a particular region. 119 type QuotaLimit struct { 120 // Region is the region in which this limit has affect 121 Region string 122 123 // RegionLimit is the quota limit that applies to any allocation within a 124 // referencing namespace in the region. A value of zero is treated as 125 // unlimited and a negative value is treated as fully disallowed. This is 126 // useful for once we support GPUs 127 RegionLimit *Resources 128 129 // Hash is the hash of the object and is used to make replication efficient. 130 Hash []byte 131 } 132 133 // QuotaUsage is the resource usage of a Quota 134 type QuotaUsage struct { 135 Name string 136 Used map[string]*QuotaLimit 137 CreateIndex uint64 138 ModifyIndex uint64 139 } 140 141 // QuotaSpecIndexSort is a wrapper to sort QuotaSpecs by CreateIndex. We 142 // reverse the test so that we get the highest index first. 143 type QuotaSpecIndexSort []*QuotaSpec 144 145 func (q QuotaSpecIndexSort) Len() int { 146 return len(q) 147 } 148 149 func (q QuotaSpecIndexSort) Less(i, j int) bool { 150 return q[i].CreateIndex > q[j].CreateIndex 151 } 152 153 func (q QuotaSpecIndexSort) Swap(i, j int) { 154 q[i], q[j] = q[j], q[i] 155 } 156 157 // QuotaUsageIndexSort is a wrapper to sort QuotaUsages by CreateIndex. We 158 // reverse the test so that we get the highest index first. 159 type QuotaUsageIndexSort []*QuotaUsage 160 161 func (q QuotaUsageIndexSort) Len() int { 162 return len(q) 163 } 164 165 func (q QuotaUsageIndexSort) Less(i, j int) bool { 166 return q[i].CreateIndex > q[j].CreateIndex 167 } 168 169 func (q QuotaUsageIndexSort) Swap(i, j int) { 170 q[i], q[j] = q[j], q[i] 171 } 172 173 // QuotaLimitSort is a wrapper to sort QuotaLimits 174 type QuotaLimitSort []*QuotaLimit 175 176 func (q QuotaLimitSort) Len() int { 177 return len(q) 178 } 179 180 func (q QuotaLimitSort) Less(i, j int) bool { 181 return q[i].Region < q[j].Region 182 } 183 184 func (q QuotaLimitSort) Swap(i, j int) { 185 q[i], q[j] = q[j], q[i] 186 }