blob: 5f84b0e19b384c75992de0967d441102b7691337 [file] [log] [blame]
Jamie Hannaford17d2f872014-11-24 12:20:33 +01001package defsecrules
2
3import (
4 "github.com/mitchellh/mapstructure"
5
Jamie Hannaford43fa4a22014-11-24 12:49:17 +01006 "github.com/rackspace/gophercloud"
Jamie Hannaford17d2f872014-11-24 12:20:33 +01007 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11type DefaultRule secgroups.Rule
12
13// DefaultRulePage is a single page of a DefaultRule collection.
14type DefaultRulePage struct {
15 pagination.SinglePageBase
16}
17
18// IsEmpty determines whether or not a page of default rules contains any results.
19func (page DefaultRulePage) IsEmpty() (bool, error) {
20 users, err := ExtractDefaultRules(page)
21 if err != nil {
22 return false, err
23 }
24 return len(users) == 0, nil
25}
26
27// ExtractDefaultRules returns a slice of DefaultRules contained in a single
28// page of results.
29func ExtractDefaultRules(page pagination.Page) ([]DefaultRule, error) {
30 casted := page.(DefaultRulePage).Body
31 var response struct {
32 Rules []DefaultRule `mapstructure:"security_group_default_rules"`
33 }
34
35 err := mapstructure.Decode(casted, &response)
36
37 return response.Rules, err
38}
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010039
40type commonResult struct {
41 gophercloud.Result
42}
43
44// CreateResult represents the result of a create operation.
45type CreateResult struct {
46 commonResult
47}
48
49// GetResult represents the result of a get operation.
50type GetResult struct {
51 commonResult
52}
53
54// Extract will extract a DefaultRule struct from most responses.
55func (r commonResult) Extract() (*DefaultRule, error) {
56 if r.Err != nil {
57 return nil, r.Err
58 }
59
60 var response struct {
61 Rule DefaultRule `mapstructure:"security_group_default_rule"`
62 }
63
64 err := mapstructure.Decode(r.Body, &response)
65
66 return &response.Rule, err
67}