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

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