github.com/gophercloud/gophercloud@v1.11.0/openstack/imageservice/v2/tasks/requests.go (about) 1 package tasks 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // TaskStatus represents valid task status. 9 // You can use this type to compare the actual status of a task to a one of the 10 // pre-defined statuses. 11 type TaskStatus string 12 13 const ( 14 // TaskStatusPending represents status of the pending task. 15 TaskStatusPending TaskStatus = "pending" 16 17 // TaskStatusProcessing represents status of the processing task. 18 TaskStatusProcessing TaskStatus = "processing" 19 20 // TaskStatusSuccess represents status of the success task. 21 TaskStatusSuccess TaskStatus = "success" 22 23 // TaskStatusFailure represents status of the failure task. 24 TaskStatusFailure TaskStatus = "failure" 25 ) 26 27 // ListOptsBuilder allows extensions to add additional parameters to the 28 // List request. 29 type ListOptsBuilder interface { 30 ToTaskListQuery() (string, error) 31 } 32 33 // ListOpts allows the filtering and sorting of paginated collections through 34 // the OpenStack Imageservice tasks API. 35 type ListOpts struct { 36 // Integer value for the limit of values to return. 37 Limit int `q:"limit"` 38 39 // ID of the task at which you want to set a marker. 40 Marker string `q:"marker"` 41 42 // SortDir allows to select sort direction. 43 // It can be "asc" or "desc" (default). 44 SortDir string `q:"sort_dir"` 45 46 // SortKey allows to sort by one of the following tTask attributes: 47 // - created_at 48 // - expires_at 49 // - status 50 // - type 51 // - updated_at 52 // Default is created_at. 53 SortKey string `q:"sort_key"` 54 55 // ID filters on the identifier of the task. 56 ID string `json:"id"` 57 58 // Type filters on the type of the task. 59 Type string `json:"type"` 60 61 // Status filters on the status of the task. 62 Status TaskStatus `q:"status"` 63 } 64 65 // ToTaskListQuery formats a ListOpts into a query string. 66 func (opts ListOpts) ToTaskListQuery() (string, error) { 67 q, err := gophercloud.BuildQueryString(opts) 68 return q.String(), err 69 } 70 71 // List returns a Pager which allows you to iterate over a collection of the tasks. 72 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 73 url := listURL(c) 74 if opts != nil { 75 query, err := opts.ToTaskListQuery() 76 if err != nil { 77 return pagination.Pager{Err: err} 78 } 79 url += query 80 } 81 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 82 taskPage := TaskPage{ 83 serviceURL: c.ServiceURL(), 84 LinkedPageBase: pagination.LinkedPageBase{PageResult: r}, 85 } 86 87 return taskPage 88 }) 89 } 90 91 // Get retrieves a specific Imageservice task based on its ID. 92 func Get(c *gophercloud.ServiceClient, taskID string) (r GetResult) { 93 resp, err := c.Get(getURL(c, taskID), &r.Body, nil) 94 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 95 return 96 } 97 98 // CreateOptsBuilder allows to add additional parameters to the Create request. 99 type CreateOptsBuilder interface { 100 ToTaskCreateMap() (map[string]interface{}, error) 101 } 102 103 // CreateOpts specifies parameters of a new Imageservice task. 104 type CreateOpts struct { 105 Type string `json:"type" required:"true"` 106 Input map[string]interface{} `json:"input"` 107 } 108 109 // ToTaskCreateMap constructs a request body from CreateOpts. 110 func (opts CreateOpts) ToTaskCreateMap() (map[string]interface{}, error) { 111 b, err := gophercloud.BuildRequestBody(opts, "") 112 if err != nil { 113 return nil, err 114 } 115 return b, nil 116 } 117 118 // Create requests the creation of a new Imageservice task on the server. 119 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 120 b, err := opts.ToTaskCreateMap() 121 if err != nil { 122 r.Err = err 123 return 124 } 125 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 126 OkCodes: []int{201}, 127 }) 128 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 129 return 130 }