blob: 8f5477edf4a6c1abb7af58069475e9f50bb218b2 [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 {
19 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}
25
26type IPRange struct {
27 CIDR string
28}
29
30// RolePage is a single page of a user Role collection.
31type SecurityGroupPage struct {
32 pagination.SinglePageBase
33}
34
35// IsEmpty determines whether or not a page of Security Groups contains any results.
36func (page SecurityGroupPage) IsEmpty() (bool, error) {
37 users, err := ExtractSecurityGroups(page)
38 if err != nil {
39 return false, err
40 }
41 return len(users) == 0, nil
42}
43
44// ExtractSecurityGroups returns a slice of SecurityGroups contained in a single page of results.
45func ExtractSecurityGroups(page pagination.Page) ([]SecurityGroup, error) {
46 casted := page.(SecurityGroupPage).Body
47 var response struct {
48 SecurityGroups []SecurityGroup `mapstructure:"security_groups"`
49 }
50
51 err := mapstructure.Decode(casted, &response)
52 return response.SecurityGroups, err
53}
Jamie Hannaforda493e642014-11-19 12:40:30 +010054
55type commonResult struct {
56 gophercloud.Result
57}
58
59type CreateResult struct {
60 commonResult
61}
62
63func (r commonResult) Extract() (*SecurityGroup, error) {
64 if r.Err != nil {
65 return nil, r.Err
66 }
67
68 var response struct {
69 SecurityGroup SecurityGroup `mapstructure:"security_group"`
70 }
71
72 err := mapstructure.Decode(r.Body, &response)
73
74 return &response.SecurityGroup, err
75}