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 | |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 11 | // DefaultRule represents a default rule - which is identical to a |
| 12 | // normal security rule. |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 13 | type DefaultRule secgroups.Rule |
| 14 | |
| 15 | // DefaultRulePage is a single page of a DefaultRule collection. |
| 16 | type DefaultRulePage struct { |
| 17 | pagination.SinglePageBase |
| 18 | } |
| 19 | |
| 20 | // IsEmpty determines whether or not a page of default rules contains any results. |
| 21 | func (page DefaultRulePage) IsEmpty() (bool, error) { |
| 22 | users, err := ExtractDefaultRules(page) |
| 23 | if err != nil { |
| 24 | return false, err |
| 25 | } |
| 26 | return len(users) == 0, nil |
| 27 | } |
| 28 | |
| 29 | // ExtractDefaultRules returns a slice of DefaultRules contained in a single |
| 30 | // page of results. |
| 31 | func ExtractDefaultRules(page pagination.Page) ([]DefaultRule, error) { |
| 32 | casted := page.(DefaultRulePage).Body |
| 33 | var response struct { |
| 34 | Rules []DefaultRule `mapstructure:"security_group_default_rules"` |
| 35 | } |
| 36 | |
Jamie Hannaford | c8c02c6 | 2014-11-25 12:04:57 +0100 | [diff] [blame] | 37 | err := mapstructure.WeakDecode(casted, &response) |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 38 | |
| 39 | return response.Rules, err |
| 40 | } |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 41 | |
| 42 | type commonResult struct { |
| 43 | gophercloud.Result |
| 44 | } |
| 45 | |
| 46 | // CreateResult represents the result of a create operation. |
| 47 | type CreateResult struct { |
| 48 | commonResult |
| 49 | } |
| 50 | |
| 51 | // GetResult represents the result of a get operation. |
| 52 | type GetResult struct { |
| 53 | commonResult |
| 54 | } |
| 55 | |
| 56 | // Extract will extract a DefaultRule struct from most responses. |
| 57 | func (r commonResult) Extract() (*DefaultRule, error) { |
| 58 | if r.Err != nil { |
| 59 | return nil, r.Err |
| 60 | } |
| 61 | |
| 62 | var response struct { |
| 63 | Rule DefaultRule `mapstructure:"security_group_default_rule"` |
| 64 | } |
| 65 | |
Jamie Hannaford | c8c02c6 | 2014-11-25 12:04:57 +0100 | [diff] [blame] | 66 | err := mapstructure.WeakDecode(r.Body, &response) |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 67 | |
| 68 | return &response.Rule, err |
| 69 | } |