blob: 64189708ca66cafeade969a4053813bc82e9ecf7 [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 Hannaforda493e642014-11-19 12:40:30 +010074func (r commonResult) Extract() (*SecurityGroup, error) {
75 if r.Err != nil {
76 return nil, r.Err
77 }
78
79 var response struct {
80 SecurityGroup SecurityGroup `mapstructure:"security_group"`
81 }
82
83 err := mapstructure.Decode(r.Body, &response)
84
85 return &response.SecurityGroup, err
86}