blob: f01631d2cc0262ec725f7735a798fe858c8d0e19 [file] [log] [blame]
Jamie Hannaford17d2f872014-11-24 12:20:33 +01001package defsecrules
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford17d2f872014-11-24 12:20:33 +01006)
7
8// List will return a collection of default rules.
9func List(client *gophercloud.ServiceClient) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -060010 return pagination.NewPager(client, rootURL(client), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford17d2f872014-11-24 12:20:33 +010011 return DefaultRulePage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -060012 })
Jamie Hannaford17d2f872014-11-24 12:20:33 +010013}
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010014
15// CreateOpts represents the configuration for adding a new default rule.
16type CreateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060017 // 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 Hannaford43fa4a22014-11-24 12:49:17 +010023 // 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 Hannaford558572f2014-11-24 14:31:57 +010029// CreateOptsBuilder builds the create rule options into a serializable format.
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010030type CreateOptsBuilder interface {
31 ToRuleCreateMap() (map[string]interface{}, error)
32}
33
34// ToRuleCreateMap builds the create rule options into a serializable format.
35func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060036 return gophercloud.BuildRequestBody(opts, "security_group_default_rule")
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010037}
38
Jamie Hannaford558572f2014-11-24 14:31:57 +010039// Create is the operation responsible for creating a new default rule.
Jon Perritt3860b512016-03-29 12:01:48 -050040func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060041 b, err := opts.ToRuleCreateMap()
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010042 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060043 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050044 return
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010045 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060046 _, r.Err = client.Post(rootURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010047 OkCodes: []int{200},
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010048 })
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010049}
Jamie Hannaford8031b732014-11-24 12:55:41 +010050
51// Get will return details for a particular default rule.
Jon Perritt3860b512016-03-29 12:01:48 -050052func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060053 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
Jamie Hannaford8031b732014-11-24 12:55:41 +010054}
Jamie Hannaford20e92912014-11-24 13:01:45 +010055
56// Delete will permanently delete a default rule from the project.
Jon Perritt3860b512016-03-29 12:01:48 -050057func Delete(client *gophercloud.ServiceClient, id string) (r gophercloud.ErrResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060058 _, r.Err = client.Delete(resourceURL(client, id), nil)
Jamie Hannaford20e92912014-11-24 13:01:45 +010059}