blob: 62c112e5f1a41819a1811615485f81f6d13b76a0 [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.
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010040func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060041 var r CreateResult
42 b, err := opts.ToRuleCreateMap()
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010043 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060044 r.Err = err
45 return r
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010046 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060047 _, r.Err = client.Post(rootURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010048 OkCodes: []int{200},
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010049 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060050 return r
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010051}
Jamie Hannaford8031b732014-11-24 12:55:41 +010052
53// Get will return details for a particular default rule.
Jamie Hannaford2f226172014-11-25 11:52:25 +010054func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060055 var r GetResult
56 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
57 return r
Jamie Hannaford8031b732014-11-24 12:55:41 +010058}
Jamie Hannaford20e92912014-11-24 13:01:45 +010059
60// Delete will permanently delete a default rule from the project.
Jamie Hannaford2f226172014-11-25 11:52:25 +010061func Delete(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060062 var r gophercloud.ErrResult
63 _, r.Err = client.Delete(resourceURL(client, id), nil)
64 return r
Jamie Hannaford20e92912014-11-24 13:01:45 +010065}