Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 1 | package defsecrules |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
| 11 | type DefaultRule secgroups.Rule |
| 12 | |
| 13 | // DefaultRulePage is a single page of a DefaultRule collection. |
| 14 | type DefaultRulePage struct { |
| 15 | pagination.SinglePageBase |
| 16 | } |
| 17 | |
| 18 | // IsEmpty determines whether or not a page of default rules contains any results. |
| 19 | func (page DefaultRulePage) IsEmpty() (bool, error) { |
| 20 | users, err := ExtractDefaultRules(page) |
| 21 | if err != nil { |
| 22 | return false, err |
| 23 | } |
| 24 | return len(users) == 0, nil |
| 25 | } |
| 26 | |
| 27 | // ExtractDefaultRules returns a slice of DefaultRules contained in a single |
| 28 | // page of results. |
| 29 | func ExtractDefaultRules(page pagination.Page) ([]DefaultRule, error) { |
| 30 | casted := page.(DefaultRulePage).Body |
| 31 | var response struct { |
| 32 | Rules []DefaultRule `mapstructure:"security_group_default_rules"` |
| 33 | } |
| 34 | |
| 35 | err := mapstructure.Decode(casted, &response) |
| 36 | |
| 37 | return response.Rules, err |
| 38 | } |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 39 | |
| 40 | type commonResult struct { |
| 41 | gophercloud.Result |
| 42 | } |
| 43 | |
| 44 | // CreateResult represents the result of a create operation. |
| 45 | type CreateResult struct { |
| 46 | commonResult |
| 47 | } |
| 48 | |
| 49 | // GetResult represents the result of a get operation. |
| 50 | type GetResult struct { |
| 51 | commonResult |
| 52 | } |
| 53 | |
| 54 | // Extract will extract a DefaultRule struct from most responses. |
| 55 | func (r commonResult) Extract() (*DefaultRule, error) { |
| 56 | if r.Err != nil { |
| 57 | return nil, r.Err |
| 58 | } |
| 59 | |
| 60 | var response struct { |
| 61 | Rule DefaultRule `mapstructure:"security_group_default_rule"` |
| 62 | } |
| 63 | |
| 64 | err := mapstructure.Decode(r.Body, &response) |
| 65 | |
| 66 | return &response.Rule, err |
| 67 | } |