blob: c29d46f3033d87085965d65f134b6d790b3d3f10 [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
Ash Wilson4bf41a32015-02-12 15:52:44 -050076 _, result.Err = client.Request("POST", rootURL(client), gophercloud.RequestOpts{
77 JSONResponse: &result.Body,
78 JSONBody: &reqBody,
79 OkCodes: []int{200},
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010080 })
81
82 return result
83}
Jamie Hannaford8031b732014-11-24 12:55:41 +010084
85// Get will return details for a particular default rule.
Jamie Hannaford2f226172014-11-25 11:52:25 +010086func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford8031b732014-11-24 12:55:41 +010087 var result GetResult
88
Ash Wilson59fb6c42015-02-12 16:21:13 -050089 _, result.Err = client.Request("GET", resourceURL(client, id), gophercloud.RequestOpts{
90 JSONResponse: &result.Body,
Jamie Hannaford8031b732014-11-24 12:55:41 +010091 })
92
93 return result
94}
Jamie Hannaford20e92912014-11-24 13:01:45 +010095
96// Delete will permanently delete a default rule from the project.
Jamie Hannaford2f226172014-11-25 11:52:25 +010097func Delete(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult {
Jamie Hannaford20e92912014-11-24 13:01:45 +010098 var result gophercloud.ErrResult
99
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100100 _, result.Err = client.Request("DELETE", resourceURL(client, id), gophercloud.RequestOpts{})
Jamie Hannaford20e92912014-11-24 13:01:45 +0100101
102 return result
103}