Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 1 | package defsecrules |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups" |
| 6 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 7 | ) |
| 8 | |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 9 | // DefaultRule represents a default rule - which is identical to a |
| 10 | // normal security rule. |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 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) |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 21 | return len(users) == 0, err |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | // ExtractDefaultRules returns a slice of DefaultRules contained in a single |
| 25 | // page of results. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 26 | func ExtractDefaultRules(r pagination.Page) ([]DefaultRule, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 27 | var s struct { |
| 28 | DefaultRules []DefaultRule `json:"security_group_default_rules"` |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 29 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 30 | err := (r.(DefaultRulePage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 31 | return s.DefaultRules, err |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 32 | } |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 33 | |
| 34 | type commonResult struct { |
| 35 | gophercloud.Result |
| 36 | } |
| 37 | |
| 38 | // CreateResult represents the result of a create operation. |
| 39 | type CreateResult struct { |
| 40 | commonResult |
| 41 | } |
| 42 | |
| 43 | // GetResult represents the result of a get operation. |
| 44 | type GetResult struct { |
| 45 | commonResult |
| 46 | } |
| 47 | |
| 48 | // Extract will extract a DefaultRule struct from most responses. |
| 49 | func (r commonResult) Extract() (*DefaultRule, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 50 | var s struct { |
| 51 | DefaultRule DefaultRule `json:"security_group_default_rule"` |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 52 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 53 | err := r.ExtractInto(&s) |
| 54 | return &s.DefaultRule, err |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 55 | } |