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