github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/fwaas/rules/results.go (about)

     1  package rules
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // Rule represents a firewall rule.
     9  type Rule struct {
    10  	ID                   string `json:"id"`
    11  	Name                 string `json:"name,omitempty"`
    12  	Description          string `json:"description,omitempty"`
    13  	Protocol             string `json:"protocol"`
    14  	Action               string `json:"action"`
    15  	IPVersion            int    `json:"ip_version,omitempty"`
    16  	SourceIPAddress      string `json:"source_ip_address,omitempty"`
    17  	DestinationIPAddress string `json:"destination_ip_address,omitempty"`
    18  	SourcePort           string `json:"source_port,omitempty"`
    19  	DestinationPort      string `json:"destination_port,omitempty"`
    20  	Shared               bool   `json:"shared,omitempty"`
    21  	Enabled              bool   `json:"enabled,omitempty"`
    22  	PolicyID             string `json:"firewall_policy_id"`
    23  	Position             int    `json:"position"`
    24  	TenantID             string `json:"tenant_id"`
    25  	ProjectID            string `json:"project_id"`
    26  }
    27  
    28  // RulePage is the page returned by a pager when traversing over a
    29  // collection of firewall rules.
    30  type RulePage struct {
    31  	pagination.LinkedPageBase
    32  }
    33  
    34  // NextPageURL is invoked when a paginated collection of firewall rules has
    35  // reached the end of a page and the pager seeks to traverse over a new one.
    36  // In order to do this, it needs to construct the next page's URL.
    37  func (r RulePage) NextPageURL() (string, error) {
    38  	var s struct {
    39  		Links []golangsdk.Link `json:"firewall_rules_links"`
    40  	}
    41  	err := r.ExtractInto(&s)
    42  	if err != nil {
    43  		return "", err
    44  	}
    45  	return golangsdk.ExtractNextURL(s.Links)
    46  }
    47  
    48  // IsEmpty checks whether a RulePage struct is empty.
    49  func (r RulePage) IsEmpty() (bool, error) {
    50  	is, err := ExtractRules(r)
    51  	return len(is) == 0, err
    52  }
    53  
    54  // ExtractRules accepts a Page struct, specifically a RulePage struct,
    55  // and extracts the elements into a slice of Rule structs. In other words,
    56  // a generic collection is mapped into a relevant slice.
    57  func ExtractRules(r pagination.Page) ([]Rule, error) {
    58  	var s struct {
    59  		Rules []Rule `json:"firewall_rules"`
    60  	}
    61  	err := (r.(RulePage)).ExtractInto(&s)
    62  	return s.Rules, err
    63  }
    64  
    65  type commonResult struct {
    66  	golangsdk.Result
    67  }
    68  
    69  // Extract is a function that accepts a result and extracts a firewall rule.
    70  func (r commonResult) Extract() (*Rule, error) {
    71  	var s struct {
    72  		Rule *Rule `json:"firewall_rule"`
    73  	}
    74  	err := r.ExtractInto(&s)
    75  	return s.Rule, err
    76  }
    77  
    78  // GetResult represents the result of a get operation. Call its Extract method
    79  // to interpret it as a Rule.
    80  type GetResult struct {
    81  	commonResult
    82  }
    83  
    84  // UpdateResult represents the result of an update operation. Call its Extract
    85  // method to interpret it as a Rule.
    86  type UpdateResult struct {
    87  	commonResult
    88  }
    89  
    90  // DeleteResult represents the result of a delete operation. Call its ExtractErr
    91  // method to determine if the request succeeded or failed.
    92  type DeleteResult struct {
    93  	golangsdk.ErrResult
    94  }
    95  
    96  // CreateResult represents the result of a create operation. Call its Extract
    97  // method to interpret it as a Rule.
    98  type CreateResult struct {
    99  	commonResult
   100  }