blob: f990c9991d099b33ab1f3fa739cb507eedb875c2 [file] [log] [blame]
Jamie Hannaford17d2f872014-11-24 12:20:33 +01001package defsecrules
2
3import (
jrperritt5cb543c2017-02-20 14:03:36 -06004 "encoding/json"
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
jrperritt5cb543c2017-02-20 14:03:36 -060015func (r *DefaultRule) UnmarshalJSON(b []byte) error {
16 var s secgroups.Rule
17 err := json.Unmarshal(b, &s)
18 if err != nil {
19 return err
20 }
21 *r = DefaultRule(s)
22 return nil
23}
24
Jamie Hannaford17d2f872014-11-24 12:20:33 +010025// DefaultRulePage is a single page of a DefaultRule collection.
26type DefaultRulePage struct {
27 pagination.SinglePageBase
28}
29
30// IsEmpty determines whether or not a page of default rules contains any results.
31func (page DefaultRulePage) IsEmpty() (bool, error) {
32 users, err := ExtractDefaultRules(page)
Jon Perritt31b66462016-02-25 22:25:30 -060033 return len(users) == 0, err
Jamie Hannaford17d2f872014-11-24 12:20:33 +010034}
35
36// ExtractDefaultRules returns a slice of DefaultRules contained in a single
37// page of results.
Jon Perritt31b66462016-02-25 22:25:30 -060038func ExtractDefaultRules(r pagination.Page) ([]DefaultRule, error) {
Jon Perritt12395212016-02-24 10:41:17 -060039 var s struct {
40 DefaultRules []DefaultRule `json:"security_group_default_rules"`
Jamie Hannaford17d2f872014-11-24 12:20:33 +010041 }
Jon Perritt31b66462016-02-25 22:25:30 -060042 err := (r.(DefaultRulePage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060043 return s.DefaultRules, err
Jamie Hannaford17d2f872014-11-24 12:20:33 +010044}
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010045
46type commonResult struct {
47 gophercloud.Result
48}
49
50// CreateResult represents the result of a create operation.
51type CreateResult struct {
52 commonResult
53}
54
55// GetResult represents the result of a get operation.
56type GetResult struct {
57 commonResult
58}
59
60// Extract will extract a DefaultRule struct from most responses.
61func (r commonResult) Extract() (*DefaultRule, error) {
Jon Perritt12395212016-02-24 10:41:17 -060062 var s struct {
63 DefaultRule DefaultRule `json:"security_group_default_rule"`
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010064 }
Jon Perritt12395212016-02-24 10:41:17 -060065 err := r.ExtractInto(&s)
66 return &s.DefaultRule, err
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010067}