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

     1  package providers
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // Provider is the Octavia driver that implements the load balancing mechanism
     9  type Provider struct {
    10  	// Human-readable description for the Loadbalancer.
    11  	Description string `json:"description"`
    12  
    13  	// Human-readable name for the Provider.
    14  	Name string `json:"name"`
    15  }
    16  
    17  // ProviderPage is the page returned by a pager when traversing over a
    18  // collection of providers.
    19  type ProviderPage struct {
    20  	pagination.LinkedPageBase
    21  }
    22  
    23  // NextPageURL is invoked when a paginated collection of providers has
    24  // reached the end of a page and the pager seeks to traverse over a new one.
    25  // In order to do this, it needs to construct the next page's URL.
    26  func (r ProviderPage) NextPageURL() (string, error) {
    27  	var s struct {
    28  		Links []gophercloud.Link `json:"providers_links"`
    29  	}
    30  	err := r.ExtractInto(&s)
    31  	if err != nil {
    32  		return "", err
    33  	}
    34  	return gophercloud.ExtractNextURL(s.Links)
    35  }
    36  
    37  // IsEmpty checks whether a ProviderPage struct is empty.
    38  func (r ProviderPage) IsEmpty() (bool, error) {
    39  	if r.StatusCode == 204 {
    40  		return true, nil
    41  	}
    42  
    43  	is, err := ExtractProviders(r)
    44  	return len(is) == 0, err
    45  }
    46  
    47  // ExtractProviders accepts a Page struct, specifically a ProviderPage
    48  // struct, and extracts the elements into a slice of Provider structs. In
    49  // other words, a generic collection is mapped into a relevant slice.
    50  func ExtractProviders(r pagination.Page) ([]Provider, error) {
    51  	var s struct {
    52  		Providers []Provider `json:"providers"`
    53  	}
    54  	err := (r.(ProviderPage)).ExtractInto(&s)
    55  	return s.Providers, err
    56  }
    57  
    58  type commonResult struct {
    59  	gophercloud.Result
    60  }
    61  
    62  // Extract is a function that accepts a result and extracts a provider.
    63  func (r commonResult) Extract() (*Provider, error) {
    64  	var s struct {
    65  		Provider *Provider `json:"provider"`
    66  	}
    67  	err := r.ExtractInto(&s)
    68  	return s.Provider, err
    69  }
    70  
    71  // GetResult represents the result of a get operation. Call its Extract
    72  // method to interpret it as a Provider.
    73  type GetResult struct {
    74  	commonResult
    75  }