blob: 17adf44627cf962b17a7d71387ef6c5b5bb4ab2f [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
10type SecurityGroup struct {
11 ID string
12 Name string
13 Description string
14 Rules []Rule
15 TenantID string `mapstructure:"tenant_id"`
16}
17
18type Rule struct {
Jamie Hannafordb38dd312014-11-19 13:02:11 +010019 ID string
20 FromPort int `mapstructure:"from_port"`
21 ToPort int `mapstructure:"to_port"`
22 IPProtocol string `mapstructure:"ip_protocol"`
23 IPRange IPRange `mapstructure:"ip_range"`
24 ParentGroupID string `mapstructure:"parent_group_id"`
25 Group Group
Jamie Hannaford924c09d2014-11-19 12:05:38 +010026}
27
28type IPRange struct {
29 CIDR string
30}
31
Jamie Hannafordb38dd312014-11-19 13:02:11 +010032type Group struct {
33 TenantID string `mapstructure:"tenant_id"`
34 Name string
35}
36
Jamie Hannaford924c09d2014-11-19 12:05:38 +010037// RolePage is a single page of a user Role collection.
38type SecurityGroupPage struct {
39 pagination.SinglePageBase
40}
41
42// IsEmpty determines whether or not a page of Security Groups contains any results.
43func (page SecurityGroupPage) IsEmpty() (bool, error) {
44 users, err := ExtractSecurityGroups(page)
45 if err != nil {
46 return false, err
47 }
48 return len(users) == 0, nil
49}
50
51// ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results.
52func ExtractSecurityGroups(page pagination.Page) ([]SecurityGroup, error) {
53 casted := page.(SecurityGroupPage).Body
54 var response struct {
55 SecurityGroups []SecurityGroup `mapstructure:"security_groups"`
56 }
57
58 err := mapstructure.Decode(casted, &response)
59 return response.SecurityGroups, err
60}
Jamie Hannaforda493e642014-11-19 12:40:30 +010061
62type commonResult struct {
63 gophercloud.Result
64}
65
66type CreateResult struct {
67 commonResult
68}
69
Jamie Hannafordb38dd312014-11-19 13:02:11 +010070type GetResult struct {
71 commonResult
72}
73
Jamie Hannaford30c74662014-11-19 15:37:34 +010074type UpdateResult struct {
75 commonResult
76}
77
Jamie Hannaforda493e642014-11-19 12:40:30 +010078func (r commonResult) Extract() (*SecurityGroup, error) {
79 if r.Err != nil {
80 return nil, r.Err
81 }
82
83 var response struct {
84 SecurityGroup SecurityGroup `mapstructure:"security_group"`
85 }
86
87 err := mapstructure.Decode(r.Body, &response)
88
89 return &response.SecurityGroup, err
90}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +010091
92type AddRuleResult struct {
93 gophercloud.Result
94}
95
96func (r AddRuleResult) Extract() (*Rule, error) {
97 if r.Err != nil {
98 return nil, r.Err
99 }
100
101 var response struct {
102 Rule Rule `mapstructure:"security_group_rule"`
103 }
104
105 err := mapstructure.Decode(r.Body, &response)
106
107 return &response.Rule, err
108}