File: request.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,416 kB
  • sloc: sh: 99; makefile: 21
file content (80 lines) | stat: -rw-r--r-- 2,418 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package instanceactions

import (
	"net/url"
	"time"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/pagination"
)

// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
	ToInstanceActionsListQuery() (string, error)
}

// ListOpts represents options used to filter instance action results
// in a List request.
type ListOpts struct {
	// Limit is an integer value to limit the results to return.
	// This requires microversion 2.58 or later.
	Limit int `q:"limit"`

	// Marker is the request ID of the last-seen instance action.
	// This requires microversion 2.58 or later.
	Marker string `q:"marker"`

	// ChangesSince filters the response by actions after the given time.
	// This requires microversion 2.58 or later.
	ChangesSince *time.Time `q:"changes-since"`

	// ChangesBefore filters the response by actions before the given time.
	// This requires microversion 2.66 or later.
	ChangesBefore *time.Time `q:"changes-before"`
}

// ToInstanceActionsListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToInstanceActionsListQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	if err != nil {
		return "", err
	}

	params := q.Query()

	if opts.ChangesSince != nil {
		params.Add("changes-since", opts.ChangesSince.Format(time.RFC3339))
	}

	if opts.ChangesBefore != nil {
		params.Add("changes-before", opts.ChangesBefore.Format(time.RFC3339))
	}

	q = &url.URL{RawQuery: params.Encode()}
	return q.String(), nil
}

// List makes a request against the API to list the servers actions.
func List(client *gophercloud.ServiceClient, id string, opts ListOptsBuilder) pagination.Pager {
	url := listURL(client, id)
	if opts != nil {
		query, err := opts.ToInstanceActionsListQuery()
		if err != nil {
			return pagination.Pager{Err: err}
		}
		url += query
	}
	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
		return InstanceActionPage{pagination.SinglePageBase(r)}
	})
}

// Get makes a request against the API to get a server action.
func Get(client *gophercloud.ServiceClient, serverID, requestID string) (r InstanceActionResult) {
	resp, err := client.Get(instanceActionsURL(client, serverID, requestID), &r.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}