/
requests.go
130 lines (108 loc) · 3.67 KB
/
requests.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package tasks
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// TaskStatus represents valid task status.
// You can use this type to compare the actual status of a task to a one of the
// pre-defined statuses.
type TaskStatus string
const (
// TaskStatusPending represents status of the pending task.
TaskStatusPending TaskStatus = "pending"
// TaskStatusProcessing represents status of the processing task.
TaskStatusProcessing TaskStatus = "processing"
// TaskStatusSuccess represents status of the success task.
TaskStatusSuccess TaskStatus = "success"
// TaskStatusFailure represents status of the failure task.
TaskStatusFailure TaskStatus = "failure"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToTaskListQuery() (string, error)
}
// ListOpts allows the filtering and sorting of paginated collections through
// the OpenStack Imageservice tasks API.
type ListOpts struct {
// Integer value for the limit of values to return.
Limit int `q:"limit"`
// ID of the task at which you want to set a marker.
Marker string `q:"marker"`
// SortDir allows to select sort direction.
// It can be "asc" or "desc" (default).
SortDir string `q:"sort_dir"`
// SortKey allows to sort by one of the following tTask attributes:
// - created_at
// - expires_at
// - status
// - type
// - updated_at
// Default is created_at.
SortKey string `q:"sort_key"`
// ID filters on the identifier of the task.
ID string `json:"id"`
// Type filters on the type of the task.
Type string `json:"type"`
// Status filters on the status of the task.
Status TaskStatus `q:"status"`
}
// ToTaskListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToTaskListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List returns a Pager which allows you to iterate over a collection of the tasks.
func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(c)
if opts != nil {
query, err := opts.ToTaskListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
taskPage := TaskPage{
serviceURL: c.ServiceURL(),
LinkedPageBase: pagination.LinkedPageBase{PageResult: r},
}
return taskPage
})
}
// Get retrieves a specific Imageservice task based on its ID.
func Get(c *gophercloud.ServiceClient, taskID string) (r GetResult) {
resp, err := c.Get(getURL(c, taskID), &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// CreateOptsBuilder allows to add additional parameters to the Create request.
type CreateOptsBuilder interface {
ToTaskCreateMap() (map[string]interface{}, error)
}
// CreateOpts specifies parameters of a new Imageservice task.
type CreateOpts struct {
Type string `json:"type" required:"true"`
Input map[string]interface{} `json:"input"`
}
// ToTaskCreateMap constructs a request body from CreateOpts.
func (opts CreateOpts) ToTaskCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return b, nil
}
// Create requests the creation of a new Imageservice task on the server.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToTaskCreateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}