v1.0.0
This commit is contained in:
@@ -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
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user