-
Notifications
You must be signed in to change notification settings - Fork 47
feat: Add suport for list/get/run/stop/create/update/delete task #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de4c537
feat: Add support for list/get/run/stop task
xino12 ae2d5cb
Add task service
xino12 7aed7a9
feat: Add support for create/update/delete/get/list tasks
xino12 3b7e335
Add task tests
xino12 0fd141f
Add clean policies, initialize client for cleanup policy service
xino12 6ab5348
Merge branch 'datadrivers:main' into main
xino12 9068adc
Merge branch 'main' into main
anmoel a3a7cfe
Merge branch 'datadrivers:main' into main
xino12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package task | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "github.com/datadrivers/go-nexus-client/nexus3/pkg/tools" | ||
| "github.com/datadrivers/go-nexus-client/nexus3/schema" | ||
| "net/http" | ||
| "net/url" | ||
|
|
||
| "github.com/datadrivers/go-nexus-client/nexus3/pkg/client" | ||
| "github.com/datadrivers/go-nexus-client/nexus3/schema/task" | ||
| ) | ||
|
|
||
| const ( | ||
| taskAPIEndpoint = client.BasePath + "v1/tasks" | ||
| ) | ||
|
|
||
| var ( | ||
| ErrTaskNotRunning = errors.New("task is not currently running") | ||
| ) | ||
|
|
||
| type TaskService client.Service | ||
|
|
||
| func NewTaskService(c *client.Client) *TaskService { | ||
| return &TaskService{Client: c} | ||
| } | ||
|
|
||
| func (s *TaskService) ListTasks(taskType *string, continuationToken *string) ([]task.Task, *string, error) { | ||
| q := url.Values{} | ||
| if taskType != nil { | ||
| q.Set("type", *taskType) | ||
| } | ||
| if continuationToken != nil { | ||
| q.Set("continuationToken", *continuationToken) | ||
| } | ||
|
|
||
| body, resp, err := s.Client.Get(fmt.Sprintf("%s?%s", taskAPIEndpoint, q.Encode()), nil) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, nil, fmt.Errorf("could not list task: HTTP: %d, %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| var result schema.PaginationResult[task.Task] | ||
| if err := json.Unmarshal(body, &result); err != nil { | ||
| return nil, nil, fmt.Errorf("could not unmarshal tasks: %v", err) | ||
| } | ||
|
|
||
| return result.Items, result.ContinuationToken, nil | ||
| } | ||
|
|
||
| func (s *TaskService) GetTask(id string) (*task.Task, error) { | ||
| body, resp, err := s.Client.Get(fmt.Sprintf("%s/%s", taskAPIEndpoint, id), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| switch resp.StatusCode { | ||
| case http.StatusOK: | ||
| var t task.Task | ||
| if err := json.Unmarshal(body, &t); err != nil { | ||
| return nil, fmt.Errorf("could not unmarshal task: %v", err) | ||
| } | ||
| return &t, nil | ||
| case http.StatusNotFound: | ||
| return nil, nil | ||
| default: | ||
| return nil, fmt.Errorf("could not get task '%s': HTTP: %d, %s", id, resp.StatusCode, string(body)) | ||
| } | ||
| } | ||
|
|
||
| func (s *TaskService) RunTask(id string) error { | ||
| body, resp, err := s.Client.Post(fmt.Sprintf("%s/%s/run", taskAPIEndpoint, id), nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if resp.StatusCode != http.StatusNoContent { | ||
| return fmt.Errorf("could not run task '%s': HTTP: %d, %s", id, resp.StatusCode, string(body)) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *TaskService) StopTask(id string) error { | ||
| body, resp, err := s.Client.Post(fmt.Sprintf("%s/%s/stop", taskAPIEndpoint, id), nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| switch resp.StatusCode { | ||
| case http.StatusNoContent: | ||
| return nil | ||
| case http.StatusConflict: | ||
| return ErrTaskNotRunning | ||
| default: | ||
| return fmt.Errorf("could not stop task '%s': HTTP: %d, %s", id, resp.StatusCode, string(body)) | ||
| } | ||
| } | ||
|
|
||
| func (s *TaskService) CreateTask(newTask *task.TaskCreateStruct) (*task.Task, error) { | ||
| ioReader, err := tools.JsonMarshalInterfaceToIOReader(newTask) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| body, resp, err := s.Client.Post(taskAPIEndpoint, ioReader) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusCreated { | ||
| return nil, fmt.Errorf("could not create task: HTTP: %d, %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| var createdTask task.Task | ||
| if err := json.Unmarshal(body, &createdTask); err != nil { | ||
| return nil, fmt.Errorf("could not unmarshal created task: %v", err) | ||
| } | ||
|
|
||
| return &createdTask, nil | ||
| } | ||
|
|
||
| func (s *TaskService) UpdateTask(id string, updatedTask *task.TaskCreateStruct) error { | ||
| ioReader, err := tools.JsonMarshalInterfaceToIOReader(updatedTask) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| body, resp, err := s.Client.Put(fmt.Sprintf("%s/%s", taskAPIEndpoint, id), ioReader) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusNoContent { | ||
| return fmt.Errorf("could not update task '%s': HTTP: %d, %s", id, resp.StatusCode, string(body)) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *TaskService) DeleteTask(id string) error { | ||
| body, resp, err := s.Client.Delete(fmt.Sprintf("%s/%s", taskAPIEndpoint, id)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusNoContent { | ||
| return fmt.Errorf("could not delete task '%s': HTTP: %d, %s", id, resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please seperate changes for cleanup policy into a own PR