blob: 5e18c0be84e79a9946e25e4b96a5d91da753fb05 [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.
79func ExtractSecurityGroups(page pagination.Page) ([]SecurityGroup, error) {
Jon Perritt12395212016-02-24 10:41:17 -060080 r := page.(SecurityGroupPage)
81 var s struct {
82 SecurityGroups []SecurityGroup `json:"security_groups"`
Jamie Hannaford924c09d2014-11-19 12:05:38 +010083 }
Jon Perritt12395212016-02-24 10:41:17 -060084 err := r.ExtractInto(&s)
85 return s.SecurityGroups, err
Jamie Hannaford924c09d2014-11-19 12:05:38 +010086}
Jamie Hannaforda493e642014-11-19 12:40:30 +010087
88type commonResult struct {
89 gophercloud.Result
90}
91
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010092// CreateResult represents the result of a create operation.
Jamie Hannaforda493e642014-11-19 12:40:30 +010093type CreateResult struct {
94 commonResult
95}
96
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010097// GetResult represents the result of a get operation.
Jamie Hannafordb38dd312014-11-19 13:02:11 +010098type GetResult struct {
99 commonResult
100}
101
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100102// UpdateResult represents the result of an update operation.
Jamie Hannaford30c74662014-11-19 15:37:34 +0100103type UpdateResult struct {
104 commonResult
105}
106
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100107// Extract will extract a SecurityGroup struct from most responses.
Jamie Hannaforda493e642014-11-19 12:40:30 +0100108func (r commonResult) Extract() (*SecurityGroup, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600109 var s struct {
110 SecurityGroup *SecurityGroup `json:"security_group"`
Jamie Hannaforda493e642014-11-19 12:40:30 +0100111 }
Jon Perritt12395212016-02-24 10:41:17 -0600112 err := r.ExtractInto(&s)
113 return s.SecurityGroup, err
Jamie Hannaforda493e642014-11-19 12:40:30 +0100114}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100115
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100116// CreateRuleResult represents the result when adding rules to a security group.
117type CreateRuleResult struct {
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100118 gophercloud.Result
119}
120
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100121// Extract will extract a Rule struct from a CreateRuleResult.
122func (r CreateRuleResult) Extract() (*Rule, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600123 var s struct {
124 Rule *Rule `json:"security_group_rule"`
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100125 }
Jon Perritt12395212016-02-24 10:41:17 -0600126 err := r.ExtractInto(&s)
127 return s.Rule, err
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100128}