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 | |
| 28 | type CreateOpts struct { |
| 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 | |
| 38 | func Create(client *gophercloud.ServiceClient, opts CreateOpts) CreateResult { |
| 39 | var result CreateResult |
| 40 | |
| 41 | reqBody := struct { |
| 42 | CreateOpts `json:"security_group"` |
| 43 | }{opts} |
| 44 | |
| 45 | _, result.Err = perigee.Request("POST", rootURL(client), perigee.Options{ |
| 46 | Results: &result.Body, |
| 47 | ReqBody: &reqBody, |
| 48 | MoreHeaders: client.AuthenticatedHeaders(), |
| 49 | OkCodes: []int{200}, |
| 50 | }) |
| 51 | |
| 52 | return result |
| 53 | } |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 54 | |
| 55 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 56 | var result GetResult |
| 57 | |
| 58 | _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{ |
| 59 | Results: &result.Body, |
| 60 | MoreHeaders: client.AuthenticatedHeaders(), |
| 61 | OkCodes: []int{200}, |
| 62 | }) |
| 63 | |
| 64 | return result |
| 65 | } |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 66 | |
| 67 | func Delete(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult { |
| 68 | var result gophercloud.ErrResult |
| 69 | |
| 70 | _, result.Err = perigee.Request("DELETE", resourceURL(client, id), perigee.Options{ |
| 71 | MoreHeaders: client.AuthenticatedHeaders(), |
| 72 | OkCodes: []int{202}, |
| 73 | }) |
| 74 | |
| 75 | return result |
| 76 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 77 | |
| 78 | type AddRuleOpts struct { |
| 79 | // Required - the ID of the group that this rule will be added to. |
| 80 | ParentGroupID string `json:"parent_group_id"` |
| 81 | |
| 82 | // Required - the lower bound of the port range that will be opened. |
| 83 | FromPort int `json:"from_port"` |
| 84 | |
| 85 | // Required - the upper bound of the port range that will be opened. |
| 86 | ToPort int `json:"to_port"` |
| 87 | |
| 88 | // Required - the protocol type that will be allowed, e.g. TCP. |
| 89 | IPProtocol string `json:"ip_protocol"` |
| 90 | |
| 91 | // ONLY required if FromGroupID is blank. This represents the IP range that |
| 92 | // will be the source of network traffic to your security group. Use |
| 93 | // 0.0.0.0/0 to allow all IP addresses. |
| 94 | CIDR string `json:"cidr,omitempty"` |
| 95 | |
| 96 | // ONLY required if CIDR is blank. This value represents the ID of a group |
| 97 | // that forwards traffic to the parent group. So, instead of accepting |
| 98 | // network traffic from an entire IP range, you can instead refine the |
| 99 | // inbound source by an existing security group. |
| 100 | FromGroupID string `json:"group_id,omitempty"` |
| 101 | } |
| 102 | |
| 103 | func AddRule(client *gophercloud.ServiceClient, opts AddRuleOpts) AddRuleResult { |
| 104 | var result AddRuleResult |
| 105 | |
| 106 | if opts.ParentGroupID == "" { |
| 107 | result.Err = errors.New("A ParentGroupID must be set") |
| 108 | return result |
| 109 | } |
| 110 | if opts.FromPort == 0 { |
| 111 | result.Err = errors.New("A FromPort must be set") |
| 112 | return result |
| 113 | } |
| 114 | if opts.ToPort == 0 { |
| 115 | result.Err = errors.New("A ToPort must be set") |
| 116 | return result |
| 117 | } |
| 118 | if opts.IPProtocol == "" { |
| 119 | result.Err = errors.New("A IPProtocol must be set") |
| 120 | return result |
| 121 | } |
| 122 | if opts.CIDR == "" && opts.FromGroupID == "" { |
| 123 | result.Err = errors.New("A CIDR or FromGroupID must be set") |
| 124 | return result |
| 125 | } |
| 126 | |
| 127 | reqBody := struct { |
| 128 | AddRuleOpts `json:"security_group_rule"` |
| 129 | }{opts} |
| 130 | |
| 131 | _, result.Err = perigee.Request("POST", rootRuleURL(client), perigee.Options{ |
| 132 | Results: &result.Body, |
| 133 | ReqBody: &reqBody, |
| 134 | MoreHeaders: client.AuthenticatedHeaders(), |
| 135 | OkCodes: []int{200}, |
| 136 | }) |
| 137 | |
| 138 | return result |
| 139 | } |
Jamie Hannaford | 61f81ca | 2014-11-19 14:44:33 +0100 | [diff] [blame^] | 140 | |
| 141 | func DeleteRule(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult { |
| 142 | var result gophercloud.ErrResult |
| 143 | |
| 144 | _, result.Err = perigee.Request("DELETE", resourceRuleURL(client, id), perigee.Options{ |
| 145 | MoreHeaders: client.AuthenticatedHeaders(), |
| 146 | OkCodes: []int{202}, |
| 147 | }) |
| 148 | |
| 149 | return result |
| 150 | } |