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