Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 1 | package secgroups |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 6 | ) |
| 7 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 8 | // SecurityGroup represents a security group. |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 9 | type SecurityGroup struct { |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 10 | // The unique ID of the group. If Neutron is installed, this ID will be |
| 11 | // represented as a string UUID; if Neutron is not installed, it will be a |
| 12 | // numeric ID. For the sake of consistency, we always cast it to a string. |
| 13 | ID string |
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. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 25 | TenantID string `json:"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 | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 31 | // The unique ID. If Neutron is installed, this ID will be |
| 32 | // represented as a string UUID; if Neutron is not installed, it will be a |
| 33 | // numeric ID. For the sake of consistency, we always cast it to a string. |
| 34 | ID string |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 35 | |
| 36 | // The lower bound of the port range which this security group should open up |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 37 | FromPort int `json:"from_port"` |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 38 | |
| 39 | // The upper bound of the port range which this security group should open up |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 40 | ToPort int `json:"to_port"` |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 41 | |
| 42 | // The IP protocol (e.g. TCP) which the security group accepts |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 43 | IPProtocol string `json:"ip_protocol"` |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 44 | |
| 45 | // The CIDR IP range whose traffic can be received |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 46 | IPRange IPRange `json:"ip_range"` |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 47 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 48 | // The security group ID to which this rule belongs |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 49 | ParentGroupID string `json:"parent_group_id"` |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 50 | |
| 51 | // Not documented. |
| 52 | Group Group |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 55 | // IPRange represents the IP range whose traffic will be accepted by the |
| 56 | // security group. |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 57 | type IPRange struct { |
| 58 | CIDR string |
| 59 | } |
| 60 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 61 | // Group represents a group. |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 62 | type Group struct { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 63 | TenantID string `json:"tenant_id"` |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 64 | Name string |
| 65 | } |
| 66 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 67 | // SecurityGroupPage is a single page of a SecurityGroup collection. |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 68 | type SecurityGroupPage struct { |
| 69 | pagination.SinglePageBase |
| 70 | } |
| 71 | |
| 72 | // IsEmpty determines whether or not a page of Security Groups contains any results. |
| 73 | func (page SecurityGroupPage) IsEmpty() (bool, error) { |
| 74 | users, err := ExtractSecurityGroups(page) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 75 | return len(users) == 0, err |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results. |
| 79 | func ExtractSecurityGroups(page pagination.Page) ([]SecurityGroup, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 80 | r := page.(SecurityGroupPage) |
| 81 | var s struct { |
| 82 | SecurityGroups []SecurityGroup `json:"security_groups"` |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 83 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 84 | err := r.ExtractInto(&s) |
| 85 | return s.SecurityGroups, err |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 86 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 87 | |
| 88 | type commonResult struct { |
| 89 | gophercloud.Result |
| 90 | } |
| 91 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 92 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 93 | type CreateResult struct { |
| 94 | commonResult |
| 95 | } |
| 96 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 97 | // GetResult represents the result of a get operation. |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 98 | type GetResult struct { |
| 99 | commonResult |
| 100 | } |
| 101 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 102 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 103 | type UpdateResult struct { |
| 104 | commonResult |
| 105 | } |
| 106 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 107 | // Extract will extract a SecurityGroup struct from most responses. |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 108 | func (r commonResult) Extract() (*SecurityGroup, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 109 | var s struct { |
| 110 | SecurityGroup *SecurityGroup `json:"security_group"` |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 111 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 112 | err := r.ExtractInto(&s) |
| 113 | return s.SecurityGroup, err |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 114 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 115 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 116 | // CreateRuleResult represents the result when adding rules to a security group. |
| 117 | type CreateRuleResult struct { |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 118 | gophercloud.Result |
| 119 | } |
| 120 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 121 | // Extract will extract a Rule struct from a CreateRuleResult. |
| 122 | func (r CreateRuleResult) Extract() (*Rule, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 123 | var s struct { |
| 124 | Rule *Rule `json:"security_group_rule"` |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 125 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 126 | err := r.ExtractInto(&s) |
| 127 | return s.Rule, err |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 128 | } |