github.com/leeclow-ops/gophercloud@v1.2.1/openstack/networking/v2/extensions/fwaas_v2/policies/results.go (about) 1 package policies 2 3 import ( 4 "github.com/leeclow-ops/gophercloud" 5 "github.com/leeclow-ops/gophercloud/pagination" 6 ) 7 8 // Policy is a firewall policy. 9 type Policy struct { 10 ID string `json:"id"` 11 Name string `json:"name"` 12 Description string `json:"description"` 13 TenantID string `json:"tenant_id"` 14 Audited bool `json:"audited"` 15 Shared bool `json:"shared"` 16 Rules []string `json:"firewall_rules,omitempty"` 17 } 18 19 type commonResult struct { 20 gophercloud.Result 21 } 22 23 type shortResult struct { 24 gophercloud.Result 25 } 26 27 // Extract is a function that accepts a result and extracts a firewall policy. 28 func (r commonResult) Extract() (*Policy, error) { 29 var s struct { 30 Policy *Policy `json:"firewall_policy"` 31 } 32 err := r.ExtractInto(&s) 33 return s.Policy, err 34 } 35 36 // Extract is a function that accepts a shortResult and extracts a firewall policy. 37 func (r shortResult) Extract() (*Policy, error) { 38 var policy *Policy 39 err := r.ExtractInto(&policy) 40 return policy, err 41 } 42 43 // PolicyPage is the page returned by a pager when traversing over a 44 // collection of firewall policies. 45 type PolicyPage struct { 46 pagination.LinkedPageBase 47 } 48 49 // NextPageURL is invoked when a paginated collection of firewall policies has 50 // reached the end of a page and the pager seeks to traverse over a new one. 51 // In order to do this, it needs to construct the next page's URL. 52 func (r PolicyPage) NextPageURL() (string, error) { 53 var s struct { 54 Links []gophercloud.Link `json:"firewall_policies_links"` 55 } 56 err := r.ExtractInto(&s) 57 if err != nil { 58 return "", err 59 } 60 return gophercloud.ExtractNextURL(s.Links) 61 } 62 63 // IsEmpty checks whether a PolicyPage struct is empty. 64 func (r PolicyPage) IsEmpty() (bool, error) { 65 if r.StatusCode == 204 { 66 return true, nil 67 } 68 69 is, err := ExtractPolicies(r) 70 return len(is) == 0, err 71 } 72 73 // ExtractPolicies accepts a Page struct, specifically a PolicyPage struct, 74 // and extracts the elements into a slice of Policy structs. In other words, 75 // a generic collection is mapped into a relevant slice. 76 func ExtractPolicies(r pagination.Page) ([]Policy, error) { 77 var s struct { 78 Policies []Policy `json:"firewall_policies"` 79 } 80 err := (r.(PolicyPage)).ExtractInto(&s) 81 return s.Policies, err 82 } 83 84 // GetResult represents the result of a get operation. 85 type GetResult struct { 86 commonResult 87 } 88 89 // UpdateResult represents the result of an update operation. 90 type UpdateResult struct { 91 commonResult 92 } 93 94 // DeleteResult represents the result of a delete operation. 95 type DeleteResult struct { 96 gophercloud.ErrResult 97 } 98 99 // CreateResult represents the result of a create operation. 100 type CreateResult struct { 101 commonResult 102 } 103 104 // InsertRuleResult represents the result of an InsertRule operation. 105 type InsertRuleResult struct { 106 shortResult 107 } 108 109 // RemoveRuleResult represents the result of a RemoveRule operation. 110 type RemoveRuleResult struct { 111 shortResult 112 }