github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/imageservice/v2/tasks/results.go (about)

     1  package tasks
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  )
     9  
    10  type commonResult struct {
    11  	golangsdk.Result
    12  }
    13  
    14  // GetResult represents the result of a Get operation. Call its Extract
    15  // method to interpret it as a Task.
    16  type GetResult struct {
    17  	commonResult
    18  }
    19  
    20  // CreateResult represents the result of a Create operation. Call its Extract
    21  // method to interpret it as a Task.
    22  type CreateResult struct {
    23  	commonResult
    24  }
    25  
    26  // Task represents a single task of the OpenStack Image service.
    27  type Task struct {
    28  	// ID is a unique identifier of the task.
    29  	ID string `json:"id"`
    30  
    31  	// Type represents the type of the task.
    32  	Type string `json:"type"`
    33  
    34  	// Status represents current status of the task.
    35  	// You can use the TaskStatus custom type to unmarshal raw JSON response into
    36  	// the pre-defined valid task status.
    37  	Status string `json:"status"`
    38  
    39  	// Input represents different parameters for the task.
    40  	Input map[string]interface{} `json:"input"`
    41  
    42  	// Result represents task result details.
    43  	Result map[string]interface{} `json:"result"`
    44  
    45  	// Owner is a unique identifier of the task owner.
    46  	Owner string `json:"owner"`
    47  
    48  	// Message represents human-readable message that is usually populated
    49  	// on task failure.
    50  	Message string `json:"message"`
    51  
    52  	// ExpiresAt contains the timestamp of when the task will become a subject of
    53  	// removal.
    54  	ExpiresAt time.Time `json:"expires_at"`
    55  
    56  	// CreatedAt contains the task creation timestamp.
    57  	CreatedAt time.Time `json:"created_at"`
    58  
    59  	// UpdatedAt contains the latest timestamp of when the task was updated.
    60  	UpdatedAt time.Time `json:"updated_at"`
    61  
    62  	// Self contains URI for the task.
    63  	Self string `json:"self"`
    64  
    65  	// Schema the path to the JSON-schema that represent the task.
    66  	Schema string `json:"schema"`
    67  }
    68  
    69  // Extract interprets any commonResult as a Task.
    70  func (r commonResult) Extract() (*Task, error) {
    71  	var s *Task
    72  	err := r.ExtractInto(&s)
    73  	return s, err
    74  }
    75  
    76  // TaskPage represents the results of a List request.
    77  type TaskPage struct {
    78  	serviceURL string
    79  	pagination.LinkedPageBase
    80  }
    81  
    82  // IsEmpty returns true if a TaskPage contains no Tasks results.
    83  func (r TaskPage) IsEmpty() (bool, error) {
    84  	tasks, err := ExtractTasks(r)
    85  	return len(tasks) == 0, err
    86  }
    87  
    88  // NextPageURL uses the response's embedded link reference to navigate to
    89  // the next page of results.
    90  func (r TaskPage) NextPageURL() (string, error) {
    91  	var s struct {
    92  		Next string `json:"next"`
    93  	}
    94  	err := r.ExtractInto(&s)
    95  	if err != nil {
    96  		return "", err
    97  	}
    98  
    99  	if s.Next == "" {
   100  		return "", nil
   101  	}
   102  
   103  	return nextPageURL(r.serviceURL, s.Next)
   104  }
   105  
   106  // ExtractTasks interprets the results of a single page from a List() call,
   107  // producing a slice of Task entities.
   108  func ExtractTasks(r pagination.Page) ([]Task, error) {
   109  	var s struct {
   110  		Tasks []Task `json:"tasks"`
   111  	}
   112  	err := (r.(TaskPage)).ExtractInto(&s)
   113  	return s.Tasks, err
   114  }