github.com/arunkumar7540/cli@v6.45.0+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"` 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 } 34 35 // CreateApplicationTask runs a command in the Application environment 36 // associated with the provided Application GUID. 37 func (client *Client) CreateApplicationTask(appGUID string, task Task) (Task, Warnings, error) { 38 bodyBytes, err := json.Marshal(task) 39 if err != nil { 40 return Task{}, nil, err 41 } 42 43 request, err := client.newHTTPRequest(requestOptions{ 44 RequestName: internal.PostApplicationTasksRequest, 45 URIParams: internal.Params{ 46 "app_guid": appGUID, 47 }, 48 Body: bytes.NewReader(bodyBytes), 49 }) 50 if err != nil { 51 return Task{}, nil, err 52 } 53 54 var responseTask Task 55 response := cloudcontroller.Response{ 56 DecodeJSONResponseInto: &responseTask, 57 } 58 59 err = client.connection.Make(request, &response) 60 return responseTask, response.Warnings, err 61 } 62 63 // GetApplicationTasks returns a list of tasks associated with the provided 64 // application GUID. Results can be filtered by providing URL queries. 65 func (client *Client) GetApplicationTasks(appGUID string, query ...Query) ([]Task, Warnings, error) { 66 request, err := client.newHTTPRequest(requestOptions{ 67 RequestName: internal.GetApplicationTasksRequest, 68 URIParams: internal.Params{ 69 "app_guid": appGUID, 70 }, 71 Query: query, 72 }) 73 if err != nil { 74 return nil, nil, err 75 } 76 77 var fullTasksList []Task 78 warnings, err := client.paginate(request, Task{}, func(item interface{}) error { 79 if task, ok := item.(Task); ok { 80 fullTasksList = append(fullTasksList, task) 81 } else { 82 return ccerror.UnknownObjectInListError{ 83 Expected: Task{}, 84 Unexpected: item, 85 } 86 } 87 return nil 88 }) 89 90 return fullTasksList, warnings, err 91 } 92 93 // UpdateTaskCancel cancels a task. 94 func (client *Client) UpdateTaskCancel(taskGUID string) (Task, Warnings, error) { 95 request, err := client.newHTTPRequest(requestOptions{ 96 RequestName: internal.PutTaskCancelRequest, 97 URIParams: internal.Params{ 98 "task_guid": taskGUID, 99 }, 100 }) 101 if err != nil { 102 return Task{}, nil, err 103 } 104 105 var task Task 106 response := cloudcontroller.Response{ 107 DecodeJSONResponseInto: &task, 108 } 109 110 err = client.connection.Make(request, &response) 111 if err != nil { 112 return Task{}, response.Warnings, err 113 } 114 115 return task, response.Warnings, nil 116 }