blob: cfb2df8f14872e6f1870ea5874cf80d15e0a0d21 [file] [log] [blame]
Jamie Hannaford924c09d2014-11-19 12:05:38 +01001package secgroups
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford924c09d2014-11-19 12:05:38 +01006)
7
Jamie Hannaford19151792014-11-19 12:46:47 +01008func commonList(client *gophercloud.ServiceClient, url string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -06009 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jamie Hannaford924c09d2014-11-19 12:05:38 +010010 return SecurityGroupPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -060011 })
Jamie Hannaford19151792014-11-19 12:46:47 +010012}
13
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010014// List will return a collection of all the security groups for a particular
15// tenant.
Jamie Hannaford19151792014-11-19 12:46:47 +010016func List(client *gophercloud.ServiceClient) pagination.Pager {
17 return commonList(client, rootURL(client))
18}
19
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010020// ListByServer will return a collection of all the security groups which are
21// associated with a particular server.
Jamie Hannaford19151792014-11-19 12:46:47 +010022func ListByServer(client *gophercloud.ServiceClient, serverID string) pagination.Pager {
23 return commonList(client, listByServerURL(client, serverID))
Jamie Hannaford924c09d2014-11-19 12:05:38 +010024}
Jamie Hannaforda493e642014-11-19 12:40:30 +010025
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010026// GroupOpts is the underlying struct responsible for creating or updating
27// security groups. It therefore represents the mutable attributes of a
28// security group.
Jamie Hannaford30c74662014-11-19 15:37:34 +010029type GroupOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060030 // the name of your security group.
31 Name string `json:"name" required:"true"`
32 // the description of your security group.
33 Description string `json:"description" required:"true"`
Jamie Hannaforda493e642014-11-19 12:40:30 +010034}
35
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010036// CreateOpts is the struct responsible for creating a security group.
Jamie Hannaford30c74662014-11-19 15:37:34 +010037type CreateOpts GroupOpts
38
Jamie Hannaford04abbc72014-11-21 11:27:57 +010039// CreateOptsBuilder builds the create options into a serializable format.
40type CreateOptsBuilder interface {
41 ToSecGroupCreateMap() (map[string]interface{}, error)
42}
43
44// ToSecGroupCreateMap builds the create options into a serializable format.
45func (opts CreateOpts) ToSecGroupCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060046 return gophercloud.BuildRequestBody(opts, "security_group")
Jamie Hannaford04abbc72014-11-21 11:27:57 +010047}
48
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010049// Create will create a new security group.
Jon Perritt3860b512016-03-29 12:01:48 -050050func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060051 b, err := opts.ToSecGroupCreateMap()
Jamie Hannaford04abbc72014-11-21 11:27:57 +010052 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060053 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050054 return
Jamie Hannaford04abbc72014-11-21 11:27:57 +010055 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060056 _, r.Err = client.Post(rootURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010057 OkCodes: []int{200},
Jamie Hannaforda493e642014-11-19 12:40:30 +010058 })
Jamie Hannaforda493e642014-11-19 12:40:30 +010059}
Jamie Hannafordb38dd312014-11-19 13:02:11 +010060
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010061// UpdateOpts is the struct responsible for updating an existing security group.
Jamie Hannaford30c74662014-11-19 15:37:34 +010062type UpdateOpts GroupOpts
63
Jamie Hannaford04abbc72014-11-21 11:27:57 +010064// UpdateOptsBuilder builds the update options into a serializable format.
65type UpdateOptsBuilder interface {
66 ToSecGroupUpdateMap() (map[string]interface{}, error)
67}
68
69// ToSecGroupUpdateMap builds the update options into a serializable format.
70func (opts UpdateOpts) ToSecGroupUpdateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060071 return gophercloud.BuildRequestBody(opts, "security_group")
Jamie Hannaford04abbc72014-11-21 11:27:57 +010072}
73
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010074// Update will modify the mutable properties of a security group, notably its
75// name and description.
Jon Perritt3860b512016-03-29 12:01:48 -050076func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060077 b, err := opts.ToSecGroupUpdateMap()
Jamie Hannaford04abbc72014-11-21 11:27:57 +010078 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060079 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050080 return
Jamie Hannaford04abbc72014-11-21 11:27:57 +010081 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060082 _, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010083 OkCodes: []int{200},
Jamie Hannaford30c74662014-11-19 15:37:34 +010084 })
Jamie Hannaford30c74662014-11-19 15:37:34 +010085}
86
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010087// Get will return details for a particular security group.
Jon Perritt3860b512016-03-29 12:01:48 -050088func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060089 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
Jamie Hannafordb38dd312014-11-19 13:02:11 +010090}
Jamie Hannafordd276e612014-11-19 13:56:28 +010091
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010092// Delete will permanently delete a security group from the project.
Jon Perritt3860b512016-03-29 12:01:48 -050093func Delete(client *gophercloud.ServiceClient, id string) (r gophercloud.ErrResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060094 _, r.Err = client.Delete(resourceURL(client, id), nil)
Jamie Hannafordd276e612014-11-19 13:56:28 +010095}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +010096
Jamie Hannaford04abbc72014-11-21 11:27:57 +010097// CreateRuleOpts represents the configuration for adding a new rule to an
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010098// existing security group.
Jamie Hannaford04abbc72014-11-21 11:27:57 +010099type CreateRuleOpts struct {
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100100 // Required - the ID of the group that this rule will be added to.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600101 ParentGroupID string `json:"parent_group_id" required:"true"`
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100102 // Required - the lower bound of the port range that will be opened.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600103 FromPort int `json:"from_port" required:"true"`
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100104 // Required - the upper bound of the port range that will be opened.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600105 ToPort int `json:"to_port" required:"true"`
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100106 // Required - the protocol type that will be allowed, e.g. TCP.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600107 IPProtocol string `json:"ip_protocol" required:"true"`
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100108 // ONLY required if FromGroupID is blank. This represents the IP range that
109 // will be the source of network traffic to your security group. Use
110 // 0.0.0.0/0 to allow all IP addresses.
111 CIDR string `json:"cidr,omitempty"`
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100112 // ONLY required if CIDR is blank. This value represents the ID of a group
113 // that forwards traffic to the parent group. So, instead of accepting
114 // network traffic from an entire IP range, you can instead refine the
115 // inbound source by an existing security group.
116 FromGroupID string `json:"group_id,omitempty"`
117}
118
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100119// CreateRuleOptsBuilder builds the create rule options into a serializable format.
120type CreateRuleOptsBuilder interface {
121 ToRuleCreateMap() (map[string]interface{}, error)
122}
123
124// ToRuleCreateMap builds the create rule options into a serializable format.
125func (opts CreateRuleOpts) ToRuleCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600126 return gophercloud.BuildRequestBody(opts, "security_group_rule")
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100127}
128
129// CreateRule will add a new rule to an existing security group (whose ID is
130// specified in CreateRuleOpts). You have the option of controlling inbound
131// traffic from either an IP range (CIDR) or from another security group.
Jon Perritt3860b512016-03-29 12:01:48 -0500132func CreateRule(client *gophercloud.ServiceClient, opts CreateRuleOptsBuilder) (r CreateRuleResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600133 b, err := opts.ToRuleCreateMap()
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100134 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600135 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500136 return
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100137 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600138 _, r.Err = client.Post(rootRuleURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100139 OkCodes: []int{200},
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100140 })
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100141}
Jamie Hannaford61f81ca2014-11-19 14:44:33 +0100142
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100143// DeleteRule will permanently delete a rule from a security group.
Jon Perritt3860b512016-03-29 12:01:48 -0500144func DeleteRule(client *gophercloud.ServiceClient, id string) (r gophercloud.ErrResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600145 _, r.Err = client.Delete(resourceRuleURL(client, id), nil)
Jamie Hannaford61f81ca2014-11-19 14:44:33 +0100146}
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100147
148func actionMap(prefix, groupName string) map[string]map[string]string {
149 return map[string]map[string]string{
150 prefix + "SecurityGroup": map[string]string{"name": groupName},
151 }
152}
153
Jon Perrittdb0ae142016-03-13 00:33:41 -0600154// AddServer will associate a server and a security group, enforcing the
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100155// rules of the group on the server.
Jon Perritt3860b512016-03-29 12:01:48 -0500156func AddServer(client *gophercloud.ServiceClient, serverID, groupName string) (r gophercloud.ErrResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600157 _, r.Err = client.Post(serverActionURL(client, serverID), actionMap("add", groupName), &r.Body, nil)
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100158}
159
Jon Perrittdb0ae142016-03-13 00:33:41 -0600160// RemoveServer will disassociate a server from a security group.
Jon Perritt3860b512016-03-29 12:01:48 -0500161func RemoveServer(client *gophercloud.ServiceClient, serverID, groupName string) (r gophercloud.ErrResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600162 _, r.Err = client.Post(serverActionURL(client, serverID), actionMap("remove", groupName), &r.Body, nil)
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100163}