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

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