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 { |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 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 | ParentGroupID string `mapstructure:"parent_group_id"` |
| 25 | Group Group |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | type IPRange struct { |
| 29 | CIDR string |
| 30 | } |
| 31 | |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 32 | type Group struct { |
| 33 | TenantID string `mapstructure:"tenant_id"` |
| 34 | Name string |
| 35 | } |
| 36 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 37 | // RolePage is a single page of a user Role collection. |
| 38 | type SecurityGroupPage struct { |
| 39 | pagination.SinglePageBase |
| 40 | } |
| 41 | |
| 42 | // IsEmpty determines whether or not a page of Security Groups contains any results. |
| 43 | func (page SecurityGroupPage) IsEmpty() (bool, error) { |
| 44 | users, err := ExtractSecurityGroups(page) |
| 45 | if err != nil { |
| 46 | return false, err |
| 47 | } |
| 48 | return len(users) == 0, nil |
| 49 | } |
| 50 | |
| 51 | // ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results. |
| 52 | func ExtractSecurityGroups(page pagination.Page) ([]SecurityGroup, error) { |
| 53 | casted := page.(SecurityGroupPage).Body |
| 54 | var response struct { |
| 55 | SecurityGroups []SecurityGroup `mapstructure:"security_groups"` |
| 56 | } |
| 57 | |
| 58 | err := mapstructure.Decode(casted, &response) |
| 59 | return response.SecurityGroups, err |
| 60 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 61 | |
| 62 | type commonResult struct { |
| 63 | gophercloud.Result |
| 64 | } |
| 65 | |
| 66 | type CreateResult struct { |
| 67 | commonResult |
| 68 | } |
| 69 | |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 70 | type GetResult struct { |
| 71 | commonResult |
| 72 | } |
| 73 | |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 74 | func (r commonResult) Extract() (*SecurityGroup, error) { |
| 75 | if r.Err != nil { |
| 76 | return nil, r.Err |
| 77 | } |
| 78 | |
| 79 | var response struct { |
| 80 | SecurityGroup SecurityGroup `mapstructure:"security_group"` |
| 81 | } |
| 82 | |
| 83 | err := mapstructure.Decode(r.Body, &response) |
| 84 | |
| 85 | return &response.SecurityGroup, err |
| 86 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame^] | 87 | |
| 88 | type AddRuleResult struct { |
| 89 | gophercloud.Result |
| 90 | } |
| 91 | |
| 92 | func (r AddRuleResult) Extract() (*Rule, error) { |
| 93 | if r.Err != nil { |
| 94 | return nil, r.Err |
| 95 | } |
| 96 | |
| 97 | var response struct { |
| 98 | Rule Rule `mapstructure:"security_group_rule"` |
| 99 | } |
| 100 | |
| 101 | err := mapstructure.Decode(r.Body, &response) |
| 102 | |
| 103 | return &response.Rule, err |
| 104 | } |