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