Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame^] | 1 | package secgroups |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | type SecurityGroup struct { |
| 10 | ID string |
| 11 | Name string |
| 12 | Description string |
| 13 | Rules []Rule |
| 14 | TenantID string `mapstructure:"tenant_id"` |
| 15 | } |
| 16 | |
| 17 | type Rule struct { |
| 18 | ID string |
| 19 | FromPort int `mapstructure:"from_port"` |
| 20 | ToPort int `mapstructure:"to_port"` |
| 21 | IPProtocol string `mapstructure:"ip_protocol"` |
| 22 | IPRange IPRange `mapstructure:"ip_range"` |
| 23 | } |
| 24 | |
| 25 | type IPRange struct { |
| 26 | CIDR string |
| 27 | } |
| 28 | |
| 29 | // RolePage is a single page of a user Role collection. |
| 30 | type SecurityGroupPage struct { |
| 31 | pagination.SinglePageBase |
| 32 | } |
| 33 | |
| 34 | // IsEmpty determines whether or not a page of Security Groups contains any results. |
| 35 | func (page SecurityGroupPage) IsEmpty() (bool, error) { |
| 36 | users, err := ExtractSecurityGroups(page) |
| 37 | if err != nil { |
| 38 | return false, err |
| 39 | } |
| 40 | return len(users) == 0, nil |
| 41 | } |
| 42 | |
| 43 | // ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results. |
| 44 | func ExtractSecurityGroups(page pagination.Page) ([]SecurityGroup, error) { |
| 45 | casted := page.(SecurityGroupPage).Body |
| 46 | var response struct { |
| 47 | SecurityGroups []SecurityGroup `mapstructure:"security_groups"` |
| 48 | } |
| 49 | |
| 50 | err := mapstructure.Decode(casted, &response) |
| 51 | return response.SecurityGroups, err |
| 52 | } |