Dan Kirkwood | ceb8409 | 2016-03-01 13:58:34 -0700 | [diff] [blame^] | 1 | package quotas |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | // Quota is a set of operational limits that allow for control of compute usage. |
| 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 | |
| 29 | type Quota struct { |
| 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 | |
| 58 | // QuotaPage stores a single, only page of Quota results from a List call. |
| 59 | type QuotaPage struct { |
| 60 | pagination.SinglePageBase |
| 61 | } |
| 62 | |
| 63 | // IsEmpty determines whether or not a QuotaPage is empty. |
| 64 | func (page QuotaPage) IsEmpty() (bool, error) { |
| 65 | ks, err := ExtractQuotas(page) |
| 66 | return len(ks) == 0, err |
| 67 | } |
| 68 | |
| 69 | // ExtractQuotas interprets a page of results as a slice of Quotas. |
| 70 | func ExtractQuotas(page pagination.Page) ([]Quota, error) { |
| 71 | var resp struct { |
| 72 | Quotas []Quota `mapstructure:"quotas"` |
| 73 | } |
| 74 | |
| 75 | err := mapstructure.Decode(page.(QuotaPage).Body, &resp) |
| 76 | results := make([]Quota, len(resp.Quotas)) |
| 77 | for i, q := range resp.Quotas { |
| 78 | results[i] = q |
| 79 | } |
| 80 | return results, err |
| 81 | } |
| 82 | |
| 83 | type quotaResult struct { |
| 84 | gophercloud.Result |
| 85 | } |
| 86 | |
| 87 | // Extract is a method that attempts to interpret any Quota resource response as a Quota struct. |
| 88 | func (r quotaResult) Extract() (*Quota, error) { |
| 89 | if r.Err != nil { |
| 90 | return nil, r.Err |
| 91 | } |
| 92 | |
| 93 | var res struct { |
| 94 | Quota *Quota `json:"quota_set" mapstructure:"quota_set"` |
| 95 | } |
| 96 | |
| 97 | err := mapstructure.Decode(r.Body, &res) |
| 98 | return res.Quota, err |
| 99 | } |
| 100 | |
| 101 | // CreateResult is the response from a Create operation. Call its Extract method to interpret it |
| 102 | // as a Quota. |
| 103 | type CreateResult struct { |
| 104 | quotaResult |
| 105 | } |
| 106 | |
| 107 | // GetResult is the response from a Get operation. Call its Extract method to interpret it |
| 108 | // as a Quota. |
| 109 | type GetResult struct { |
| 110 | quotaResult |
| 111 | } |
| 112 | |
| 113 | // DeleteResult is the response from a Delete operation. Call its Extract method to determine if |
| 114 | // the call succeeded or failed. |
| 115 | type DeleteResult struct { |
| 116 | gophercloud.ErrResult |
| 117 | } |