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