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 | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 9 | // QuotaSet is a set of operational limits that allow for control of compute usage. |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 10 | type QuotaSet struct { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 11 | //ID is tenant associated with this quota_set |
| 12 | ID string `mapstructure:"id"` |
| 13 | //FixedIps is number of fixed ips alloted this quota_set |
| 14 | FixedIps int `mapstructure:"fixed_ips"` |
Dan Kirkwood | 7aadf86 | 2016-03-16 12:41:11 -0600 | [diff] [blame] | 15 | // FloatingIps is number of floating ips alloted this quota_set |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 16 | FloatingIps int `mapstructure:"floating_ips"` |
| 17 | // InjectedFileContentBytes is content bytes allowed for each injected file |
| 18 | InjectedFileContentBytes int `mapstructure:"injected_file_content_bytes"` |
| 19 | // InjectedFilePathBytes is allowed bytes for each injected file path |
| 20 | InjectedFilePathBytes int `mapstructure:"injected_file_path_bytes"` |
| 21 | // InjectedFiles is injected files allowed for each project |
| 22 | InjectedFiles int `mapstructure:"injected_files"` |
| 23 | // KeyPairs is number of ssh keypairs |
| 24 | KeyPairs int `mapstructure:"keypairs"` |
| 25 | // MetadataItems is number of metadata items allowed for each instance |
| 26 | MetadataItems int `mapstructure:"metadata_items"` |
| 27 | // Ram is megabytes allowed for each instance |
| 28 | Ram int `mapstructure:"ram"` |
| 29 | // SecurityGroupRules is rules allowed for each security group |
| 30 | SecurityGroupRules int `mapstructure:"security_group_rules"` |
| 31 | // SecurityGroups security groups allowed for each project |
| 32 | SecurityGroups int `mapstructure:"security_groups"` |
| 33 | // Cores is number of instance cores allowed for each project |
| 34 | Cores int `mapstructure:"cores"` |
| 35 | // Instances is number of instances allowed for each project |
| 36 | Instances int `mapstructure:"instances"` |
| 37 | } |
| 38 | |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 39 | // QuotaSetPage stores a single, only page of QuotaSet results from a List call. |
| 40 | type QuotaSetPage struct { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 41 | pagination.SinglePageBase |
| 42 | } |
| 43 | |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 44 | // IsEmpty determines whether or not a QuotaSetsetPage is empty. |
| 45 | func (page QuotaSetPage) IsEmpty() (bool, error) { |
| 46 | ks, err := ExtractQuotaSets(page) |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 47 | return len(ks) == 0, err |
| 48 | } |
| 49 | |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 50 | // ExtractQuotaSets interprets a page of results as a slice of QuotaSets. |
| 51 | func ExtractQuotaSets(page pagination.Page) ([]QuotaSet, error) { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 52 | var resp struct { |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 53 | QuotaSets []QuotaSet `mapstructure:"quotas"` |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 56 | err := mapstructure.Decode(page.(QuotaSetPage).Body, &resp) |
| 57 | results := make([]QuotaSet, len(resp.QuotaSets)) |
| 58 | for i, q := range resp.QuotaSets { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 59 | results[i] = q |
| 60 | } |
| 61 | return results, err |
| 62 | } |
| 63 | |
| 64 | type quotaResult struct { |
| 65 | gophercloud.Result |
| 66 | } |
| 67 | |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 68 | // Extract is a method that attempts to interpret any QuotaSet resource response as a QuotaSet struct. |
| 69 | func (r quotaResult) Extract() (*QuotaSet, error) { |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 70 | if r.Err != nil { |
| 71 | return nil, r.Err |
| 72 | } |
| 73 | |
| 74 | var res struct { |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 75 | QuotaSet *QuotaSet `json:"quota_set" mapstructure:"quota_set"` |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | err := mapstructure.Decode(r.Body, &res) |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 79 | return res.QuotaSet, err |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 82 | // GetResult is the response from a Get operation. Call its Extract method to interpret it |
Dan Kirkwood | 7e8d8ed | 2016-03-08 14:05:57 -0700 | [diff] [blame] | 83 | // as a QuotaSet. |
Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame] | 84 | type GetResult struct { |
| 85 | quotaResult |
| 86 | } |