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