blob: d411a3718715c49b2c166177823a3a2bf1a1db39 [file] [log] [blame]
Jamie Hannaford17d2f872014-11-24 12:20:33 +01001package defsecrules
2
3import (
Jamie Hannaford43fa4a22014-11-24 12:49:17 +01004 "errors"
5
6 "github.com/racker/perigee"
7
Jamie Hannaford17d2f872014-11-24 12:20:33 +01008 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
10)
11
12// List will return a collection of default rules.
13func List(client *gophercloud.ServiceClient) pagination.Pager {
14 createPage := func(r pagination.PageResult) pagination.Page {
15 return DefaultRulePage{pagination.SinglePageBase(r)}
16 }
17
18 return pagination.NewPager(client, rootURL(client), createPage)
19}
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010020
21// CreateOpts represents the configuration for adding a new default rule.
22type CreateOpts struct {
23 // Required - the lower bound of the port range that will be opened.
24 FromPort int `json:"from_port"`
25
26 // Required - the upper bound of the port range that will be opened.
27 ToPort int `json:"to_port"`
28
29 // Required - the protocol type that will be allowed, e.g. TCP.
30 IPProtocol string `json:"ip_protocol"`
31
32 // ONLY required if FromGroupID is blank. This represents the IP range that
33 // will be the source of network traffic to your security group. Use
34 // 0.0.0.0/0 to allow all IP addresses.
35 CIDR string `json:"cidr,omitempty"`
36}
37
Jamie Hannaford558572f2014-11-24 14:31:57 +010038// CreateOptsBuilder builds the create rule options into a serializable format.
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010039type CreateOptsBuilder interface {
40 ToRuleCreateMap() (map[string]interface{}, error)
41}
42
43// ToRuleCreateMap builds the create rule options into a serializable format.
44func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) {
45 rule := make(map[string]interface{})
46
47 if opts.FromPort == 0 {
48 return rule, errors.New("A FromPort must be set")
49 }
50 if opts.ToPort == 0 {
51 return rule, errors.New("A ToPort must be set")
52 }
53 if opts.IPProtocol == "" {
54 return rule, errors.New("A IPProtocol must be set")
55 }
56 if opts.CIDR == "" {
57 return rule, errors.New("A CIDR must be set")
58 }
59
60 rule["from_port"] = opts.FromPort
61 rule["to_port"] = opts.ToPort
62 rule["ip_protocol"] = opts.IPProtocol
63 rule["cidr"] = opts.CIDR
64
65 return map[string]interface{}{"security_group_default_rule": rule}, nil
66}
67
Jamie Hannaford558572f2014-11-24 14:31:57 +010068// Create is the operation responsible for creating a new default rule.
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010069func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
70 var result CreateResult
71
72 reqBody, err := opts.ToRuleCreateMap()
73 if err != nil {
74 result.Err = err
75 return result
76 }
77
78 _, result.Err = perigee.Request("POST", rootURL(client), perigee.Options{
79 Results: &result.Body,
80 ReqBody: &reqBody,
81 MoreHeaders: client.AuthenticatedHeaders(),
82 OkCodes: []int{200},
83 })
84
85 return result
86}
Jamie Hannaford8031b732014-11-24 12:55:41 +010087
88// Get will return details for a particular default rule.
Jamie Hannaford558572f2014-11-24 14:31:57 +010089func Get(client *gophercloud.ServiceClient, id int) GetResult {
Jamie Hannaford8031b732014-11-24 12:55:41 +010090 var result GetResult
91
92 _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{
93 Results: &result.Body,
94 MoreHeaders: client.AuthenticatedHeaders(),
95 OkCodes: []int{200},
96 })
97
98 return result
99}
Jamie Hannaford20e92912014-11-24 13:01:45 +0100100
101// Delete will permanently delete a default rule from the project.
Jamie Hannaford558572f2014-11-24 14:31:57 +0100102func Delete(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
Jamie Hannaford20e92912014-11-24 13:01:45 +0100103 var result gophercloud.ErrResult
104
105 _, result.Err = perigee.Request("DELETE", resourceURL(client, id), perigee.Options{
106 MoreHeaders: client.AuthenticatedHeaders(),
107 OkCodes: []int{202},
108 })
109
110 return result
111}