v9.0.0
This commit is contained in:
47
pkg/cloudapi/tasks/get.go
Normal file
47
pkg/cloudapi/tasks/get.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/internal/validators"
|
||||
)
|
||||
|
||||
// GetRequest struct to get information about task
|
||||
type GetRequest struct {
|
||||
// ID of audit
|
||||
// Required: true
|
||||
AuditID string `url:"auditId" json:"auditId" validate:"required"`
|
||||
}
|
||||
|
||||
// Get gets background API task status and result as a RecordAsyncTask struct
|
||||
func (t Tasks) Get(ctx context.Context, req GetRequest) (*RecordAsyncTask, error) {
|
||||
res, err := t.GetRaw(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := RecordAsyncTask{}
|
||||
|
||||
err = json.Unmarshal(res, &info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
|
||||
}
|
||||
|
||||
// GetRaw gets background API task status and result as an array of bytes
|
||||
func (t Tasks) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudapi/tasks/get"
|
||||
|
||||
res, err := t.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
return res, err
|
||||
}
|
||||
87
pkg/cloudapi/tasks/list.go
Normal file
87
pkg/cloudapi/tasks/list.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/internal/validators"
|
||||
)
|
||||
|
||||
// ListRequest struct to get list of tasks
|
||||
type ListRequest struct {
|
||||
// Find by guId
|
||||
// Required: false
|
||||
TaskID string `url:"taskId,omitempty" json:"taskId,omitempty"`
|
||||
|
||||
// Find by auditId
|
||||
// Required: false
|
||||
AuditID string `url:"auditId,omitempty" json:"auditId,omitempty"`
|
||||
|
||||
// Find by status
|
||||
// Required: false
|
||||
Status string `url:"status,omitempty" json:"status,omitempty"`
|
||||
|
||||
// Find by completed True or False
|
||||
// Required: false
|
||||
Completed interface{} `url:"completed,omitempty" json:"completed,omitempty" validate:"omitempty,isBool"`
|
||||
|
||||
// Sort by one of supported fields, format +|-(field)
|
||||
// Required: false
|
||||
SortBy string `url:"sortBy,omitempty" json:"sortBy,omitempty" validate:"omitempty,sortBy"`
|
||||
|
||||
// Find all tasks after point in time (unixtime)
|
||||
// Required: false
|
||||
UpdateTimeAt uint64 `url:"updateTimeAt,omitempty" json:"updateTimeAt,omitempty"`
|
||||
|
||||
// Find all tasks before point in time (unixtime)
|
||||
// Required: false
|
||||
UpdateTimeTo uint64 `url:"updateTimeTo,omitempty" json:"updateTimeTo,omitempty"`
|
||||
|
||||
// Page number
|
||||
// Default: 0
|
||||
// Default: 0
|
||||
// Required: false
|
||||
Page uint64 `url:"page,omitempty" json:"page,omitempty"`
|
||||
|
||||
// Page size
|
||||
// Default: 0
|
||||
// Default: 0
|
||||
// Required: false
|
||||
Size uint64 `url:"size,omitempty" json:"size,omitempty"`
|
||||
}
|
||||
|
||||
// List gets list of user API tasks with status PROCESSING as a ListTasks struct
|
||||
func (t Tasks) List(ctx context.Context, req ListRequest) (*ListTasks, error) {
|
||||
|
||||
res, err := t.ListRaw(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := ListTasks{}
|
||||
|
||||
err = json.Unmarshal(res, &list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &list, nil
|
||||
}
|
||||
|
||||
// ListRaw gets list of user API tasks with status PROCESSING as an array of bytes
|
||||
func (t Tasks) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
|
||||
|
||||
if err := validators.ValidateRequest(req); err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
if err := validators.ValidateRequest(req); err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudapi/tasks/list"
|
||||
|
||||
res, err := t.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
return res, err
|
||||
}
|
||||
146
pkg/cloudapi/tasks/models.go
Normal file
146
pkg/cloudapi/tasks/models.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Result structure of the task to provide methods
|
||||
type Result struct {
|
||||
Result interface{} `json:"result"`
|
||||
}
|
||||
|
||||
// Detailed information about task
|
||||
type RecordAsyncTask struct {
|
||||
// Audit ID
|
||||
AuditID string `json:"auditId"`
|
||||
|
||||
// Completed
|
||||
Completed bool `json:"completed"`
|
||||
|
||||
// Error
|
||||
Error string `json:"error"`
|
||||
|
||||
// List of logs
|
||||
Log []string `json:"log"`
|
||||
|
||||
// Final Result
|
||||
Result
|
||||
|
||||
// Stage
|
||||
Stage string `json:"stage"`
|
||||
|
||||
// Status
|
||||
Status string `json:"status"`
|
||||
|
||||
// Update time
|
||||
UpdateTime uint64 `json:"updateTime"`
|
||||
|
||||
// Updated by
|
||||
UpdatedBy string `json:"updatedBy"`
|
||||
|
||||
// Updated time
|
||||
UpdatedTime uint64 `json:"updatedTime"`
|
||||
}
|
||||
|
||||
// Detailed information about task
|
||||
type ItemAsyncTask struct {
|
||||
RecordAsyncTask
|
||||
|
||||
// GUID
|
||||
GUID string `json:"guid"`
|
||||
}
|
||||
|
||||
// List of tasks
|
||||
type ListTasks struct {
|
||||
Data []ItemAsyncTask `json:"data"`
|
||||
|
||||
EntryCount uint64 `json:"entryCount"`
|
||||
}
|
||||
|
||||
// ID returns ID of cluster or WG or any other resource successfully created as a Result of the task.
|
||||
// It returns error if Result does not contain any resource ID.
|
||||
func (r Result) ID() (int, error) {
|
||||
// check id from cluster - it comes as slice, like [1234, "cluster-name"]
|
||||
slice, ok := r.Result.([]interface{})
|
||||
if ok {
|
||||
if len(slice) == 0 {
|
||||
return 0, fmt.Errorf("could not get ID from empty slice")
|
||||
}
|
||||
|
||||
idFloat64, ok := slice[0].(float64)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("could not get ID from first slice element (%v)", slice[0])
|
||||
}
|
||||
|
||||
return int(idFloat64), nil
|
||||
}
|
||||
|
||||
// check id from other resources - it comes as id
|
||||
idFloat64, ok := r.Result.(float64)
|
||||
if ok {
|
||||
return int(idFloat64), nil
|
||||
}
|
||||
|
||||
return 0, errors.New("could not get ID because result is neither slice nor number (%v)")
|
||||
}
|
||||
|
||||
// Name returns name of cluster or WG successfully created as a Result of the task.
|
||||
// It returns error if Result does not contain k8s name.
|
||||
func (r Result) Name() (string, error) {
|
||||
slice, ok := r.Result.([]interface{})
|
||||
if !ok {
|
||||
return "", fmt.Errorf("could not convert Result (%v) to slice", r.Result)
|
||||
}
|
||||
|
||||
if len(slice) < 2 {
|
||||
return "", fmt.Errorf("could not get name from second slice element")
|
||||
}
|
||||
|
||||
var name string
|
||||
name, ok = slice[1].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("could not get name from second slice element (%v)", slice[1])
|
||||
}
|
||||
|
||||
return name, nil
|
||||
}
|
||||
|
||||
// ToMaps converts Result to a slice of maps containing back-up information as a result of the task.
|
||||
// It returns error if Result does not contain back-up information.
|
||||
func (r Result) ToMaps() ([]map[string]interface{}, error) {
|
||||
slice, ok := r.Result.([]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("could not convert Result (%v) to slice", r.Result)
|
||||
}
|
||||
|
||||
if len(slice) == 0 {
|
||||
return nil, fmt.Errorf("could not get maps from empty slice")
|
||||
}
|
||||
|
||||
result := make([]map[string]interface{}, 0, len(slice))
|
||||
for _, s := range slice {
|
||||
elem, ok := s.(map[string]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("could not get map[string]interface{} from slice element (%v)", s)
|
||||
}
|
||||
result = append(result, elem)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ToString converts Result to non-empty string.
|
||||
// It returns error if Result is not a string or is an empty string.
|
||||
func (r Result) ToString() (string, error) {
|
||||
status, ok := r.Result.(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("could not convert Result (%v) to string", r.Result)
|
||||
}
|
||||
|
||||
if status == "" {
|
||||
return "", fmt.Errorf("info contains empty string")
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
18
pkg/cloudapi/tasks/tasks.go
Normal file
18
pkg/cloudapi/tasks/tasks.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// User API tasks interface
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/interfaces"
|
||||
)
|
||||
|
||||
// Structure for creating request to tasks
|
||||
type Tasks struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
// Builder for tasks endpoints
|
||||
func New(client interfaces.Caller) *Tasks {
|
||||
return &Tasks{
|
||||
client,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user