Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 1 | package defsecrules |
| 2 | |
| 3 | import ( |
jrperritt | e0ba105 | 2016-04-13 17:19:54 -0500 | [diff] [blame] | 4 | "strings" |
| 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 7 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | // List will return a collection of default rules. |
| 11 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 12 | return pagination.NewPager(client, rootURL(client), func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 13 | return DefaultRulePage{pagination.SinglePageBase(r)} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 14 | }) |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 15 | } |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 16 | |
| 17 | // CreateOpts represents the configuration for adding a new default rule. |
| 18 | type CreateOpts struct { |
jrperritt | bc54861 | 2016-04-13 17:03:59 -0500 | [diff] [blame] | 19 | // The lower bound of the port range that will be opened.s |
jrperritt | e0ba105 | 2016-04-13 17:19:54 -0500 | [diff] [blame] | 20 | FromPort int `json:"from_port"` |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 21 | // The upper bound of the port range that will be opened. |
jrperritt | e0ba105 | 2016-04-13 17:19:54 -0500 | [diff] [blame] | 22 | ToPort int `json:"to_port"` |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 23 | // The protocol type that will be allowed, e.g. TCP. |
| 24 | IPProtocol string `json:"ip_protocol" required:"true"` |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 25 | // ONLY required if FromGroupID is blank. This represents the IP range that |
| 26 | // will be the source of network traffic to your security group. Use |
| 27 | // 0.0.0.0/0 to allow all IP addresses. |
| 28 | CIDR string `json:"cidr,omitempty"` |
| 29 | } |
| 30 | |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 31 | // CreateOptsBuilder builds the create rule options into a serializable format. |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 32 | type CreateOptsBuilder interface { |
| 33 | ToRuleCreateMap() (map[string]interface{}, error) |
| 34 | } |
| 35 | |
| 36 | // ToRuleCreateMap builds the create rule options into a serializable format. |
| 37 | func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) { |
jrperritt | e0ba105 | 2016-04-13 17:19:54 -0500 | [diff] [blame] | 38 | if opts.FromPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" { |
| 39 | return nil, gophercloud.ErrMissingInput{Argument: "FromPort"} |
| 40 | } |
| 41 | if opts.ToPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" { |
| 42 | return nil, gophercloud.ErrMissingInput{Argument: "ToPort"} |
| 43 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 44 | return gophercloud.BuildRequestBody(opts, "security_group_default_rule") |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 47 | // Create is the operation responsible for creating a new default rule. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 48 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 49 | b, err := opts.ToRuleCreateMap() |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 50 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 51 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 52 | return |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 53 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 54 | _, r.Err = client.Post(rootURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 55 | OkCodes: []int{200}, |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 56 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 57 | return |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 58 | } |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 59 | |
| 60 | // Get will return details for a particular default rule. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 61 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 62 | _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 63 | return |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 64 | } |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame] | 65 | |
| 66 | // Delete will permanently delete a default rule from the project. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 67 | func Delete(client *gophercloud.ServiceClient, id string) (r gophercloud.ErrResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 68 | _, r.Err = client.Delete(resourceURL(client, id), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 69 | return |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame] | 70 | } |