Refactoring

This commit is contained in:
stSolo
2022-08-11 16:39:39 +03:00
parent 9563097dcb
commit 39a6f9a1ce
300 changed files with 64 additions and 58 deletions

View File

@@ -0,0 +1,102 @@
package image
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 {
Name string `url:"name"`
URL string `url:"url"`
GID uint64 `url:"gid"`
BootType string `url:"boottype"`
ImageType string `url:"imagetype"`
Hotresize bool `url:"hotresize,omitempty"`
Username string `url:"username,omitempty"`
Password string `url:"password,omitempty"`
AccountId uint64 `url:"accountId,omitempty"`
UsernameDL string `url:"usernameDL,omitempty"`
PasswordDL string `url:"passwordDL,omitempty"`
SepId uint64 `url:"sepId,omitempty"`
Pool string `url:"poolName,omitempty"`
Architecture string `url:"architecture,omitempty"`
Drivers []string `url:"drivers"`
}
func (irq CreateRequest) Validate() error {
if irq.Name == "" {
return errors.New("validation-error: field Name can not be empty")
}
if irq.URL == "" {
return errors.New("validation-error: field URL can not be empty")
}
if irq.GID == 0 {
return errors.New("validation-error: field GID can not be empty or equal to 0")
}
if irq.BootType == "" {
return errors.New("validation-error: field BootType can not be empty")
}
validate := validators.StringInSlice(irq.BootType, []string{"bios", "uefi"})
if !validate {
return errors.New("validation-error: field BootType can be bios or uefi")
}
if irq.ImageType == "" {
return errors.New("validation-error: field ImageType can not be empty")
}
validate = validators.StringInSlice(irq.ImageType, []string{"bios", "uefi"})
if !validate {
return errors.New("validation-error: field ImageType can be windows, linux or other")
}
if len(irq.Drivers) == 0 || len(irq.Drivers) > 1 {
return errors.New("validation-error: field Drivers can not be empty or have 2 or more elements")
}
for _, v := range irq.Drivers {
validate := validators.StringInSlice(v, []string{"KVM_X86"})
if !validate {
return errors.New("validation-error: field Drivers can be KVM_X86 only")
}
}
return nil
}
func (i Image) Create(ctx context.Context, req CreateRequest, options ...opts.DecortOpts) (uint64, error) {
err := req.Validate()
if err != nil {
return 0, err
}
url := "/image/create"
prefix := "/cloudapi"
option := opts.New(options)
if option != nil {
if option.IsAdmin {
prefix = "/" + option.AdminValue
}
}
url = prefix + url
res, err := i.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
}

View File

@@ -0,0 +1,58 @@
package image
import (
"context"
"errors"
"strconv"
"github.com/rudecs/decort-sdk/opts"
"github.com/rudecs/decort-sdk/typed"
)
type CreateVirtualRequest struct {
Name string `url:"name"`
TargetId uint64 `url:"targetId"`
}
func (irq CreateVirtualRequest) Validate() error {
if irq.Name == "" {
return errors.New("validation-error: field Name can not be empty")
}
if irq.TargetId == 0 {
return errors.New("validation-error: field TargetId can not be empty or equal to 0")
}
return nil
}
func (i Image) CreateVirtual(ctx context.Context, req CreateVirtualRequest, options ...opts.DecortOpts) (uint64, error) {
err := req.Validate()
if err != nil {
return 0, err
}
url := "/image/createVirtual"
prefix := "/cloudapi"
option := opts.New(options)
if option != nil {
if option.IsAdmin {
prefix = "/" + option.AdminValue
}
}
url = prefix + url
res, err := i.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
}

View File

@@ -0,0 +1,54 @@
package image
import (
"context"
"errors"
"strconv"
"github.com/rudecs/decort-sdk/opts"
"github.com/rudecs/decort-sdk/typed"
)
type DeleteRequest struct {
ImageId uint64 `url:"imageId"`
Permanently bool `url:"permanently"`
}
func (irq DeleteRequest) Validate() error {
if irq.ImageId == 0 {
return errors.New("validation-error: field ImageId can not be empty or equal to 0")
}
return nil
}
func (i Image) Delete(ctx context.Context, req DeleteRequest, options ...opts.DecortOpts) (bool, error) {
err := req.Validate()
if err != nil {
return false, err
}
url := "/image/delete"
prefix := "/cloudapi"
option := opts.New(options)
if option != nil {
if option.IsAdmin {
prefix = "/" + option.AdminValue
}
}
url = prefix + url
res, err := i.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
}

54
pkg/cloudapi/image/get.go Normal file
View File

@@ -0,0 +1,54 @@
package image
import (
"context"
"encoding/json"
"errors"
"github.com/rudecs/decort-sdk/opts"
"github.com/rudecs/decort-sdk/typed"
)
type GetRequest struct {
ImageId uint64 `url:"imageId"`
ShowAll bool `url:"show_all,omitempty"`
}
func (irq GetRequest) Validate() error {
if irq.ImageId == 0 {
return errors.New("validation-error: field ImageId can not be empty or equal to 0")
}
return nil
}
func (i Image) Get(ctx context.Context, req GetRequest, options ...opts.DecortOpts) (*ImageExtend, error) {
err := req.Validate()
if err != nil {
return nil, err
}
url := "/image/get"
prefix := "/cloudapi"
option := opts.New(options)
if option != nil {
if option.IsAdmin {
prefix = "/" + option.AdminValue
}
}
url = prefix + url
res, err := i.client.DecortApiCall(ctx, typed.POST, url, req)
if err != nil {
return nil, err
}
imageInfo := &ImageExtend{}
err = json.Unmarshal(res, imageInfo)
if err != nil {
return nil, err
}
return imageInfo, nil
}

View File

@@ -0,0 +1,15 @@
package image
import (
"github.com/rudecs/decort-sdk/interfaces"
)
type Image struct {
client interfaces.Caller
}
func New(client interfaces.Caller) *Image {
return &Image{
client,
}
}

View File

@@ -0,0 +1,57 @@
package image
import (
"context"
"errors"
"strconv"
"github.com/rudecs/decort-sdk/opts"
"github.com/rudecs/decort-sdk/typed"
)
type LinkRequest struct {
ImageId uint64 `url:"imageId"`
TargetId uint64 `url:"targetId"`
}
func (irq LinkRequest) Validate() error {
if irq.ImageId == 0 {
return errors.New("validation-error: field ImageId can not be empty or equal to 0")
}
if irq.TargetId == 0 {
return errors.New("validation-error: field TargetId can not be empty or equal to 0")
}
return nil
}
func (i Image) Link(ctx context.Context, req LinkRequest, options ...opts.DecortOpts) (bool, error) {
err := req.Validate()
if err != nil {
return false, err
}
url := "/image/link"
prefix := "/cloudapi"
option := opts.New(options)
if option != nil {
if option.IsAdmin {
prefix = "/" + option.AdminValue
}
}
url = prefix + url
res, err := i.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
}

View File

@@ -0,0 +1,42 @@
package image
import (
"context"
"encoding/json"
"github.com/rudecs/decort-sdk/opts"
"github.com/rudecs/decort-sdk/typed"
)
type ListRequest struct {
AccountId uint64 `json:"accountId"`
Page uint64 `json:"page"`
Size uint64 `json:"size"`
}
func (i Image) List(ctx context.Context, req ListRequest, options ...opts.DecortOpts) (ImageList, error) {
url := "/image/list"
prefix := "/cloudapi"
option := opts.New(options)
if option != nil {
if option.IsAdmin {
prefix = "/" + option.AdminValue
}
}
url = prefix + url
res, err := i.client.DecortApiCall(ctx, typed.POST, url, req)
if err != nil {
return nil, err
}
imageList := ImageList{}
err = json.Unmarshal(res, &imageList)
if err != nil {
return nil, err
}
return imageList, nil
}

View File

@@ -0,0 +1,68 @@
package image
type ImageRecord struct {
AccountId uint64 `json:"accountId"`
Architecture string `json:"architecture"`
BootType string `json:"bootType"`
Bootable bool `json:"bootable"`
CDROM bool `json:"cdrom"`
Description string `json:"desc"`
Drivers []string `json:"drivers"`
HotResize bool `json:"hotResize"`
ID uint64 `json:"id"`
LinkTo uint64 `json:"linkTo"`
Name string `json:"name"`
Pool string `json:"pool"`
SepId uint64 `json:"sepId"`
Size uint64 `json:"size"`
Status string `json:"status"`
Type string `json:"type"`
Username string `json:"username"`
Virtual bool `json:"virtual"`
}
type ImageList []ImageRecord
type History struct {
GUID string `json:"guid"`
ID uint64 `json:"id"`
Timestamp uint64 `json:"timestamp"`
}
type ImageExtend struct {
UNCPath string `json:"UNCPath"`
CKey string `json:"_ckey"`
AccountId uint64 `json:"accountId"`
Acl interface{} `json:"acl"`
Architecture string `json:"architecture"`
BootType string `json:"bootType"`
Bootable bool `json:"bootable"`
ComputeCiId uint64 `json:"computeciId"`
DeletedTime uint64 `json:"deletedTime"`
Description string `json:"desc"`
Drivers []string `json:"drivers"`
Enabled bool `json:"enabled"`
GID uint64 `json:"gid"`
GUID uint64 `json:"guid"`
History []History `json:"history"`
HotResize bool `json:"hotResize"`
ID uint64 `json:"id"`
LastModified uint64 `json:"lastModified"`
LinkTo uint64 `json:"linkTo"`
Milestones uint64 `json:"milestones"`
Name string `json:"name"`
Password string `json:"password"`
Pool string `json:"pool"`
ProviderName string `json:"provider_name"`
PurgeAttempts int `json:"purgeAttempts"`
ResId string `json:"resId"`
RescueCD bool `json:"rescuecd"`
SepId uint64 `json:"sepId"`
SharedWith []int `json:"sharedWith"`
Size uint64 `json:"size"`
Status string `json:"status"`
TechStatus string `json:"techStatus"`
Type string `json:"type"`
Username string `json:"username"`
Version string `json:"version"`
}

View File

@@ -0,0 +1,58 @@
package image
import (
"context"
"errors"
"strconv"
"github.com/rudecs/decort-sdk/opts"
"github.com/rudecs/decort-sdk/typed"
)
type RenameRequest struct {
ImageId uint64 `url:"imageId"`
Name string `url:"name"`
}
func (irq RenameRequest) Validate() error {
if irq.ImageId == 0 {
return errors.New("validation-error: field ImageId can not be empty or equal to 0")
}
if irq.Name == "" {
return errors.New("validation-error: field Name can not be empty")
}
return nil
}
func (i Image) Rename(ctx context.Context, req RenameRequest, options ...opts.DecortOpts) (bool, error) {
err := req.Validate()
if err != nil {
return false, err
}
url := "/image/rename"
prefix := "/cloudapi"
option := opts.New(options)
if option != nil {
if option.IsAdmin {
prefix = "/" + option.AdminValue
}
}
url = prefix + url
res, err := i.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
}