blob: 7d304de76843f90b5309a805144d69b0824e1ef0 [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
38// CreateRuleOptsBuilder builds the create rule options into a serializable format.
39type 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
68func 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
77 _, result.Err = perigee.Request("POST", rootURL(client), perigee.Options{
78 Results: &result.Body,
79 ReqBody: &reqBody,
80 MoreHeaders: client.AuthenticatedHeaders(),
81 OkCodes: []int{200},
82 })
83
84 return result
85}
Jamie Hannaford8031b732014-11-24 12:55:41 +010086
87// Get will return details for a particular default rule.
88func Get(client *gophercloud.ServiceClient, id string) GetResult {
89 var result GetResult
90
91 _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{
92 Results: &result.Body,
93 MoreHeaders: client.AuthenticatedHeaders(),
94 OkCodes: []int{200},
95 })
96
97 return result
98}