blob: 764f5806ff169a9221fcb4965eafda6c07bf2ffa [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 Hannaford7f34d8e2014-11-20 12:24:55 +01008// SecurityGroup represents a security group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +01009type SecurityGroup struct {
Jamie Hannaford2f226172014-11-25 11:52:25 +010010 // The unique ID of the group. If Neutron is installed, this ID will be
11 // represented as a string UUID; if Neutron is not installed, it will be a
12 // numeric ID. For the sake of consistency, we always cast it to a string.
13 ID string
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010014
15 // The human-readable name of the group, which needs to be unique.
16 Name string
17
18 // The human-readable description of the group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010019 Description string
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010020
21 // The rules which determine how this security group operates.
22 Rules []Rule
23
Jamie Hannaford04abbc72014-11-21 11:27:57 +010024 // The ID of the tenant to which this security group belongs.
Jon Perritt12395212016-02-24 10:41:17 -060025 TenantID string `json:"tenant_id"`
Jamie Hannaford924c09d2014-11-19 12:05:38 +010026}
27
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010028// Rule represents a security group rule, a policy which determines how a
29// security group operates and what inbound traffic it allows in.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010030type Rule struct {
Jamie Hannaford2f226172014-11-25 11:52:25 +010031 // The unique ID. If Neutron is installed, this ID will be
32 // represented as a string UUID; if Neutron is not installed, it will be a
33 // numeric ID. For the sake of consistency, we always cast it to a string.
34 ID string
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010035
36 // The lower bound of the port range which this security group should open up
Jon Perritt12395212016-02-24 10:41:17 -060037 FromPort int `json:"from_port"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010038
39 // The upper bound of the port range which this security group should open up
Jon Perritt12395212016-02-24 10:41:17 -060040 ToPort int `json:"to_port"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010041
42 // The IP protocol (e.g. TCP) which the security group accepts
Jon Perritt12395212016-02-24 10:41:17 -060043 IPProtocol string `json:"ip_protocol"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010044
45 // The CIDR IP range whose traffic can be received
Jon Perritt12395212016-02-24 10:41:17 -060046 IPRange IPRange `json:"ip_range"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010047
Jamie Hannaford04abbc72014-11-21 11:27:57 +010048 // The security group ID to which this rule belongs
Jon Perritt12395212016-02-24 10:41:17 -060049 ParentGroupID string `json:"parent_group_id"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010050
51 // Not documented.
52 Group Group
Jamie Hannaford924c09d2014-11-19 12:05:38 +010053}
54
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010055// IPRange represents the IP range whose traffic will be accepted by the
56// security group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010057type IPRange struct {
58 CIDR string
59}
60
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010061// Group represents a group.
Jamie Hannafordb38dd312014-11-19 13:02:11 +010062type Group struct {
Jon Perritt12395212016-02-24 10:41:17 -060063 TenantID string `json:"tenant_id"`
Jamie Hannafordb38dd312014-11-19 13:02:11 +010064 Name string
65}
66
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010067// SecurityGroupPage is a single page of a SecurityGroup collection.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010068type SecurityGroupPage struct {
69 pagination.SinglePageBase
70}
71
72// IsEmpty determines whether or not a page of Security Groups contains any results.
73func (page SecurityGroupPage) IsEmpty() (bool, error) {
74 users, err := ExtractSecurityGroups(page)
Jon Perritt12395212016-02-24 10:41:17 -060075 return len(users) == 0, err
Jamie Hannaford924c09d2014-11-19 12:05:38 +010076}
77
78// ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results.
Jon Perritt31b66462016-02-25 22:25:30 -060079func ExtractSecurityGroups(r pagination.Page) ([]SecurityGroup, error) {
Jon Perritt12395212016-02-24 10:41:17 -060080 var s struct {
81 SecurityGroups []SecurityGroup `json:"security_groups"`
Jamie Hannaford924c09d2014-11-19 12:05:38 +010082 }
Jon Perritt31b66462016-02-25 22:25:30 -060083 err := (r.(SecurityGroupPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060084 return s.SecurityGroups, err
Jamie Hannaford924c09d2014-11-19 12:05:38 +010085}
Jamie Hannaforda493e642014-11-19 12:40:30 +010086
87type commonResult struct {
88 gophercloud.Result
89}
90
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010091// CreateResult represents the result of a create operation.
Jamie Hannaforda493e642014-11-19 12:40:30 +010092type CreateResult struct {
93 commonResult
94}
95
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010096// GetResult represents the result of a get operation.
Jamie Hannafordb38dd312014-11-19 13:02:11 +010097type GetResult struct {
98 commonResult
99}
100
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100101// UpdateResult represents the result of an update operation.
Jamie Hannaford30c74662014-11-19 15:37:34 +0100102type UpdateResult struct {
103 commonResult
104}
105
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100106// Extract will extract a SecurityGroup struct from most responses.
Jamie Hannaforda493e642014-11-19 12:40:30 +0100107func (r commonResult) Extract() (*SecurityGroup, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600108 var s struct {
109 SecurityGroup *SecurityGroup `json:"security_group"`
Jamie Hannaforda493e642014-11-19 12:40:30 +0100110 }
Jon Perritt12395212016-02-24 10:41:17 -0600111 err := r.ExtractInto(&s)
112 return s.SecurityGroup, err
Jamie Hannaforda493e642014-11-19 12:40:30 +0100113}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100114
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100115// CreateRuleResult represents the result when adding rules to a security group.
116type CreateRuleResult struct {
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100117 gophercloud.Result
118}
119
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100120// Extract will extract a Rule struct from a CreateRuleResult.
121func (r CreateRuleResult) Extract() (*Rule, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600122 var s struct {
123 Rule *Rule `json:"security_group_rule"`
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100124 }
Jon Perritt12395212016-02-24 10:41:17 -0600125 err := r.ExtractInto(&s)
126 return s.Rule, err
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100127}