Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 1 | package secgroups |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 4 | "errors" |
| 5 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 10 | func commonList(client *gophercloud.ServiceClient, url string) pagination.Pager { |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 11 | createPage := func(r pagination.PageResult) pagination.Page { |
| 12 | return SecurityGroupPage{pagination.SinglePageBase(r)} |
| 13 | } |
| 14 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 15 | return pagination.NewPager(client, url, createPage) |
| 16 | } |
| 17 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 18 | // List will return a collection of all the security groups for a particular |
| 19 | // tenant. |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 20 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 21 | return commonList(client, rootURL(client)) |
| 22 | } |
| 23 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 24 | // ListByServer will return a collection of all the security groups which are |
| 25 | // associated with a particular server. |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 26 | func ListByServer(client *gophercloud.ServiceClient, serverID string) pagination.Pager { |
| 27 | return commonList(client, listByServerURL(client, serverID)) |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 28 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 29 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 30 | // GroupOpts is the underlying struct responsible for creating or updating |
| 31 | // security groups. It therefore represents the mutable attributes of a |
| 32 | // security group. |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 33 | type GroupOpts struct { |
Jamie Hannaford | 415ff94 | 2014-11-25 15:25:57 +0100 | [diff] [blame] | 34 | // Required - the name of your security group. |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 35 | Name string `json:"name"` |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 36 | |
Jamie Hannaford | 415ff94 | 2014-11-25 15:25:57 +0100 | [diff] [blame] | 37 | // Required - the description of your security group. |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 38 | Description string `json:"description"` |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 41 | // CreateOpts is the struct responsible for creating a security group. |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 42 | type CreateOpts GroupOpts |
| 43 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 44 | // CreateOptsBuilder builds the create options into a serializable format. |
| 45 | type CreateOptsBuilder interface { |
| 46 | ToSecGroupCreateMap() (map[string]interface{}, error) |
| 47 | } |
| 48 | |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 49 | var ( |
| 50 | errName = errors.New("Name is a required field") |
| 51 | errDesc = errors.New("Description is a required field") |
| 52 | ) |
| 53 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 54 | // ToSecGroupCreateMap builds the create options into a serializable format. |
| 55 | func (opts CreateOpts) ToSecGroupCreateMap() (map[string]interface{}, error) { |
| 56 | sg := make(map[string]interface{}) |
| 57 | |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 58 | if opts.Name == "" { |
| 59 | return sg, errName |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 60 | } |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 61 | if opts.Description == "" { |
| 62 | return sg, errDesc |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 65 | sg["name"] = opts.Name |
| 66 | sg["description"] = opts.Description |
| 67 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 68 | return map[string]interface{}{"security_group": sg}, nil |
| 69 | } |
| 70 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 71 | // Create will create a new security group. |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 72 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 73 | var result CreateResult |
| 74 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 75 | reqBody, err := opts.ToSecGroupCreateMap() |
| 76 | if err != nil { |
| 77 | result.Err = err |
| 78 | return result |
| 79 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 80 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 81 | _, result.Err = client.Request("POST", rootURL(client), gophercloud.RequestOpts{ |
| 82 | JSONResponse: &result.Body, |
| 83 | JSONBody: &reqBody, |
| 84 | OkCodes: []int{200}, |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 85 | }) |
| 86 | |
| 87 | return result |
| 88 | } |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 89 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 90 | // UpdateOpts is the struct responsible for updating an existing security group. |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 91 | type UpdateOpts GroupOpts |
| 92 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 93 | // UpdateOptsBuilder builds the update options into a serializable format. |
| 94 | type UpdateOptsBuilder interface { |
| 95 | ToSecGroupUpdateMap() (map[string]interface{}, error) |
| 96 | } |
| 97 | |
| 98 | // ToSecGroupUpdateMap builds the update options into a serializable format. |
| 99 | func (opts UpdateOpts) ToSecGroupUpdateMap() (map[string]interface{}, error) { |
| 100 | sg := make(map[string]interface{}) |
| 101 | |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 102 | if opts.Name == "" { |
| 103 | return sg, errName |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 104 | } |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 105 | if opts.Description == "" { |
| 106 | return sg, errDesc |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 109 | sg["name"] = opts.Name |
| 110 | sg["description"] = opts.Description |
| 111 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 112 | return map[string]interface{}{"security_group": sg}, nil |
| 113 | } |
| 114 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 115 | // Update will modify the mutable properties of a security group, notably its |
| 116 | // name and description. |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 117 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 118 | var result UpdateResult |
| 119 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 120 | reqBody, err := opts.ToSecGroupUpdateMap() |
| 121 | if err != nil { |
| 122 | result.Err = err |
| 123 | return result |
| 124 | } |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 125 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 126 | _, result.Err = client.Request("PUT", resourceURL(client, id), gophercloud.RequestOpts{ |
| 127 | JSONResponse: &result.Body, |
| 128 | JSONBody: &reqBody, |
| 129 | OkCodes: []int{200}, |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 130 | }) |
| 131 | |
| 132 | return result |
| 133 | } |
| 134 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 135 | // Get will return details for a particular security group. |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 136 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 137 | var result GetResult |
| 138 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 139 | _, result.Err = client.Request("GET", resourceURL(client, id), gophercloud.RequestOpts{ |
| 140 | JSONResponse: &result.Body, |
| 141 | OkCodes: []int{200}, |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 142 | }) |
| 143 | |
| 144 | return result |
| 145 | } |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 146 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 147 | // Delete will permanently delete a security group from the project. |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 148 | func Delete(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult { |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 149 | var result gophercloud.ErrResult |
| 150 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 151 | _, result.Err = client.Request("DELETE", resourceURL(client, id), gophercloud.RequestOpts{ |
| 152 | OkCodes: []int{202}, |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 153 | }) |
| 154 | |
| 155 | return result |
| 156 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 157 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 158 | // CreateRuleOpts represents the configuration for adding a new rule to an |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 159 | // existing security group. |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 160 | type CreateRuleOpts struct { |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 161 | // Required - the ID of the group that this rule will be added to. |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 162 | ParentGroupID string `json:"parent_group_id"` |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 163 | |
| 164 | // Required - the lower bound of the port range that will be opened. |
| 165 | FromPort int `json:"from_port"` |
| 166 | |
| 167 | // Required - the upper bound of the port range that will be opened. |
| 168 | ToPort int `json:"to_port"` |
| 169 | |
| 170 | // Required - the protocol type that will be allowed, e.g. TCP. |
| 171 | IPProtocol string `json:"ip_protocol"` |
| 172 | |
| 173 | // ONLY required if FromGroupID is blank. This represents the IP range that |
| 174 | // will be the source of network traffic to your security group. Use |
| 175 | // 0.0.0.0/0 to allow all IP addresses. |
| 176 | CIDR string `json:"cidr,omitempty"` |
| 177 | |
| 178 | // ONLY required if CIDR is blank. This value represents the ID of a group |
| 179 | // that forwards traffic to the parent group. So, instead of accepting |
| 180 | // network traffic from an entire IP range, you can instead refine the |
| 181 | // inbound source by an existing security group. |
| 182 | FromGroupID string `json:"group_id,omitempty"` |
| 183 | } |
| 184 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 185 | // CreateRuleOptsBuilder builds the create rule options into a serializable format. |
| 186 | type CreateRuleOptsBuilder interface { |
| 187 | ToRuleCreateMap() (map[string]interface{}, error) |
| 188 | } |
| 189 | |
| 190 | // ToRuleCreateMap builds the create rule options into a serializable format. |
| 191 | func (opts CreateRuleOpts) ToRuleCreateMap() (map[string]interface{}, error) { |
| 192 | rule := make(map[string]interface{}) |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 193 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 194 | if opts.ParentGroupID == "" { |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 195 | return rule, errors.New("A ParentGroupID must be set") |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 196 | } |
| 197 | if opts.FromPort == 0 { |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 198 | return rule, errors.New("A FromPort must be set") |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 199 | } |
| 200 | if opts.ToPort == 0 { |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 201 | return rule, errors.New("A ToPort must be set") |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 202 | } |
| 203 | if opts.IPProtocol == "" { |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 204 | return rule, errors.New("A IPProtocol must be set") |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 205 | } |
| 206 | if opts.CIDR == "" && opts.FromGroupID == "" { |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 207 | return rule, errors.New("A CIDR or FromGroupID must be set") |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 210 | rule["parent_group_id"] = opts.ParentGroupID |
| 211 | rule["from_port"] = opts.FromPort |
| 212 | rule["to_port"] = opts.ToPort |
| 213 | rule["ip_protocol"] = opts.IPProtocol |
| 214 | |
| 215 | if opts.CIDR != "" { |
| 216 | rule["cidr"] = opts.CIDR |
| 217 | } |
| 218 | if opts.FromGroupID != "" { |
| 219 | rule["from_group_id"] = opts.FromGroupID |
| 220 | } |
| 221 | |
| 222 | return map[string]interface{}{"security_group_rule": rule}, nil |
| 223 | } |
| 224 | |
| 225 | // CreateRule will add a new rule to an existing security group (whose ID is |
| 226 | // specified in CreateRuleOpts). You have the option of controlling inbound |
| 227 | // traffic from either an IP range (CIDR) or from another security group. |
| 228 | func CreateRule(client *gophercloud.ServiceClient, opts CreateRuleOptsBuilder) CreateRuleResult { |
| 229 | var result CreateRuleResult |
| 230 | |
| 231 | reqBody, err := opts.ToRuleCreateMap() |
| 232 | if err != nil { |
| 233 | result.Err = err |
| 234 | return result |
| 235 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 236 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 237 | _, result.Err = client.Request("POST", rootRuleURL(client), gophercloud.RequestOpts{ |
| 238 | JSONResponse: &result.Body, |
| 239 | JSONBody: &reqBody, |
| 240 | OkCodes: []int{200}, |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 241 | }) |
| 242 | |
| 243 | return result |
| 244 | } |
Jamie Hannaford | 61f81ca | 2014-11-19 14:44:33 +0100 | [diff] [blame] | 245 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 246 | // DeleteRule will permanently delete a rule from a security group. |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 247 | func DeleteRule(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult { |
Jamie Hannaford | 61f81ca | 2014-11-19 14:44:33 +0100 | [diff] [blame] | 248 | var result gophercloud.ErrResult |
| 249 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 250 | _, result.Err = client.Request("DELETE", resourceRuleURL(client, id), gophercloud.RequestOpts{ |
| 251 | OkCodes: []int{202}, |
Jamie Hannaford | 61f81ca | 2014-11-19 14:44:33 +0100 | [diff] [blame] | 252 | }) |
| 253 | |
| 254 | return result |
| 255 | } |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 256 | |
| 257 | func actionMap(prefix, groupName string) map[string]map[string]string { |
| 258 | return map[string]map[string]string{ |
| 259 | prefix + "SecurityGroup": map[string]string{"name": groupName}, |
| 260 | } |
| 261 | } |
| 262 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 263 | // AddServerToGroup will associate a server and a security group, enforcing the |
| 264 | // rules of the group on the server. |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 265 | func AddServerToGroup(client *gophercloud.ServiceClient, serverID, groupName string) gophercloud.ErrResult { |
| 266 | var result gophercloud.ErrResult |
| 267 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 268 | _, result.Err = client.Request("POST", serverActionURL(client, serverID), gophercloud.RequestOpts{ |
| 269 | JSONResponse: &result.Body, |
| 270 | JSONBody: actionMap("add", groupName), |
| 271 | OkCodes: []int{202}, |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 272 | }) |
| 273 | |
| 274 | return result |
| 275 | } |
| 276 | |
Jamie Hannaford | 7f34d8e | 2014-11-20 12:24:55 +0100 | [diff] [blame] | 277 | // RemoveServerFromGroup will disassociate a server from a security group. |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 278 | func RemoveServerFromGroup(client *gophercloud.ServiceClient, serverID, groupName string) gophercloud.ErrResult { |
| 279 | var result gophercloud.ErrResult |
| 280 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 281 | _, result.Err = client.Request("POST", serverActionURL(client, serverID), gophercloud.RequestOpts{ |
| 282 | JSONResponse: &result.Body, |
| 283 | JSONBody: actionMap("remove", groupName), |
| 284 | OkCodes: []int{202}, |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 285 | }) |
| 286 | |
| 287 | return result |
| 288 | } |