Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 1 | package quotasets |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 9 | // Quotaset is a set of operational limits that allow for control of compute usage. |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 10 | const sample = ` |
| 11 | { |
| 12 | "quota_set" : { |
| 13 | "fixed_ips" : -1, |
| 14 | "security_groups" : 10, |
| 15 | "id" : "56b6c3eb639e48c691052919e5a60dc3", |
| 16 | "injected_files" : 5, |
| 17 | "injected_file_path_bytes" : 255, |
| 18 | "cores" : 108, |
| 19 | "security_group_rules" : 20, |
| 20 | "keypairs" : 10, |
| 21 | "instances" : 25, |
| 22 | "ram" : 204800, |
| 23 | "metadata_items" : 128, |
| 24 | "injected_file_content_bytes" : 10240 |
| 25 | } |
| 26 | } |
| 27 | ` |
| 28 | |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 29 | type Quotaset struct { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 30 | //ID is tenant associated with this quota_set |
| 31 | ID string `mapstructure:"id"` |
| 32 | //FixedIps is number of fixed ips alloted this quota_set |
| 33 | FixedIps int `mapstructure:"fixed_ips"` |
| 34 | // FloatingIps is number of floatinh ips alloted this quota_set |
| 35 | FloatingIps int `mapstructure:"floating_ips"` |
| 36 | // InjectedFileContentBytes is content bytes allowed for each injected file |
| 37 | InjectedFileContentBytes int `mapstructure:"injected_file_content_bytes"` |
| 38 | // InjectedFilePathBytes is allowed bytes for each injected file path |
| 39 | InjectedFilePathBytes int `mapstructure:"injected_file_path_bytes"` |
| 40 | // InjectedFiles is injected files allowed for each project |
| 41 | InjectedFiles int `mapstructure:"injected_files"` |
| 42 | // KeyPairs is number of ssh keypairs |
| 43 | KeyPairs int `mapstructure:"keypairs"` |
| 44 | // MetadataItems is number of metadata items allowed for each instance |
| 45 | MetadataItems int `mapstructure:"metadata_items"` |
| 46 | // Ram is megabytes allowed for each instance |
| 47 | Ram int `mapstructure:"ram"` |
| 48 | // SecurityGroupRules is rules allowed for each security group |
| 49 | SecurityGroupRules int `mapstructure:"security_group_rules"` |
| 50 | // SecurityGroups security groups allowed for each project |
| 51 | SecurityGroups int `mapstructure:"security_groups"` |
| 52 | // Cores is number of instance cores allowed for each project |
| 53 | Cores int `mapstructure:"cores"` |
| 54 | // Instances is number of instances allowed for each project |
| 55 | Instances int `mapstructure:"instances"` |
| 56 | } |
| 57 | |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 58 | // QuotasetPage stores a single, only page of Quotaset results from a List call. |
| 59 | type QuotasetPage struct { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 60 | pagination.SinglePageBase |
| 61 | } |
| 62 | |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 63 | // IsEmpty determines whether or not a QuotasetsetPage is empty. |
| 64 | func (page QuotasetPage) IsEmpty() (bool, error) { |
| 65 | ks, err := ExtractQuotasets(page) |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 66 | return len(ks) == 0, err |
| 67 | } |
| 68 | |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 69 | // ExtractQuotasets interprets a page of results as a slice of Quotasets. |
| 70 | func ExtractQuotasets(page pagination.Page) ([]Quotaset, error) { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 71 | var resp struct { |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 72 | Quotasets []Quotaset `mapstructure:"quotas"` |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 75 | err := mapstructure.Decode(page.(QuotasetPage).Body, &resp) |
| 76 | results := make([]Quotaset, len(resp.Quotasets)) |
| 77 | for i, q := range resp.Quotasets { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 78 | results[i] = q |
| 79 | } |
| 80 | return results, err |
| 81 | } |
| 82 | |
| 83 | type quotaResult struct { |
| 84 | gophercloud.Result |
| 85 | } |
| 86 | |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 87 | // Extract is a method that attempts to interpret any Quotaset resource response as a Quotaset struct. |
| 88 | func (r quotaResult) Extract() (*Quotaset, error) { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 89 | if r.Err != nil { |
| 90 | return nil, r.Err |
| 91 | } |
| 92 | |
| 93 | var res struct { |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 94 | Quotaset *Quotaset `json:"quota_set" mapstructure:"quota_set"` |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | err := mapstructure.Decode(r.Body, &res) |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 98 | return res.Quotaset, err |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 101 | // GetResult is the response from a Get operation. Call its Extract method to interpret it |
Dan Kirkwood | c0a8099 | 2016-03-07 13:47:25 -0700 | [diff] [blame^] | 102 | // as a Quotaset. |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 103 | type GetResult struct { |
| 104 | quotaResult |
| 105 | } |