github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/api/cloudcontroller/ccv3/task.go (about) 1 package ccv3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "net/url" 9 10 "code.cloudfoundry.org/cli/api/cloudcontroller" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 12 ) 13 14 // Task represents a Cloud Controller V3 Task. 15 type Task struct { 16 GUID string `json:"guid"` 17 SequenceID int `json:"sequence_id"` 18 Name string `json:"name"` 19 Command string `json:"command"` 20 State string `json:"state"` 21 CreatedAt string `json:"created_at"` 22 MemoryInMB uint64 `json:"memory_in_mb"` 23 DiskInMB uint64 `json:"disk_in_mb"` 24 } 25 26 // NewTaskBody represents the body of the request to create a Task. 27 type NewTaskBody struct { 28 Command string `json:"command"` 29 Name string `json:"name,omitempty"` 30 MemoryInMB uint64 `json:"memory_in_mb,omitempty"` 31 DiskInMB uint64 `json:"disk_in_mb,omitempty"` 32 } 33 34 // NewTask runs a command in the Application environment associated with the 35 // provided Application GUID. 36 func (client *Client) NewTask(appGUID string, command string, name string, memory uint64, disk uint64) (Task, Warnings, error) { 37 bodyBytes, err := json.Marshal(NewTaskBody{ 38 Command: command, 39 Name: name, 40 MemoryInMB: memory, 41 DiskInMB: disk, 42 }) 43 if err != nil { 44 return Task{}, nil, err 45 } 46 47 request, err := client.newHTTPRequest(requestOptions{ 48 RequestName: internal.NewAppTaskRequest, 49 URIParams: internal.Params{ 50 "guid": appGUID, 51 }, 52 Body: bytes.NewBuffer(bodyBytes), 53 }) 54 if err != nil { 55 return Task{}, nil, err 56 } 57 58 var task Task 59 response := cloudcontroller.Response{ 60 Result: &task, 61 } 62 63 err = client.connection.Make(request, &response) 64 if err != nil { 65 return Task{}, response.Warnings, err 66 } 67 68 return task, response.Warnings, nil 69 } 70 71 // GetApplicationTasks returns a list of tasks associated with the provided 72 // application GUID. Results can be filtered by providing URL queries. 73 func (client *Client) GetApplicationTasks(appGUID string, query url.Values) ([]Task, Warnings, error) { 74 request, err := client.newHTTPRequest(requestOptions{ 75 RequestName: internal.GetAppTasksRequest, 76 URIParams: internal.Params{ 77 "guid": appGUID, 78 }, 79 Query: query, 80 }) 81 if err != nil { 82 return nil, nil, err 83 } 84 85 var fullTasksList []Task 86 warnings, err := client.paginate(request, Task{}, func(item interface{}) error { 87 if task, ok := item.(Task); ok { 88 fullTasksList = append(fullTasksList, task) 89 } else { 90 return cloudcontroller.UnknownObjectInListError{ 91 Expected: Task{}, 92 Unexpected: item, 93 } 94 } 95 return nil 96 }) 97 98 return fullTasksList, warnings, err 99 } 100 101 // UpdateTask cancels a task. 102 func (client *Client) UpdateTask(taskGUID string) (Task, Warnings, error) { 103 request, err := client.newHTTPRequest(requestOptions{ 104 URL: fmt.Sprintf("%s/v3/tasks/%s/cancel", client.cloudControllerURL, taskGUID), 105 Method: http.MethodPut, 106 }) 107 if err != nil { 108 return Task{}, nil, err 109 } 110 111 var task Task 112 response := cloudcontroller.Response{ 113 Result: &task, 114 } 115 116 err = client.connection.Make(request, &response) 117 if err != nil { 118 return Task{}, response.Warnings, err 119 } 120 121 return task, response.Warnings, nil 122 }