blob: 5e2d6868a2e8e5c59796b703b835375f469156ae [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 {
10 createPage := func(r pagination.PageResult) pagination.Page {
11 return DefaultRulePage{pagination.SinglePageBase(r)}
12 }
13
14 return pagination.NewPager(client, rootURL(client), createPage)
15}
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010016
17// CreateOpts represents the configuration for adding a new default rule.
18type CreateOpts struct {
19 // Required - the lower bound of the port range that will be opened.
20 FromPort int `json:"from_port"`
21
22 // Required - the upper bound of the port range that will be opened.
23 ToPort int `json:"to_port"`
24
25 // Required - the protocol type that will be allowed, e.g. TCP.
26 IPProtocol string `json:"ip_protocol"`
27
28 // ONLY required if FromGroupID is blank. This represents the IP range that
29 // will be the source of network traffic to your security group. Use
30 // 0.0.0.0/0 to allow all IP addresses.
31 CIDR string `json:"cidr,omitempty"`
32}
33
Jamie Hannaford558572f2014-11-24 14:31:57 +010034// CreateOptsBuilder builds the create rule options into a serializable format.
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010035type CreateOptsBuilder interface {
36 ToRuleCreateMap() (map[string]interface{}, error)
37}
38
39// ToRuleCreateMap builds the create rule options into a serializable format.
40func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) {
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010041 if opts.FromPort == 0 {
Jon Perrittf094fef2016-03-07 01:41:59 -060042 err := gophercloud.ErrMissingInput{}
43 err.Function = "defsecrules.ToRuleCreateMap"
44 err.Argument = "defsecrules.CreateOpts.FromPort"
45 return nil, err
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010046 }
47 if opts.ToPort == 0 {
Jon Perrittf094fef2016-03-07 01:41:59 -060048 err := gophercloud.ErrMissingInput{}
49 err.Function = "defsecrules.ToRuleCreateMap"
50 err.Argument = "defsecrules.CreateOpts.ToPort"
51 return nil, err
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010052 }
53 if opts.IPProtocol == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -060054 err := gophercloud.ErrMissingInput{}
55 err.Function = "defsecrules.ToRuleCreateMap"
56 err.Argument = "defsecrules.CreateOpts.IPProtocol"
57 return nil, err
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010058 }
59 if opts.CIDR == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -060060 err := gophercloud.ErrMissingInput{}
61 err.Function = "defsecrules.ToRuleCreateMap"
62 err.Argument = "defsecrules.CreateOpts.CIDR"
63 return nil, err
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010064 }
65
Jon Perrittf094fef2016-03-07 01:41:59 -060066 rule := make(map[string]interface{})
67
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010068 rule["from_port"] = opts.FromPort
69 rule["to_port"] = opts.ToPort
70 rule["ip_protocol"] = opts.IPProtocol
71 rule["cidr"] = opts.CIDR
72
73 return map[string]interface{}{"security_group_default_rule": rule}, nil
74}
75
Jamie Hannaford558572f2014-11-24 14:31:57 +010076// Create is the operation responsible for creating a new default rule.
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010077func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
78 var result CreateResult
79
80 reqBody, err := opts.ToRuleCreateMap()
81 if err != nil {
82 result.Err = err
83 return result
84 }
85
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010086 _, result.Err = client.Post(rootURL(client), reqBody, &result.Body, &gophercloud.RequestOpts{
87 OkCodes: []int{200},
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010088 })
89
90 return result
91}
Jamie Hannaford8031b732014-11-24 12:55:41 +010092
93// Get will return details for a particular default rule.
Jamie Hannaford2f226172014-11-25 11:52:25 +010094func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford8031b732014-11-24 12:55:41 +010095 var result GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010096 _, result.Err = client.Get(resourceURL(client, id), &result.Body, nil)
Jamie Hannaford8031b732014-11-24 12:55:41 +010097 return result
98}
Jamie Hannaford20e92912014-11-24 13:01:45 +010099
100// Delete will permanently delete a default rule from the project.
Jamie Hannaford2f226172014-11-25 11:52:25 +0100101func Delete(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult {
Jamie Hannaford20e92912014-11-24 13:01:45 +0100102 var result gophercloud.ErrResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100103 _, result.Err = client.Delete(resourceURL(client, id), nil)
Jamie Hannaford20e92912014-11-24 13:01:45 +0100104 return result
105}