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