github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv3/task.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    11  )
    12  
    13  // Task represents a Cloud Controller V3 Task.
    14  type Task struct {
    15  	GUID       string             `json:"guid,omitempty"`
    16  	SequenceID int                `json:"sequence_id,omitempty"`
    17  	Name       string             `json:"name,omitempty"`
    18  	Command    string             `json:"command"`
    19  	State      constant.TaskState `json:"state,omitempty"`
    20  	CreatedAt  string             `json:"created_at,omitempty"`
    21  	MemoryInMB uint64             `json:"memory_in_mb,omitempty"`
    22  	DiskInMB   uint64             `json:"disk_in_mb,omitempty"`
    23  }
    24  
    25  // CreateApplicationTask runs a command in the Application environment
    26  // associated with the provided Application GUID.
    27  func (client *Client) CreateApplicationTask(appGUID string, task Task) (Task, Warnings, error) {
    28  	bodyBytes, err := json.Marshal(task)
    29  	if err != nil {
    30  		return Task{}, nil, err
    31  	}
    32  
    33  	request, err := client.newHTTPRequest(requestOptions{
    34  		RequestName: internal.PostAppTasksRequest,
    35  		URIParams: internal.Params{
    36  			"app_guid": appGUID,
    37  		},
    38  		Body: bytes.NewReader(bodyBytes),
    39  	})
    40  	if err != nil {
    41  		return Task{}, nil, err
    42  	}
    43  
    44  	var responseTask Task
    45  	response := cloudcontroller.Response{
    46  		Result: &responseTask,
    47  	}
    48  
    49  	err = client.connection.Make(request, &response)
    50  	return responseTask, response.Warnings, err
    51  }
    52  
    53  // GetApplicationTasks returns a list of tasks associated with the provided
    54  // application GUID. Results can be filtered by providing URL queries.
    55  func (client *Client) GetApplicationTasks(appGUID string, query ...Query) ([]Task, Warnings, error) {
    56  	request, err := client.newHTTPRequest(requestOptions{
    57  		RequestName: internal.GetAppTasksRequest,
    58  		URIParams: internal.Params{
    59  			"app_guid": appGUID,
    60  		},
    61  		Query: query,
    62  	})
    63  	if err != nil {
    64  		return nil, nil, err
    65  	}
    66  
    67  	var fullTasksList []Task
    68  	warnings, err := client.paginate(request, Task{}, func(item interface{}) error {
    69  		if task, ok := item.(Task); ok {
    70  			fullTasksList = append(fullTasksList, task)
    71  		} else {
    72  			return ccerror.UnknownObjectInListError{
    73  				Expected:   Task{},
    74  				Unexpected: item,
    75  			}
    76  		}
    77  		return nil
    78  	})
    79  
    80  	return fullTasksList, warnings, err
    81  }
    82  
    83  // UpdateTask cancels a task.
    84  func (client *Client) UpdateTask(taskGUID string) (Task, Warnings, error) {
    85  	request, err := client.newHTTPRequest(requestOptions{
    86  		RequestName: internal.PutTaskCancelRequest,
    87  		URIParams: internal.Params{
    88  			"task_guid": taskGUID,
    89  		},
    90  	})
    91  	if err != nil {
    92  		return Task{}, nil, err
    93  	}
    94  
    95  	var task Task
    96  	response := cloudcontroller.Response{
    97  		Result: &task,
    98  	}
    99  
   100  	err = client.connection.Make(request, &response)
   101  	if err != nil {
   102  		return Task{}, response.Warnings, err
   103  	}
   104  
   105  	return task, response.Warnings, nil
   106  }