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 | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 6 | "github.com/racker/perigee" |
| 7 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | ) |
| 11 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 12 | func commonList(client *gophercloud.ServiceClient, url string) pagination.Pager { |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 13 | createPage := func(r pagination.PageResult) pagination.Page { |
| 14 | return SecurityGroupPage{pagination.SinglePageBase(r)} |
| 15 | } |
| 16 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 17 | return pagination.NewPager(client, url, createPage) |
| 18 | } |
| 19 | |
| 20 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 21 | return commonList(client, rootURL(client)) |
| 22 | } |
| 23 | |
| 24 | func ListByServer(client *gophercloud.ServiceClient, serverID string) pagination.Pager { |
| 25 | return commonList(client, listByServerURL(client, serverID)) |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 26 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 27 | |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 28 | type GroupOpts struct { |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 29 | // Optional - the name of your security group. If no value provided, null |
| 30 | // will be set. |
| 31 | Name string `json:"name,omitempty"` |
| 32 | |
| 33 | // Optional - the description of your security group. If no value provided, |
| 34 | // null will be set. |
| 35 | Description string `json:"description,omitempty"` |
| 36 | } |
| 37 | |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 38 | type CreateOpts GroupOpts |
| 39 | |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 40 | func Create(client *gophercloud.ServiceClient, opts CreateOpts) CreateResult { |
| 41 | var result CreateResult |
| 42 | |
| 43 | reqBody := struct { |
| 44 | CreateOpts `json:"security_group"` |
| 45 | }{opts} |
| 46 | |
| 47 | _, result.Err = perigee.Request("POST", rootURL(client), perigee.Options{ |
| 48 | Results: &result.Body, |
| 49 | ReqBody: &reqBody, |
| 50 | MoreHeaders: client.AuthenticatedHeaders(), |
| 51 | OkCodes: []int{200}, |
| 52 | }) |
| 53 | |
| 54 | return result |
| 55 | } |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 56 | |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 57 | type UpdateOpts GroupOpts |
| 58 | |
| 59 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult { |
| 60 | var result UpdateResult |
| 61 | |
| 62 | reqBody := struct { |
| 63 | UpdateOpts `json:"security_group"` |
| 64 | }{opts} |
| 65 | |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 66 | _, result.Err = perigee.Request("PUT", resourceURL(client, id), perigee.Options{ |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 67 | Results: &result.Body, |
| 68 | ReqBody: &reqBody, |
| 69 | MoreHeaders: client.AuthenticatedHeaders(), |
| 70 | OkCodes: []int{200}, |
| 71 | }) |
| 72 | |
| 73 | return result |
| 74 | } |
| 75 | |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 76 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 77 | var result GetResult |
| 78 | |
| 79 | _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{ |
| 80 | Results: &result.Body, |
| 81 | MoreHeaders: client.AuthenticatedHeaders(), |
| 82 | OkCodes: []int{200}, |
| 83 | }) |
| 84 | |
| 85 | return result |
| 86 | } |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 87 | |
| 88 | func Delete(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult { |
| 89 | var result gophercloud.ErrResult |
| 90 | |
| 91 | _, result.Err = perigee.Request("DELETE", resourceURL(client, id), perigee.Options{ |
| 92 | MoreHeaders: client.AuthenticatedHeaders(), |
| 93 | OkCodes: []int{202}, |
| 94 | }) |
| 95 | |
| 96 | return result |
| 97 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 98 | |
| 99 | type AddRuleOpts struct { |
| 100 | // Required - the ID of the group that this rule will be added to. |
| 101 | ParentGroupID string `json:"parent_group_id"` |
| 102 | |
| 103 | // Required - the lower bound of the port range that will be opened. |
| 104 | FromPort int `json:"from_port"` |
| 105 | |
| 106 | // Required - the upper bound of the port range that will be opened. |
| 107 | ToPort int `json:"to_port"` |
| 108 | |
| 109 | // Required - the protocol type that will be allowed, e.g. TCP. |
| 110 | IPProtocol string `json:"ip_protocol"` |
| 111 | |
| 112 | // ONLY required if FromGroupID is blank. This represents the IP range that |
| 113 | // will be the source of network traffic to your security group. Use |
| 114 | // 0.0.0.0/0 to allow all IP addresses. |
| 115 | CIDR string `json:"cidr,omitempty"` |
| 116 | |
| 117 | // ONLY required if CIDR is blank. This value represents the ID of a group |
| 118 | // that forwards traffic to the parent group. So, instead of accepting |
| 119 | // network traffic from an entire IP range, you can instead refine the |
| 120 | // inbound source by an existing security group. |
| 121 | FromGroupID string `json:"group_id,omitempty"` |
| 122 | } |
| 123 | |
| 124 | func AddRule(client *gophercloud.ServiceClient, opts AddRuleOpts) AddRuleResult { |
| 125 | var result AddRuleResult |
| 126 | |
| 127 | if opts.ParentGroupID == "" { |
| 128 | result.Err = errors.New("A ParentGroupID must be set") |
| 129 | return result |
| 130 | } |
| 131 | if opts.FromPort == 0 { |
| 132 | result.Err = errors.New("A FromPort must be set") |
| 133 | return result |
| 134 | } |
| 135 | if opts.ToPort == 0 { |
| 136 | result.Err = errors.New("A ToPort must be set") |
| 137 | return result |
| 138 | } |
| 139 | if opts.IPProtocol == "" { |
| 140 | result.Err = errors.New("A IPProtocol must be set") |
| 141 | return result |
| 142 | } |
| 143 | if opts.CIDR == "" && opts.FromGroupID == "" { |
| 144 | result.Err = errors.New("A CIDR or FromGroupID must be set") |
| 145 | return result |
| 146 | } |
| 147 | |
| 148 | reqBody := struct { |
| 149 | AddRuleOpts `json:"security_group_rule"` |
| 150 | }{opts} |
| 151 | |
| 152 | _, result.Err = perigee.Request("POST", rootRuleURL(client), perigee.Options{ |
| 153 | Results: &result.Body, |
| 154 | ReqBody: &reqBody, |
| 155 | MoreHeaders: client.AuthenticatedHeaders(), |
| 156 | OkCodes: []int{200}, |
| 157 | }) |
| 158 | |
| 159 | return result |
| 160 | } |
Jamie Hannaford | 61f81ca | 2014-11-19 14:44:33 +0100 | [diff] [blame] | 161 | |
| 162 | func DeleteRule(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult { |
| 163 | var result gophercloud.ErrResult |
| 164 | |
| 165 | _, result.Err = perigee.Request("DELETE", resourceRuleURL(client, id), perigee.Options{ |
| 166 | MoreHeaders: client.AuthenticatedHeaders(), |
| 167 | OkCodes: []int{202}, |
| 168 | }) |
| 169 | |
| 170 | return result |
| 171 | } |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 172 | |
| 173 | func actionMap(prefix, groupName string) map[string]map[string]string { |
| 174 | return map[string]map[string]string{ |
| 175 | prefix + "SecurityGroup": map[string]string{"name": groupName}, |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func AddServerToGroup(client *gophercloud.ServiceClient, serverID, groupName string) gophercloud.ErrResult { |
| 180 | var result gophercloud.ErrResult |
| 181 | |
| 182 | _, result.Err = perigee.Request("POST", serverActionURL(client, serverID), perigee.Options{ |
| 183 | Results: &result.Body, |
| 184 | ReqBody: actionMap("add", groupName), |
| 185 | MoreHeaders: client.AuthenticatedHeaders(), |
Jamie Hannaford | d8ac5bb | 2014-11-20 12:01:37 +0100 | [diff] [blame] | 186 | OkCodes: []int{202}, |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 187 | }) |
| 188 | |
| 189 | return result |
| 190 | } |
| 191 | |
| 192 | func RemoveServerFromGroup(client *gophercloud.ServiceClient, serverID, groupName string) gophercloud.ErrResult { |
| 193 | var result gophercloud.ErrResult |
| 194 | |
| 195 | _, result.Err = perigee.Request("POST", serverActionURL(client, serverID), perigee.Options{ |
| 196 | Results: &result.Body, |
| 197 | ReqBody: actionMap("remove", groupName), |
| 198 | MoreHeaders: client.AuthenticatedHeaders(), |
Jamie Hannaford | d8ac5bb | 2014-11-20 12:01:37 +0100 | [diff] [blame] | 199 | OkCodes: []int{202}, |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 200 | }) |
| 201 | |
| 202 | return result |
| 203 | } |