github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/fwaas/rules/results.go (about)

     1  package rules
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/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 []gophercloud.Link `json:"firewall_rules_links"`
    40  	}
    41  	err := r.ExtractInto(&s)
    42  	if err != nil {
    43  		return "", err
    44  	}
    45  	return gophercloud.ExtractNextURL(s.Links)
    46  }
    47  
    48  // IsEmpty checks whether a RulePage struct is empty.
    49  func (r RulePage) IsEmpty() (bool, error) {
    50  	if r.StatusCode == 204 {
    51  		return true, nil
    52  	}
    53  
    54  	is, err := ExtractRules(r)
    55  	return len(is) == 0, err
    56  }
    57  
    58  // ExtractRules accepts a Page struct, specifically a RulePage struct,
    59  // and extracts the elements into a slice of Rule structs. In other words,
    60  // a generic collection is mapped into a relevant slice.
    61  func ExtractRules(r pagination.Page) ([]Rule, error) {
    62  	var s struct {
    63  		Rules []Rule `json:"firewall_rules"`
    64  	}
    65  	err := (r.(RulePage)).ExtractInto(&s)
    66  	return s.Rules, err
    67  }
    68  
    69  type commonResult struct {
    70  	gophercloud.Result
    71  }
    72  
    73  // Extract is a function that accepts a result and extracts a firewall rule.
    74  func (r commonResult) Extract() (*Rule, error) {
    75  	var s struct {
    76  		Rule *Rule `json:"firewall_rule"`
    77  	}
    78  	err := r.ExtractInto(&s)
    79  	return s.Rule, err
    80  }
    81  
    82  // GetResult represents the result of a get operation. Call its Extract method
    83  // to interpret it as a Rule.
    84  type GetResult struct {
    85  	commonResult
    86  }
    87  
    88  // UpdateResult represents the result of an update operation. Call its Extract
    89  // method to interpret it as a Rule.
    90  type UpdateResult struct {
    91  	commonResult
    92  }
    93  
    94  // DeleteResult represents the result of a delete operation. Call its ExtractErr
    95  // method to determine if the request succeeded or failed.
    96  type DeleteResult struct {
    97  	gophercloud.ErrResult
    98  }
    99  
   100  // CreateResult represents the result of a create operation. Call its Extract
   101  // method to interpret it as a Rule.
   102  type CreateResult struct {
   103  	commonResult
   104  }