blob: c8a320853c95123e69bfb7e22625c02062e9bce7 [file] [log] [blame]
Jamie Hannaford924c09d2014-11-19 12:05:38 +01001package secgroups
2
3import (
Joe Topjian368deee2017-01-12 14:19:23 -07004 "encoding/json"
5 "strconv"
6
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02007 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02008 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
Jamie Hannaford924c09d2014-11-19 12:05:38 +01009)
10
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010011// SecurityGroup represents a security group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010012type SecurityGroup struct {
Jamie Hannaford2f226172014-11-25 11:52:25 +010013 // The unique ID of the group. If Neutron is installed, this ID will be
14 // represented as a string UUID; if Neutron is not installed, it will be a
15 // numeric ID. For the sake of consistency, we always cast it to a string.
Joe Topjian368deee2017-01-12 14:19:23 -070016 ID string `json:"-"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010017
18 // The human-readable name of the group, which needs to be unique.
Joe Topjian368deee2017-01-12 14:19:23 -070019 Name string `json:"name"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010020
21 // The human-readable description of the group.
Joe Topjian368deee2017-01-12 14:19:23 -070022 Description string `json:"description"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010023
24 // The rules which determine how this security group operates.
Joe Topjian368deee2017-01-12 14:19:23 -070025 Rules []Rule `json:"rules"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010026
Jamie Hannaford04abbc72014-11-21 11:27:57 +010027 // The ID of the tenant to which this security group belongs.
Jon Perritt12395212016-02-24 10:41:17 -060028 TenantID string `json:"tenant_id"`
Jamie Hannaford924c09d2014-11-19 12:05:38 +010029}
30
Joe Topjian368deee2017-01-12 14:19:23 -070031func (r *SecurityGroup) UnmarshalJSON(b []byte) error {
32 type tmp SecurityGroup
33 var s struct {
34 tmp
35 ID interface{} `json:"id"`
36 }
37 err := json.Unmarshal(b, &s)
38 if err != nil {
39 return err
40 }
41
42 *r = SecurityGroup(s.tmp)
43
44 switch t := s.ID.(type) {
45 case float64:
46 r.ID = strconv.FormatFloat(t, 'f', -1, 64)
47 case string:
48 r.ID = t
49 }
50
51 return err
52}
53
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010054// Rule represents a security group rule, a policy which determines how a
55// security group operates and what inbound traffic it allows in.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010056type Rule struct {
Jamie Hannaford2f226172014-11-25 11:52:25 +010057 // The unique ID. If Neutron is installed, this ID will be
58 // represented as a string UUID; if Neutron is not installed, it will be a
59 // numeric ID. For the sake of consistency, we always cast it to a string.
Joe Topjian368deee2017-01-12 14:19:23 -070060 ID string `json:"-"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010061
62 // The lower bound of the port range which this security group should open up
Jon Perritt12395212016-02-24 10:41:17 -060063 FromPort int `json:"from_port"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010064
65 // The upper bound of the port range which this security group should open up
Jon Perritt12395212016-02-24 10:41:17 -060066 ToPort int `json:"to_port"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010067
68 // The IP protocol (e.g. TCP) which the security group accepts
Jon Perritt12395212016-02-24 10:41:17 -060069 IPProtocol string `json:"ip_protocol"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010070
71 // The CIDR IP range whose traffic can be received
Jon Perritt12395212016-02-24 10:41:17 -060072 IPRange IPRange `json:"ip_range"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010073
Jamie Hannaford04abbc72014-11-21 11:27:57 +010074 // The security group ID to which this rule belongs
Jon Perritt12395212016-02-24 10:41:17 -060075 ParentGroupID string `json:"parent_group_id"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010076
77 // Not documented.
78 Group Group
Jamie Hannaford924c09d2014-11-19 12:05:38 +010079}
80
Joe Topjian368deee2017-01-12 14:19:23 -070081func (r *Rule) UnmarshalJSON(b []byte) error {
82 type tmp Rule
83 var s struct {
84 tmp
85 ID interface{} `json:"id"`
86 ParentGroupID interface{} `json:"parent_group_id"`
87 }
88 err := json.Unmarshal(b, &s)
89 if err != nil {
90 return err
91 }
92
93 *r = Rule(s.tmp)
94
95 switch t := s.ID.(type) {
96 case float64:
97 r.ID = strconv.FormatFloat(t, 'f', -1, 64)
98 case string:
99 r.ID = t
100 }
101
102 switch t := s.ParentGroupID.(type) {
103 case float64:
104 r.ParentGroupID = strconv.FormatFloat(t, 'f', -1, 64)
105 case string:
106 r.ParentGroupID = t
107 }
108
109 return err
110}
111
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100112// IPRange represents the IP range whose traffic will be accepted by the
113// security group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +0100114type IPRange struct {
115 CIDR string
116}
117
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100118// Group represents a group.
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100119type Group struct {
Jon Perritt12395212016-02-24 10:41:17 -0600120 TenantID string `json:"tenant_id"`
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100121 Name string
122}
123
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100124// SecurityGroupPage is a single page of a SecurityGroup collection.
Jamie Hannaford924c09d2014-11-19 12:05:38 +0100125type SecurityGroupPage struct {
126 pagination.SinglePageBase
127}
128
129// IsEmpty determines whether or not a page of Security Groups contains any results.
130func (page SecurityGroupPage) IsEmpty() (bool, error) {
131 users, err := ExtractSecurityGroups(page)
Jon Perritt12395212016-02-24 10:41:17 -0600132 return len(users) == 0, err
Jamie Hannaford924c09d2014-11-19 12:05:38 +0100133}
134
135// ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results.
Jon Perritt31b66462016-02-25 22:25:30 -0600136func ExtractSecurityGroups(r pagination.Page) ([]SecurityGroup, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600137 var s struct {
138 SecurityGroups []SecurityGroup `json:"security_groups"`
Jamie Hannaford924c09d2014-11-19 12:05:38 +0100139 }
Jon Perritt31b66462016-02-25 22:25:30 -0600140 err := (r.(SecurityGroupPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600141 return s.SecurityGroups, err
Jamie Hannaford924c09d2014-11-19 12:05:38 +0100142}
Jamie Hannaforda493e642014-11-19 12:40:30 +0100143
144type commonResult struct {
145 gophercloud.Result
146}
147
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100148// CreateResult represents the result of a create operation.
Jamie Hannaforda493e642014-11-19 12:40:30 +0100149type CreateResult struct {
150 commonResult
151}
152
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100153// GetResult represents the result of a get operation.
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100154type GetResult struct {
155 commonResult
156}
157
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100158// UpdateResult represents the result of an update operation.
Jamie Hannaford30c74662014-11-19 15:37:34 +0100159type UpdateResult struct {
160 commonResult
161}
162
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100163// Extract will extract a SecurityGroup struct from most responses.
Jamie Hannaforda493e642014-11-19 12:40:30 +0100164func (r commonResult) Extract() (*SecurityGroup, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600165 var s struct {
166 SecurityGroup *SecurityGroup `json:"security_group"`
Jamie Hannaforda493e642014-11-19 12:40:30 +0100167 }
Jon Perritt12395212016-02-24 10:41:17 -0600168 err := r.ExtractInto(&s)
169 return s.SecurityGroup, err
Jamie Hannaforda493e642014-11-19 12:40:30 +0100170}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100171
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100172// CreateRuleResult represents the result when adding rules to a security group.
173type CreateRuleResult struct {
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100174 gophercloud.Result
175}
176
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100177// Extract will extract a Rule struct from a CreateRuleResult.
178func (r CreateRuleResult) Extract() (*Rule, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600179 var s struct {
180 Rule *Rule `json:"security_group_rule"`
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100181 }
Jon Perritt12395212016-02-24 10:41:17 -0600182 err := r.ExtractInto(&s)
183 return s.Rule, err
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100184}