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