github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/task.go (about)

     1  package v7action
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"sort"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  )
    12  
    13  // Task represents a V3 actor Task.
    14  type Task ccv3.Task
    15  
    16  // RunTask runs the provided command in the application environment associated
    17  // with the provided application GUID.
    18  func (actor Actor) RunTask(appGUID string, task Task) (Task, Warnings, error) {
    19  	createdTask, warnings, err := actor.CloudControllerClient.CreateApplicationTask(appGUID, ccv3.Task(task))
    20  	if err != nil {
    21  		if e, ok := err.(ccerror.TaskWorkersUnavailableError); ok {
    22  			return Task{}, Warnings(warnings), actionerror.TaskWorkersUnavailableError{Message: e.Error()}
    23  		}
    24  	}
    25  
    26  	return Task(createdTask), Warnings(warnings), err
    27  }
    28  
    29  // GetApplicationTasks returns a list of tasks associated with the provided
    30  // appplication GUID.
    31  func (actor Actor) GetApplicationTasks(appGUID string, sortOrder SortOrder) ([]Task, Warnings, error) {
    32  	tasks, warnings, err := actor.CloudControllerClient.GetApplicationTasks(appGUID)
    33  	actorWarnings := Warnings(warnings)
    34  	if err != nil {
    35  		return nil, actorWarnings, err
    36  	}
    37  
    38  	allTasks := []Task{}
    39  	for _, task := range tasks {
    40  		allTasks = append(allTasks, Task(task))
    41  	}
    42  
    43  	if sortOrder == Descending {
    44  		sort.Slice(allTasks, func(i int, j int) bool { return allTasks[i].SequenceID > allTasks[j].SequenceID })
    45  	} else {
    46  		sort.Slice(allTasks, func(i int, j int) bool { return allTasks[i].SequenceID < allTasks[j].SequenceID })
    47  	}
    48  
    49  	return allTasks, actorWarnings, nil
    50  }
    51  
    52  func (actor Actor) GetTaskBySequenceIDAndApplication(sequenceID int, appGUID string) (Task, Warnings, error) {
    53  	tasks, warnings, err := actor.CloudControllerClient.GetApplicationTasks(
    54  		appGUID,
    55  		ccv3.Query{Key: ccv3.SequenceIDFilter, Values: []string{strconv.Itoa(sequenceID)}},
    56  	)
    57  	if err != nil {
    58  		return Task{}, Warnings(warnings), err
    59  	}
    60  
    61  	if len(tasks) == 0 {
    62  		return Task{}, Warnings(warnings), actionerror.TaskNotFoundError{SequenceID: sequenceID}
    63  	}
    64  
    65  	return Task(tasks[0]), Warnings(warnings), nil
    66  }
    67  
    68  func (actor Actor) TerminateTask(taskGUID string) (Task, Warnings, error) {
    69  	task, warnings, err := actor.CloudControllerClient.UpdateTaskCancel(taskGUID)
    70  	return Task(task), Warnings(warnings), err
    71  }