github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/instanceactions/request.go (about)

     1  package instanceactions
     2  
     3  import (
     4  	"net/url"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // ListOptsBuilder allows extensions to add additional parameters to the
    12  // List request.
    13  type ListOptsBuilder interface {
    14  	ToInstanceActionsListQuery() (string, error)
    15  }
    16  
    17  // ListOpts represents options used to filter instance action results
    18  // in a List request.
    19  type ListOpts struct {
    20  	// Limit is an integer value to limit the results to return.
    21  	// This requires microversion 2.58 or later.
    22  	Limit int `q:"limit"`
    23  
    24  	// Marker is the request ID of the last-seen instance action.
    25  	// This requires microversion 2.58 or later.
    26  	Marker string `q:"marker"`
    27  
    28  	// ChangesSince filters the response by actions after the given time.
    29  	// This requires microversion 2.58 or later.
    30  	ChangesSince *time.Time `q:"changes-since"`
    31  
    32  	// ChangesBefore filters the response by actions before the given time.
    33  	// This requires microversion 2.66 or later.
    34  	ChangesBefore *time.Time `q:"changes-before"`
    35  }
    36  
    37  // ToInstanceActionsListQuery formats a ListOpts into a query string.
    38  func (opts ListOpts) ToInstanceActionsListQuery() (string, error) {
    39  	q, err := gophercloud.BuildQueryString(opts)
    40  	if err != nil {
    41  		return "", err
    42  	}
    43  
    44  	params := q.Query()
    45  
    46  	if opts.ChangesSince != nil {
    47  		params.Add("changes-since", opts.ChangesSince.Format(time.RFC3339))
    48  	}
    49  
    50  	if opts.ChangesBefore != nil {
    51  		params.Add("changes-before", opts.ChangesBefore.Format(time.RFC3339))
    52  	}
    53  
    54  	q = &url.URL{RawQuery: params.Encode()}
    55  	return q.String(), nil
    56  }
    57  
    58  // List makes a request against the API to list the servers actions.
    59  func List(client *gophercloud.ServiceClient, id string, opts ListOptsBuilder) pagination.Pager {
    60  	url := listURL(client, id)
    61  	if opts != nil {
    62  		query, err := opts.ToInstanceActionsListQuery()
    63  		if err != nil {
    64  			return pagination.Pager{Err: err}
    65  		}
    66  		url += query
    67  	}
    68  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    69  		return InstanceActionPage{pagination.SinglePageBase(r)}
    70  	})
    71  }
    72  
    73  // Get makes a request against the API to get a server action.
    74  func Get(client *gophercloud.ServiceClient, serverID, requestID string) (r InstanceActionResult) {
    75  	resp, err := client.Get(instanceActionsURL(client, serverID, requestID), &r.Body, &gophercloud.RequestOpts{
    76  		OkCodes: []int{200},
    77  	})
    78  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    79  	return
    80  }