Merge 'dev' into 'main'
This commit is contained in:
45
pkg/cloudbroker/image/computeci_set.go
Normal file
45
pkg/cloudbroker/image/computeci_set.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ComputeCISetRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
ComputeCIID uint64 `url:"computeciId"`
|
||||
}
|
||||
|
||||
func (irq ComputeCISetRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
if irq.ComputeCIID == 0 {
|
||||
return errors.New("validation-error: field ComputeCIID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) ComputeCISet(ctx context.Context, req ComputeCISetRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/computeciSet"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
41
pkg/cloudbroker/image/computeci_unset.go
Normal file
41
pkg/cloudbroker/image/computeci_unset.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ComputeCIUnsetRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
}
|
||||
|
||||
func (irq ComputeCIUnsetRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) ComputeCIUnset(ctx context.Context, req ComputeCIUnsetRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/сomputeciUnset"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
68
pkg/cloudbroker/image/create_cdrom_image.go
Normal file
68
pkg/cloudbroker/image/create_cdrom_image.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/rudecs/decort-sdk/internal/validators"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type CreateCDROMImageRequest struct {
|
||||
Name string `url:"name"`
|
||||
URL string `url:"url"`
|
||||
GID uint64 `url:"gid"`
|
||||
AccountID uint64 `url:"accountId,omitempty"`
|
||||
SepID uint64 `url:"sep_id,omitempty"`
|
||||
PoolName string `url:"pool_name,omitempty"`
|
||||
UsernameDL string `url:"usernameDL,omitempty"`
|
||||
PasswordDl string `url:"passwordDL,omitempty"`
|
||||
Architecture string `url:"architecture,omitempty"`
|
||||
Drivers []string `url:"drivers"`
|
||||
}
|
||||
|
||||
func (irq CreateCDROMImageRequest) Validate() error {
|
||||
if irq.Name == "" {
|
||||
return errors.New("validation-error: field Name must be set")
|
||||
}
|
||||
if irq.URL == "" {
|
||||
return errors.New("validation-error: field URL must be set")
|
||||
}
|
||||
if irq.GID == 0 {
|
||||
return errors.New("validation-error: field GID must be set")
|
||||
}
|
||||
|
||||
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) CreateCDROMImage(ctx context.Context, req CreateCDROMImageRequest) (uint64, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/createCDROMImage"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, 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
|
||||
}
|
||||
78
pkg/cloudbroker/image/create_image.go
Normal file
78
pkg/cloudbroker/image/create_image.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/rudecs/decort-sdk/internal/validators"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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"`
|
||||
PoolName string `url:"poolName,omitempty"`
|
||||
Architecture string `url:"architecture,omitempty"`
|
||||
Drivers []string `url:"drivers,omitempty"`
|
||||
Bootable bool `url:"bootable,omitempty"`
|
||||
}
|
||||
|
||||
func (irq CreateRequest) Validate() error {
|
||||
if irq.Name == "" {
|
||||
return errors.New("validation-error: field Name must be set")
|
||||
}
|
||||
if irq.URL == "" {
|
||||
return errors.New("validation-error: field URL must be set")
|
||||
}
|
||||
if irq.GID == 0 {
|
||||
return errors.New("validation-error: field GID must be set")
|
||||
}
|
||||
if irq.BootType == "" {
|
||||
return errors.New("validation-error: field BootType must be set")
|
||||
}
|
||||
if irq.ImageType == "" {
|
||||
return errors.New("validation-error: field ImageType must be set")
|
||||
}
|
||||
|
||||
validate := validators.StringInSlice(irq.BootType, []string{"bios", "uefi"})
|
||||
if !validate {
|
||||
return errors.New("validation-error: field BootType can be bios or uefi")
|
||||
}
|
||||
validate = validators.StringInSlice(irq.ImageType, []string{"windows", "linux", "other"})
|
||||
if !validate {
|
||||
return errors.New("validation-error: field ImageType can be windows, linux or other")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) CreateImage(ctx context.Context, req CreateRequest) (uint64, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/createImage"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, 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
|
||||
}
|
||||
45
pkg/cloudbroker/image/create_virtual.go
Normal file
45
pkg/cloudbroker/image/create_virtual.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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 must be set")
|
||||
}
|
||||
if irq.TargetID == 0 {
|
||||
return errors.New("validation-error: field TargetID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) CreateVirtual(ctx context.Context, req CreateVirtualRequest) (uint64, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/createVirtual"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, 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
|
||||
}
|
||||
46
pkg/cloudbroker/image/delete.go
Normal file
46
pkg/cloudbroker/image/delete.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DeleteRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
Reason string `url:"reason"`
|
||||
Permanently bool `url:"permanently,omitempty"`
|
||||
}
|
||||
|
||||
func (irq DeleteRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
if irq.Reason == "" {
|
||||
return errors.New("validation-error: field Reason must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) Delete(ctx context.Context, req DeleteRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/delete"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
42
pkg/cloudbroker/image/delete_cdrom_image.go
Normal file
42
pkg/cloudbroker/image/delete_cdrom_image.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DeleteCDROMImageRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
Permanently bool `url:"permanently"`
|
||||
}
|
||||
|
||||
func (irq DeleteCDROMImageRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) DeleteCDROMImage(ctx context.Context, req DeleteCDROMImageRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/deleteCDROMImage"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
46
pkg/cloudbroker/image/delete_images.go
Normal file
46
pkg/cloudbroker/image/delete_images.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DeleteImagesRequest struct {
|
||||
ImageIDs []uint64 `url:"imageIds"`
|
||||
Reason string `url:"reason"`
|
||||
Permanently bool `url:"permanently"`
|
||||
}
|
||||
|
||||
func (irq DeleteImagesRequest) Validate() error {
|
||||
if len(irq.ImageIDs) == 0 {
|
||||
return errors.New("validation-error: field ImageIDs must be set")
|
||||
}
|
||||
if irq.Reason == "" {
|
||||
return errors.New("validation-error: field Reason must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) DeleteImages(ctx context.Context, req DeleteImagesRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/deleteImages"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
41
pkg/cloudbroker/image/disable.go
Normal file
41
pkg/cloudbroker/image/disable.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DisableRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
}
|
||||
|
||||
func (irq DisableRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) Disable(ctx context.Context, req DisableRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/disable"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
47
pkg/cloudbroker/image/edit.go
Normal file
47
pkg/cloudbroker/image/edit.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type EditRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
Name string `url:"name,omitempty"`
|
||||
Username string `url:"username,omitempty"`
|
||||
Password string `url:"password,omitempty"`
|
||||
AccountID uint64 `url:"accountId,omitempty"`
|
||||
HotResize bool `url:"hotresize,omitempty"`
|
||||
Bootable bool `url:"bootable,omitempty"`
|
||||
}
|
||||
|
||||
func (irq EditRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) Edit(ctx context.Context, req EditRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/edit"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
41
pkg/cloudbroker/image/enable.go
Normal file
41
pkg/cloudbroker/image/enable.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type EnableRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
}
|
||||
|
||||
func (irq EnableRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must br set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) Enable(ctx context.Context, req EnableRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/enable"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, 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/cloudbroker/image/get.go
Normal file
43
pkg/cloudbroker/image/get.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type GetRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
}
|
||||
|
||||
func (irq GetRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) Get(ctx context.Context, req GetRequest) (*ImageRecord, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/get"
|
||||
|
||||
result := ImageRecord{}
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(res, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
13
pkg/cloudbroker/image/image.go
Normal file
13
pkg/cloudbroker/image/image.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package image
|
||||
|
||||
import "github.com/rudecs/decort-sdk/interfaces"
|
||||
|
||||
type Image struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
func New(client interfaces.Caller) *Image {
|
||||
return &Image{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
45
pkg/cloudbroker/image/link.go
Normal file
45
pkg/cloudbroker/image/link.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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 must be set")
|
||||
}
|
||||
if irq.TargetID == 0 {
|
||||
return errors.New("validation-error: field TargetID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) Link(ctx context.Context, req LinkRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/link"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
32
pkg/cloudbroker/image/list.go
Normal file
32
pkg/cloudbroker/image/list.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ListRequest struct {
|
||||
SepID uint64 `url:"sepId,omitempty"`
|
||||
SharedWith uint64 `url:"sharedWith,omitempty"`
|
||||
Page uint64 `url:"page,omitempty"`
|
||||
Size uint64 `url:"size,omitempty"`
|
||||
}
|
||||
|
||||
func (i Image) List(ctx context.Context, req ListRequest) (ListImages, error) {
|
||||
url := "/cloudbroker/image/list"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := ListImages{}
|
||||
|
||||
err = json.Unmarshal(res, &list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
45
pkg/cloudbroker/image/list_stacks.go
Normal file
45
pkg/cloudbroker/image/list_stacks.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ListStacksRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
Page uint64 `url:"page,omitempty"`
|
||||
Size uint64 `url:"size,omitempty"`
|
||||
}
|
||||
|
||||
func (irq ListStacksRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) ListStacks(ctx context.Context, req ListStacksRequest) (ListStacks, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/listStacks"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := ListStacks{}
|
||||
|
||||
err = json.Unmarshal(res, &list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
82
pkg/cloudbroker/image/models.go
Normal file
82
pkg/cloudbroker/image/models.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package image
|
||||
|
||||
type ImageRecord struct {
|
||||
UNCPath string `json:"UNCPath"`
|
||||
CKey string `json:"_ckey"`
|
||||
Meta []interface{} `json:"_meta"`
|
||||
AccountID uint64 `json:"accountId"`
|
||||
ACL []ACL `json:"acl"`
|
||||
Architecture string `json:"architecture"`
|
||||
BootType string `json:"bootType"`
|
||||
Bootable bool `json:"bootable"`
|
||||
ComputeCIID uint64 `json:"computeciId"`
|
||||
DeletedTime uint64 `json:"deletedTime"`
|
||||
Desc 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"`
|
||||
ReferenceID string `json:"referenceId"`
|
||||
ResID string `json:"resId"`
|
||||
ResName string `json:"resName"`
|
||||
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"`
|
||||
URL string `json:"url"`
|
||||
Username string `json:"username"`
|
||||
Version string `json:"version"`
|
||||
Virtual bool `json:"virtual"`
|
||||
}
|
||||
|
||||
type ListImages []ImageRecord
|
||||
|
||||
type ACL struct {
|
||||
Explicit bool `json:"explicit"`
|
||||
GUID string `json:"guid"`
|
||||
Right string `json:"right"`
|
||||
Status string `json:"status"`
|
||||
Type string `json:"type"`
|
||||
UserGroupID string `json:"userGroupId"`
|
||||
}
|
||||
type History struct {
|
||||
GUID string `json:"guid"`
|
||||
ID uint64 `json:"id"`
|
||||
Timestamp uint64 `json:"timestamp"`
|
||||
}
|
||||
|
||||
type ListStacks []struct {
|
||||
CKey string `json:"_ckey"`
|
||||
Meta []interface{} `json:"_meta"`
|
||||
APIURL string `json:"apiUrl"`
|
||||
APIKey string `json:"apikey"`
|
||||
AppID string `json:"appId"`
|
||||
Desc string `json:"desc"`
|
||||
Drivers []string `json:"drivers"`
|
||||
Eco interface{} `json:"eco"`
|
||||
Error uint64 `json:"error"`
|
||||
GID uint64 `json:"gid"`
|
||||
GUID uint64 `json:"guid"`
|
||||
ID uint64 `json:"id"`
|
||||
Images []uint64 `json:"images"`
|
||||
Login string `json:"login"`
|
||||
Name string `json:"name"`
|
||||
Passwd string `json:"passwd"`
|
||||
ReferenceID string `json:"referenceId"`
|
||||
Status string `json:"status"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
45
pkg/cloudbroker/image/rename.go
Normal file
45
pkg/cloudbroker/image/rename.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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 must be set")
|
||||
}
|
||||
if irq.Name == "" {
|
||||
return errors.New("validation-error: field Name must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) Rename(ctx context.Context, req RenameRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/rename"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseBool(string(res))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
45
pkg/cloudbroker/image/share.go
Normal file
45
pkg/cloudbroker/image/share.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ShareRequest struct {
|
||||
ImageId uint64 `url:"imageId"`
|
||||
AccountIDs []uint64 `url:"accounts"`
|
||||
}
|
||||
|
||||
func (irq ShareRequest) Validate() error {
|
||||
if irq.ImageId == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
if len(irq.AccountIDs) == 0 || irq.AccountIDs == nil {
|
||||
return errors.New("validation-error: field must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) Share(ctx context.Context, req ShareRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/share"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, 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/cloudbroker/image/sync_create_image.go
Normal file
89
pkg/cloudbroker/image/sync_create_image.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/rudecs/decort-sdk/internal/validators"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type SyncCreateRequest 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"`
|
||||
PoolName string `url:"poolName,omitempty"`
|
||||
Architecture string `url:"architecture,omitempty"`
|
||||
Drivers []string `url:"drivers"`
|
||||
Bootable bool `url:"bootable,omitempty"`
|
||||
}
|
||||
|
||||
func (irq SyncCreateRequest) Validate() error {
|
||||
if irq.Name == "" {
|
||||
return errors.New("validation-error: field Name must be set")
|
||||
}
|
||||
if irq.URL == "" {
|
||||
return errors.New("validation-error: field URL must be set")
|
||||
}
|
||||
if irq.GID == 0 {
|
||||
return errors.New("validation-error: field GID must be set")
|
||||
}
|
||||
if irq.BootType == "" {
|
||||
return errors.New("validation-error: field BootType must be set")
|
||||
}
|
||||
if irq.ImageType == "" {
|
||||
return errors.New("validation-error: field ImageType must be set")
|
||||
}
|
||||
|
||||
validate := validators.StringInSlice(irq.BootType, []string{"bios", "uefi"})
|
||||
if !validate {
|
||||
return errors.New("validation-error: field BootType can be bios or uefi")
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (i Image) SyncCreate(ctx context.Context, req SyncCreateRequest) (uint64, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/syncCreateImage"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, 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
|
||||
}
|
||||
42
pkg/cloudbroker/image/update_nodes.go
Normal file
42
pkg/cloudbroker/image/update_nodes.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type UpdateNodesRequest struct {
|
||||
ImageID uint64 `url:"imageId"`
|
||||
EnabledStacks []uint64 `url:"enabledStacks,omitempty"`
|
||||
}
|
||||
|
||||
func (irq UpdateNodesRequest) Validate() error {
|
||||
if irq.ImageID == 0 {
|
||||
return errors.New("validation-error: field ImageID must be set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Image) UpdateNodes(ctx context.Context, req UpdateNodesRequest) (bool, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
url := "/cloudbroker/image/updateNodes"
|
||||
|
||||
res, err := i.client.DecortApiCall(ctx, http.MethodPost, 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