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 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 10 | // SecurityGroup represents a security group. |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 11 | type SecurityGroup struct { |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 12 | // The unique ID of the group. |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame^] | 13 | ID int |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 14 | |
| 15 | // The human-readable name of the group, which needs to be unique. |
| 16 | Name string |
| 17 | |
| 18 | // The human-readable description of the group. |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 19 | Description string |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 20 | |
| 21 | // The rules which determine how this security group operates. |
| 22 | Rules []Rule |
| 23 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 24 | // The ID of the tenant to which this security group belongs. |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 25 | TenantID string `mapstructure:"tenant_id"` |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 26 | } |
| 27 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 28 | // Rule represents a security group rule, a policy which determines how a |
| 29 | // security group operates and what inbound traffic it allows in. |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 30 | type Rule struct { |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 31 | // The unique ID |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame^] | 32 | ID int |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 33 | |
| 34 | // The lower bound of the port range which this security group should open up |
| 35 | FromPort int `mapstructure:"from_port"` |
| 36 | |
| 37 | // The upper bound of the port range which this security group should open up |
| 38 | ToPort int `mapstructure:"to_port"` |
| 39 | |
| 40 | // The IP protocol (e.g. TCP) which the security group accepts |
| 41 | IPProtocol string `mapstructure:"ip_protocol"` |
| 42 | |
| 43 | // The CIDR IP range whose traffic can be received |
| 44 | IPRange IPRange `mapstructure:"ip_range"` |
| 45 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 46 | // The security group ID to which this rule belongs |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame^] | 47 | ParentGroupID int `mapstructure:"parent_group_id"` |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 48 | |
| 49 | // Not documented. |
| 50 | Group Group |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 53 | // IPRange represents the IP range whose traffic will be accepted by the |
| 54 | // security group. |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 55 | type IPRange struct { |
| 56 | CIDR string |
| 57 | } |
| 58 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 59 | // Group represents a group. |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 60 | type Group struct { |
| 61 | TenantID string `mapstructure:"tenant_id"` |
| 62 | Name string |
| 63 | } |
| 64 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 65 | // SecurityGroupPage is a single page of a SecurityGroup collection. |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 66 | type SecurityGroupPage struct { |
| 67 | pagination.SinglePageBase |
| 68 | } |
| 69 | |
| 70 | // IsEmpty determines whether or not a page of Security Groups contains any results. |
| 71 | func (page SecurityGroupPage) IsEmpty() (bool, error) { |
| 72 | users, err := ExtractSecurityGroups(page) |
| 73 | if err != nil { |
| 74 | return false, err |
| 75 | } |
| 76 | return len(users) == 0, nil |
| 77 | } |
| 78 | |
| 79 | // ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results. |
| 80 | func ExtractSecurityGroups(page pagination.Page) ([]SecurityGroup, error) { |
| 81 | casted := page.(SecurityGroupPage).Body |
| 82 | var response struct { |
| 83 | SecurityGroups []SecurityGroup `mapstructure:"security_groups"` |
| 84 | } |
| 85 | |
| 86 | err := mapstructure.Decode(casted, &response) |
| 87 | return response.SecurityGroups, err |
| 88 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 89 | |
| 90 | type commonResult struct { |
| 91 | gophercloud.Result |
| 92 | } |
| 93 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 94 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 95 | type CreateResult struct { |
| 96 | commonResult |
| 97 | } |
| 98 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 99 | // GetResult represents the result of a get operation. |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 100 | type GetResult struct { |
| 101 | commonResult |
| 102 | } |
| 103 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 104 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 105 | type UpdateResult struct { |
| 106 | commonResult |
| 107 | } |
| 108 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 109 | // Extract will extract a SecurityGroup struct from most responses. |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 110 | func (r commonResult) Extract() (*SecurityGroup, error) { |
| 111 | if r.Err != nil { |
| 112 | return nil, r.Err |
| 113 | } |
| 114 | |
| 115 | var response struct { |
| 116 | SecurityGroup SecurityGroup `mapstructure:"security_group"` |
| 117 | } |
| 118 | |
| 119 | err := mapstructure.Decode(r.Body, &response) |
| 120 | |
| 121 | return &response.SecurityGroup, err |
| 122 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 123 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 124 | // CreateRuleResult represents the result when adding rules to a security group. |
| 125 | type CreateRuleResult struct { |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 126 | gophercloud.Result |
| 127 | } |
| 128 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 129 | // Extract will extract a Rule struct from a CreateRuleResult. |
| 130 | func (r CreateRuleResult) Extract() (*Rule, error) { |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 131 | if r.Err != nil { |
| 132 | return nil, r.Err |
| 133 | } |
| 134 | |
| 135 | var response struct { |
| 136 | Rule Rule `mapstructure:"security_group_rule"` |
| 137 | } |
| 138 | |
| 139 | err := mapstructure.Decode(r.Body, &response) |
| 140 | |
| 141 | return &response.Rule, err |
| 142 | } |