| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 1 | package secgroups | 
|  | 2 |  | 
|  | 3 | import ( | 
| Joe Topjian | 368deee | 2017-01-12 14:19:23 -0700 | [diff] [blame] | 4 | "encoding/json" | 
|  | 5 | "strconv" | 
|  | 6 |  | 
| Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame] | 7 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" | 
| Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 8 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 9 | ) | 
|  | 10 |  | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 11 | // SecurityGroup represents a security group. | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 12 | type SecurityGroup struct { | 
| Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 13 | // The unique ID of the group. If Neutron is installed, this ID will be | 
|  | 14 | // represented as a string UUID; if Neutron is not installed, it will be a | 
|  | 15 | // numeric ID. For the sake of consistency, we always cast it to a string. | 
| Joe Topjian | 368deee | 2017-01-12 14:19:23 -0700 | [diff] [blame] | 16 | ID string `json:"-"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 17 |  | 
|  | 18 | // The human-readable name of the group, which needs to be unique. | 
| Joe Topjian | 368deee | 2017-01-12 14:19:23 -0700 | [diff] [blame] | 19 | Name string `json:"name"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 20 |  | 
|  | 21 | // The human-readable description of the group. | 
| Joe Topjian | 368deee | 2017-01-12 14:19:23 -0700 | [diff] [blame] | 22 | Description string `json:"description"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 23 |  | 
|  | 24 | // The rules which determine how this security group operates. | 
| Joe Topjian | 368deee | 2017-01-12 14:19:23 -0700 | [diff] [blame] | 25 | Rules []Rule `json:"rules"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 26 |  | 
| Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 27 | // The ID of the tenant to which this security group belongs. | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 28 | TenantID string `json:"tenant_id"` | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 29 | } | 
|  | 30 |  | 
| Joe Topjian | 368deee | 2017-01-12 14:19:23 -0700 | [diff] [blame] | 31 | func (r *SecurityGroup) UnmarshalJSON(b []byte) error { | 
|  | 32 | type tmp SecurityGroup | 
|  | 33 | var s struct { | 
|  | 34 | tmp | 
|  | 35 | ID interface{} `json:"id"` | 
|  | 36 | } | 
|  | 37 | err := json.Unmarshal(b, &s) | 
|  | 38 | if err != nil { | 
|  | 39 | return err | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | *r = SecurityGroup(s.tmp) | 
|  | 43 |  | 
|  | 44 | switch t := s.ID.(type) { | 
|  | 45 | case float64: | 
|  | 46 | r.ID = strconv.FormatFloat(t, 'f', -1, 64) | 
|  | 47 | case string: | 
|  | 48 | r.ID = t | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | return err | 
|  | 52 | } | 
|  | 53 |  | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 54 | // Rule represents a security group rule, a policy which determines how a | 
|  | 55 | // security group operates and what inbound traffic it allows in. | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 56 | type Rule struct { | 
| Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 57 | // The unique ID. If Neutron is installed, this ID will be | 
|  | 58 | // represented as a string UUID; if Neutron is not installed, it will be a | 
|  | 59 | // numeric ID. For the sake of consistency, we always cast it to a string. | 
| Joe Topjian | 368deee | 2017-01-12 14:19:23 -0700 | [diff] [blame] | 60 | ID string `json:"-"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 61 |  | 
|  | 62 | // 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] | 63 | FromPort int `json:"from_port"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 64 |  | 
|  | 65 | // 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] | 66 | ToPort int `json:"to_port"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 67 |  | 
|  | 68 | // The IP protocol (e.g. TCP) which the security group accepts | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 69 | IPProtocol string `json:"ip_protocol"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 70 |  | 
|  | 71 | // The CIDR IP range whose traffic can be received | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 72 | IPRange IPRange `json:"ip_range"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 73 |  | 
| Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 74 | // The security group ID to which this rule belongs | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 75 | ParentGroupID string `json:"parent_group_id"` | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 76 |  | 
|  | 77 | // Not documented. | 
|  | 78 | Group Group | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
| Joe Topjian | 368deee | 2017-01-12 14:19:23 -0700 | [diff] [blame] | 81 | func (r *Rule) UnmarshalJSON(b []byte) error { | 
|  | 82 | type tmp Rule | 
|  | 83 | var s struct { | 
|  | 84 | tmp | 
|  | 85 | ID            interface{} `json:"id"` | 
|  | 86 | ParentGroupID interface{} `json:"parent_group_id"` | 
|  | 87 | } | 
|  | 88 | err := json.Unmarshal(b, &s) | 
|  | 89 | if err != nil { | 
|  | 90 | return err | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | *r = Rule(s.tmp) | 
|  | 94 |  | 
|  | 95 | switch t := s.ID.(type) { | 
|  | 96 | case float64: | 
|  | 97 | r.ID = strconv.FormatFloat(t, 'f', -1, 64) | 
|  | 98 | case string: | 
|  | 99 | r.ID = t | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | switch t := s.ParentGroupID.(type) { | 
|  | 103 | case float64: | 
|  | 104 | r.ParentGroupID = strconv.FormatFloat(t, 'f', -1, 64) | 
|  | 105 | case string: | 
|  | 106 | r.ParentGroupID = t | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | return err | 
|  | 110 | } | 
|  | 111 |  | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 112 | // IPRange represents the IP range whose traffic will be accepted by the | 
|  | 113 | // security group. | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 114 | type IPRange struct { | 
|  | 115 | CIDR string | 
|  | 116 | } | 
|  | 117 |  | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 118 | // Group represents a group. | 
| Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 119 | type Group struct { | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 120 | TenantID string `json:"tenant_id"` | 
| Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 121 | Name     string | 
|  | 122 | } | 
|  | 123 |  | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 124 | // SecurityGroupPage is a single page of a SecurityGroup collection. | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 125 | type SecurityGroupPage struct { | 
|  | 126 | pagination.SinglePageBase | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | // IsEmpty determines whether or not a page of Security Groups contains any results. | 
|  | 130 | func (page SecurityGroupPage) IsEmpty() (bool, error) { | 
|  | 131 | users, err := ExtractSecurityGroups(page) | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 132 | return len(users) == 0, err | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
|  | 135 | // ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results. | 
| Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 136 | func ExtractSecurityGroups(r pagination.Page) ([]SecurityGroup, error) { | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 137 | var s struct { | 
|  | 138 | SecurityGroups []SecurityGroup `json:"security_groups"` | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 139 | } | 
| Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 140 | err := (r.(SecurityGroupPage)).ExtractInto(&s) | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 141 | return s.SecurityGroups, err | 
| Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 142 | } | 
| Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 143 |  | 
|  | 144 | type commonResult struct { | 
|  | 145 | gophercloud.Result | 
|  | 146 | } | 
|  | 147 |  | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 148 | // CreateResult represents the result of a create operation. | 
| Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 149 | type CreateResult struct { | 
|  | 150 | commonResult | 
|  | 151 | } | 
|  | 152 |  | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 153 | // GetResult represents the result of a get operation. | 
| Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 154 | type GetResult struct { | 
|  | 155 | commonResult | 
|  | 156 | } | 
|  | 157 |  | 
| Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 158 | // UpdateResult represents the result of an update operation. | 
| Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 159 | type UpdateResult struct { | 
|  | 160 | commonResult | 
|  | 161 | } | 
|  | 162 |  | 
| Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 163 | // Extract will extract a SecurityGroup struct from most responses. | 
| Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 164 | func (r commonResult) Extract() (*SecurityGroup, error) { | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 165 | var s struct { | 
|  | 166 | SecurityGroup *SecurityGroup `json:"security_group"` | 
| Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 167 | } | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 168 | err := r.ExtractInto(&s) | 
|  | 169 | return s.SecurityGroup, err | 
| Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 170 | } | 
| Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 171 |  | 
| Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 172 | // CreateRuleResult represents the result when adding rules to a security group. | 
|  | 173 | type CreateRuleResult struct { | 
| Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 174 | gophercloud.Result | 
|  | 175 | } | 
|  | 176 |  | 
| Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 177 | // Extract will extract a Rule struct from a CreateRuleResult. | 
|  | 178 | func (r CreateRuleResult) Extract() (*Rule, error) { | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 179 | var s struct { | 
|  | 180 | Rule *Rule `json:"security_group_rule"` | 
| Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 181 | } | 
| Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 182 | err := r.ExtractInto(&s) | 
|  | 183 | return s.Rule, err | 
| Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 184 | } |