v1.0.0
This commit is contained in:
47
pkg/cloudbroker/tasks/get.go
Normal file
47
pkg/cloudbroker/tasks/get.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Request struct for get background API task status and result
|
||||
type GetRequest struct {
|
||||
// ID of audit GUID
|
||||
// Required: true
|
||||
AuditID string `url:"auditId"`
|
||||
}
|
||||
|
||||
func (trq GetRequest) validate() error {
|
||||
if trq.AuditID == "" {
|
||||
return errors.New("validation-error: field AuditID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get gets background API task status and result
|
||||
func (t Tasks) Get(ctx context.Context, req GetRequest) (*RecordTask, error) {
|
||||
err := req.validate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/tasks/get"
|
||||
|
||||
res, err := t.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
item := RecordTask{}
|
||||
|
||||
err = json.Unmarshal(res, &item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &item, nil
|
||||
}
|
||||
37
pkg/cloudbroker/tasks/list.go
Normal file
37
pkg/cloudbroker/tasks/list.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Request struct for get list audits
|
||||
type ListRequest struct {
|
||||
// Page number
|
||||
// Required: false
|
||||
Page uint64 `url:"page,omitempty"`
|
||||
|
||||
// Page size
|
||||
// Required: false
|
||||
Size uint64 `url:"size,omitempty"`
|
||||
}
|
||||
|
||||
// List gets list user API task with status PROCESSING
|
||||
func (t Tasks) List(ctx context.Context, req ListRequest) (ListTasks, error) {
|
||||
url := "/cloudbroker/tasks/list"
|
||||
|
||||
res, err := t.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
item := ListTasks{}
|
||||
|
||||
err = json.Unmarshal(res, &item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return item, nil
|
||||
}
|
||||
71
pkg/cloudbroker/tasks/models.go
Normal file
71
pkg/cloudbroker/tasks/models.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Global variable for converting field to desired data type
|
||||
type InfoResult int
|
||||
|
||||
// Method for convert field
|
||||
func (r *InfoResult) UnmarshalJSON(b []byte) error {
|
||||
if b[0] == '"' {
|
||||
b := b[1 : len(b)-1]
|
||||
if len(b) == 0 {
|
||||
*r = 0
|
||||
return nil
|
||||
}
|
||||
n, err := strconv.Atoi(string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*r = InfoResult(n)
|
||||
} else if b[0] == '[' {
|
||||
res := []interface{}{}
|
||||
if err := json.Unmarshal(b, &res); err != nil {
|
||||
return err
|
||||
}
|
||||
if n, ok := res[0].(float64); ok {
|
||||
*r = InfoResult(n)
|
||||
} else {
|
||||
return fmt.Errorf("could not unmarshal %v into int", res[0])
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Detailed information about task
|
||||
type RecordTask 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 InfoResult `json:"result"`
|
||||
|
||||
// Stage
|
||||
Stage string `json:"stage"`
|
||||
|
||||
// Status
|
||||
Status string `json:"status"`
|
||||
|
||||
// Update time
|
||||
UpdateTime uint64 `json:"updateTime"`
|
||||
|
||||
// Updated time
|
||||
UpdatedTime uint64 `json:"updatedTime"`
|
||||
}
|
||||
|
||||
// List of tasks
|
||||
type ListTasks []RecordTask
|
||||
16
pkg/cloudbroker/tasks/tasks.go
Normal file
16
pkg/cloudbroker/tasks/tasks.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// User API tasks interface
|
||||
package tasks
|
||||
|
||||
import "github.com/rudecs/decort-sdk/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: client,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user