github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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  	// Command represents the command that will be executed. May be excluded
    16  	// based on the user's role.
    17  	Command string `json:"command,omitempty"`
    18  	// CreatedAt represents the time with zone when the object was created.
    19  	CreatedAt string `json:"created_at,omitempty"`
    20  	// DiskInMB represents the disk in MB allocated for the task.
    21  	DiskInMB uint64 `json:"disk_in_mb,omitempty"`
    22  	// GUID represents the unique task identifier.
    23  	GUID string `json:"guid,omitempty"`
    24  	// MemoryInMB represents the memory in MB allocated for the task.
    25  	MemoryInMB uint64 `json:"memory_in_mb,omitempty"`
    26  	// Name represents the name of the task.
    27  	Name string `json:"name,omitempty"`
    28  	// SequenceID represents the user-facing id of the task. This number is
    29  	// unique for every task associated with a given app.
    30  	SequenceID int64 `json:"sequence_id,omitempty"`
    31  	// State represents the task state.
    32  	State constant.TaskState `json:"state,omitempty"`
    33  	// Tasks can use a process as a template to fill in
    34  	// command, memory, disk values
    35  	//
    36  	// Using a pointer so that it can be set to nil to prevent
    37  	// json serialization when no template is used
    38  	Template *TaskTemplate `json:"template,omitempty"`
    39  }
    40  
    41  type TaskTemplate struct {
    42  	Process TaskProcessTemplate `json:"process,omitempty"`
    43  }
    44  
    45  type TaskProcessTemplate struct {
    46  	Guid string `json:"guid,omitempty"`
    47  }
    48  
    49  // CreateApplicationTask runs a command in the Application environment
    50  // associated with the provided Application GUID.
    51  func (client *Client) CreateApplicationTask(appGUID string, task Task) (Task, Warnings, error) {
    52  	bodyBytes, err := json.Marshal(task)
    53  	if err != nil {
    54  		return Task{}, nil, err
    55  	}
    56  
    57  	request, err := client.newHTTPRequest(requestOptions{
    58  		RequestName: internal.PostApplicationTasksRequest,
    59  		URIParams: internal.Params{
    60  			"app_guid": appGUID,
    61  		},
    62  		Body: bytes.NewReader(bodyBytes),
    63  	})
    64  	if err != nil {
    65  		return Task{}, nil, err
    66  	}
    67  
    68  	var responseTask Task
    69  	response := cloudcontroller.Response{
    70  		DecodeJSONResponseInto: &responseTask,
    71  	}
    72  
    73  	err = client.connection.Make(request, &response)
    74  	return responseTask, response.Warnings, err
    75  }
    76  
    77  // GetApplicationTasks returns a list of tasks associated with the provided
    78  // application GUID. Results can be filtered by providing URL queries.
    79  func (client *Client) GetApplicationTasks(appGUID string, query ...Query) ([]Task, Warnings, error) {
    80  	request, err := client.newHTTPRequest(requestOptions{
    81  		RequestName: internal.GetApplicationTasksRequest,
    82  		URIParams: internal.Params{
    83  			"app_guid": appGUID,
    84  		},
    85  		Query: query,
    86  	})
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  
    91  	var fullTasksList []Task
    92  	warnings, err := client.paginate(request, Task{}, func(item interface{}) error {
    93  		if task, ok := item.(Task); ok {
    94  			fullTasksList = append(fullTasksList, task)
    95  		} else {
    96  			return ccerror.UnknownObjectInListError{
    97  				Expected:   Task{},
    98  				Unexpected: item,
    99  			}
   100  		}
   101  		return nil
   102  	})
   103  
   104  	return fullTasksList, warnings, err
   105  }
   106  
   107  // UpdateTaskCancel cancels a task.
   108  func (client *Client) UpdateTaskCancel(taskGUID string) (Task, Warnings, error) {
   109  	request, err := client.newHTTPRequest(requestOptions{
   110  		RequestName: internal.PutTaskCancelRequest,
   111  		URIParams: internal.Params{
   112  			"task_guid": taskGUID,
   113  		},
   114  	})
   115  	if err != nil {
   116  		return Task{}, nil, err
   117  	}
   118  
   119  	var task Task
   120  	response := cloudcontroller.Response{
   121  		DecodeJSONResponseInto: &task,
   122  	}
   123  
   124  	err = client.connection.Make(request, &response)
   125  	if err != nil {
   126  		return Task{}, response.Warnings, err
   127  	}
   128  
   129  	return task, response.Warnings, nil
   130  }