update go.mod and imports
This commit is contained in:
@@ -2,30 +2,29 @@ package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// Request struct for create image
|
||||
type CreateRequest struct {
|
||||
// Name of the rescue disk
|
||||
// Required: true
|
||||
Name string `url:"name" json:"name"`
|
||||
Name string `url:"name" json:"name" validate:"required"`
|
||||
|
||||
// URL where to download media from
|
||||
// Required: true
|
||||
URL string `url:"url" json:"url"`
|
||||
URL string `url:"url" json:"url" validate:"required,url"`
|
||||
|
||||
// Grid (platform) ID where this template should be create in
|
||||
// Required: true
|
||||
GID uint64 `url:"gid" json:"gid"`
|
||||
GID uint64 `url:"gid" json:"gid" validate:"required"`
|
||||
|
||||
// Boot type of image bios or UEFI
|
||||
// Required: true
|
||||
BootType string `url:"boottype" json:"boottype"`
|
||||
BootType string `url:"boottype" json:"boottype" validate:"required,bootType"`
|
||||
|
||||
// Image type
|
||||
// Should be:
|
||||
@@ -33,7 +32,7 @@ type CreateRequest struct {
|
||||
// - windows
|
||||
// - or other
|
||||
// Required: true
|
||||
ImageType string `url:"imagetype" json:"imagetype"`
|
||||
ImageType string `url:"imagetype" json:"imagetype" validate:"required,imageType"`
|
||||
|
||||
// Does this machine supports hot resize
|
||||
// Required: false
|
||||
@@ -72,56 +71,21 @@ type CreateRequest struct {
|
||||
// - X86_64
|
||||
// - PPC64_LE
|
||||
// Required: false
|
||||
Architecture string `url:"architecture,omitempty" json:"architecture,omitempty"`
|
||||
Architecture string `url:"architecture,omitempty" json:"architecture,omitempty" validate:"omitempty,imageArchitecture"`
|
||||
|
||||
// List of types of compute suitable for image
|
||||
// Example: [ "KVM_X86" ]
|
||||
// Required: true
|
||||
Drivers []string `url:"drivers" json:"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{"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 {
|
||||
return errors.New("validation-error: field Drivers can be KVM_X86 only")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
Drivers []string `url:"drivers" json:"drivers" validate:"min=1,max=2,imageDrivers"`
|
||||
}
|
||||
|
||||
// Create creates image from a media identified by URL
|
||||
func (i Image) Create(ctx context.Context, req CreateRequest) (uint64, error) {
|
||||
err := req.validate()
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
for _, validationError := range validators.GetErrors(err) {
|
||||
return 0, validators.ValidationError(validationError)
|
||||
}
|
||||
}
|
||||
|
||||
url := "/cloudapi/image/create"
|
||||
|
||||
@@ -2,38 +2,30 @@ package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// Request struct for create virtual image
|
||||
type CreateVirtualRequest struct {
|
||||
// Name of the virtual image to create
|
||||
// Required: true
|
||||
Name string `url:"name" json:"name"`
|
||||
Name string `url:"name" json:"name" validate:"required"`
|
||||
|
||||
// ID of real image to link this virtual image to upon creation
|
||||
// Required: true
|
||||
TargetID uint64 `url:"targetId" json:"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
|
||||
TargetID uint64 `url:"targetId" json:"targetId" validate:"required"`
|
||||
}
|
||||
|
||||
// CreateVirtual creates virtual image
|
||||
func (i Image) CreateVirtual(ctx context.Context, req CreateVirtualRequest) (uint64, error) {
|
||||
err := req.validate()
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
for _, validationError := range validators.GetErrors(err) {
|
||||
return 0, validators.ValidationError(validationError)
|
||||
}
|
||||
}
|
||||
|
||||
url := "/cloudapi/image/createVirtual"
|
||||
|
||||
@@ -2,35 +2,30 @@ package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// Request struct for delete image
|
||||
type DeleteRequest struct {
|
||||
// ID of the image to delete
|
||||
// Required: true
|
||||
ImageID uint64 `url:"imageId" json:"imageId"`
|
||||
ImageID uint64 `url:"imageId" json:"imageId" validate:"required"`
|
||||
|
||||
// Whether to completely delete the image
|
||||
// Required: false
|
||||
Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Delete deletes image by ID
|
||||
func (i Image) Delete(ctx context.Context, req DeleteRequest) (bool, error) {
|
||||
err := req.validate()
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
for _, validationError := range validators.GetErrors(err) {
|
||||
return false, validators.ValidationError(validationError)
|
||||
}
|
||||
}
|
||||
|
||||
url := "/cloudapi/image/delete"
|
||||
|
||||
124
pkg/cloudapi/image/filter_test.go
Normal file
124
pkg/cloudapi/image/filter_test.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package image
|
||||
|
||||
import "testing"
|
||||
|
||||
var images = ListImages{
|
||||
ItemImage{
|
||||
AccountID: 0,
|
||||
Architecture: "X86_64",
|
||||
BootType: "bios",
|
||||
Bootable: true,
|
||||
Description: "",
|
||||
Drivers: []string{
|
||||
"KVM_X86",
|
||||
},
|
||||
HotResize: true,
|
||||
ID: 9882,
|
||||
LinkTo: 0,
|
||||
Name: "u16",
|
||||
Pool: "vmstor",
|
||||
Size: 5,
|
||||
Status: "CREATED",
|
||||
Type: "linux",
|
||||
Username: "",
|
||||
Virtual: false,
|
||||
},
|
||||
ItemImage{
|
||||
AccountID: 0,
|
||||
Architecture: "X86_64",
|
||||
BootType: "bois",
|
||||
Bootable: true,
|
||||
Description: "",
|
||||
Drivers: []string{
|
||||
"KVM_X86",
|
||||
},
|
||||
HotResize: false,
|
||||
ID: 9884,
|
||||
LinkTo: 0,
|
||||
Name: "alpine-virt-3.17",
|
||||
Pool: "vmstor",
|
||||
Size: 1,
|
||||
Status: "CREATED",
|
||||
Type: "linux",
|
||||
Username: "",
|
||||
Virtual: true,
|
||||
},
|
||||
ItemImage{
|
||||
AccountID: 1,
|
||||
Architecture: "X86_64",
|
||||
BootType: "bios",
|
||||
Bootable: true,
|
||||
Description: "",
|
||||
Drivers: []string{
|
||||
"KVM_X86",
|
||||
},
|
||||
HotResize: true,
|
||||
ID: 9885,
|
||||
LinkTo: 0,
|
||||
Name: "test",
|
||||
Pool: "vmstor",
|
||||
Size: 4,
|
||||
Status: "DESTROYED",
|
||||
Type: "linux",
|
||||
Username: "",
|
||||
Virtual: false,
|
||||
},
|
||||
}
|
||||
|
||||
func TestFilterByID(t *testing.T) {
|
||||
actual := images.FilterByID(9885).FindOne()
|
||||
|
||||
if actual.ID != 9885 {
|
||||
t.Fatal("expected ID 9885, found: ", actual.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByName(t *testing.T) {
|
||||
actual := images.FilterByName("u16").FindOne()
|
||||
|
||||
if actual.Name != "u16" {
|
||||
t.Fatal("expected Name 'u16', found: ", actual.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByStatus(t *testing.T) {
|
||||
actual := images.FilterByStatus("CREATED")
|
||||
|
||||
if len(actual) != 2 {
|
||||
t.Fatal("expected 2 found, actual: ", len(actual))
|
||||
}
|
||||
|
||||
for _, item := range actual {
|
||||
if item.Status != "CREATED" {
|
||||
t.Fatal("expected Status 'CREATED', found: ", item.Status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByBootType(t *testing.T) {
|
||||
actual := images.FilterByBootType("bios")
|
||||
|
||||
if len(actual) != 2 {
|
||||
t.Fatal("expected 2 found, actual: ", len(actual))
|
||||
}
|
||||
|
||||
for _, item := range actual {
|
||||
if item.BootType != "bios" {
|
||||
t.Fatal("expected BootType 'bios', found: ", item.BootType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFunc(t *testing.T) {
|
||||
actual := images.FilterFunc(func(ii ItemImage) bool {
|
||||
return ii.Virtual == true
|
||||
})
|
||||
|
||||
if len(actual) != 1 {
|
||||
t.Fatal("expected 1 found, actual: ", len(actual))
|
||||
}
|
||||
|
||||
if actual[0].Virtual != true {
|
||||
t.Fatal("expected Virtual true, found false")
|
||||
}
|
||||
}
|
||||
@@ -3,35 +3,30 @@ package image
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// Request struct for get detailed information about image
|
||||
type GetRequest struct {
|
||||
// ID of image to get
|
||||
// Required: true
|
||||
ImageID uint64 `url:"imageId" json:"imageId"`
|
||||
ImageID uint64 `url:"imageId" json:"imageId" validate:"required"`
|
||||
|
||||
// If set to False returns only images in status CREATED
|
||||
// Required: false
|
||||
ShowAll bool `url:"show_all,omitempty" json:"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
|
||||
}
|
||||
|
||||
// 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()
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
for _, validationError := range validators.GetErrors(err) {
|
||||
return nil, validators.ValidationError(validationError)
|
||||
}
|
||||
}
|
||||
|
||||
url := "/cloudapi/image/get"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/interfaces"
|
||||
)
|
||||
|
||||
// Structure for creating request to image
|
||||
|
||||
@@ -2,38 +2,30 @@ package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// Request struct for link virtual image to another image
|
||||
type LinkRequest struct {
|
||||
// ID of the virtual image
|
||||
// Required: true
|
||||
ImageID uint64 `url:"imageId" json:"imageId"`
|
||||
ImageID uint64 `url:"imageId" json:"imageId" validate:"required"`
|
||||
|
||||
// ID of real image to link this virtual image to
|
||||
// Required: true
|
||||
TargetID uint64 `url:"targetId" json:"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
|
||||
TargetID uint64 `url:"targetId" json:"targetId" validate:"required"`
|
||||
}
|
||||
|
||||
// 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 := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
for _, validationError := range validators.GetErrors(err) {
|
||||
return false, validators.ValidationError(validationError)
|
||||
}
|
||||
}
|
||||
|
||||
url := "/cloudapi/image/link"
|
||||
|
||||
@@ -2,38 +2,30 @@ package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// Request struct for rename image
|
||||
type RenameRequest struct {
|
||||
// ID of the virtual image to rename
|
||||
// Required: true
|
||||
ImageID uint64 `url:"imageId" json:"imageId"`
|
||||
ImageID uint64 `url:"imageId" json:"imageId" validate:"required"`
|
||||
|
||||
// New name
|
||||
// Required: true
|
||||
Name string `url:"name" json:"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
|
||||
Name string `url:"name" json:"name" validate:"required"`
|
||||
}
|
||||
|
||||
// Rename renames image
|
||||
func (i Image) Rename(ctx context.Context, req RenameRequest) (bool, error) {
|
||||
err := req.validate()
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
for _, validationError := range validators.GetErrors(err) {
|
||||
return false, validators.ValidationError(validationError)
|
||||
}
|
||||
}
|
||||
|
||||
url := "/cloudapi/image/rename"
|
||||
|
||||
@@ -3,7 +3,7 @@ package image
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/serialization"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/serialization"
|
||||
)
|
||||
|
||||
// Serialize returns JSON-serialized []byte. Used as a wrapper over json.Marshal and json.MarshalIndent functions.
|
||||
|
||||
Reference in New Issue
Block a user