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