github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/defsecrules/results.go (about) 1 package defsecrules 2 3 import ( 4 "encoding/json" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups" 8 "github.com/gophercloud/gophercloud/pagination" 9 ) 10 11 // DefaultRule represents a rule belonging to the "default" security group. 12 // It is identical to an openstack/compute/v2/extensions/secgroups.Rule. 13 type DefaultRule secgroups.Rule 14 15 func (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 25 // DefaultRulePage is a single page of a DefaultRule collection. 26 type DefaultRulePage struct { 27 pagination.SinglePageBase 28 } 29 30 // IsEmpty determines whether or not a page of default rules contains any results. 31 func (page DefaultRulePage) IsEmpty() (bool, error) { 32 if page.StatusCode == 204 { 33 return true, nil 34 } 35 36 users, err := ExtractDefaultRules(page) 37 return len(users) == 0, err 38 } 39 40 // ExtractDefaultRules returns a slice of DefaultRules contained in a single 41 // page of results. 42 func ExtractDefaultRules(r pagination.Page) ([]DefaultRule, error) { 43 var s struct { 44 DefaultRules []DefaultRule `json:"security_group_default_rules"` 45 } 46 err := (r.(DefaultRulePage)).ExtractInto(&s) 47 return s.DefaultRules, err 48 } 49 50 type commonResult struct { 51 gophercloud.Result 52 } 53 54 // CreateResult represents the result of a create operation. 55 type CreateResult struct { 56 commonResult 57 } 58 59 // GetResult represents the result of a get operation. 60 type GetResult struct { 61 commonResult 62 } 63 64 // Extract will extract a DefaultRule struct from a Create or Get response. 65 func (r commonResult) Extract() (*DefaultRule, error) { 66 var s struct { 67 DefaultRule DefaultRule `json:"security_group_default_rule"` 68 } 69 err := r.ExtractInto(&s) 70 return &s.DefaultRule, err 71 } 72 73 // DeleteResult is the response from a delete operation. Call its ExtractErr 74 // method to determine if the request succeeded or failed. 75 type DeleteResult struct { 76 gophercloud.ErrResult 77 }