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