github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/image/v2/tasks/requests.go (about)

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