Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 1 | package defsecrules |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 4 | "errors" |
| 5 | |
| 6 | "github.com/racker/perigee" |
| 7 | |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | ) |
| 11 | |
| 12 | // List will return a collection of default rules. |
| 13 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 14 | createPage := func(r pagination.PageResult) pagination.Page { |
| 15 | return DefaultRulePage{pagination.SinglePageBase(r)} |
| 16 | } |
| 17 | |
| 18 | return pagination.NewPager(client, rootURL(client), createPage) |
| 19 | } |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 20 | |
| 21 | // CreateOpts represents the configuration for adding a new default rule. |
| 22 | type CreateOpts struct { |
| 23 | // Required - the lower bound of the port range that will be opened. |
| 24 | FromPort int `json:"from_port"` |
| 25 | |
| 26 | // Required - the upper bound of the port range that will be opened. |
| 27 | ToPort int `json:"to_port"` |
| 28 | |
| 29 | // Required - the protocol type that will be allowed, e.g. TCP. |
| 30 | IPProtocol string `json:"ip_protocol"` |
| 31 | |
| 32 | // ONLY required if FromGroupID is blank. This represents the IP range that |
| 33 | // will be the source of network traffic to your security group. Use |
| 34 | // 0.0.0.0/0 to allow all IP addresses. |
| 35 | CIDR string `json:"cidr,omitempty"` |
| 36 | } |
| 37 | |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 38 | // CreateOptsBuilder builds the create rule options into a serializable format. |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 39 | type CreateOptsBuilder interface { |
| 40 | ToRuleCreateMap() (map[string]interface{}, error) |
| 41 | } |
| 42 | |
| 43 | // ToRuleCreateMap builds the create rule options into a serializable format. |
| 44 | func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) { |
| 45 | rule := make(map[string]interface{}) |
| 46 | |
| 47 | if opts.FromPort == 0 { |
| 48 | return rule, errors.New("A FromPort must be set") |
| 49 | } |
| 50 | if opts.ToPort == 0 { |
| 51 | return rule, errors.New("A ToPort must be set") |
| 52 | } |
| 53 | if opts.IPProtocol == "" { |
| 54 | return rule, errors.New("A IPProtocol must be set") |
| 55 | } |
| 56 | if opts.CIDR == "" { |
| 57 | return rule, errors.New("A CIDR must be set") |
| 58 | } |
| 59 | |
| 60 | rule["from_port"] = opts.FromPort |
| 61 | rule["to_port"] = opts.ToPort |
| 62 | rule["ip_protocol"] = opts.IPProtocol |
| 63 | rule["cidr"] = opts.CIDR |
| 64 | |
| 65 | return map[string]interface{}{"security_group_default_rule": rule}, nil |
| 66 | } |
| 67 | |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 68 | // Create is the operation responsible for creating a new default rule. |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 69 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 70 | var result CreateResult |
| 71 | |
| 72 | reqBody, err := opts.ToRuleCreateMap() |
| 73 | if err != nil { |
| 74 | result.Err = err |
| 75 | return result |
| 76 | } |
| 77 | |
| 78 | _, result.Err = perigee.Request("POST", rootURL(client), perigee.Options{ |
| 79 | Results: &result.Body, |
| 80 | ReqBody: &reqBody, |
| 81 | MoreHeaders: client.AuthenticatedHeaders(), |
| 82 | OkCodes: []int{200}, |
| 83 | }) |
| 84 | |
| 85 | return result |
| 86 | } |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 87 | |
| 88 | // Get will return details for a particular default rule. |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 89 | func Get(client *gophercloud.ServiceClient, id int) GetResult { |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 90 | var result GetResult |
| 91 | |
| 92 | _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{ |
| 93 | Results: &result.Body, |
| 94 | MoreHeaders: client.AuthenticatedHeaders(), |
| 95 | OkCodes: []int{200}, |
| 96 | }) |
| 97 | |
| 98 | return result |
| 99 | } |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame] | 100 | |
| 101 | // Delete will permanently delete a default rule from the project. |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 102 | func Delete(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult { |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame] | 103 | var result gophercloud.ErrResult |
| 104 | |
| 105 | _, result.Err = perigee.Request("DELETE", resourceURL(client, id), perigee.Options{ |
| 106 | MoreHeaders: client.AuthenticatedHeaders(), |
| 107 | OkCodes: []int{202}, |
| 108 | }) |
| 109 | |
| 110 | return result |
| 111 | } |