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

     1  package apiversions
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk/pagination"
     5  )
     6  
     7  // APIVersion represents an API version for Neutron. It contains the status of
     8  // the API, and its unique ID.
     9  type APIVersion struct {
    10  	Status string `son:"status"`
    11  	ID     string `json:"id"`
    12  }
    13  
    14  // APIVersionPage is the page returned by a pager when traversing over a
    15  // collection of API versions.
    16  type APIVersionPage struct {
    17  	pagination.SinglePageBase
    18  }
    19  
    20  // IsEmpty checks whether an APIVersionPage struct is empty.
    21  func (r APIVersionPage) IsEmpty() (bool, error) {
    22  	is, err := ExtractAPIVersions(r)
    23  	return len(is) == 0, err
    24  }
    25  
    26  // ExtractAPIVersions takes a collection page, extracts all of the elements,
    27  // and returns them a slice of APIVersion structs. It is effectively a cast.
    28  func ExtractAPIVersions(r pagination.Page) ([]APIVersion, error) {
    29  	var s struct {
    30  		Versions []APIVersion `json:"versions"`
    31  	}
    32  	err := (r.(APIVersionPage)).ExtractInto(&s)
    33  	return s.Versions, err
    34  }
    35  
    36  // APIVersionResource represents a generic API resource. It contains the name
    37  // of the resource and its plural collection name.
    38  type APIVersionResource struct {
    39  	Name       string `json:"name"`
    40  	Collection string `json:"collection"`
    41  }
    42  
    43  // APIVersionResourcePage is a concrete type which embeds the common
    44  // SinglePageBase struct, and is used when traversing API versions collections.
    45  type APIVersionResourcePage struct {
    46  	pagination.SinglePageBase
    47  }
    48  
    49  // IsEmpty is a concrete function which indicates whether an
    50  // APIVersionResourcePage is empty or not.
    51  func (r APIVersionResourcePage) IsEmpty() (bool, error) {
    52  	is, err := ExtractVersionResources(r)
    53  	return len(is) == 0, err
    54  }
    55  
    56  // ExtractVersionResources accepts a Page struct, specifically a
    57  // APIVersionResourcePage struct, and extracts the elements into a slice of
    58  // APIVersionResource structs. In other words, the collection is mapped into
    59  // a relevant slice.
    60  func ExtractVersionResources(r pagination.Page) ([]APIVersionResource, error) {
    61  	var s struct {
    62  		APIVersionResources []APIVersionResource `json:"resources"`
    63  	}
    64  	err := (r.(APIVersionResourcePage)).ExtractInto(&s)
    65  	return s.APIVersionResources, err
    66  }