Refactoring
This commit is contained in:
77
pkg/cloudapi/disks/create.go
Normal file
77
pkg/cloudapi/disks/create.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/internal/validators"
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type CreateRequest struct {
|
||||
AccountId uint64 `url:"accountId"`
|
||||
GID uint64 `url:"gid"`
|
||||
Name string `url:"name"`
|
||||
Description string `url:"description,omitempty"`
|
||||
Size uint64 `url:"size,omitempty"`
|
||||
Type string `url:"type"`
|
||||
SSDSize uint64 `url:"ssdSize,omitempty"`
|
||||
IOps uint64 `url:"iops"`
|
||||
SepId uint64 `url:"sep_id,omitempty"`
|
||||
Pool string `url:"pool,omitempty"`
|
||||
}
|
||||
|
||||
func (drq CreateRequest) Validate() error {
|
||||
if drq.AccountId == 0 {
|
||||
return errors.New("validation-error: field AccountId can not be empty or equal to 0")
|
||||
}
|
||||
if drq.GID == 0 {
|
||||
return errors.New("validation-error: field Gid can not be empty or equal to 0")
|
||||
}
|
||||
if drq.Name == "" {
|
||||
return errors.New("validation-error: field Name can not be empty")
|
||||
}
|
||||
|
||||
validType := validators.StringInSlice(drq.Type, []string{"B", "D", "T"})
|
||||
if !validType {
|
||||
return errors.New("validation-error: field Type must be set as B, D or T")
|
||||
}
|
||||
|
||||
if drq.IOps == 0 {
|
||||
return errors.New("validation-error: field IOps must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) Create(ctx context.Context, req CreateRequest, options ...opts.DecortOpts) (uint64, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
url := "/disks/create"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseUint(string(res), 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
|
||||
}
|
||||
53
pkg/cloudapi/disks/delete.go
Normal file
53
pkg/cloudapi/disks/delete.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type DeleteRequest struct {
|
||||
DiskId uint64 `url:"diskId"`
|
||||
Detach bool `url:"detach,omitempty"`
|
||||
Permanently bool `url:"permanently,omitempty"`
|
||||
Reason string `url:"reason,omitempty"`
|
||||
}
|
||||
|
||||
func (d DeleteRequest) Validate() error {
|
||||
|
||||
if d.DiskId == 0 {
|
||||
return errors.New("validation-error: field DiskId must be set")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) Delete(ctx context.Context, req DeleteRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/disks/delete"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
55
pkg/cloudapi/disks/delete_disks.go
Normal file
55
pkg/cloudapi/disks/delete_disks.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type DisksDeleteRequest struct {
|
||||
DisksIds []uint64 `url:"diskIds"`
|
||||
Reason string `url:"reason"`
|
||||
Permanently bool `url:"permanently"`
|
||||
}
|
||||
|
||||
func (drq DisksDeleteRequest) Validate() error {
|
||||
if len(drq.DisksIds) == 0 {
|
||||
return errors.New("validation-error: field DisksIds must include one or more disks ids")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) DeleteDisks(ctx context.Context, req DisksDeleteRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/disks/deleteDisks"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
15
pkg/cloudapi/disks/disks.go
Normal file
15
pkg/cloudapi/disks/disks.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"github.com/rudecs/decort-sdk/interfaces"
|
||||
)
|
||||
|
||||
type Disks struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
func New(client interfaces.Caller) *Disks {
|
||||
return &Disks{
|
||||
client,
|
||||
}
|
||||
}
|
||||
55
pkg/cloudapi/disks/get.go
Normal file
55
pkg/cloudapi/disks/get.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type GetRequest struct {
|
||||
DiskId uint64 `url:"diskId"`
|
||||
}
|
||||
|
||||
func (drq GetRequest) Validate() error {
|
||||
if drq.DiskId == 0 {
|
||||
return errors.New("validation-error: field DiskId can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) Get(ctx context.Context, req GetRequest, options ...opts.DecortOpts) (*DiskRecord, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := "/disks/get"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
disk := &DiskRecord{}
|
||||
|
||||
err = json.Unmarshal(res, disk)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return disk, nil
|
||||
|
||||
}
|
||||
67
pkg/cloudapi/disks/limitio.go
Normal file
67
pkg/cloudapi/disks/limitio.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type LimitIORequest struct {
|
||||
DiskId uint64 `url:"diskId"`
|
||||
IOps uint64 `url:"iops"`
|
||||
TotalBytesSec uint64 `url:"total_bytes_sec"`
|
||||
ReadBytesSec uint64 `url:"read_bytes_sec"`
|
||||
WriteBytesSec uint64 `url:"write_bytes_sec"`
|
||||
TotalIOpsSec uint64 `url:"total_iops_sec"`
|
||||
ReadIOpsSec uint64 `url:"read_iops_sec"`
|
||||
WriteIOpsSec uint64 `url:"write_iops_sec"`
|
||||
TotalBytesSecMax uint64 `url:"total_bytes_sec_max"`
|
||||
ReadBytesSecMax uint64 `url:"read_bytes_sec_max"`
|
||||
WriteBytesSecMax uint64 `url:"write_bytes_sec_max"`
|
||||
TotalIOpsSecMax uint64 `url:"total_iops_sec_max"`
|
||||
ReadIOpsSecMax uint64 `url:"read_iops_sec_max"`
|
||||
WriteIOpsSecMax uint64 `url:"write_iops_sec_max"`
|
||||
SizeIOpsSec uint64 `url:"size_iops_sec"`
|
||||
}
|
||||
|
||||
func (drq LimitIORequest) Validate() error {
|
||||
if drq.DiskId == 0 {
|
||||
return errors.New("validation-error: field DiskId can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) LimitIO(ctx context.Context, req LimitIORequest, options ...opts.DecortOpts) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/disks/limitIO"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
72
pkg/cloudapi/disks/list.go
Normal file
72
pkg/cloudapi/disks/list.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type ListRequest struct {
|
||||
AccountId uint64 `url:"accountId,omitempty"`
|
||||
Type string `url:"type,omitempty"`
|
||||
Page uint64 `url:"page,omitempty"`
|
||||
Size uint64 `url:"size,omitempty"`
|
||||
}
|
||||
|
||||
func (d Disks) List(ctx context.Context, req ListRequest, options ...opts.DecortOpts) (DiskList, error) {
|
||||
url := "/disks/list"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
diskList := DiskList{}
|
||||
|
||||
err = json.Unmarshal(res, &diskList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return diskList, nil
|
||||
|
||||
}
|
||||
|
||||
func (d Disks) ListDeleted(ctx context.Context, req ListRequest, options ...opts.DecortOpts) (DiskList, error) {
|
||||
url := "/disks/listDeleted"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
diskList := DiskList{}
|
||||
|
||||
err = json.Unmarshal(res, &diskList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return diskList, nil
|
||||
|
||||
}
|
||||
41
pkg/cloudapi/disks/list_types.go
Normal file
41
pkg/cloudapi/disks/list_types.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type ListTypesRequest struct {
|
||||
Detailed bool `url:"detailed"`
|
||||
}
|
||||
|
||||
func (d Disks) ListTypes(ctx context.Context, req ListTypesRequest, options ...opts.DecortOpts) ([]interface{}, error) {
|
||||
url := "/disks/listTypes"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
typesList := make([]interface{}, 0)
|
||||
|
||||
err = json.Unmarshal(res, &typesList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return typesList, nil
|
||||
|
||||
}
|
||||
41
pkg/cloudapi/disks/list_unattached.go
Normal file
41
pkg/cloudapi/disks/list_unattached.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type ListUnattachedRequest struct {
|
||||
AccountId uint64 `url:"accountId"`
|
||||
}
|
||||
|
||||
func (d Disks) ListUnattached(ctx context.Context, req ListUnattachedRequest, options ...opts.DecortOpts) (DiskList, error) {
|
||||
url := "/disks/listUnattached"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
diskList := DiskList{}
|
||||
|
||||
err = json.Unmarshal(res, &diskList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return diskList, nil
|
||||
|
||||
}
|
||||
116
pkg/cloudapi/disks/models.go
Normal file
116
pkg/cloudapi/disks/models.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package disks
|
||||
|
||||
type Disk struct {
|
||||
Acl map[string]interface{} `json:"acl"`
|
||||
AccountID int `json:"accountId"`
|
||||
AccountName string `json:"accountName"`
|
||||
BootPartition int `json:"bootPartition"`
|
||||
CreatedTime uint64 `json:"createdTime"`
|
||||
ComputeID int `json:"computeId"`
|
||||
ComputeName string `json:"computeName"`
|
||||
DeletedTime uint64 `json:"deletedTime"`
|
||||
DeviceName string `json:"devicename"`
|
||||
Description string `json:"desc"`
|
||||
DestructionTime uint64 `json:"destructionTime"`
|
||||
GID uint64 `json:"gid"`
|
||||
ID uint64 `json:"id"`
|
||||
ImageID uint64 `json:"imageId"`
|
||||
Images []uint64 `json:"images"`
|
||||
IOTune IOTune `json:"iotune"`
|
||||
MachineId int `json:"machineId"`
|
||||
MachineName string `json:"machineName"`
|
||||
Name string `json:"name"`
|
||||
Order int `json:"order"`
|
||||
Params string `json:"params"`
|
||||
ParentId uint64 `json:"parentId"`
|
||||
PciSlot uint64 `json:"pciSlot"`
|
||||
Pool string `json:"pool"`
|
||||
PurgeTime uint64 `json:"purgeTime"`
|
||||
ResID string `json:"resId"`
|
||||
ResName string `json:"resName"`
|
||||
Role string `json:"role"`
|
||||
SepType string `json:"sepType"`
|
||||
SepID int `json:"sepId"` // NOTE: absent from compute/get output
|
||||
SizeMax int `json:"sizeMax"`
|
||||
Snapshots []Snapshot `json:"snapshots"`
|
||||
Status string `json:"status"`
|
||||
TechStatus string `json:"techStatus"`
|
||||
Type string `json:"type"`
|
||||
VMID int `json:"vmid"`
|
||||
}
|
||||
|
||||
type Snapshot struct {
|
||||
Guid string `json:"guid"`
|
||||
Label string `json:"label"`
|
||||
ResId string `json:"resId"`
|
||||
SnapSetGuid string `json:"snapSetGuid"`
|
||||
SnapSetTime uint64 `json:"snapSetTime"`
|
||||
TimeStamp uint64 `json:"timestamp"`
|
||||
}
|
||||
|
||||
type SnapshotList []Snapshot
|
||||
|
||||
type IOTune struct {
|
||||
ReadBytesSec int `json:"read_bytes_sec"`
|
||||
ReadBytesSecMax int `json:"read_bytes_sec_max"`
|
||||
ReadIopsSec int `json:"read_iops_sec"`
|
||||
ReadIopsSecMax int `json:"read_iops_sec_max"`
|
||||
SizeIopsSec int `json:"size_iops_sec"`
|
||||
TotalBytesSec int `json:"total_bytes_sec"`
|
||||
TotalBytesSecMax int `json:"total_bytes_sec_max"`
|
||||
TotalIopsSec int `json:"total_iops_sec"`
|
||||
TotalIopsSecMax int `json:"total_iops_sec_max"`
|
||||
WriteBytesSec int `json:"write_bytes_sec"`
|
||||
WriteBytesSecMax int `json:"write_bytes_sec_max"`
|
||||
WriteIopsSec int `json:"write_iops_sec"`
|
||||
WriteIopsSecMax int `json:"write_iops_sec_max"`
|
||||
}
|
||||
|
||||
type DiskList []Disk
|
||||
|
||||
type DisksTypesListCoomon []string
|
||||
|
||||
type DisksTypesListDetailed struct {
|
||||
Pools []Pool `json:"pools"`
|
||||
SepId uint64 `json:"sepId"`
|
||||
}
|
||||
|
||||
type Pool struct {
|
||||
Name string `json:"name"`
|
||||
Types []string `json:"types"`
|
||||
}
|
||||
|
||||
type DiskRecord struct {
|
||||
Acl map[string]interface{} `json:"acl"`
|
||||
AccountID int `json:"accountId"`
|
||||
AccountName string `json:"accountName"`
|
||||
CreatedTime uint64 `json:"createdTime"`
|
||||
DeletedTime uint64 `json:"deletedTime"`
|
||||
DeviceName string `json:"devicename"`
|
||||
Description string `json:"desc"`
|
||||
DestructionTime uint64 `json:"destructionTime"`
|
||||
GID int `json:"gid"`
|
||||
ID uint `json:"id"`
|
||||
ImageID int `json:"imageId"`
|
||||
Images []int `json:"images"`
|
||||
IOTune IOTune `json:"iotune"`
|
||||
Name string `json:"name"`
|
||||
Order int `json:"order"`
|
||||
Params string `json:"params"`
|
||||
ParentId int `json:"parentId"`
|
||||
PciSlot int `json:"pciSlot"`
|
||||
Pool string `json:"pool"`
|
||||
PurgeTime uint64 `json:"purgeTime"`
|
||||
ResID string `json:"resId"`
|
||||
ResName string `json:"resName"`
|
||||
Role string `json:"role"`
|
||||
SepType string `json:"sepType"`
|
||||
SepID int `json:"sepId"` // NOTE: absent from compute/get output
|
||||
SizeMax int `json:"sizeMax"`
|
||||
SizeUsed int `json:"sizeUsed"` // sum over all snapshots of this disk to report total consumed space
|
||||
Snapshots []Snapshot `json:"snapshots"`
|
||||
Status string `json:"status"`
|
||||
TechStatus string `json:"techStatus"`
|
||||
Type string `json:"type"`
|
||||
VMID int `json:"vmid"`
|
||||
}
|
||||
58
pkg/cloudapi/disks/rename.go
Normal file
58
pkg/cloudapi/disks/rename.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type RenameRequest struct {
|
||||
DiskId uint64 `url:"diskId"`
|
||||
Name string `url:"name"`
|
||||
}
|
||||
|
||||
func (drq RenameRequest) Validate() error {
|
||||
if drq.DiskId == 0 {
|
||||
return errors.New("validation-error: field DiskId can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
if drq.Name == "" {
|
||||
return errors.New("validation-error: field Name can not be empty")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) Rename(ctx context.Context, req RenameRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/disks/rename"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
89
pkg/cloudapi/disks/resize.go
Normal file
89
pkg/cloudapi/disks/resize.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type ResizeRequest struct {
|
||||
DiskId uint64 `url:"diskId"`
|
||||
Size uint64 `url:"size"`
|
||||
}
|
||||
|
||||
func (drq ResizeRequest) Validate() error {
|
||||
if drq.DiskId == 0 {
|
||||
return errors.New("validation-error: field DiskId can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
if drq.Size == 0 {
|
||||
return errors.New("validation-error: field Size can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) Resize(ctx context.Context, req ResizeRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/disks/resize"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
|
||||
func (d Disks) Resize2(ctx context.Context, req ResizeRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/disks/resize2"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
58
pkg/cloudapi/disks/restore.go
Normal file
58
pkg/cloudapi/disks/restore.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type RestoreRequest struct {
|
||||
DiskId uint64 `url:"diskId"`
|
||||
Reason string `url:"reason"`
|
||||
}
|
||||
|
||||
func (drq RestoreRequest) Validate() error {
|
||||
if drq.DiskId == 0 {
|
||||
return errors.New("validation-error: field DiskId can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
if drq.Reason == "" {
|
||||
return errors.New("validation-error: field Reason can not be empty")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) Restore(ctx context.Context, req RestoreRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/disks/restore"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
43
pkg/cloudapi/disks/search.go
Normal file
43
pkg/cloudapi/disks/search.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type SearchRequest struct {
|
||||
AccountId uint64 `url:"accountId,omitempty"`
|
||||
Name string `url:"name,omitempty"`
|
||||
ShowAll bool `url:"show_all,omitempty"`
|
||||
}
|
||||
|
||||
func (d Disks) Search(ctx context.Context, req SearchRequest, options ...opts.DecortOpts) (DiskList, error) {
|
||||
url := "/disks/search"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
diskList := DiskList{}
|
||||
|
||||
err = json.Unmarshal(res, &diskList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return diskList, nil
|
||||
|
||||
}
|
||||
58
pkg/cloudapi/disks/snapshot_delete.go
Normal file
58
pkg/cloudapi/disks/snapshot_delete.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type SnapshotDeleteRequest struct {
|
||||
DiskId uint64 `url:"diskId"`
|
||||
Label string `url:"label"`
|
||||
}
|
||||
|
||||
func (drq SnapshotDeleteRequest) Validate() error {
|
||||
if drq.DiskId == 0 {
|
||||
return errors.New("validation-error: field DiskId can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
if drq.Label == "" {
|
||||
return errors.New("validation-error: field Label can not be empty")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) SnapshotDelete(ctx context.Context, req SnapshotDeleteRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/disks/snapshotDelete"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
59
pkg/cloudapi/disks/snapshot_rollback.go
Normal file
59
pkg/cloudapi/disks/snapshot_rollback.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package disks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type SnapshotRollbackRequest struct {
|
||||
DiskId uint64 `url:"diskId"`
|
||||
Label string `url:"label"`
|
||||
TimeStamp uint64 `url:"timestamp"`
|
||||
}
|
||||
|
||||
func (drq SnapshotRollbackRequest) Validate() error {
|
||||
if drq.DiskId == 0 {
|
||||
return errors.New("validation-error: field DiskId can not be empty or equal to 0")
|
||||
}
|
||||
|
||||
if drq.Label == "" && drq.TimeStamp == 0 {
|
||||
return errors.New("validation-error: field Label or field TimeStamp can not be empty")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Disks) SnapshotRollback(ctx context.Context, req SnapshotRollbackRequest, options ...opts.DecortOpts) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/disks/snapshotRollback"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
res, err := d.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user