This commit is contained in:
stSolo
2022-12-22 17:56:47 +03:00
parent 8712561853
commit d4b1ab7133
672 changed files with 28509 additions and 4419 deletions

View File

@@ -9,39 +9,90 @@ import (
"github.com/rudecs/decort-sdk/internal/validators"
)
// Request struct for create image
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"`
// Name of the rescue disk
// Required: true
Name string `url:"name"`
// URL where to download media from
// Required: true
URL string `url:"url"`
// Grid (platform) ID where this template should be create in
// Required: true
GID uint64 `url:"gid"`
// Boot type of image bios or UEFI
// Required: true
BootType string `url:"boottype"`
// Image type
// Should be:
// - linux
// - windows
// - or other
// Required: true
ImageType string `url:"imagetype"`
// Does this machine supports hot resize
// Required: false
HotResize bool `url:"hotresize,omitempty"`
// Optional username for the image
// Required: false
Username string `url:"username,omitempty"`
// Optional password for the image
// Required: false
Password string `url:"password,omitempty"`
// Account ID to make the image exclusive
// Required: false
AccountID uint64 `url:"accountId,omitempty"`
// Username for upload binary media
// Required: false
UsernameDL string `url:"usernameDL,omitempty"`
// Password for upload binary media
// Required: false
PasswordDL string `url:"passwordDL,omitempty"`
// Storage endpoint provider ID
// Required: false
SEPID uint64 `url:"sepId,omitempty"`
// Pool for image create
// Required: false
Pool string `url:"poolName,omitempty"`
// Binary architecture of this image
// Should be:
// - X86_64
// - PPC64_LE
// Required: false
Architecture string `url:"architecture,omitempty"`
// List of types of compute suitable for image
// Example: [ "KVM_X86" ]
// Required: true
Drivers []string `url:"drivers"`
}
func (irq CreateRequest) Validate() error {
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")
@@ -49,16 +100,13 @@ func (irq CreateRequest) Validate() error {
if irq.ImageType == "" {
return errors.New("validation-error: field ImageType can not be empty")
}
validate = validators.StringInSlice(irq.ImageType, []string{"windows", "linux", "other"})
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 {
@@ -69,8 +117,9 @@ func (irq CreateRequest) Validate() error {
return nil
}
// Create creates image from a media identified by URL
func (i Image) Create(ctx context.Context, req CreateRequest) (uint64, error) {
err := req.Validate()
err := req.validate()
if err != nil {
return 0, err
}
@@ -88,5 +137,4 @@ func (i Image) Create(ctx context.Context, req CreateRequest) (uint64, error) {
}
return result, nil
}

View File

@@ -7,16 +7,21 @@ import (
"strconv"
)
// Request struct for create virtual image
type CreateVirtualRequest struct {
Name string `url:"name"`
// Name of the virtual image to create
// Required: true
Name string `url:"name"`
// ID of real image to link this virtual image to upon creation
// Required: true
TargetID uint64 `url:"targetId"`
}
func (irq CreateVirtualRequest) Validate() error {
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")
}
@@ -24,8 +29,9 @@ func (irq CreateVirtualRequest) Validate() error {
return nil
}
// CreateVirtual creates virtual image
func (i Image) CreateVirtual(ctx context.Context, req CreateVirtualRequest) (uint64, error) {
err := req.Validate()
err := req.validate()
if err != nil {
return 0, err
}
@@ -43,5 +49,4 @@ func (i Image) CreateVirtual(ctx context.Context, req CreateVirtualRequest) (uin
}
return result, nil
}

View File

@@ -7,12 +7,18 @@ import (
"strconv"
)
// Request struct for delete image
type DeleteRequest struct {
ImageID uint64 `url:"imageId"`
Permanently bool `url:"permanently"`
// ID of the image to delete
// Required: true
ImageID uint64 `url:"imageId"`
// Whether to completely delete the image
// Required: false
Permanently bool `url:"permanently,omitempty"`
}
func (irq DeleteRequest) Validate() error {
func (irq DeleteRequest) validate() error {
if irq.ImageID == 0 {
return errors.New("validation-error: field ImageID can not be empty or equal to 0")
}
@@ -20,8 +26,9 @@ func (irq DeleteRequest) Validate() error {
return nil
}
// Delete deletes image by ID
func (i Image) Delete(ctx context.Context, req DeleteRequest) (bool, error) {
err := req.Validate()
err := req.validate()
if err != nil {
return false, err
}
@@ -39,5 +46,4 @@ func (i Image) Delete(ctx context.Context, req DeleteRequest) (bool, error) {
}
return result, nil
}

View File

@@ -7,12 +7,18 @@ import (
"net/http"
)
// Request struct for get detailed information about image
type GetRequest struct {
// ID of image to get
// Required: true
ImageID uint64 `url:"imageId"`
ShowAll bool `url:"show_all,omitempty"`
// If set to False returns only images in status CREATED
// Required: false
ShowAll bool `url:"show_all,omitempty"`
}
func (irq GetRequest) Validate() error {
func (irq GetRequest) validate() error {
if irq.ImageID == 0 {
return errors.New("validation-error: field ImageID can not be empty or equal to 0")
}
@@ -20,8 +26,10 @@ func (irq GetRequest) Validate() error {
return nil
}
func (i Image) Get(ctx context.Context, req GetRequest) (*ImageExtend, error) {
err := req.Validate()
// Get gets image by ID.
// Returns image if user has rights on it
func (i Image) Get(ctx context.Context, req GetRequest) (*RecordImage, error) {
err := req.validate()
if err != nil {
return nil, err
}
@@ -33,12 +41,12 @@ func (i Image) Get(ctx context.Context, req GetRequest) (*ImageExtend, error) {
return nil, err
}
imageInfo := &ImageExtend{}
info := RecordImage{}
err = json.Unmarshal(res, imageInfo)
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return imageInfo, nil
return &info, nil
}

View File

@@ -1,13 +1,16 @@
// Lists all the images. A image is a template which can be used to deploy machines
package image
import (
"github.com/rudecs/decort-sdk/interfaces"
)
// Structure for creating request to image
type Image struct {
client interfaces.Caller
}
// Builder for image endpoints
func New(client interfaces.Caller) *Image {
return &Image{
client,

View File

@@ -7,12 +7,18 @@ import (
"strconv"
)
// Request struct for link virtual image to another image
type LinkRequest struct {
ImageID uint64 `url:"imageId"`
// ID of the virtual image
// Required: true
ImageID uint64 `url:"imageId"`
// ID of real image to link this virtual image to
// Required: true
TargetID uint64 `url:"targetId"`
}
func (irq LinkRequest) Validate() error {
func (irq LinkRequest) validate() error {
if irq.ImageID == 0 {
return errors.New("validation-error: field ImageID can not be empty or equal to 0")
}
@@ -23,8 +29,9 @@ func (irq LinkRequest) Validate() error {
return nil
}
// Link links virtual image to another image in the platform
func (i Image) Link(ctx context.Context, req LinkRequest) (bool, error) {
err := req.Validate()
err := req.validate()
if err != nil {
return false, err
}
@@ -42,5 +49,4 @@ func (i Image) Link(ctx context.Context, req LinkRequest) (bool, error) {
}
return result, nil
}

View File

@@ -6,14 +6,23 @@ import (
"net/http"
)
// Request struct for get list available images
type ListRequest struct {
AccountID uint64 `json:"accountId"`
Page uint64 `json:"page"`
Size uint64 `json:"size"`
// Optional account ID to include account images
// Required: false
AccountID uint64 `json:"accountId,omitempty"`
// Page number
// Required: false
Page uint64 `json:"page,omitempty"`
// Page size
// Required: false
Size uint64 `json:"size,omitempty"`
}
func (i Image) List(ctx context.Context, req ListRequest) (ImageList, error) {
// List gets list available images, optionally filtering by account ID
func (i Image) List(ctx context.Context, req ListRequest) (ListImages, error) {
url := "/cloudapi/image/list"
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
@@ -21,12 +30,12 @@ func (i Image) List(ctx context.Context, req ListRequest) (ImageList, error) {
return nil, err
}
imageList := ImageList{}
list := ListImages{}
err = json.Unmarshal(res, &imageList)
err = json.Unmarshal(res, &list)
if err != nil {
return nil, err
}
return imageList, nil
return list, nil
}

View File

@@ -1,68 +1,181 @@
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"`
// Main information about image
type ItemImage struct {
// Account ID
AccountID uint64 `json:"accountId"`
// Architecture
Architecture string `json:"architecture"`
// Boot type
BootType string `json:"bootType"`
// Bootable
Bootable bool `json:"bootable"`
// CDROM
CDROM bool `json:"cdrom"`
// Description
Description string `json:"desc"`
// List drivers
Drivers []string `json:"drivers"`
// HotResize
HotResize bool `json:"hotResize"`
// ID
ID uint64 `json:"id"`
// Link to
LinkTo uint64 `json:"linkTo"`
// Name
Name string `json:"name"`
// Pool
Pool string `json:"pool"`
// SepID
SepID uint64 `json:"sepId"`
// Size
Size uint64 `json:"size"`
// Status
Status string `json:"status"`
// Type
Type string `json:"type"`
// Username
Username string `json:"username"`
// Virtual
Virtual bool `json:"virtual"`
}
type ImageList []ImageRecord
// List of information about images
type ListImages []ItemImage
// History
type History struct {
GUID string `json:"guid"`
ID uint64 `json:"id"`
// GUID
GUID string `json:"guid"`
// ID
ID uint64 `json:"id"`
// Timestamp
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 uint64 `json:"purgeAttempts"`
ResID string `json:"resId"`
RescueCD bool `json:"rescuecd"`
SepID uint64 `json:"sepId"`
SharedWith []uint64 `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"`
// Detailed information about image
type RecordImage struct {
// UNCPathj
UNCPath string `json:"UNCPath"`
// CKey
CKey string `json:"_ckey"`
// Account ID
AccountID uint64 `json:"accountId"`
// Access Control List
ACL interface{} `json:"acl"`
// Architecture
Architecture string `json:"architecture"`
// Boot type
BootType string `json:"bootType"`
// Bootable
Bootable bool `json:"bootable"`
// ComputeCI ID
ComputeCIID uint64 `json:"computeciId"`
// Deleted time
DeletedTime uint64 `json:"deletedTime"`
// Description
Description string `json:"desc"`
// List of drivers
Drivers []string `json:"drivers"`
// Enabled
Enabled bool `json:"enabled"`
// Grid ID
GID uint64 `json:"gid"`
// GUID
GUID uint64 `json:"guid"`
// History
History []History `json:"history"`
// HotResize
HotResize bool `json:"hotResize"`
// ID
ID uint64 `json:"id"`
// Last modified
LastModified uint64 `json:"lastModified"`
// Link to
LinkTo uint64 `json:"linkTo"`
// Milestones
Milestones uint64 `json:"milestones"`
// Name
Name string `json:"name"`
// Password
Password string `json:"password"`
// Pool
Pool string `json:"pool"`
// ProviderName
ProviderName string `json:"provider_name"`
// Purge attempts
PurgeAttempts uint64 `json:"purgeAttempts"`
// Resource ID
ResID string `json:"resId"`
// RescueCD
RescueCD bool `json:"rescuecd"`
// SepID
SepID uint64 `json:"sepId"`
// SharedWith list
SharedWith []uint64 `json:"sharedWith"`
// Size
Size uint64 `json:"size"`
// Status
Status string `json:"status"`
// Tech status
TechStatus string `json:"techStatus"`
// Type
Type string `json:"type"`
// Username
Username string `json:"username"`
// Version
Version string `json:"version"`
}

View File

@@ -7,16 +7,21 @@ import (
"strconv"
)
// Request struct for rename image
type RenameRequest struct {
// ID of the virtual image to rename
// Required: true
ImageID uint64 `url:"imageId"`
Name string `url:"name"`
// New name
// Required: true
Name string `url:"name"`
}
func (irq RenameRequest) Validate() error {
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")
}
@@ -24,8 +29,9 @@ func (irq RenameRequest) Validate() error {
return nil
}
// Rename renames image
func (i Image) Rename(ctx context.Context, req RenameRequest) (bool, error) {
err := req.Validate()
err := req.validate()
if err != nil {
return false, err
}
@@ -43,5 +49,4 @@ func (i Image) Rename(ctx context.Context, req RenameRequest) (bool, error) {
}
return result, nil
}