blob: aee8fbd89372307ae80a1999caa86d12d8c3b42c [file] [log] [blame]
Jamie Hannaford17d2f872014-11-24 12:20:33 +01001package defsecrules
2
3import (
jrperritte0ba1052016-04-13 17:19:54 -05004 "strings"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02007 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
Jamie Hannaford17d2f872014-11-24 12:20:33 +01008)
9
10// List will return a collection of default rules.
11func List(client *gophercloud.ServiceClient) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -060012 return pagination.NewPager(client, rootURL(client), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford17d2f872014-11-24 12:20:33 +010013 return DefaultRulePage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -060014 })
Jamie Hannaford17d2f872014-11-24 12:20:33 +010015}
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010016
17// CreateOpts represents the configuration for adding a new default rule.
18type CreateOpts struct {
jrperrittbc548612016-04-13 17:03:59 -050019 // The lower bound of the port range that will be opened.s
jrperritte0ba1052016-04-13 17:19:54 -050020 FromPort int `json:"from_port"`
Jon Perrittdb0ae142016-03-13 00:33:41 -060021 // The upper bound of the port range that will be opened.
jrperritte0ba1052016-04-13 17:19:54 -050022 ToPort int `json:"to_port"`
Jon Perrittdb0ae142016-03-13 00:33:41 -060023 // The protocol type that will be allowed, e.g. TCP.
24 IPProtocol string `json:"ip_protocol" required:"true"`
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010025 // ONLY required if FromGroupID is blank. This represents the IP range that
26 // will be the source of network traffic to your security group. Use
27 // 0.0.0.0/0 to allow all IP addresses.
28 CIDR string `json:"cidr,omitempty"`
29}
30
Jamie Hannaford558572f2014-11-24 14:31:57 +010031// CreateOptsBuilder builds the create rule options into a serializable format.
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010032type CreateOptsBuilder interface {
33 ToRuleCreateMap() (map[string]interface{}, error)
34}
35
36// ToRuleCreateMap builds the create rule options into a serializable format.
37func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) {
jrperritte0ba1052016-04-13 17:19:54 -050038 if opts.FromPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" {
39 return nil, gophercloud.ErrMissingInput{Argument: "FromPort"}
40 }
41 if opts.ToPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" {
42 return nil, gophercloud.ErrMissingInput{Argument: "ToPort"}
43 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060044 return gophercloud.BuildRequestBody(opts, "security_group_default_rule")
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010045}
46
Jamie Hannaford558572f2014-11-24 14:31:57 +010047// Create is the operation responsible for creating a new default rule.
Jon Perritt3860b512016-03-29 12:01:48 -050048func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060049 b, err := opts.ToRuleCreateMap()
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010050 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060051 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050052 return
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010053 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060054 _, r.Err = client.Post(rootURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010055 OkCodes: []int{200},
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010056 })
jrperritt29ae6b32016-04-13 12:59:37 -050057 return
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010058}
Jamie Hannaford8031b732014-11-24 12:55:41 +010059
60// Get will return details for a particular default rule.
Jon Perritt3860b512016-03-29 12:01:48 -050061func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060062 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050063 return
Jamie Hannaford8031b732014-11-24 12:55:41 +010064}
Jamie Hannaford20e92912014-11-24 13:01:45 +010065
66// Delete will permanently delete a default rule from the project.
Jon Perritt3860b512016-03-29 12:01:48 -050067func Delete(client *gophercloud.ServiceClient, id string) (r gophercloud.ErrResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060068 _, r.Err = client.Delete(resourceURL(client, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -050069 return
Jamie Hannaford20e92912014-11-24 13:01:45 +010070}