blob: 478c5dc0973f73d3e1d0c0d3b809806232921422 [file] [log] [blame]
Jamie Hannaford924c09d2014-11-19 12:05:38 +01001package secgroups
2
3import (
4 "github.com/mitchellh/mapstructure"
5
Jamie Hannaforda493e642014-11-19 12:40:30 +01006 "github.com/rackspace/gophercloud"
Jamie Hannaford924c09d2014-11-19 12:05:38 +01007 "github.com/rackspace/gophercloud/pagination"
8)
9
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010010// SecurityGroup represents a security group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010011type SecurityGroup struct {
Jamie Hannaford2f226172014-11-25 11:52:25 +010012 // The unique ID of the group. If Neutron is installed, this ID will be
13 // represented as a string UUID; if Neutron is not installed, it will be a
14 // numeric ID. For the sake of consistency, we always cast it to a string.
15 ID string
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010016
17 // The human-readable name of the group, which needs to be unique.
18 Name string
19
20 // The human-readable description of the group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010021 Description string
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010022
23 // The rules which determine how this security group operates.
24 Rules []Rule
25
Jamie Hannaford04abbc72014-11-21 11:27:57 +010026 // The ID of the tenant to which this security group belongs.
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010027 TenantID string `mapstructure:"tenant_id"`
Jamie Hannaford924c09d2014-11-19 12:05:38 +010028}
29
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010030// Rule represents a security group rule, a policy which determines how a
31// security group operates and what inbound traffic it allows in.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010032type Rule struct {
Jamie Hannaford2f226172014-11-25 11:52:25 +010033 // The unique ID. If Neutron is installed, this ID will be
34 // represented as a string UUID; if Neutron is not installed, it will be a
35 // numeric ID. For the sake of consistency, we always cast it to a string.
36 ID string
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010037
38 // The lower bound of the port range which this security group should open up
39 FromPort int `mapstructure:"from_port"`
40
41 // The upper bound of the port range which this security group should open up
42 ToPort int `mapstructure:"to_port"`
43
44 // The IP protocol (e.g. TCP) which the security group accepts
45 IPProtocol string `mapstructure:"ip_protocol"`
46
47 // The CIDR IP range whose traffic can be received
48 IPRange IPRange `mapstructure:"ip_range"`
49
Jamie Hannaford04abbc72014-11-21 11:27:57 +010050 // The security group ID to which this rule belongs
Jamie Hannaford2f226172014-11-25 11:52:25 +010051 ParentGroupID string `mapstructure:"parent_group_id"`
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010052
53 // Not documented.
54 Group Group
Jamie Hannaford924c09d2014-11-19 12:05:38 +010055}
56
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010057// IPRange represents the IP range whose traffic will be accepted by the
58// security group.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010059type IPRange struct {
60 CIDR string
61}
62
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010063// Group represents a group.
Jamie Hannafordb38dd312014-11-19 13:02:11 +010064type Group struct {
65 TenantID string `mapstructure:"tenant_id"`
66 Name string
67}
68
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010069// SecurityGroupPage is a single page of a SecurityGroup collection.
Jamie Hannaford924c09d2014-11-19 12:05:38 +010070type SecurityGroupPage struct {
71 pagination.SinglePageBase
72}
73
74// IsEmpty determines whether or not a page of Security Groups contains any results.
75func (page SecurityGroupPage) IsEmpty() (bool, error) {
76 users, err := ExtractSecurityGroups(page)
77 if err != nil {
78 return false, err
79 }
80 return len(users) == 0, nil
81}
82
83// ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results.
84func ExtractSecurityGroups(page pagination.Page) ([]SecurityGroup, error) {
85 casted := page.(SecurityGroupPage).Body
86 var response struct {
87 SecurityGroups []SecurityGroup `mapstructure:"security_groups"`
88 }
89
Jamie Hannafordc8c02c62014-11-25 12:04:57 +010090 err := mapstructure.WeakDecode(casted, &response)
91
Jamie Hannaford924c09d2014-11-19 12:05:38 +010092 return response.SecurityGroups, err
93}
Jamie Hannaforda493e642014-11-19 12:40:30 +010094
95type commonResult struct {
96 gophercloud.Result
97}
98
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +010099// CreateResult represents the result of a create operation.
Jamie Hannaforda493e642014-11-19 12:40:30 +0100100type CreateResult struct {
101 commonResult
102}
103
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100104// GetResult represents the result of a get operation.
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100105type GetResult struct {
106 commonResult
107}
108
Jamie Hannaford7f34d8e2014-11-20 12:24:55 +0100109// UpdateResult represents the result of an update operation.
Jamie Hannaford30c74662014-11-19 15:37:34 +0100110type UpdateResult struct {
111 commonResult
112}
113
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100114// Extract will extract a SecurityGroup struct from most responses.
Jamie Hannaforda493e642014-11-19 12:40:30 +0100115func (r commonResult) Extract() (*SecurityGroup, error) {
116 if r.Err != nil {
117 return nil, r.Err
118 }
119
120 var response struct {
121 SecurityGroup SecurityGroup `mapstructure:"security_group"`
122 }
123
Jamie Hannafordcb0c19a2014-11-25 11:57:35 +0100124 err := mapstructure.WeakDecode(r.Body, &response)
Jamie Hannaforda493e642014-11-19 12:40:30 +0100125
126 return &response.SecurityGroup, err
127}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100128
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100129// CreateRuleResult represents the result when adding rules to a security group.
130type CreateRuleResult struct {
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100131 gophercloud.Result
132}
133
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100134// Extract will extract a Rule struct from a CreateRuleResult.
135func (r CreateRuleResult) Extract() (*Rule, error) {
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100136 if r.Err != nil {
137 return nil, r.Err
138 }
139
140 var response struct {
141 Rule Rule `mapstructure:"security_group_rule"`
142 }
143
Jamie Hannafordc8c02c62014-11-25 12:04:57 +0100144 err := mapstructure.WeakDecode(r.Body, &response)
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100145
146 return &response.Rule, err
147}