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

     1  package apiversions
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/gophercloud/gophercloud/pagination"
     7  )
     8  
     9  // APIVersion represents an API version for Cinder.
    10  type APIVersion struct {
    11  	// ID is the unique identifier of the API version.
    12  	ID string `json:"id"`
    13  
    14  	// MinVersion is the minimum microversion supported.
    15  	MinVersion string `json:"min_version"`
    16  
    17  	// Status represents the status of the API version.
    18  	Status string `json:"status"`
    19  
    20  	// Updated is the date the API version was updated.
    21  	Updated time.Time `json:"updated"`
    22  
    23  	// Version is the current version and microversion.
    24  	Version string `json:"version"`
    25  }
    26  
    27  // APIVersionPage is the page returned by a pager when traversing over a
    28  // collection of API versions.
    29  type APIVersionPage struct {
    30  	pagination.SinglePageBase
    31  }
    32  
    33  // IsEmpty checks whether an APIVersionPage struct is empty.
    34  func (r APIVersionPage) IsEmpty() (bool, error) {
    35  	if r.StatusCode == 204 {
    36  		return true, nil
    37  	}
    38  
    39  	is, err := ExtractAPIVersions(r)
    40  	return len(is) == 0, err
    41  }
    42  
    43  // ExtractAPIVersions takes a collection page, extracts all of the elements,
    44  // and returns them a slice of APIVersion structs. It is effectively a cast.
    45  func ExtractAPIVersions(r pagination.Page) ([]APIVersion, error) {
    46  	var s struct {
    47  		Versions []APIVersion `json:"versions"`
    48  	}
    49  	err := (r.(APIVersionPage)).ExtractInto(&s)
    50  	return s.Versions, err
    51  }
    52  
    53  // ExtractAPIVersion takes a List result and extracts a single requested
    54  // version, which is returned as an APIVersion
    55  func ExtractAPIVersion(r pagination.Page, v string) (*APIVersion, error) {
    56  	allVersions, err := ExtractAPIVersions(r)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	for _, version := range allVersions {
    62  		if version.ID == v {
    63  			return &version, nil
    64  		}
    65  	}
    66  
    67  	return nil, ErrVersionNotFound{}
    68  }