github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/flavors/results.go (about)

     1  package flavors
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // Flavor provide specs for the creation of a load balancer.
     9  type Flavor struct {
    10  	// The unique ID for the Flavor
    11  	ID string `json:"id"`
    12  
    13  	// Human-readable name for the Flavor. Does not have to be unique.
    14  	Name string `json:"name"`
    15  
    16  	// Human-readable description for the Flavor.
    17  	Description string `json:"description"`
    18  
    19  	// Status of the Flavor.
    20  	Enabled bool `json:"enabled"`
    21  
    22  	// Flavor Profile apply to this Flavor.
    23  	FlavorProfileId string `json:"flavor_profile_id"`
    24  }
    25  
    26  // FlavorPage is the page returned by a pager when traversing over a
    27  // collection of flavors.
    28  type FlavorPage struct {
    29  	pagination.LinkedPageBase
    30  }
    31  
    32  // NextPageURL is invoked when a paginated collection of flavors has
    33  // reached the end of a page and the pager seeks to traverse over a new one.
    34  // In order to do this, it needs to construct the next page's URL.
    35  func (r FlavorPage) NextPageURL() (string, error) {
    36  	var s struct {
    37  		Links []gophercloud.Link `json:"flavors_links"`
    38  	}
    39  	err := r.ExtractInto(&s)
    40  	if err != nil {
    41  		return "", err
    42  	}
    43  	return gophercloud.ExtractNextURL(s.Links)
    44  }
    45  
    46  // IsEmpty checks whether a FlavorPage struct is empty.
    47  func (r FlavorPage) IsEmpty() (bool, error) {
    48  	is, err := ExtractFlavors(r)
    49  	return len(is) == 0, err
    50  }
    51  
    52  // ExtractFlavors accepts a Page struct, specifically a FlavorPage
    53  // struct, and extracts the elements into a slice of Flavor structs. In
    54  // other words, a generic collection is mapped into a relevant slice.
    55  func ExtractFlavors(r pagination.Page) ([]Flavor, error) {
    56  	var s struct {
    57  		Flavors []Flavor `json:"flavors"`
    58  	}
    59  	err := (r.(FlavorPage)).ExtractInto(&s)
    60  	return s.Flavors, err
    61  }
    62  
    63  type commonResult struct {
    64  	gophercloud.Result
    65  }
    66  
    67  // Extract is a function that accepts a result and extracts a flavor.
    68  func (r commonResult) Extract() (*Flavor, error) {
    69  	var s struct {
    70  		Flavor *Flavor `json:"flavor"`
    71  	}
    72  	err := r.ExtractInto(&s)
    73  	return s.Flavor, err
    74  }
    75  
    76  // CreateResult represents the result of a create operation. Call its Extract
    77  // method to interpret it as a Flavor.
    78  type CreateResult struct {
    79  	commonResult
    80  }
    81  
    82  // GetResult represents the result of a get operation. Call its Extract
    83  // method to interpret it as a Flavor.
    84  type GetResult struct {
    85  	commonResult
    86  }
    87  
    88  // UpdateResult represents the result of an update operation. Call its Extract
    89  // method to interpret it as a Flavor.
    90  type UpdateResult struct {
    91  	commonResult
    92  }
    93  
    94  // DeleteResult represents the result of a delete operation. Call its
    95  // ExtractErr method to determine if the request succeeded or failed.
    96  type DeleteResult struct {
    97  	gophercloud.ErrResult
    98  }