blob: d0098e600fc9537ec80594d005fd75ce884e970b [file] [log] [blame]
Jamie Hannaford17d2f872014-11-24 12:20:33 +01001package defsecrules
2
3import (
Jamie Hannaford43fa4a22014-11-24 12:49:17 +01004 "errors"
Denys Zhdanovaf498472016-01-19 11:34:21 +01005 "strings"
Jamie Hannaford43fa4a22014-11-24 12:49:17 +01006
Jamie Hannaford17d2f872014-11-24 12:20:33 +01007 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11// List will return a collection of default rules.
12func List(client *gophercloud.ServiceClient) pagination.Pager {
13 createPage := func(r pagination.PageResult) pagination.Page {
14 return DefaultRulePage{pagination.SinglePageBase(r)}
15 }
16
17 return pagination.NewPager(client, rootURL(client), createPage)
18}
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010019
20// CreateOpts represents the configuration for adding a new default rule.
21type CreateOpts struct {
22 // Required - the lower bound of the port range that will be opened.
23 FromPort int `json:"from_port"`
24
25 // Required - the upper bound of the port range that will be opened.
26 ToPort int `json:"to_port"`
27
28 // Required - the protocol type that will be allowed, e.g. TCP.
29 IPProtocol string `json:"ip_protocol"`
30
31 // ONLY required if FromGroupID is blank. This represents the IP range that
32 // will be the source of network traffic to your security group. Use
33 // 0.0.0.0/0 to allow all IP addresses.
34 CIDR string `json:"cidr,omitempty"`
35}
36
Jamie Hannaford558572f2014-11-24 14:31:57 +010037// CreateOptsBuilder builds the create rule options into a serializable format.
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010038type CreateOptsBuilder interface {
39 ToRuleCreateMap() (map[string]interface{}, error)
40}
41
42// ToRuleCreateMap builds the create rule options into a serializable format.
43func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) {
44 rule := make(map[string]interface{})
45
Denys Zhdanovaf498472016-01-19 11:34:21 +010046 if opts.FromPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" {
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010047 return rule, errors.New("A FromPort must be set")
48 }
Denys Zhdanovaf498472016-01-19 11:34:21 +010049 if opts.ToPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" {
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010050 return rule, errors.New("A ToPort must be set")
51 }
52 if opts.IPProtocol == "" {
53 return rule, errors.New("A IPProtocol must be set")
54 }
55 if opts.CIDR == "" {
56 return rule, errors.New("A CIDR must be set")
57 }
58
59 rule["from_port"] = opts.FromPort
60 rule["to_port"] = opts.ToPort
61 rule["ip_protocol"] = opts.IPProtocol
62 rule["cidr"] = opts.CIDR
63
64 return map[string]interface{}{"security_group_default_rule": rule}, nil
65}
66
Jamie Hannaford558572f2014-11-24 14:31:57 +010067// Create is the operation responsible for creating a new default rule.
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010068func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
69 var result CreateResult
70
71 reqBody, err := opts.ToRuleCreateMap()
72 if err != nil {
73 result.Err = err
74 return result
75 }
76
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010077 _, result.Err = client.Post(rootURL(client), reqBody, &result.Body, &gophercloud.RequestOpts{
78 OkCodes: []int{200},
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010079 })
80
81 return result
82}
Jamie Hannaford8031b732014-11-24 12:55:41 +010083
84// Get will return details for a particular default rule.
Jamie Hannaford2f226172014-11-25 11:52:25 +010085func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford8031b732014-11-24 12:55:41 +010086 var result GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010087 _, result.Err = client.Get(resourceURL(client, id), &result.Body, nil)
Jamie Hannaford8031b732014-11-24 12:55:41 +010088 return result
89}
Jamie Hannaford20e92912014-11-24 13:01:45 +010090
91// Delete will permanently delete a default rule from the project.
Jamie Hannaford2f226172014-11-25 11:52:25 +010092func Delete(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult {
Jamie Hannaford20e92912014-11-24 13:01:45 +010093 var result gophercloud.ErrResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010094 _, result.Err = client.Delete(resourceURL(client, id), nil)
Jamie Hannaford20e92912014-11-24 13:01:45 +010095 return result
96}