github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/imageservice/v2/tasks/requests.go (about) 1 package tasks 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/golangsdk/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 := golangsdk.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 *golangsdk.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 *golangsdk.ServiceClient, taskID string) (r GetResult) { 93 _, r.Err = c.Get(getURL(c, taskID), &r.Body, nil) 94 return 95 } 96 97 // CreateOptsBuilder allows to add additional parameters to the Create request. 98 type CreateOptsBuilder interface { 99 ToTaskCreateMap() (map[string]interface{}, error) 100 } 101 102 // CreateOpts specifies parameters of a new Imageservice task. 103 type CreateOpts struct { 104 Type string `json:"type" required:"true"` 105 Input map[string]interface{} `json:"input"` 106 } 107 108 // ToTaskCreateMap constructs a request body from CreateOpts. 109 func (opts CreateOpts) ToTaskCreateMap() (map[string]interface{}, error) { 110 b, err := golangsdk.BuildRequestBody(opts, "") 111 if err != nil { 112 return nil, err 113 } 114 return b, nil 115 } 116 117 // Create requests the creation of a new Imageservice task on the server. 118 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 119 b, err := opts.ToTaskCreateMap() 120 if err != nil { 121 r.Err = err 122 return 123 } 124 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 125 OkCodes: []int{201}, 126 }) 127 return 128 }