github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/baremetal/v1/conductors/requests.go (about)

     1  package conductors
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     9  )
    10  
    11  // ListOptsBuilder allows extensions to add additional parameters to the
    12  // List request.
    13  type ListOptsBuilder interface {
    14  	ToConductorListQuery() (string, error)
    15  }
    16  
    17  // ListOpts allows the filtering and sorting of paginated collections through
    18  // the API. Filtering is achieved by passing in struct field values that map to
    19  // the conductor attributes you want to see returned. Marker and Limit are used
    20  // for pagination.
    21  type ListOpts struct {
    22  	// One or more fields to be returned in the response.
    23  	Fields []string `q:"fields" format:"comma-separated"`
    24  
    25  	// Requests a page size of items.
    26  	Limit int `q:"limit"`
    27  
    28  	// The ID of the last-seen item.
    29  	Marker string `q:"marker"`
    30  
    31  	// Sorts the response by the requested sort direction.
    32  	SortDir string `q:"sort_dir"`
    33  
    34  	// Sorts the response by the this attribute value.
    35  	SortKey string `q:"sort_key"`
    36  
    37  	// Provide additional information for the BIOS Settings
    38  	Detail bool `q:"detail"`
    39  }
    40  
    41  // ToConductorListQuery formats a ListOpts into a query string.
    42  func (opts ListOpts) ToConductorListQuery() (string, error) {
    43  	if opts.Detail && len(opts.Fields) > 0 {
    44  		return "", fmt.Errorf("cannot have both fields and detail options for conductors")
    45  	}
    46  
    47  	q, err := gophercloud.BuildQueryString(opts)
    48  	return q.String(), err
    49  }
    50  
    51  // List makes a request against the API to list conductors accessible to you.
    52  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    53  	url := listURL(client)
    54  	if opts != nil {
    55  		query, err := opts.ToConductorListQuery()
    56  		if err != nil {
    57  			return pagination.Pager{Err: err}
    58  		}
    59  		url += query
    60  	}
    61  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    62  		return ConductorPage{pagination.LinkedPageBase{PageResult: r}}
    63  	})
    64  }
    65  
    66  // Get requests details on a single conductor by hostname
    67  func Get(ctx context.Context, client *gophercloud.ServiceClient, name string) (r GetResult) {
    68  	resp, err := client.Get(ctx, getURL(client, name), &r.Body, &gophercloud.RequestOpts{
    69  		OkCodes: []int{200},
    70  	})
    71  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    72  	return
    73  }