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

     1  package groups
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // Group is a firewall group.
     9  type Group struct {
    10  	ID                      string   `json:"id"`
    11  	TenantID                string   `json:"tenant_id"`
    12  	Name                    string   `json:"name"`
    13  	Description             string   `json:"description"`
    14  	IngressFirewallPolicyID string   `json:"ingress_firewall_policy_id"`
    15  	EgressFirewallPolicyID  string   `json:"egress_firewall_policy_id"`
    16  	AdminStateUp            bool     `json:"admin_state_up"`
    17  	Ports                   []string `json:"ports"`
    18  	Status                  string   `json:"status"`
    19  	Shared                  bool     `json:"shared"`
    20  	ProjectID               string   `json:"project_id"`
    21  }
    22  
    23  type commonResult struct {
    24  	gophercloud.Result
    25  }
    26  
    27  // Extract is a function that accepts a result and extracts a firewall group.
    28  func (r commonResult) Extract() (*Group, error) {
    29  	var s struct {
    30  		Group *Group `json:"firewall_group"`
    31  	}
    32  	err := r.ExtractInto(&s)
    33  	return s.Group, err
    34  }
    35  
    36  // GroupPage is the page returned by a pager when traversing over a
    37  // collection of firewall groups.
    38  type GroupPage struct {
    39  	pagination.LinkedPageBase
    40  }
    41  
    42  // NextPageURL is invoked when a paginated collection of firewall groups has
    43  // reached the end of a page and the pager seeks to traverse over a new one.
    44  // In order to do this, it needs to construct the next page's URL.
    45  func (r GroupPage) NextPageURL() (string, error) {
    46  	var s struct {
    47  		Links []gophercloud.Link `json:"firewall_groups_links"`
    48  	}
    49  	err := r.ExtractInto(&s)
    50  	if err != nil {
    51  		return "", err
    52  	}
    53  	return gophercloud.ExtractNextURL(s.Links)
    54  }
    55  
    56  // IsEmpty checks whether a GroupPage struct is empty.
    57  func (r GroupPage) IsEmpty() (bool, error) {
    58  	if r.StatusCode == 204 {
    59  		return true, nil
    60  	}
    61  
    62  	is, err := ExtractGroups(r)
    63  	return len(is) == 0, err
    64  }
    65  
    66  // ExtractGroups accepts a Page struct, specifically a GroupPage struct,
    67  // and extracts the elements into a slice of Group structs. In other words,
    68  // a generic collection is mapped into a relevant slice.
    69  func ExtractGroups(r pagination.Page) ([]Group, error) {
    70  	var s struct {
    71  		Groups []Group `json:"firewall_groups"`
    72  	}
    73  	err := (r.(GroupPage)).ExtractInto(&s)
    74  	return s.Groups, err
    75  }
    76  
    77  // GetResult represents the result of a get operation.
    78  type GetResult struct {
    79  	commonResult
    80  }
    81  
    82  // CreateResult represents the result of a create operation.
    83  type CreateResult struct {
    84  	commonResult
    85  }
    86  
    87  // UpdateResult represents the result of an update operation.
    88  type UpdateResult struct {
    89  	commonResult
    90  }
    91  
    92  // DeleteResult represents the result of a delete operation.
    93  type DeleteResult struct {
    94  	gophercloud.ErrResult
    95  }