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

     1  package apiversions
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud/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  	if r.StatusCode == 204 {
    23  		return true, nil
    24  	}
    25  
    26  	is, err := ExtractAPIVersions(r)
    27  	return len(is) == 0, err
    28  }
    29  
    30  // ExtractAPIVersions takes a collection page, extracts all of the elements,
    31  // and returns them a slice of APIVersion structs. It is effectively a cast.
    32  func ExtractAPIVersions(r pagination.Page) ([]APIVersion, error) {
    33  	var s struct {
    34  		Versions []APIVersion `json:"versions"`
    35  	}
    36  	err := (r.(APIVersionPage)).ExtractInto(&s)
    37  	return s.Versions, err
    38  }
    39  
    40  // APIVersionResource represents a generic API resource. It contains the name
    41  // of the resource and its plural collection name.
    42  type APIVersionResource struct {
    43  	Name       string `json:"name"`
    44  	Collection string `json:"collection"`
    45  }
    46  
    47  // APIVersionResourcePage is a concrete type which embeds the common
    48  // SinglePageBase struct, and is used when traversing API versions collections.
    49  type APIVersionResourcePage struct {
    50  	pagination.SinglePageBase
    51  }
    52  
    53  // IsEmpty is a concrete function which indicates whether an
    54  // APIVersionResourcePage is empty or not.
    55  func (r APIVersionResourcePage) IsEmpty() (bool, error) {
    56  	if r.StatusCode == 204 {
    57  		return true, nil
    58  	}
    59  
    60  	is, err := ExtractVersionResources(r)
    61  	return len(is) == 0, err
    62  }
    63  
    64  // ExtractVersionResources accepts a Page struct, specifically a
    65  // APIVersionResourcePage struct, and extracts the elements into a slice of
    66  // APIVersionResource structs. In other words, the collection is mapped into
    67  // a relevant slice.
    68  func ExtractVersionResources(r pagination.Page) ([]APIVersionResource, error) {
    69  	var s struct {
    70  		APIVersionResources []APIVersionResource `json:"resources"`
    71  	}
    72  	err := (r.(APIVersionResourcePage)).ExtractInto(&s)
    73  	return s.APIVersionResources, err
    74  }