Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame^] | 1 | package defsecrules |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | type DefaultRule secgroups.Rule |
| 11 | |
| 12 | // DefaultRulePage is a single page of a DefaultRule collection. |
| 13 | type DefaultRulePage struct { |
| 14 | pagination.SinglePageBase |
| 15 | } |
| 16 | |
| 17 | // IsEmpty determines whether or not a page of default rules contains any results. |
| 18 | func (page DefaultRulePage) IsEmpty() (bool, error) { |
| 19 | users, err := ExtractDefaultRules(page) |
| 20 | if err != nil { |
| 21 | return false, err |
| 22 | } |
| 23 | return len(users) == 0, nil |
| 24 | } |
| 25 | |
| 26 | // ExtractDefaultRules returns a slice of DefaultRules contained in a single |
| 27 | // page of results. |
| 28 | func ExtractDefaultRules(page pagination.Page) ([]DefaultRule, error) { |
| 29 | casted := page.(DefaultRulePage).Body |
| 30 | var response struct { |
| 31 | Rules []DefaultRule `mapstructure:"security_group_default_rules"` |
| 32 | } |
| 33 | |
| 34 | err := mapstructure.Decode(casted, &response) |
| 35 | |
| 36 | return response.Rules, err |
| 37 | } |