diff --git a/README.md b/README.md index a3f9d72..42a0979 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Decort SDK - это библиотека, написанная на языке G Выполните команду в терминале: ```bash -go get -u github.com/rudecs/decort-sdk +go get -u repository.basistech.ru/BASIS/decort-golang-sdk ``` ## Список API @@ -685,4 +685,4 @@ func main() { fmt.Println(res) } -``` \ No newline at end of file +``` diff --git a/client.go b/client.go index bcb813e..9f8ded6 100644 --- a/client.go +++ b/client.go @@ -7,12 +7,12 @@ import ( "net/http" "strings" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker" "github.com/google/go-querystring/query" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/config" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/client" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/client" ) // HTTP-client for platform diff --git a/go.mod b/go.mod index 65b9669..3dc5575 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,17 @@ -module repos.digitalenergy.online/BASIS/decort-golang-sdk +module repository.basistech.ru/BASIS/decort-golang-sdk go 1.20 -require github.com/google/go-querystring v1.1.0 +require ( + github.com/go-playground/validator/v10 v10.11.2 + github.com/google/go-querystring v1.1.0 +) + +require ( + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/leodido/go-urn v1.2.1 // indirect + golang.org/x/crypto v0.5.0 // indirect + golang.org/x/sys v0.4.0 // indirect + golang.org/x/text v0.6.0 // indirect +) diff --git a/go.sum b/go.sum index f99081b..a653d6f 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,30 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU= +github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= +github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= +golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/internal/client/http-client.go b/internal/client/http-client.go index 78d5717..2fb4719 100644 --- a/internal/client/http-client.go +++ b/internal/client/http-client.go @@ -5,7 +5,7 @@ import ( "net/http" "time" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/config" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" ) func NewHttpClient(cfg config.Config) *http.Client { diff --git a/internal/client/legacy-http-client.go b/internal/client/legacy-http-client.go index 8d3c4b7..0f60a07 100644 --- a/internal/client/legacy-http-client.go +++ b/internal/client/legacy-http-client.go @@ -6,7 +6,7 @@ import ( "net/url" "time" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/config" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" ) // NewLegacyHttpClient creates legacy HTTP Client diff --git a/internal/validators/custom.go b/internal/validators/custom.go new file mode 100644 index 0000000..7d9a924 --- /dev/null +++ b/internal/validators/custom.go @@ -0,0 +1,110 @@ +package validators + +import "github.com/go-playground/validator/v10" + +// accountCUTypeValidator is used to validate CUType field. +func accountCUTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, accountCUTypeValues) +} + +// accountAccessTypeValidator is used to validate AccessType field. +func accountAccessTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, accountAccessTypeValues) +} + +// bserviceDriverValidator is used to validate Driver field. +func bserviceDriverValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, bserviceDriverValues) +} + +// bserviceModeValidator is used to validate Mode field. +func bserviceModeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, bserviceModeValues) +} + +// computeTopologyValidator is used to validate Topology field. +func computeTopologyValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, computeTopologyValues) +} + +// computePolicyValidator is used to validate Policy field. +func computePolicyValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, computePolicyValues) +} + +// computeModeValidator is used to validate Mode field. +func computeModeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, computeModeValues) +} + +// computeDiskTypeValidator is used to validate DiskType field. +func computeDiskTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, computeDiskTypeValues) +} + +// computeNetTypeValidator is used to validate NetType field. +func computeNetTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, computeNetTypeValues) +} + +// computeProtoValidator is used to validate Proto field. +func computeProtoValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, computeProtoValues) +} + +// imageBootTypeValidator is used to validate BootType field. +func imageBootTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, imageBootTypeValues) +} + +// imageTypeValidator is used to validate ImageType field. +func imageTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, imageTypeValues) +} + +// imageDriversValidator is used to validate Drivers field. +func imageDriversValidator(fe validator.FieldLevel) bool { + fieldSlice, ok := fe.Field().Interface().([]string) + if !ok { + return false + } + + for _, item := range fieldSlice { + if !StringInSlice(item, imageDriversValues) { + return false + } + } + + return true +} + +// imageArchitectureValidator is used to validate Architecture field. +func imageArchitectureValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, imageArchitectureValues) +} diff --git a/internal/validators/helper.go b/internal/validators/helper.go index e639ab8..213bd55 100644 --- a/internal/validators/helper.go +++ b/internal/validators/helper.go @@ -1,5 +1,25 @@ package validators +import ( + "errors" + + "github.com/go-playground/validator/v10" +) + +func ValidateRequest(req interface{}) error { + validate := getDecortValidator() + return validate.Struct(req) +} + +func ValidationError(fe validator.FieldError) error { + return errors.New(errorMessage(fe)) +} + +//nolint:errorlint +func GetErrors(err error) validator.ValidationErrors { + return err.(validator.ValidationErrors) +} + func StringInSlice(str string, target []string) bool { for _, v := range target { if v == str { diff --git a/internal/validators/messages.go b/internal/validators/messages.go new file mode 100644 index 0000000..521ad46 --- /dev/null +++ b/internal/validators/messages.go @@ -0,0 +1,124 @@ +package validators + +import ( + "fmt" + "strings" + + "github.com/go-playground/validator/v10" +) + +func errorMessage(fe validator.FieldError) string { + prefix := "validation-error:" + + switch fe.Tag() { + + // Default Validators + case "required": + return fmt.Sprintf("%s %s is required", prefix, fe.Field()) + case "gt": + return fmt.Sprintf("%s %s can't be less or equal to zero", prefix, fe.Field()) + case "min": + return fmt.Sprintf("%s %s: not enough elements", prefix, fe.Field()) + case "max": + return fmt.Sprintf("%s %s: too many elements", prefix, fe.Field()) + case "url": + return fmt.Sprintf("%s %s: unexpected URL format", prefix, fe.Field()) + case "email": + return fmt.Sprintf("%s %s: unexpected E-Mail format", prefix, fe.Field()) + + // Account Validators + case "accountAccessType": + return fmt.Sprintf("%s %s must be one of the followng: %s", + prefix, + fe.Field(), + joinValues(accountAccessTypeValues)) + + case "accountCUType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(accountCUTypeValues)) + + // BService Validators + case "bserviceDriver": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(bserviceDriverValues)) + + case "bserviceMode": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(bserviceModeValues)) + + // Compute Validators + case "computeTopology": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(computeTopologyValues)) + + case "computePolicy": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(computePolicyValues)) + + case "computeMode": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(computeModeValues)) + + case "computeDiskType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(computeDiskTypeValues)) + + case "computeNetType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(computeNetTypeValues)) + + case "computeProto": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(computeProtoValues)) + + // Image Validators + case "bootType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(imageBootTypeValues)) + + case "imageType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(imageTypeValues)) + + case "imageDrivers": + return fmt.Sprintf("%s %s must contain only the following: %s", + prefix, + fe.Field(), + joinValues(imageDriversValues)) + + case "imageArchitecture": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(imageArchitectureValues)) + + } + + return fe.Error() +} + +func joinValues(values []string) string { + return strings.Join(values, ", ") +} diff --git a/internal/validators/validator.go b/internal/validators/validator.go new file mode 100644 index 0000000..8ff71dc --- /dev/null +++ b/internal/validators/validator.go @@ -0,0 +1,108 @@ +package validators + +import ( + "sync" + + "github.com/go-playground/validator/v10" +) + +var ( + once sync.Once + instance *DecortValidator +) + +type DecortValidator struct { + decortValidator *validator.Validate +} + +// getDecortValidator returns singleton instance of DecortValidator. +func getDecortValidator() *validator.Validate { + if instance == nil { + once.Do(func() { + instance = new(DecortValidator) + instance.decortValidator = validator.New() + + err := registerAllValidators(instance.decortValidator) + if err != nil { + panic(err) + } + }) + } + + return instance.decortValidator +} + +// registerAllValidators registers all custom validators in DecortValidator. +func registerAllValidators(validate *validator.Validate) error { + err := validate.RegisterValidation("bootType", imageBootTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("imageType", imageTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("imageDrivers", imageDriversValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("imageArchitecture", imageArchitectureValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("accountAccessType", accountAccessTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("accountCUType", accountCUTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("bserviceDriver", bserviceDriverValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("bserviceMode", bserviceModeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("computeTopology", computeTopologyValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("computePolicy", computePolicyValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("computeMode", computeModeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("computeDiskType", computeDiskTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("computeNetType", computeNetTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("computeProto", computeProtoValidator) + if err != nil { + return err + } + + return nil +} diff --git a/internal/validators/values.go b/internal/validators/values.go new file mode 100644 index 0000000..b28922b --- /dev/null +++ b/internal/validators/values.go @@ -0,0 +1,21 @@ +package validators + +var ( + accountAccessTypeValues = []string{"R", "RCX", "ARCXDU"} + accountCUTypeValues = []string{"CU_M", "CU_C", "CU_D", "CU_S", "CU_A", "CU_NO", "CU_I", "CU_NP"} + + bserviceDriverValues = []string{"KVM_X86, KVM_PPC"} + bserviceModeValues = []string{"ABSOLUTE", "RELATIVE"} + + computeTopologyValues = []string{"compute", "node"} + computePolicyValues = []string{"RECOMMENDED", "REQUIRED"} + computeModeValues = []string{"EQ", "EN", "ANY"} + computeDiskTypeValues = []string{"D", "B"} + computeNetTypeValues = []string{"EXTNET", "VINS"} + computeProtoValues = []string{"tcp", "udp"} + + imageBootTypeValues = []string{"uefi", "bios"} + imageTypeValues = []string{"windows", "linux", "other"} + imageDriversValues = []string{"KVM_X86"} + imageArchitectureValues = []string{"X86_64", "PPC64_LE"} +) diff --git a/legacy-client.go b/legacy-client.go index a2f83b6..d1f71c4 100644 --- a/legacy-client.go +++ b/legacy-client.go @@ -8,10 +8,10 @@ import ( "strings" "github.com/google/go-querystring/query" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/config" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/client" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/client" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker" ) // Legacy HTTP-client for platform diff --git a/pkg/cloudapi/account.go b/pkg/cloudapi/account.go index 55963c8..9ba5656 100644 --- a/pkg/cloudapi/account.go +++ b/pkg/cloudapi/account.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/account" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/account" ) // Accessing the Account method group diff --git a/pkg/cloudapi/account/account.go b/pkg/cloudapi/account/account.go index 3e05011..22e9efa 100644 --- a/pkg/cloudapi/account/account.go +++ b/pkg/cloudapi/account/account.go @@ -2,7 +2,7 @@ package account import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to account diff --git a/pkg/cloudapi/account/add_user.go b/pkg/cloudapi/account/add_user.go index 9d50aab..0f87627 100644 --- a/pkg/cloudapi/account/add_user.go +++ b/pkg/cloudapi/account/add_user.go @@ -2,54 +2,37 @@ package account 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 adding permission to access to account for a user type AddUserRequest struct { // ID of account to add to // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Name of the user to be given rights // Required: true - UserID string `url:"userId" json:"userId"` + UserID string `url:"userId" json:"userId" validate:"required"` // Account permission types: // - 'R' for read only access // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype"` -} - -func (arq AddUserRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - if arq.UserID == "" { - return errors.New("validation-error: field UserID can not be empty") - } - if arq.AccessType == "" { - return errors.New("validation-error: field AccessType can not be empty") - } - isValid := validators.StringInSlice(arq.AccessType, []string{"R", "RCX", "ARCXDU"}) - if !isValid { - return errors.New("validation-error: field AccessType can be only R, RCX or ARCXDU") - } - - return nil + AccessType string `url:"accesstype" json:"accesstype" validate:"required,accountAccessType"` } // AddUser gives a user access rights. func (a Account) AddUser(ctx context.Context, req AddUserRequest) (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/account/addUser" diff --git a/pkg/cloudapi/account/audits.go b/pkg/cloudapi/account/audits.go index a928a6b..9367368 100644 --- a/pkg/cloudapi/account/audits.go +++ b/pkg/cloudapi/account/audits.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for give list account audits type AuditsRequest struct { // ID of the account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq AuditsRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // Audits gets audit records for the specified account object func (a Account) Audits(ctx context.Context, req AuditsRequest) (ListAudits, 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/account/audits" diff --git a/pkg/cloudapi/account/create.go b/pkg/cloudapi/account/create.go index 0e5be15..a2f7b8e 100644 --- a/pkg/cloudapi/account/create.go +++ b/pkg/cloudapi/account/create.go @@ -2,24 +2,25 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for creating account type CreateRequest struct { // Display name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Name of the account // Required: true - Username string `url:"username" json:"username"` + Username string `url:"username" json:"username" validate:"required"` // Email // Required: false - EmailAddress string `url:"emailaddress,omitempty" json:"emailaddress,omitempty"` + EmailAddress string `url:"emailaddress,omitempty" json:"emailaddress,omitempty" validate:"omitempty,email"` // Max size of memory in MB // Required: false @@ -50,23 +51,14 @@ type CreateRequest struct { GPUUnits int64 `url:"gpu_units,omitempty" json:"gpu_units,omitempty"` } -func (arq CreateRequest) validate() error { - if arq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if arq.Username == "" { - return errors.New("validation-error: field Username can not be empty") - } - - return nil -} - // Create creates account // Setting a cloud unit maximum to -1 or empty will not put any restrictions on the resource func (a Account) 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/account/create" diff --git a/pkg/cloudapi/account/delete.go b/pkg/cloudapi/account/delete.go index 60077d5..7e712be 100644 --- a/pkg/cloudapi/account/delete.go +++ b/pkg/cloudapi/account/delete.go @@ -2,33 +2,29 @@ package account import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete account type DeleteRequest struct { // ID of account to delete // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Whether to completely delete the account // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (arq DeleteRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - return nil -} - // Delete completes delete an account from the system Returns true if account is deleted or was already deleted or never existed func (a Account) 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/account/delete" diff --git a/pkg/cloudapi/account/delete_user.go b/pkg/cloudapi/account/delete_user.go index 28aac0c..81ebae1 100644 --- a/pkg/cloudapi/account/delete_user.go +++ b/pkg/cloudapi/account/delete_user.go @@ -2,42 +2,34 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for revoke access to account type DeleteUserRequest struct { // ID of the account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // ID or emailaddress of the user to remove // Required: true - UserID string `url:"userId" json:"userId"` + UserID string `url:"userId" json:"userId" validate:"required"` // Recursively revoke access rights from owned cloudspaces and vmachines // Required: false RecursiveDelete bool `url:"recursivedelete,omitempty" json:"recursivedelete,omitempty"` } -func (arq DeleteUserRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - if arq.UserID == "" { - return errors.New("validation-error: field UserID can not be empty") - } - - return nil -} - // DeleteUser revokes user access from the account func (a Account) DeleteUser(ctx context.Context, req DeleteUserRequest) (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/account/deleteUser" diff --git a/pkg/cloudapi/account/disable_enable.go b/pkg/cloudapi/account/disable_enable.go index 8f25e49..9b2f990 100644 --- a/pkg/cloudapi/account/disable_enable.go +++ b/pkg/cloudapi/account/disable_enable.go @@ -2,31 +2,26 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for change status of account -type DisabelEnableRequest struct { +type DisableEnableRequest struct { // ID of account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq DisabelEnableRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // Disable disables an account -func (a Account) Disable(ctx context.Context, req DisabelEnableRequest) (bool, error) { - err := req.validate() +func (a Account) Disable(ctx context.Context, req DisableEnableRequest) (bool, error) { + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/account/disable" @@ -45,10 +40,12 @@ func (a Account) Disable(ctx context.Context, req DisabelEnableRequest) (bool, e } // Enable enables an account -func (a Account) Enable(ctx context.Context, req DisabelEnableRequest) (bool, error) { - err := req.validate() +func (a Account) Enable(ctx context.Context, req DisableEnableRequest) (bool, error) { + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/account/enable" diff --git a/pkg/cloudapi/account/get.go b/pkg/cloudapi/account/get.go index 07abe4d..f5612aa 100644 --- a/pkg/cloudapi/account/get.go +++ b/pkg/cloudapi/account/get.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about account type GetRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq GetRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // Get gets account details func (a Account) Get(ctx context.Context, req GetRequest) (*RecordAccount, 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/account/get" diff --git a/pkg/cloudapi/account/get_consumed_account_units.go b/pkg/cloudapi/account/get_consumed_account_units.go index a6ed700..02aeab2 100644 --- a/pkg/cloudapi/account/get_consumed_account_units.go +++ b/pkg/cloudapi/account/get_consumed_account_units.go @@ -3,23 +3,16 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for calculate the currently consumed units for all cloudspaces and resource groups in the account type GetConsumedAccountUnitsRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq GetConsumedAccountUnitsRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // GetConsumedAccountUnits calculates the currently consumed units for all cloudspaces and resource groups in the account. @@ -29,9 +22,11 @@ func (arq GetConsumedAccountUnitsRequest) validate() error { // - CU_D: consumed vdisk storage in GB // - CU_I: number of public IPs func (a Account) GetConsumedAccountUnits(ctx context.Context, req GetConsumedAccountUnitsRequest) (*ResourceLimits, 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/account/getConsumedAccountUnits" diff --git a/pkg/cloudapi/account/get_consumed_cloud_units_by_type.go b/pkg/cloudapi/account/get_consumed_cloud_units_by_type.go index 5021542..2347980 100644 --- a/pkg/cloudapi/account/get_consumed_cloud_units_by_type.go +++ b/pkg/cloudapi/account/get_consumed_cloud_units_by_type.go @@ -2,37 +2,21 @@ package account 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 calculate the currently consumed cloud units of the specified type for all cloudspaces and resource groups in the account type GetConsumedCloudUnitsByTypeRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Cloud unit resource type // Required: true - CUType string `url:"cutype" json:"cutype"` -} - -func (arq GetConsumedCloudUnitsByTypeRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - if arq.CUType == "" { - return errors.New("validation-error: field CUType can not be empty") - } - isValid := validators.StringInSlice(arq.CUType, []string{"CU_M", "CU_C", "CU_D", "CU_S", "CU_A", "CU_NO", "CU_I", "CU_NP"}) - if !isValid { - return errors.New("validation-error: field AccessType can be only CU_M, CU_C, CU_D, CU_S, CU_A, CU_NO, CU_I or CU_NP") - } - - return nil + CUType string `url:"cutype" json:"cutype" validate:"required,accountCUType"` } // GetConsumedCloudUnitsByType calculates the currently consumed cloud units of the specified type for all cloudspaces @@ -48,9 +32,11 @@ func (arq GetConsumedCloudUnitsByTypeRequest) validate() error { // - CU_NP: returns sent/received network transfer peering in GB // - CU_I: returns number of public IPs func (a Account) GetConsumedCloudUnitsByType(ctx context.Context, req GetConsumedCloudUnitsByTypeRequest) (float64, 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/account/getConsumedCloudUnitsByType" diff --git a/pkg/cloudapi/account/get_consumption.go b/pkg/cloudapi/account/get_consumption.go index 8092eab..f36ea92 100644 --- a/pkg/cloudapi/account/get_consumption.go +++ b/pkg/cloudapi/account/get_consumption.go @@ -2,47 +2,36 @@ package account import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for download the resources tracking files for an account -type GetConsumtionRequest struct { +type GetConsumptionRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Epoch represents the start time // Required: true - Start uint64 `url:"start" json:"start"` + Start uint64 `url:"start" json:"start" validate:"required"` // Epoch represents the end time // Required: true - End uint64 `url:"end" json:"end"` + End uint64 `url:"end" json:"end" validate:"required"` } -func (arq GetConsumtionRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - if arq.Start == 0 { - return errors.New("validation-error: field Start can not be empty or equal to 0") - } - if arq.End == 0 { - return errors.New("validation-error: field End can not be empty or equal to 0") - } - - return nil -} - -// GetConsumtion downloads the resources tracking files for an account within a given period -func (a Account) GetConsumtion(ctx context.Context, req GetConsumtionRequest) (string, error) { - err := req.validate() +// GetConsumption downloads the resources tracking files for an account within a given period +func (a Account) GetConsumption(ctx context.Context, req GetConsumptionRequest) (string, error) { + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } - url := "/cloudapi/account/getConsumtion" + url := "/cloudapi/account/getConsumption" res, err := a.client.DecortApiCall(ctx, http.MethodPost, url, req) if err != nil { @@ -53,14 +42,16 @@ func (a Account) GetConsumtion(ctx context.Context, req GetConsumtionRequest) (s } -// GetConsumtionGet downloads the resources tracking files for an account within a given period -func (a Account) GetConsumtionGet(ctx context.Context, req GetConsumtionRequest) (string, error) { - err := req.validate() +// GetConsumptionGet downloads the resources tracking files for an account within a given period +func (a Account) GetConsumptionGet(ctx context.Context, req GetConsumptionRequest) (string, error) { + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } - url := "/cloudapi//account/getConsumtion" + url := "/cloudapi/account/getConsumption" res, err := a.client.DecortApiCall(ctx, http.MethodGet, url, req) if err != nil { diff --git a/pkg/cloudapi/account/get_reserved_account_units.go b/pkg/cloudapi/account/get_reserved_account_units.go index 037e032..d8d3ab0 100644 --- a/pkg/cloudapi/account/get_reserved_account_units.go +++ b/pkg/cloudapi/account/get_reserved_account_units.go @@ -3,23 +3,16 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for calculate the reserved units for all cloudspaces and resource groups in the account type GetReservedAccountUnitsRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq GetReservedAccountUnitsRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // GetReservedAccountUnits calculates the reserved units for all cloudspaces and resource groups in the account. @@ -30,9 +23,11 @@ func (arq GetReservedAccountUnitsRequest) validate() error { // - CU_D: reserved vdisk storage in GB // - CU_I: number of public IPs func (a Account) GetReservedAccountUnits(ctx context.Context, req GetReservedAccountUnitsRequest) (*ResourceLimits, 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/account/getReservedAccountUnits" diff --git a/pkg/cloudapi/account/list_computes.go b/pkg/cloudapi/account/list_computes.go index 7bf91fd..ed71792 100644 --- a/pkg/cloudapi/account/list_computes.go +++ b/pkg/cloudapi/account/list_computes.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for a get list compute instances type ListComputesRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListComputesRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListComputes gets list all compute instances under specified account, accessible by the user func (a Account) ListComputes(ctx context.Context, req ListComputesRequest) (ListComputes, 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/account/listComputes" diff --git a/pkg/cloudapi/account/list_disks.go b/pkg/cloudapi/account/list_disks.go index b4b487a..63b059f 100644 --- a/pkg/cloudapi/account/list_disks.go +++ b/pkg/cloudapi/account/list_disks.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list deleted disks type ListDisksRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListDisksRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListDisks gets list all currently unattached disks under specified account func (a Account) ListDisks(ctx context.Context, req ListDisksRequest) (ListDisks, 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/account/listDisks" diff --git a/pkg/cloudapi/account/list_flipgroups.go b/pkg/cloudapi/account/list_flipgroups.go index 93c64b6..4a634cc 100644 --- a/pkg/cloudapi/account/list_flipgroups.go +++ b/pkg/cloudapi/account/list_flipgroups.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list FLIPGroups type ListFLIPGroupsRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListFLIPGroupsRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListFLIPGroups gets list all FLIPGroups under specified account, accessible by the user func (a Account) ListFLIPGroups(ctx context.Context, req ListFLIPGroupsRequest) (ListFLIPGroups, 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/account/listFlipGroups" diff --git a/pkg/cloudapi/account/list_rg.go b/pkg/cloudapi/account/list_rg.go index c8dacfb..f3059d3 100644 --- a/pkg/cloudapi/account/list_rg.go +++ b/pkg/cloudapi/account/list_rg.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list resource groups type ListRGRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListRGRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListRG gets list all resource groups under specified account, accessible by the user func (a Account) ListRG(ctx context.Context, req ListRGRequest) (ListRG, 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/account/listRG" diff --git a/pkg/cloudapi/account/list_templates.go b/pkg/cloudapi/account/list_templates.go index c7090b6..b22a45a 100644 --- a/pkg/cloudapi/account/list_templates.go +++ b/pkg/cloudapi/account/list_templates.go @@ -3,34 +3,29 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list templates type ListTemplatesRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Include deleted images // Required: false IncludeDeleted bool `url:"includedeleted" json:"includedeleted"` } -func (arq ListTemplatesRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil -} - // ListTemplates gets list templates which can be managed by this account func (a Account) ListTemplates(ctx context.Context, req ListTemplatesRequest) (ListTemplates, 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/account/listTemplates" diff --git a/pkg/cloudapi/account/list_vins.go b/pkg/cloudapi/account/list_vins.go index 3e51b72..990aa99 100644 --- a/pkg/cloudapi/account/list_vins.go +++ b/pkg/cloudapi/account/list_vins.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list VINS type ListVINSRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListVINSRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListVINS gets list all ViNSes under specified account, accessible by the user func (a Account) ListVINS(ctx context.Context, req ListVINSRequest) (ListVINS, 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/account/listVins" diff --git a/pkg/cloudapi/account/restore.go b/pkg/cloudapi/account/restore.go index 24dfe8d..c0d0bd3 100644 --- a/pkg/cloudapi/account/restore.go +++ b/pkg/cloudapi/account/restore.go @@ -2,31 +2,26 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore a deleted account type RestoreRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq RestoreRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // Restore restores a deleted account func (a Account) Restore(ctx context.Context, req RestoreRequest) (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/account/restore" diff --git a/pkg/cloudapi/account/serialize.go b/pkg/cloudapi/account/serialize.go index 6c2dac5..8ee25ca 100644 --- a/pkg/cloudapi/account/serialize.go +++ b/pkg/cloudapi/account/serialize.go @@ -3,7 +3,7 @@ package account 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. diff --git a/pkg/cloudapi/account/update.go b/pkg/cloudapi/account/update.go index 95c1c0d..29d00cd 100644 --- a/pkg/cloudapi/account/update.go +++ b/pkg/cloudapi/account/update.go @@ -2,16 +2,17 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for updaate account type UpdateRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Name of the account // Required: false @@ -46,19 +47,13 @@ type UpdateRequest struct { GPUUnits uint64 `url:"gpu_units,omitempty" json:"gpu_units,omitempty"` } -func (arq UpdateRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil -} - // Update updates an account name and resource types and limits func (a Account) Update(ctx context.Context, req UpdateRequest) (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/account/update" diff --git a/pkg/cloudapi/account/update_user.go b/pkg/cloudapi/account/update_user.go index f33e99b..5da4cde 100644 --- a/pkg/cloudapi/account/update_user.go +++ b/pkg/cloudapi/account/update_user.go @@ -2,54 +2,37 @@ package account 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 update user access rights type UpdateUserRequest struct { // ID of the account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Userid/Email for registered users or emailaddress for unregistered users // Required: true - UserID string `url:"userId" json:"userId"` + UserID string `url:"userId" json:"userId" validate:"required"` // Account permission types: // - 'R' for read only access // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype"` -} - -func (arq UpdateUserRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - if arq.UserID == "" { - return errors.New("validation-error: field UserID can not be empty") - } - if arq.AccessType == "" { - return errors.New("validation-error: field AccessType can not be empty") - } - isValid := validators.StringInSlice(arq.AccessType, []string{"R", "RCX", "ARCXDU"}) - if !isValid { - return errors.New("validation-error: field AccessType can be only R, RCX or ARCXDU") - } - - return nil + AccessType string `url:"accesstype" json:"accesstype" validate:"required,accountAccessType"` } // UpdateUser updates user access rights func (a Account) UpdateUser(ctx context.Context, req UpdateUserRequest) (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/account/updateUser" diff --git a/pkg/cloudapi/bservice.go b/pkg/cloudapi/bservice.go index 7372528..4a80d4c 100644 --- a/pkg/cloudapi/bservice.go +++ b/pkg/cloudapi/bservice.go @@ -1,6 +1,6 @@ package cloudapi -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/bservice" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/bservice" // Accessing the BService method group func (ca *CloudAPI) BService() *bservice.BService { diff --git a/pkg/cloudapi/bservice/bservice.go b/pkg/cloudapi/bservice/bservice.go index ad80d7b..df548e0 100644 --- a/pkg/cloudapi/bservice/bservice.go +++ b/pkg/cloudapi/bservice/bservice.go @@ -1,7 +1,7 @@ // API Actor for managing Compute Group. This actor is a final API for endusers to manage Compute Group package bservice -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to bservice type BService struct { diff --git a/pkg/cloudapi/bservice/create.go b/pkg/cloudapi/bservice/create.go index a8c3823..fa2272f 100644 --- a/pkg/cloudapi/bservice/create.go +++ b/pkg/cloudapi/bservice/create.go @@ -2,20 +2,21 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for BasicService type CreateRequest struct { // Name of the service // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // ID of the Resource Group where this service will be placed // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of the user to deploy SSH key for. Pass empty string if no SSH key deployment is required // Required: false @@ -26,22 +27,13 @@ type CreateRequest struct { SSHKey string `url:"sshKey,omitempty" json:"sshKey,omitempty"` } -func (bsrq CreateRequest) validate() error { - if bsrq.Name == "" { - return errors.New("field Name can not be empty") - } - if bsrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // Create creates blank BasicService instance func (b BService) 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/bservice/create" diff --git a/pkg/cloudapi/bservice/delete.go b/pkg/cloudapi/bservice/delete.go index ab03493..cfc85a2 100644 --- a/pkg/cloudapi/bservice/delete.go +++ b/pkg/cloudapi/bservice/delete.go @@ -2,35 +2,30 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete basic service type DeleteRequest struct { // ID of the BasicService to be delete // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // If set to False, Basic service will be deleted to recycle bin. Otherwise destroyed immediately - // Required: true + // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (bsrq DeleteRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - - return nil -} - // Delete deletes BasicService instance func (b BService) 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/bservice/delete" diff --git a/pkg/cloudapi/bservice/disable.go b/pkg/cloudapi/bservice/disable.go index ff1979b..180a535 100644 --- a/pkg/cloudapi/bservice/disable.go +++ b/pkg/cloudapi/bservice/disable.go @@ -2,33 +2,28 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable service type DisableRequest struct { // ID of the service to disable // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` -} - -func (bsrq DisableRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - - return nil + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` } // Disable disables service. // Disabling a service technically means setting model status // of all computes and service itself to DISABLED and stopping all computes. func (b BService) Disable(ctx context.Context, req DisableRequest) (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/bservice/delete" diff --git a/pkg/cloudapi/bservice/enable.go b/pkg/cloudapi/bservice/enable.go index a1fa632..e0d3ac3 100644 --- a/pkg/cloudapi/bservice/enable.go +++ b/pkg/cloudapi/bservice/enable.go @@ -2,24 +2,17 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable service type EnableRequest struct { // ID of the service to enable // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` -} - -func (bsrq EnableRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - - return nil + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` } // Enable enables service. @@ -27,9 +20,11 @@ func (bsrq EnableRequest) validate() error { // all computes and service itself to ENABLED. // It does not start computes. func (b BService) Enable(ctx context.Context, req EnableRequest) (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/bservice/enable" diff --git a/pkg/cloudapi/bservice/filter_test.go b/pkg/cloudapi/bservice/filter_test.go new file mode 100644 index 0000000..cb7930d --- /dev/null +++ b/pkg/cloudapi/bservice/filter_test.go @@ -0,0 +1,152 @@ +package bservice + +import "testing" + +var bservices = ListBasicServices{ + { + AccountID: 1, + AccountName: "std_1", + BaseDomain: "", + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1677743675, + DeletedBy: "", + DeletedTime: 0, + GID: 212, + Groups: []uint64{}, + GUID: 1, + ID: 1, + Name: "bservice_1", + ParentSrvID: 0, + ParentSrvType: "", + RGID: 7971, + RGName: "rg_1", + SSHUser: "", + Status: "CREATED", + TechStatus: "STOPPED", + UpdatedBy: "", + UpdatedTime: 0, + UserManaged: true, + }, + { + AccountID: 2, + AccountName: "std_2", + BaseDomain: "", + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1677743736, + DeletedBy: "", + DeletedTime: 0, + GID: 212, + Groups: []uint64{}, + GUID: 2, + ID: 2, + Name: "bservice_2", + ParentSrvID: 0, + ParentSrvType: "", + RGID: 7972, + RGName: "rg_2", + SSHUser: "", + Status: "CREATED", + TechStatus: "STOPPED", + UpdatedBy: "", + UpdatedTime: 0, + UserManaged: true, + }, + { + AccountID: 3, + AccountName: "std_3", + BaseDomain: "", + CreatedBy: "sample_user_2@decs3o", + CreatedTime: 1677743830, + DeletedBy: "", + DeletedTime: 0, + GID: 212, + Groups: []uint64{}, + GUID: 3, + ID: 3, + Name: "bservice_3", + ParentSrvID: 0, + ParentSrvType: "", + RGID: 7973, + RGName: "rg_3", + SSHUser: "", + Status: "ENABLED", + TechStatus: "STARTED", + UpdatedBy: "", + UpdatedTime: 0, + UserManaged: true, + }, +} + +func TestFilterByID(t *testing.T) { + actual := bservices.FilterByID(1).FindOne() + + if actual.ID != 1 { + t.Fatal("expected ID 1, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := bservices.FilterByName("bservice_3").FindOne() + + if actual.Name != "bservice_3" { + t.Fatal("expected Name 'bservice_3', found: ", actual.Name) + } +} + +func TestFilterByRGID(t *testing.T) { + actual := bservices.FilterByRGID(7971).FindOne() + + if actual.RGID != 7971 { + t.Fatal("expected RGID 7971, found: ", actual.RGID) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := bservices.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 TestFilterByTechStatus(t *testing.T) { + actual := bservices.FilterByTechStatus("STOPPED") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.TechStatus != "STOPPED" { + t.Fatal("expected TechStatus 'STOPPED', found: ", item.TechStatus) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := bservices.FilterFunc(func(ibs ItemBasicService) bool { + return ibs.CreatedBy == "sample_user_2@decs3o" + }) + + if len(actual) > 1 { + t.Fatal("expected 1 found, actual: ", len(actual)) + } + + if actual.FindOne().CreatedBy != "sample_user_2@decs3o" { + t.Fatal("expected 'sample_user_2@decs3o', found: ", actual.FindOne().CreatedBy) + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := bservices.SortByCreatedTime(true) + + if actual[0].CreatedTime != 1677743830 || actual[2].CreatedTime != 1677743675 { + t.Fatal("expected descending order, found ascending") + } +} diff --git a/pkg/cloudapi/bservice/get.go b/pkg/cloudapi/bservice/get.go index 73611c0..55e827a 100644 --- a/pkg/cloudapi/bservice/get.go +++ b/pkg/cloudapi/bservice/get.go @@ -3,30 +3,25 @@ package bservice import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get detailed information about service type GetRequest struct { // ID of the service to query information // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` -} - -func (bsrq GetRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - - return nil + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` } // Get gets detailed specifications for the BasicService. func (b BService) Get(ctx context.Context, req GetRequest) (*RecordBasicService, 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/bservice/get" diff --git a/pkg/cloudapi/bservice/group_add.go b/pkg/cloudapi/bservice/group_add.go index 69d7f41..d830bc8 100644 --- a/pkg/cloudapi/bservice/group_add.go +++ b/pkg/cloudapi/bservice/group_add.go @@ -2,47 +2,48 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create new compute group within BasicService type GroupAddRequest struct { // ID of the Basic Service to add a group to // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // Name of the Compute Group to add // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Computes number. Defines how many computes must be there in the group // Required: true - Count uint64 `url:"count" json:"count"` + Count uint64 `url:"count" json:"count" validate:"required"` // Compute CPU number. All computes in the group have the same CPU count // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Compute RAM volume in MB. All computes in the group have the same RAM volume // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // Compute boot disk size in GB // Required: true - Disk uint64 `url:"disk" json:"disk"` + Disk uint64 `url:"disk" json:"disk" validate:"required"` // OS image ID to create computes from // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Compute driver // should be one of: // - KVM_X86 // - KVM_PPC // Required: true - Driver string `url:"driver" json:"driver"` + Driver string `url:"driver" json:"driver" validate:"bserviceDriver"` // Storage endpoint provider ID // Required: false @@ -66,45 +67,18 @@ type GroupAddRequest struct { // Time of Compute Group readiness // Required: false - TimeoutStart uint64 `url:"timeoutStart" json:"timeoutStart"` -} - -func (bsrq GroupAddRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.Name == "" { - return errors.New("field Name can not be empty") - } - if bsrq.Count == 0 { - return errors.New("field Count can not be empty or equal to 0") - } - if bsrq.CPU == 0 { - return errors.New("field CPU can not be empty or equal to 0") - } - if bsrq.RAM == 0 { - return errors.New("field RAM can not be empty or equal to 0") - } - if bsrq.Disk == 0 { - return errors.New("field Disk can not be empty or equal to 0") - } - if bsrq.ImageID == 0 { - return errors.New("field ImageID can not be empty or equal to 0") - } - if bsrq.Driver == "" { - return errors.New("field Driver can not be empty") - } - - return nil + TimeoutStart uint64 `url:"timeoutStart,omitempty" json:"timeoutStart,omitempty"` } // GroupAdd creates new Compute Group within BasicService. // Compute Group is NOT started automatically, // so you need to explicitly start it func (b BService) GroupAdd(ctx context.Context, req GroupAddRequest) (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/bservice/groupAdd" diff --git a/pkg/cloudapi/bservice/group_compute_remove.go b/pkg/cloudapi/bservice/group_compute_remove.go index d3b16a4..4acb098 100644 --- a/pkg/cloudapi/bservice/group_compute_remove.go +++ b/pkg/cloudapi/bservice/group_compute_remove.go @@ -2,45 +2,34 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove group compute type GroupComputeRemoveRequest struct { // ID of the Basic Service // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute GROUP // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` // ID of the Compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (bsrq GroupComputeRemoveRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - if bsrq.ComputeID == 0 { - return errors.New("field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // GroupComputeRemove makes group compute remove of the Basic Service func (b BService) GroupComputeRemove(ctx context.Context, req GroupComputeRemoveRequest) (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/bservice/groupComputeRemove" diff --git a/pkg/cloudapi/bservice/group_get.go b/pkg/cloudapi/bservice/group_get.go index cf80b6c..bf913dc 100644 --- a/pkg/cloudapi/bservice/group_get.go +++ b/pkg/cloudapi/bservice/group_get.go @@ -3,37 +3,29 @@ package bservice import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get detailed information about Compute Group type GroupGetRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` -} - -func (bsrq GroupGetRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - - return nil + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` } // GroupGet gets detailed specifications for the Compute Group func (b BService) GroupGet(ctx context.Context, req GroupGetRequest) (*RecordGroup, 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/bservice/groupGet" diff --git a/pkg/cloudapi/bservice/group_parent_add.go b/pkg/cloudapi/bservice/group_parent_add.go index 4fbead0..cb4385c 100644 --- a/pkg/cloudapi/bservice/group_parent_add.go +++ b/pkg/cloudapi/bservice/group_parent_add.go @@ -2,9 +2,10 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add parent Compute Group relation emove parent Compute Group @@ -12,36 +13,24 @@ import ( type GroupParentAddRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` // ID of the parent Compute Group to register with the current Compute Group // Required: true - ParentID uint64 `url:"parentId" json:"parentId"` -} - -func (bsrq GroupParentAddRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - if bsrq.ParentID == 0 { - return errors.New("field ParentID can not be empty or equal to 0") - } - - return nil + ParentID uint64 `url:"parentId" json:"parentId" validate:"required"` } // GroupParentAdd add parent Compute Group relation to the specified Compute Group func (b BService) GroupParentAdd(ctx context.Context, req GroupParentAddRequest) (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/bservice/groupParentAdd" diff --git a/pkg/cloudapi/bservice/group_parent_remove.go b/pkg/cloudapi/bservice/group_parent_remove.go index 8989310..7e4e419 100644 --- a/pkg/cloudapi/bservice/group_parent_remove.go +++ b/pkg/cloudapi/bservice/group_parent_remove.go @@ -2,9 +2,10 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove parent Compute Group @@ -12,37 +13,25 @@ import ( type GroupParentRemoveRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` // ID of the parent Compute Group // to remove from the current Compute Group // Required: true - ParentID uint64 `url:"parentId" json:"parentId"` -} - -func (bsrq GroupParentRemoveRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - if bsrq.ParentID == 0 { - return errors.New("field ParentID can not be empty or equal to 0") - } - - return nil + ParentID uint64 `url:"parentId" json:"parentId" validate:"required"` } // GroupParentRemove removes parent Compute Group relation to the specified Compute Group func (b BService) GroupParentRemove(ctx context.Context, req GroupParentRemoveRequest) (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/bservice/groupParentRemove" diff --git a/pkg/cloudapi/bservice/group_remove.go b/pkg/cloudapi/bservice/group_remove.go index 2944f78..c3aa5e0 100644 --- a/pkg/cloudapi/bservice/group_remove.go +++ b/pkg/cloudapi/bservice/group_remove.go @@ -2,38 +2,30 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for destroy the specified Compute Group type GroupRemoveRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` -} - -func (bsrq GroupRemoveRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - - return nil + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` } // GroupRemove destroy the specified Compute Group func (b BService) GroupRemove(ctx context.Context, req GroupRemoveRequest) (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/bservice/groupRemove" diff --git a/pkg/cloudapi/bservice/group_resize.go b/pkg/cloudapi/bservice/group_resize.go index 70d3fec..3298a0a 100644 --- a/pkg/cloudapi/bservice/group_resize.go +++ b/pkg/cloudapi/bservice/group_resize.go @@ -2,58 +2,41 @@ package bservice 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 resize the group type GroupResizeRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group to resize // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` // Either delta or absolute value of computes // Required: true - Count int64 `url:"count" json:"count"` + Count int64 `url:"count" json:"count" validate:"required"` // Either delta or absolute value of computes // Should be one of: // - ABSOLUTE // - RELATIVE // Required: true - Mode string `url:"mode" json:"mode"` -} - -func (bsrq GroupResizeRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - if bsrq.Mode == "RELATIVE" && bsrq.Count == 0 { - return errors.New("field Count can not be equal to 0 if Mode if 'RELATIVE'") - } - validate := validators.StringInSlice(bsrq.Mode, []string{"RELATIVE", "ABSOLUTE"}) - if !validate { - return errors.New("field Mode can only be one of 'RELATIVE' or 'ABSOLUTE'") - } - - return nil + Mode string `url:"mode" json:"mode" validate:"bserviceMode"` } // GroupResize resize the group by changing the number of computes func (b BService) GroupResize(ctx context.Context, req GroupResizeRequest) (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/bservice/groupResize" diff --git a/pkg/cloudapi/bservice/group_start.go b/pkg/cloudapi/bservice/group_start.go index bb1714f..38c4aed 100644 --- a/pkg/cloudapi/bservice/group_start.go +++ b/pkg/cloudapi/bservice/group_start.go @@ -2,38 +2,30 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start the specified Compute Group type GroupStartRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group to start // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` -} - -func (bsrq GroupStartRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - - return nil + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` } // GroupStart starts the specified Compute Group within BasicService func (b BService) GroupStart(ctx context.Context, req GroupStartRequest) (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/bservice/groupStart" diff --git a/pkg/cloudapi/bservice/group_stop.go b/pkg/cloudapi/bservice/group_stop.go index 7917e12..eb515b7 100644 --- a/pkg/cloudapi/bservice/group_stop.go +++ b/pkg/cloudapi/bservice/group_stop.go @@ -2,42 +2,34 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for stop the specified Compute Group type GroupStopRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group to stop // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` // Force stop Compute Group - // Required: true + // Required: false Force bool `url:"force,omitempty" json:"force,omitempty"` } -func (bsrq GroupStopRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - - return nil -} - // GroupStop stops the specified Compute Group within BasicService func (b BService) GroupStop(ctx context.Context, req GroupStopRequest) (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/bservice/groupStop" diff --git a/pkg/cloudapi/bservice/group_update.go b/pkg/cloudapi/bservice/group_update.go index c64640b..68df830 100644 --- a/pkg/cloudapi/bservice/group_update.go +++ b/pkg/cloudapi/bservice/group_update.go @@ -2,20 +2,21 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update existing Compute group type GroupUpdateRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` // Specify non-empty string to update Compute Group name // Required: false @@ -42,22 +43,13 @@ type GroupUpdateRequest struct { Force bool `url:"force,omitempty" json:"force,omitempty"` } -func (bsrq GroupUpdateRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - - return nil -} - // GroupUpdate updates existing Compute group within Basic Service and apply new settings to its computes as necessary func (b BService) GroupUpdate(ctx context.Context, req GroupUpdateRequest) (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/bservice/groupUpdate" diff --git a/pkg/cloudapi/bservice/group_update_extnet.go b/pkg/cloudapi/bservice/group_update_extnet.go index a67f085..adcce48 100644 --- a/pkg/cloudapi/bservice/group_update_extnet.go +++ b/pkg/cloudapi/bservice/group_update_extnet.go @@ -2,42 +2,34 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update External Network settings type GroupUpdateExtNetRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` // List of Extnets to connect computes // Required: false ExtNets []uint64 `url:"extnets,omitempty" json:"extnets,omitempty"` } -func (bsrq GroupUpdateExtNetRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - - return nil -} - // GroupUpdateExtNet updates External Network settings for the group according to the new list func (b BService) GroupUpdateExtNet(ctx context.Context, req GroupUpdateExtNetRequest) (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/bservice/groupUpdateExtnet" diff --git a/pkg/cloudapi/bservice/group_update_vins.go b/pkg/cloudapi/bservice/group_update_vins.go index 369be29..8c2cf02 100644 --- a/pkg/cloudapi/bservice/group_update_vins.go +++ b/pkg/cloudapi/bservice/group_update_vins.go @@ -2,42 +2,34 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update VINS settings type GroupUpdateVINSRequest struct { // ID of the Basic Service of Compute Group // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // ID of the Compute Group // Required: true - CompGroupID uint64 `url:"compgroupId" json:"compgroupId"` + CompGroupID uint64 `url:"compgroupId" json:"compgroupId" validate:"required"` // List of ViNSes to connect computes // Required: false VINSes []uint64 `url:"vinses,omitempty" json:"vinses,omitempty"` } -func (bsrq GroupUpdateVINSRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.CompGroupID == 0 { - return errors.New("field CompGroupID can not be empty or equal to 0") - } - - return nil -} - // GroupUpdateVINS update ViNS settings for the group according to the new list func (b BService) GroupUpdateVINS(ctx context.Context, req GroupUpdateVINSRequest) (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/bservice/groupUpdateVins" diff --git a/pkg/cloudapi/bservice/restore.go b/pkg/cloudapi/bservice/restore.go index 62b4d80..64e36f2 100644 --- a/pkg/cloudapi/bservice/restore.go +++ b/pkg/cloudapi/bservice/restore.go @@ -2,31 +2,26 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restores BasicService instance type RestoreRequest struct { // ID of the BasicService to be restored // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` -} - -func (bsrq RestoreRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - - return nil + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` } // Restore restores BasicService instance func (b BService) Restore(ctx context.Context, req RestoreRequest) (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/bservice/restore" diff --git a/pkg/cloudapi/bservice/serialize.go b/pkg/cloudapi/bservice/serialize.go index a2e221a..8968bc4 100644 --- a/pkg/cloudapi/bservice/serialize.go +++ b/pkg/cloudapi/bservice/serialize.go @@ -3,7 +3,7 @@ package bservice 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. diff --git a/pkg/cloudapi/bservice/snapshot_create.go b/pkg/cloudapi/bservice/snapshot_create.go index 526ce2d..54158c7 100644 --- a/pkg/cloudapi/bservice/snapshot_create.go +++ b/pkg/cloudapi/bservice/snapshot_create.go @@ -2,38 +2,30 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create snapshot type SnapshotCreateRequest struct { // ID of the Basic Service // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // Label of the snapshot // Required: true - Label string `url:"label" json:"label"` -} - -func (bsrq SnapshotCreateRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.Label == "" { - return errors.New("field Label can not be empty") - } - - return nil + Label string `url:"label" json:"label" validate:"required"` } // SnapshotCreate create snapshot of the Basic Service func (b BService) SnapshotCreate(ctx context.Context, req SnapshotCreateRequest) (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/bservice/snapshotCreate" diff --git a/pkg/cloudapi/bservice/snapshot_delete.go b/pkg/cloudapi/bservice/snapshot_delete.go index b1d6b7f..54944f7 100644 --- a/pkg/cloudapi/bservice/snapshot_delete.go +++ b/pkg/cloudapi/bservice/snapshot_delete.go @@ -2,38 +2,30 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete snapshot type SnapshotDeleteRequest struct { // ID of the Basic Service // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // Label of the snapshot // Required: true - Label string `url:"label" json:"label"` -} - -func (bsrq SnapshotDeleteRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.Label == "" { - return errors.New("field Label can not be empty") - } - - return nil + Label string `url:"label" json:"label" validate:"required"` } // SnapshotDelete delete snapshot of the Basic Service func (b BService) SnapshotDelete(ctx context.Context, req SnapshotDeleteRequest) (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/bservice/snapshotDelete" diff --git a/pkg/cloudapi/bservice/snapshot_list.go b/pkg/cloudapi/bservice/snapshot_list.go index 0c1e75d..af6381e 100644 --- a/pkg/cloudapi/bservice/snapshot_list.go +++ b/pkg/cloudapi/bservice/snapshot_list.go @@ -3,30 +3,25 @@ package bservice import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list existing snapshots type SnapshotListRequest struct { // ID of the Basic Service // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` -} - -func (bsrq SnapshotListRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - - return nil + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` } // SnapshotList gets list existing snapshots of the Basic Service func (b BService) SnapshotList(ctx context.Context, req SnapshotListRequest) (ListSnapshots, 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/bservice/snapshotList" diff --git a/pkg/cloudapi/bservice/snapshot_rollback.go b/pkg/cloudapi/bservice/snapshot_rollback.go index 96b0cdb..a1fca1b 100644 --- a/pkg/cloudapi/bservice/snapshot_rollback.go +++ b/pkg/cloudapi/bservice/snapshot_rollback.go @@ -2,38 +2,30 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for rollback snapshot type SnapshotRollbackRequest struct { // ID of the Basic Service // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` // Label of the snapshot // Required: true - Label string `url:"label" json:"label"` -} - -func (bsrq SnapshotRollbackRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - if bsrq.Label == "" { - return errors.New("field Label can not be empty") - } - - return nil + Label string `url:"label" json:"label" validate:"required"` } // SnapshotRollback rollback snapshot of the Basic Service func (b BService) SnapshotRollback(ctx context.Context, req SnapshotRollbackRequest) (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/bservice/snapshotRollback" diff --git a/pkg/cloudapi/bservice/start.go b/pkg/cloudapi/bservice/start.go index d551839..4bed140 100644 --- a/pkg/cloudapi/bservice/start.go +++ b/pkg/cloudapi/bservice/start.go @@ -2,33 +2,28 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start service type StartRequest struct { // ID of the service to start // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` -} - -func (bsrq StartRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - - return nil + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` } // Start starts service. // Starting a service technically means starting computes from all // service groups according to group relations func (b BService) Start(ctx context.Context, req StartRequest) (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/bservice/start" diff --git a/pkg/cloudapi/bservice/stop.go b/pkg/cloudapi/bservice/stop.go index 5f5dd20..b37651c 100644 --- a/pkg/cloudapi/bservice/stop.go +++ b/pkg/cloudapi/bservice/stop.go @@ -2,33 +2,28 @@ package bservice import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for stop service type StopRequest struct { // ID of the service to stop // Required: true - ServiceID uint64 `url:"serviceId" json:"serviceId"` -} - -func (bsrq StopRequest) validate() error { - if bsrq.ServiceID == 0 { - return errors.New("field ServiceID can not be empty or equal to 0") - } - - return nil + ServiceID uint64 `url:"serviceId" json:"serviceId" validate:"required"` } // Stop stops service. // Stopping a service technically means stopping computes from // all service groups func (b BService) Stop(ctx context.Context, req StopRequest) (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/bservice/stop" diff --git a/pkg/cloudapi/cloudapi.go b/pkg/cloudapi/cloudapi.go index 83f3e57..4501810 100644 --- a/pkg/cloudapi/cloudapi.go +++ b/pkg/cloudapi/cloudapi.go @@ -2,7 +2,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to CloudAPI groups diff --git a/pkg/cloudapi/compute.go b/pkg/cloudapi/compute.go index 8cdcb51..ca52578 100644 --- a/pkg/cloudapi/compute.go +++ b/pkg/cloudapi/compute.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/compute" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/compute" ) // Accessing the Compute method group diff --git a/pkg/cloudapi/compute/affinity_group_check_start.go b/pkg/cloudapi/compute/affinity_group_check_start.go index d8f6fa2..74f964a 100644 --- a/pkg/cloudapi/compute/affinity_group_check_start.go +++ b/pkg/cloudapi/compute/affinity_group_check_start.go @@ -2,37 +2,29 @@ package compute import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for check all computes with current affinity label can start type AffinityGroupCheckStartRequest struct { // ID of the resource group // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Affinity group label // Required: true - AffinityLabel string `url:"affinityLabel" json:"affinityLabel"` -} - -func (crq AffinityGroupCheckStartRequest) validate() error { - if crq.RGID == 0 { - return errors.New("validation-error: field RGID can not be empty or equal to 0") - } - if crq.AffinityLabel == "" { - return errors.New("validation-error: field AffinityLabel can not be empty") - } - - return nil + AffinityLabel string `url:"affinityLabel" json:"affinityLabel" validate:"required"` } // AffinityGroupCheckStart check all computes with current affinity label can start func (c Compute) AffinityGroupCheckStart(ctx context.Context, req AffinityGroupCheckStartRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/compute/affinityGroupCheckStart" diff --git a/pkg/cloudapi/compute/affinity_label_remove.go b/pkg/cloudapi/compute/affinity_label_remove.go index ad6db3a..58a10bf 100644 --- a/pkg/cloudapi/compute/affinity_label_remove.go +++ b/pkg/cloudapi/compute/affinity_label_remove.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for clear affinity label for compute type AffinityLabelRemoveRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq AffinityLabelRemoveRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // AffinityLabelRemove clear affinity label for compute func (c Compute) AffinityLabelRemove(ctx context.Context, req AffinityLabelRemoveRequest) (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/compute/affinityLabelRemove" diff --git a/pkg/cloudapi/compute/affinity_label_set.go b/pkg/cloudapi/compute/affinity_label_set.go index 2ab6cec..f30c571 100644 --- a/pkg/cloudapi/compute/affinity_label_set.go +++ b/pkg/cloudapi/compute/affinity_label_set.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set affinity label for compute type AffinityLabelSetRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Affinity group label // Required: true - AffinityLabel string `url:"affinityLabel" json:"affinityLabel"` -} - -func (crq AffinityLabelSetRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.AffinityLabel == "" { - return errors.New("validation-error: field AffinityLabel can not be empty") - } - - return nil + AffinityLabel string `url:"affinityLabel" json:"affinityLabel" validate:"required"` } // AffinityLabelSet set affinity label for compute func (c Compute) AffinityLabelSet(ctx context.Context, req AffinityLabelSetRequest) (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/compute/affinityLabelSet" diff --git a/pkg/cloudapi/compute/affinity_relations.go b/pkg/cloudapi/compute/affinity_relations.go index f1ae0db..46a7b85 100644 --- a/pkg/cloudapi/compute/affinity_relations.go +++ b/pkg/cloudapi/compute/affinity_relations.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get dict of computes type AffinityRelationsRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq AffinityRelationsRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // AffinityRelations gets dict of computes divided by affinity and anti affinity rules func (c Compute) AffinityRelations(ctx context.Context, req AffinityRelationsRequest) (*RecordAffinityRelations, 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/compute/affinityRelations" diff --git a/pkg/cloudapi/compute/affinity_rule_add.go b/pkg/cloudapi/compute/affinity_rule_add.go index d7fa5ca..cf1f0a6 100644 --- a/pkg/cloudapi/compute/affinity_rule_add.go +++ b/pkg/cloudapi/compute/affinity_rule_add.go @@ -2,29 +2,28 @@ package compute 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 add affinity rule type AffinityRuleAddRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Compute or node, for whom rule applies // Required: true - Topology string `url:"topology" json:"topology"` + Topology string `url:"topology" json:"topology" validate:"computeTopology"` // The degree of 'strictness' of this rule // Should be one of: // - RECOMMENDED // - REQUIRED // Required: true - Policy string `url:"policy" json:"policy"` + Policy string `url:"policy" json:"policy" validate:"computePolicy"` // The comparison mode is 'value', recorded by the specified 'key' // Should be one of: @@ -32,57 +31,24 @@ type AffinityRuleAddRequest struct { // - EN // - ANY // Required: true - Mode string `url:"mode" json:"mode"` + Mode string `url:"mode" json:"mode" validate:"computeMode"` // Key that are taken into account when analyzing this rule will be identified // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Value that must match the key to be taken into account when analyzing this rule // Required: true - Value string `url:"value" json:"value"` -} - -func (crq AffinityRuleAddRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Topology == "" { - return errors.New("validation-error: field Topology can not be empty") - } - validator := validators.StringInSlice(crq.Topology, []string{"compute", "node"}) - if !validator { - return errors.New("validation-error: field Topology can be only compute or node") - } - if crq.Policy == "" { - return errors.New("validation-error: field Policy can not be empty") - } - validator = validators.StringInSlice(crq.Policy, []string{"RECOMMENDED", "REQUIRED"}) - if !validator { - return errors.New("validation-error: field Policy can be only RECOMMENDED or REQUIRED") - } - if crq.Mode == "" { - return errors.New("validation-error: field Mode can not be empty") - } - validator = validators.StringInSlice(crq.Mode, []string{"EQ", "NE", "ANY"}) - if !validator { - return errors.New("validation-error: field Mode can be only EQ, NE or ANY") - } - if crq.Key == "" { - return errors.New("validation-error: field Key can not be empty") - } - if crq.Value == "" { - return errors.New("validation-error: field Value can not be empty") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // AffinityRuleAdd add affinity rule func (c Compute) AffinityRuleAdd(ctx context.Context, req AffinityRuleAddRequest) (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/compute/affinityRuleAdd" diff --git a/pkg/cloudapi/compute/affinity_rule_remove.go b/pkg/cloudapi/compute/affinity_rule_remove.go index 8cfa915..74f149e 100644 --- a/pkg/cloudapi/compute/affinity_rule_remove.go +++ b/pkg/cloudapi/compute/affinity_rule_remove.go @@ -2,29 +2,28 @@ package compute 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 remove affinity rule type AffinityRuleRemoveRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Compute or node, for whom rule applies // Required: true - Topology string `url:"topology" json:"topology"` + Topology string `url:"topology" json:"topology" validate:"computeTopology"` // The degree of 'strictness' of this rule // Should be one of: // - RECOMMENDED // - REQUIRED // Required: true - Policy string `url:"policy" json:"policy"` + Policy string `url:"policy" json:"policy" validate:"computePolicy"` // The comparison mode is 'value', recorded by the specified 'key' // Should be one of: @@ -32,57 +31,24 @@ type AffinityRuleRemoveRequest struct { // - EN // - ANY // Required: true - Mode string `url:"mode" json:"mode"` + Mode string `url:"mode" json:"mode" validate:"computeMode"` // Key that are taken into account when analyzing this rule will be identified // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Value that must match the key to be taken into account when analyzing this rule // Required: true - Value string `url:"value" json:"value"` -} - -func (crq AffinityRuleRemoveRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Topology == "" { - return errors.New("validation-error: field Topology can not be empty") - } - validator := validators.StringInSlice(crq.Topology, []string{"compute", "node"}) - if !validator { - return errors.New("validation-error: field Topology can be only compute or node") - } - if crq.Policy == "" { - return errors.New("validation-error: field Policy can not be empty") - } - validator = validators.StringInSlice(crq.Policy, []string{"RECOMMENDED", "REQUIRED"}) - if !validator { - return errors.New("validation-error: field Policy can be only RECOMMENDED or REQUIRED") - } - if crq.Mode == "" { - return errors.New("validation-error: field Mode can not be empty") - } - validator = validators.StringInSlice(crq.Mode, []string{"EQ", "NE", "ANY"}) - if !validator { - return errors.New("validation-error: field Mode can be only EQ, NE or ANY") - } - if crq.Key == "" { - return errors.New("validation-error: field Key can not be empty") - } - if crq.Value == "" { - return errors.New("validation-error: field Value can not be empty") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // AffinityRuleRemove remove affinity rule func (c Compute) AffinityRuleRemove(ctx context.Context, req AffinityRuleRemoveRequest) (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/compute/affinityRuleRemove" diff --git a/pkg/cloudapi/compute/affinity_rules_clear.go b/pkg/cloudapi/compute/affinity_rules_clear.go index 9bfba9e..64aa042 100644 --- a/pkg/cloudapi/compute/affinity_rules_clear.go +++ b/pkg/cloudapi/compute/affinity_rules_clear.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for clear affinity rules type AffinityRulesClearRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq AffinityRulesClearRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // AffinityRulesClear clear affinity rules func (c Compute) AffinityRulesClear(ctx context.Context, req AffinityRulesClearRequest) (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/compute/affinityRulesClear" diff --git a/pkg/cloudapi/compute/anti_affinity_rule_add.go b/pkg/cloudapi/compute/anti_affinity_rule_add.go index 776f4c6..fe11c58 100644 --- a/pkg/cloudapi/compute/anti_affinity_rule_add.go +++ b/pkg/cloudapi/compute/anti_affinity_rule_add.go @@ -2,29 +2,28 @@ package compute 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 add anti affinity rule type AntiAffinityRuleAddRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Compute or node, for whom rule applies // Required: true - Topology string `url:"topology" json:"topology"` + Topology string `url:"topology" json:"topology" validate:"computeTopology"` // The degree of 'strictness' of this rule // Should be one of: // - RECOMMENDED // - REQUIRED // Required: true - Policy string `url:"policy" json:"policy"` + Policy string `url:"policy" json:"policy" validate:"computePolicy"` // The comparison mode is 'value', recorded by the specified 'key' // Should be one of: @@ -32,57 +31,24 @@ type AntiAffinityRuleAddRequest struct { // - EN // - ANY // Required: true - Mode string `url:"mode" json:"mode"` + Mode string `url:"mode" json:"mode" validate:"computeMode"` // Key that are taken into account when analyzing this rule will be identified // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Value that must match the key to be taken into account when analyzing this rule // Required: true - Value string `url:"value" json:"value"` -} - -func (crq AntiAffinityRuleAddRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Topology == "" { - return errors.New("validation-error: field Topology can not be empty") - } - validator := validators.StringInSlice(crq.Topology, []string{"compute", "node"}) - if !validator { - return errors.New("validation-error: field Topology can be only compute or node") - } - if crq.Policy == "" { - return errors.New("validation-error: field Policy can not be empty") - } - validator = validators.StringInSlice(crq.Policy, []string{"RECOMMENDED", "REQUIRED"}) - if !validator { - return errors.New("validation-error: field Policy can be only RECOMMENDED or REQUIRED") - } - if crq.Mode == "" { - return errors.New("validation-error: field Mode can not be empty") - } - validator = validators.StringInSlice(crq.Mode, []string{"EQ", "NE", "ANY"}) - if !validator { - return errors.New("validation-error: field Mode can be only EQ, NE or ANY") - } - if crq.Key == "" { - return errors.New("validation-error: field Key can not be empty") - } - if crq.Value == "" { - return errors.New("validation-error: field Value can not be empty") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // AntiAffinityRuleAdd add anti affinity rule func (c Compute) AntiAffinityRuleAdd(ctx context.Context, req AntiAffinityRuleAddRequest) (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/compute/antiAffinityRuleAdd" diff --git a/pkg/cloudapi/compute/anti_affinity_rule_remove.go b/pkg/cloudapi/compute/anti_affinity_rule_remove.go index 1b84244..f54cf1d 100644 --- a/pkg/cloudapi/compute/anti_affinity_rule_remove.go +++ b/pkg/cloudapi/compute/anti_affinity_rule_remove.go @@ -2,29 +2,28 @@ package compute 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 remove anti affinity rule type AntiAffinityRuleRemoveRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Compute or node, for whom rule applies // Required: true - Topology string `url:"topology" json:"topology"` + Topology string `url:"topology" json:"topology" validate:"computeTopology"` // The degree of 'strictness' of this rule // Should be one of: // - RECOMMENDED // - REQUIRED // Required: true - Policy string `url:"policy" json:"policy"` + Policy string `url:"policy" json:"policy" validate:"computePolicy"` // The comparison mode is 'value', recorded by the specified 'key' // Should be one of: @@ -32,57 +31,24 @@ type AntiAffinityRuleRemoveRequest struct { // - EN // - ANY // Required: true - Mode string `url:"mode" json:"mode"` + Mode string `url:"mode" json:"mode" validate:"computeMode"` // Key that are taken into account when analyzing this rule will be identified // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Value that must match the key to be taken into account when analyzing this rule // Required: true - Value string `url:"value" json:"value"` -} - -func (crq AntiAffinityRuleRemoveRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Topology == "" { - return errors.New("validation-error: field Topology can not be empty") - } - validator := validators.StringInSlice(crq.Topology, []string{"compute", "node"}) - if !validator { - return errors.New("validation-error: field Topology can be only compute or node") - } - if crq.Policy == "" { - return errors.New("validation-error: field Policy can not be empty") - } - validator = validators.StringInSlice(crq.Policy, []string{"RECOMMENDED", "REQUIRED"}) - if !validator { - return errors.New("validation-error: field Policy can be only RECOMMENDED or REQUIRED") - } - if crq.Mode == "" { - return errors.New("validation-error: field Mode can not be empty") - } - validator = validators.StringInSlice(crq.Mode, []string{"EQ", "NE", "ANY"}) - if !validator { - return errors.New("validation-error: field Mode can be only EQ, NE or ANY") - } - if crq.Key == "" { - return errors.New("validation-error: field Key can not be empty") - } - if crq.Value == "" { - return errors.New("validation-error: field Value can not be empty") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // AntiAffinityRuleRemove remove anti affinity rule func (c Compute) AntiAffinityRuleRemove(ctx context.Context, req AntiAffinityRuleRemoveRequest) (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/compute/antiAffinityRuleRemove" diff --git a/pkg/cloudapi/compute/anti_affinity_rules_clear.go b/pkg/cloudapi/compute/anti_affinity_rules_clear.go index 80283cd..857b2a9 100644 --- a/pkg/cloudapi/compute/anti_affinity_rules_clear.go +++ b/pkg/cloudapi/compute/anti_affinity_rules_clear.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for clear anti affinity rules type AntiAffinityRulesClearRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq AntiAffinityRulesClearRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // AntiAffinityRulesClear clear anti affinity rules func (c Compute) AntiAffinityRulesClear(ctx context.Context, req AntiAffinityRulesClearRequest) (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/compute/antiAffinityRulesClear" diff --git a/pkg/cloudapi/compute/attach_gpu.go b/pkg/cloudapi/compute/attach_gpu.go index 449e6bc..b07eb33 100644 --- a/pkg/cloudapi/compute/attach_gpu.go +++ b/pkg/cloudapi/compute/attach_gpu.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for attach GPU for compute type AttachGPURequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Identifier vGPU // Required: true - VGPUID uint64 `url:"vgpuId" json:"vgpuId"` -} - -func (crq AttachGPURequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.VGPUID == 0 { - return errors.New("validation-error: field VGPUID can not be empty or equal to 0") - } - - return nil + VGPUID uint64 `url:"vgpuId" json:"vgpuId" validate:"required"` } // AttachGPU attach GPU for compute, returns vgpu id on success func (c Compute) AttachGPU(ctx context.Context, req AttachGPURequest) (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/compute/attachGpu" diff --git a/pkg/cloudapi/compute/attach_pci_device.go b/pkg/cloudapi/compute/attach_pci_device.go index 421335f..19b2305 100644 --- a/pkg/cloudapi/compute/attach_pci_device.go +++ b/pkg/cloudapi/compute/attach_pci_device.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for atttach PCI device type AttachPCIDeviceRequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // PCI device ID // Required: true - DeviceID uint64 `url:"deviceId" json:"deviceId"` -} - -func (crq AttachPCIDeviceRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.DeviceID == 0 { - return errors.New("validation-error: field DeviceID can not be empty or equal to 0") - } - - return nil + DeviceID uint64 `url:"deviceId" json:"deviceId" validate:"required"` } // AttachPCIDevice attach PCI device func (c Compute) AttachPCIDevice(ctx context.Context, req AttachPCIDeviceRequest) (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/compute/attachPciDevice" diff --git a/pkg/cloudapi/compute/audits.go b/pkg/cloudapi/compute/audits.go index 408327e..23b9c06 100644 --- a/pkg/cloudapi/compute/audits.go +++ b/pkg/cloudapi/compute/audits.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get audit records type AuditsRequest struct { // ID of the compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq AuditsRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Audits gets audit records for the specified compute object func (c Compute) Audits(ctx context.Context, req AuditsRequest) (ListAudits, 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/compute/audits" diff --git a/pkg/cloudapi/compute/cd_eject.go b/pkg/cloudapi/compute/cd_eject.go index 31186b0..6f0d054 100644 --- a/pkg/cloudapi/compute/cd_eject.go +++ b/pkg/cloudapi/compute/cd_eject.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for eject CD image type CDEjectRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq CDEjectRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // CDEject eject CD image to compute's CD-ROM func (c Compute) CDEject(ctx context.Context, req CDEjectRequest) (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/compute/cdEject" diff --git a/pkg/cloudapi/compute/cd_insert.go b/pkg/cloudapi/compute/cd_insert.go index 93756bd..149e23f 100644 --- a/pkg/cloudapi/compute/cd_insert.go +++ b/pkg/cloudapi/compute/cd_insert.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for insert new CD image type CDInsertRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of CD-ROM image // Required: true - CDROMID uint64 `url:"cdromId" json:"cdromId"` -} - -func (crq CDInsertRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.CDROMID == 0 { - return errors.New("validation-error: field CDROMID can not be empty or equal to 0") - } - - return nil + CDROMID uint64 `url:"cdromId" json:"cdromId" validate:"required"` } // CDInsert insert new CD image to compute's CD-ROM func (c Compute) CDInsert(ctx context.Context, req CDInsertRequest) (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/compute/cdInsert" diff --git a/pkg/cloudapi/compute/clone.go b/pkg/cloudapi/compute/clone.go index 4b0d8aa..955c4a4 100644 --- a/pkg/cloudapi/compute/clone.go +++ b/pkg/cloudapi/compute/clone.go @@ -2,46 +2,38 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for clone compute instance type CloneRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name of the clone // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Timestamp of the parent's snapshot to create clone from // Required: false - SnapshotTimestamp uint64 `url:"snapshotTimestamp" json:"snapshotTimestamp"` + SnapshotTimestamp uint64 `url:"snapshotTimestamp,omitempty" json:"snapshotTimestamp,omitempty"` // Name of the parent's snapshot to create clone from // Required: false - SnapshotName string `url:"snapshotName" json:"snapshotName"` -} - -func (crq CloneRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Name == "" { - return errors.New("validation-error: field Name can not be empty or equal to 0") - } - - return nil + SnapshotName string `url:"snapshotName,omitempty" json:"snapshotName,omitempty"` } // Clone clones compute instance func (c Compute) Clone(ctx context.Context, req CloneRequest) (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/compute/clone" diff --git a/pkg/cloudapi/compute/compute.go b/pkg/cloudapi/compute/compute.go index e0fcc9a..1bc0fe6 100644 --- a/pkg/cloudapi/compute/compute.go +++ b/pkg/cloudapi/compute/compute.go @@ -2,7 +2,7 @@ package compute import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to compute diff --git a/pkg/cloudapi/compute/create_template.go b/pkg/cloudapi/compute/create_template.go index 3c43951..428f630 100644 --- a/pkg/cloudapi/compute/create_template.go +++ b/pkg/cloudapi/compute/create_template.go @@ -2,21 +2,22 @@ package compute import ( "context" - "errors" "net/http" "strconv" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create template type CreateTemplateRequest struct { // ID of the compute to create template from // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name to assign to the template being created // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Async API call // For async call use CreateTemplateAsync @@ -25,22 +26,13 @@ type CreateTemplateRequest struct { async bool `url:"async"` } -func (crq CreateTemplateRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - - return nil -} - // CreateTemplate create template from compute instance func (c Compute) CreateTemplate(ctx context.Context, req CreateTemplateRequest) (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) + } } req.async = false @@ -62,9 +54,11 @@ func (c Compute) CreateTemplate(ctx context.Context, req CreateTemplateRequest) // CreateTemplateAsync create template from compute instance func (c Compute) CreateTemplateAsync(ctx context.Context, req CreateTemplateRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } req.async = true diff --git a/pkg/cloudapi/compute/delete.go b/pkg/cloudapi/compute/delete.go index 7e94e84..2dff1c9 100644 --- a/pkg/cloudapi/compute/delete.go +++ b/pkg/cloudapi/compute/delete.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete compute type DeleteRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Delete permanently // Required: false @@ -22,19 +23,13 @@ type DeleteRequest struct { DetachDisks bool `url:"detachDisks,omitempty" json:"detachDisks,omitempty"` } -func (crq DeleteRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // Delete deletes compute func (c Compute) 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/compute/delete" diff --git a/pkg/cloudapi/compute/detach_gpu.go b/pkg/cloudapi/compute/detach_gpu.go index f5a161e..e486d7b 100644 --- a/pkg/cloudapi/compute/detach_gpu.go +++ b/pkg/cloudapi/compute/detach_gpu.go @@ -2,36 +2,31 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach vgpu for compute type DetachGPURequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Identifier virtual GPU // Required: false VGPUID int64 `url:"vgpuId,omitempty" json:"vgpuId,omitempty"` } -func (crq DetachGPURequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // DetachGPU detach vgpu for compute. // If param vgpuid is equivalent -1, then detach all vgpu for compute func (c Compute) DetachGPU(ctx context.Context, req DetachGPURequest) (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/compute/detachGpu" diff --git a/pkg/cloudapi/compute/detach_pci_device.go b/pkg/cloudapi/compute/detach_pci_device.go index 37201b9..37c2be4 100644 --- a/pkg/cloudapi/compute/detach_pci_device.go +++ b/pkg/cloudapi/compute/detach_pci_device.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach PCI device type DetachPCIDeviceRequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Pci device ID // Required: true - DeviceID uint64 `url:"deviceId" json:"deviceId"` -} - -func (crq DetachPCIDeviceRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.DeviceID == 0 { - return errors.New("validation-error: field DeviceID can not be empty or equal to 0") - } - - return nil + DeviceID uint64 `url:"deviceId" json:"deviceId" validate:"required"` } // DetachPCIDevice detach PCI device func (c Compute) DetachPCIDevice(ctx context.Context, req DetachPCIDeviceRequest) (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/compute/detachPciDevice" diff --git a/pkg/cloudapi/compute/disable.go b/pkg/cloudapi/compute/disable.go index d6af32a..815201f 100644 --- a/pkg/cloudapi/compute/disable.go +++ b/pkg/cloudapi/compute/disable.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable compute type DisableRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq DisableRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Disable disables compute func (c Compute) Disable(ctx context.Context, req DisableRequest) (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/compute/disable" diff --git a/pkg/cloudapi/compute/disk_add.go b/pkg/cloudapi/compute/disk_add.go index 60e7ae4..45895ec 100644 --- a/pkg/cloudapi/compute/disk_add.go +++ b/pkg/cloudapi/compute/disk_add.go @@ -2,31 +2,32 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create and attach disk to compute type DiskAddRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name for disk // Required: true - DiskName string `url:"diskName" json:"diskName"` + DiskName string `url:"diskName" json:"diskName" validate:"required"` // Disk size in GB // Required: true - Size uint64 `url:"size" json:"size"` + Size uint64 `url:"size" json:"size" validate:"required"` // Type of the disk // Should be one of: // - D // - B // Required: false - DiskType string `url:"diskType,omitempty" json:"diskType,omitempty"` + DiskType string `url:"diskType,omitempty" json:"diskType,omitempty" validate:"omitempty,computeDiskType"` // Storage endpoint provider ID // By default the same with boot disk @@ -47,25 +48,13 @@ type DiskAddRequest struct { ImageID uint64 `url:"imageId,omitempty" json:"imageId,omitempty"` } -func (crq DiskAddRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Size == 0 { - return errors.New("validation-error: field Size can not be empty or equal to 0") - } - if crq.DiskName == "" { - return errors.New("validation-error: field DiskName can not be empty or equal to 0") - } - - return nil -} - // DiskAdd creates new disk and attach to compute func (c Compute) DiskAdd(ctx context.Context, req DiskAddRequest) (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/compute/diskAdd" diff --git a/pkg/cloudapi/compute/disk_attach.go b/pkg/cloudapi/compute/disk_attach.go index 7f5df46..150a8e1 100644 --- a/pkg/cloudapi/compute/disk_attach.go +++ b/pkg/cloudapi/compute/disk_attach.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for attach disk to compute type DiskAttachRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the disk to attach // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` -} - -func (crq DiskAttachRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - return nil + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` } // DiskAttach attach disk to compute func (c Compute) DiskAttach(ctx context.Context, req DiskAttachRequest) (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/compute/diskAttach" diff --git a/pkg/cloudapi/compute/disk_del.go b/pkg/cloudapi/compute/disk_del.go index aada288..14a8120 100644 --- a/pkg/cloudapi/compute/disk_del.go +++ b/pkg/cloudapi/compute/disk_del.go @@ -2,42 +2,34 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach and delete disk from compute type DiskDelRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of disk instance // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // False if disk is to be deleted to recycle bin // Required: true - Permanently bool `url:"permanently" json:"permanently"` -} - -func (crq DiskDelRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - return nil + Permanently bool `url:"permanently" json:"permanently" validate:"required"` } // DiskDel delete disk and detach from compute func (c Compute) DiskDel(ctx context.Context, req DiskDelRequest) (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/compute/diskDel" diff --git a/pkg/cloudapi/compute/disk_detach.go b/pkg/cloudapi/compute/disk_detach.go index 017cb60..4274599 100644 --- a/pkg/cloudapi/compute/disk_detach.go +++ b/pkg/cloudapi/compute/disk_detach.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach disk from compute type DiskDetachRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the disk to detach // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` -} - -func (crq DiskDetachRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - return nil + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` } // DiskDetach detach disk from compute func (c Compute) DiskDetach(ctx context.Context, req DiskDetachRequest) (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/compute/diskDetach" diff --git a/pkg/cloudapi/compute/disk_qos.go b/pkg/cloudapi/compute/disk_qos.go index c2fdb99..f6044f0 100644 --- a/pkg/cloudapi/compute/disk_qos.go +++ b/pkg/cloudapi/compute/disk_qos.go @@ -2,45 +2,34 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for change QoS of the disk type DiskQOSRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the disk to apply limits // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Limit IO for a certain disk total and read/write options are not allowed to be combined // Required: true - Limits string `url:"limits" json:"limits"` -} - -func (crq DiskQOSRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - if crq.Limits == "" { - return errors.New("validation-error: field Limits can not be empty") - } - - return nil + Limits string `url:"limits" json:"limits" validate:"required"` } // DiskQOS change QoS of the disk func (c Compute) DiskQOS(ctx context.Context, req DiskQOSRequest) (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/compute/diskQos" diff --git a/pkg/cloudapi/compute/disk_resize.go b/pkg/cloudapi/compute/disk_resize.go index f249a74..e04900a 100644 --- a/pkg/cloudapi/compute/disk_resize.go +++ b/pkg/cloudapi/compute/disk_resize.go @@ -2,45 +2,34 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for change disk size type DiskResizeRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the disk to resize // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // New disk size // Required: true - Size uint64 `url:"size" json:"size"` -} - -func (crq DiskResizeRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - if crq.Size == 0 { - return errors.New("validation-error: field Size can not be empty or equal to 0") - } - - return nil + Size uint64 `url:"size" json:"size" validate:"required"` } // DiskResize change disk size func (c Compute) DiskResize(ctx context.Context, req DiskResizeRequest) (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/compute/diskResize" diff --git a/pkg/cloudapi/compute/enable.go b/pkg/cloudapi/compute/enable.go index d731466..6d85bc8 100644 --- a/pkg/cloudapi/compute/enable.go +++ b/pkg/cloudapi/compute/enable.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable compute type EnableRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq EnableRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Enable enables compute func (c Compute) Enable(ctx context.Context, req EnableRequest) (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/compute/enable" diff --git a/pkg/cloudapi/compute/get.go b/pkg/cloudapi/compute/get.go index 3165118..8839aad 100644 --- a/pkg/cloudapi/compute/get.go +++ b/pkg/cloudapi/compute/get.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request for get information about compute type GetRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq GetRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Get Gets information about compute func (c Compute) Get(ctx context.Context, req GetRequest) (*RecordCompute, 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/compute/get" diff --git a/pkg/cloudapi/compute/get_audits.go b/pkg/cloudapi/compute/get_audits.go index ce953cd..4fd96f4 100644 --- a/pkg/cloudapi/compute/get_audits.go +++ b/pkg/cloudapi/compute/get_audits.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get compute audits type GetAuditsRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq GetAuditsRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // GetAudits gets compute audits func (c Compute) GetAudits(ctx context.Context, req GetAuditsRequest) (ListShortAudits, 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/compute/getAudits" diff --git a/pkg/cloudapi/compute/get_console_url.go b/pkg/cloudapi/compute/get_console_url.go index 9e27b05..d813fd6 100644 --- a/pkg/cloudapi/compute/get_console_url.go +++ b/pkg/cloudapi/compute/get_console_url.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get console URL type GetConsoleURLRequest struct { // ID of compute instance to get console for // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq GetConsoleURLRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // GetConsoleURL gets computes console URL func (c Compute) GetConsoleURL(ctx context.Context, req GetConsoleURLRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/compute/getConsoleUrl" diff --git a/pkg/cloudapi/compute/get_log.go b/pkg/cloudapi/compute/get_log.go index 6f04c3e..bdbe581 100644 --- a/pkg/cloudapi/compute/get_log.go +++ b/pkg/cloudapi/compute/get_log.go @@ -2,37 +2,29 @@ package compute import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get compute logs type GetLogRequest struct { // ID of compute instance to get log for // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Path to log file // Required: true - Path string `url:"path" json:"path"` -} - -func (crq GetLogRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Path == "" { - return errors.New("validation-error: field Path can not be empty") - } - - return nil + Path string `url:"path" json:"path" validate:"required"` } // GetLog gets compute's log file by path func (c Compute) GetLog(ctx context.Context, req GetLogRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/compute/getLog" @@ -47,9 +39,11 @@ func (c Compute) GetLog(ctx context.Context, req GetLogRequest) (string, error) // GetLogGet gets compute's log file by path func (c Compute) GetLogGet(ctx context.Context, req GetLogRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi//compute/getLog" diff --git a/pkg/cloudapi/compute/list_pci_device.go b/pkg/cloudapi/compute/list_pci_device.go index 3eb47bd..b713a6d 100644 --- a/pkg/cloudapi/compute/list_pci_device.go +++ b/pkg/cloudapi/compute/list_pci_device.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list PCI devices type ListPCIDeviceRequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq ListPCIDeviceRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // ListPCIDevice gets list PCI device func (c Compute) ListPCIDevice(ctx context.Context, req ListPCIDeviceRequest) ([]interface{}, 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/compute/listPciDevice" diff --git a/pkg/cloudapi/compute/list_vgpu.go b/pkg/cloudapi/compute/list_vgpu.go index 8c1f65b..b9a933e 100644 --- a/pkg/cloudapi/compute/list_vgpu.go +++ b/pkg/cloudapi/compute/list_vgpu.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list vGPU type ListVGPURequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq ListVGPURequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // ListVGPU gets list vGPU func (c Compute) ListVGPU(ctx context.Context, req ListVGPURequest) ([]interface{}, 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/compute/listVgpu" diff --git a/pkg/cloudapi/compute/move_to_rg.go b/pkg/cloudapi/compute/move_to_rg.go index c273c55..60fabbf 100644 --- a/pkg/cloudapi/compute/move_to_rg.go +++ b/pkg/cloudapi/compute/move_to_rg.go @@ -2,20 +2,21 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for move compute new resource group type MoveToRGRequest struct { // ID of the compute instance to move // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the target resource group // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // New name for the compute upon successful move, // if name change required. @@ -33,22 +34,13 @@ type MoveToRGRequest struct { ForceStop bool `url:"forceStop,omitempty" json:"forceStop,omitempty"` } -func (crq MoveToRGRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.RGID == 0 { - return errors.New("validation-error: field RGID can not be empty or equal to 0") - } - - return nil -} - // MoveToRG moves compute instance to new resource group func (c Compute) MoveToRG(ctx context.Context, req MoveToRGRequest) (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/compute/moveToRg" diff --git a/pkg/cloudapi/compute/net_attach.go b/pkg/cloudapi/compute/net_attach.go index 0b6236e..fe99583 100644 --- a/pkg/cloudapi/compute/net_attach.go +++ b/pkg/cloudapi/compute/net_attach.go @@ -3,58 +3,41 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for attach network type NetAttachRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Network type // 'EXTNET' for connect to external network directly // and 'VINS' for connect to ViNS // Required: true - NetType string `url:"netType" json:"netType"` + NetType string `url:"netType" json:"netType" validate:"computeNetType"` // Network ID for connect to // For EXTNET - external network ID // For VINS - VINS ID // Required: true - NetID uint64 `url:"netId" json:"netId"` + NetID uint64 `url:"netId" json:"netId" validate:"required"` // Directly required IP address for new network interface - // Required: true + // Required: false IPAddr string `url:"ipAddr,omitempty" json:"ipAddr,omitempty"` } -func (crq NetAttachRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.NetType == "" { - return errors.New("validation-error: field NetType can not be empty") - } - validator := validators.StringInSlice(crq.NetType, []string{"EXTNET", "VINS"}) - if !validator { - return errors.New("validation-error: field NetType can be only EXTNET or VINS") - } - if crq.NetID == 0 { - return errors.New("validation-error: field NetID can not be empty or equal to 0") - } - - return nil -} - // NetAttach attach network to compute and gets info about network func (c Compute) NetAttach(ctx context.Context, req NetAttachRequest) (*RecordNetAttach, 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/compute/netAttach" diff --git a/pkg/cloudapi/compute/net_detach.go b/pkg/cloudapi/compute/net_detach.go index 9367b37..acfc96d 100644 --- a/pkg/cloudapi/compute/net_detach.go +++ b/pkg/cloudapi/compute/net_detach.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach networ to compute type NetDetachRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // IP of the network interface // Required: false @@ -22,19 +23,13 @@ type NetDetachRequest struct { MAC string `url:"mac,omitempty" json:"mac,omitempty"` } -func (crq NetDetachRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // NetDetach detach network to compute func (c Compute) NetDetach(ctx context.Context, req NetDetachRequest) (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/compute/netDetach" diff --git a/pkg/cloudapi/compute/pause.go b/pkg/cloudapi/compute/pause.go index 82e5513..84ff546 100644 --- a/pkg/cloudapi/compute/pause.go +++ b/pkg/cloudapi/compute/pause.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for pause compute type PauseRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq PauseRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Pause pause compute func (c Compute) Pause(ctx context.Context, req PauseRequest) (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/compute/pause" diff --git a/pkg/cloudapi/compute/pfw_add.go b/pkg/cloudapi/compute/pfw_add.go index 9c2b4f4..bc42fdf 100644 --- a/pkg/cloudapi/compute/pfw_add.go +++ b/pkg/cloudapi/compute/pfw_add.go @@ -2,22 +2,21 @@ package compute 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 add port forward rule type PFWAddRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // External start port number for the rule // Required: true - PublicPortStart uint64 `url:"publicPortStart" json:"publicPortStart"` + PublicPortStart uint64 `url:"publicPortStart" json:"publicPortStart" validate:"required"` // End port number (inclusive) for the ranged rule // Required: false @@ -25,40 +24,21 @@ type PFWAddRequest struct { // Internal base port number // Required: true - LocalBasePort uint64 `url:"localBasePort" json:"localBasePort"` + LocalBasePort uint64 `url:"localBasePort" json:"localBasePort" validate:"required"` // Network protocol // either "tcp" or "udp" // Required: true - Proto string `url:"proto" json:"proto"` -} - -func (crq PFWAddRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.PublicPortStart == 0 { - return errors.New("validation-error: field PublicPortStart can not be empty or equal to 0") - } - if crq.LocalBasePort == 0 { - return errors.New("validation-error: field LocalBasePort can not be empty or equal to 0") - } - if crq.Proto == "" { - return errors.New("validation-error: field Proto can not be empty") - } - validator := validators.StringInSlice(crq.Proto, []string{"tcp", "udp"}) - if !validator { - return errors.New("validation-error: field Proto can be only tcp or udp") - } - - return nil + Proto string `url:"proto" json:"proto" validate:"computeProto"` } // PFWAdd add port forward rule func (c Compute) PFWAdd(ctx context.Context, req PFWAddRequest) (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/compute/pfwAdd" diff --git a/pkg/cloudapi/compute/pfw_del.go b/pkg/cloudapi/compute/pfw_del.go index ba28086..e899a6c 100644 --- a/pkg/cloudapi/compute/pfw_del.go +++ b/pkg/cloudapi/compute/pfw_del.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete port forward rule type PFWDelRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the rule to delete. If specified, all other arguments will be ignored // Required: false @@ -35,19 +36,13 @@ type PFWDelRequest struct { Proto string `url:"proto,omitempty" json:"proto,omitempty"` } -func (crq PFWDelRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // PFWDel delete port forward rule func (c Compute) PFWDel(ctx context.Context, req PFWDelRequest) (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/compute/pfwDel" diff --git a/pkg/cloudapi/compute/pfw_list.go b/pkg/cloudapi/compute/pfw_list.go index 805730f..ef21d23 100644 --- a/pkg/cloudapi/compute/pfw_list.go +++ b/pkg/cloudapi/compute/pfw_list.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list port forwards type PFWListRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq PFWListRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // PFWList gets compute port forwards list func (c Compute) PFWList(ctx context.Context, req PFWListRequest) (ListPFWs, 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/compute/pfwList" diff --git a/pkg/cloudapi/compute/pin_to_stack.go b/pkg/cloudapi/compute/pin_to_stack.go index b776b6c..3bbcfb6 100644 --- a/pkg/cloudapi/compute/pin_to_stack.go +++ b/pkg/cloudapi/compute/pin_to_stack.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for pin comptute to stack type PinToStackRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq PinToStackRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // PinToStack pin compute to current stack func (c Compute) PinToStack(ctx context.Context, req PinToStackRequest) (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/compute/pinToStack" diff --git a/pkg/cloudapi/compute/power_cycle.go b/pkg/cloudapi/compute/power_cycle.go index 110a791..ced6485 100644 --- a/pkg/cloudapi/compute/power_cycle.go +++ b/pkg/cloudapi/compute/power_cycle.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for force stop and start compute type PowerCycleRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq PowerCycleRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // PowerCycle makes force stop and start compute func (c Compute) PowerCycle(ctx context.Context, req PowerCycleRequest) (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/compute/powerCycle" diff --git a/pkg/cloudapi/compute/reboot.go b/pkg/cloudapi/compute/reboot.go index e32d301..223dcbe 100644 --- a/pkg/cloudapi/compute/reboot.go +++ b/pkg/cloudapi/compute/reboot.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reboot compute type RebootRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq RebootRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Reboot reboot compute func (c Compute) Reboot(ctx context.Context, req RebootRequest) (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/compute/reboot" diff --git a/pkg/cloudapi/compute/redeploy.go b/pkg/cloudapi/compute/redeploy.go index 3da1b3d..b993f1c 100644 --- a/pkg/cloudapi/compute/redeploy.go +++ b/pkg/cloudapi/compute/redeploy.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for redeploy type RedeployRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the new OS image, if image change is required // Required: false @@ -35,19 +36,13 @@ type RedeployRequest struct { ForceStop bool `url:"forceStop,omitempty" json:"forceStop,omitempty"` } -func (crq RedeployRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // Redeploy redeploy compute func (c Compute) Redeploy(ctx context.Context, req RedeployRequest) (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/compute/redeploy" diff --git a/pkg/cloudapi/compute/reset.go b/pkg/cloudapi/compute/reset.go index 90cf023..4a00dca 100644 --- a/pkg/cloudapi/compute/reset.go +++ b/pkg/cloudapi/compute/reset.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reset compute type ResetRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq ResetRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Reset reset compute func (c Compute) Reset(ctx context.Context, req ResetRequest) (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/compute/reset" diff --git a/pkg/cloudapi/compute/resize.go b/pkg/cloudapi/compute/resize.go index 7b82d12..76fe53b 100644 --- a/pkg/cloudapi/compute/resize.go +++ b/pkg/cloudapi/compute/resize.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for resize compute type ResizeRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // New CPU count. // Pass 0 if no change to CPU count is required @@ -28,19 +29,13 @@ type ResizeRequest struct { RAM uint64 `url:"ram,omitempty" json:"ram,omitempty"` } -func (crq ResizeRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // Resize resize compute instance func (c Compute) Resize(ctx context.Context, req ResizeRequest) (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/compute/resize" diff --git a/pkg/cloudapi/compute/restore.go b/pkg/cloudapi/compute/restore.go index 47dd2e0..f5bdfab 100644 --- a/pkg/cloudapi/compute/restore.go +++ b/pkg/cloudapi/compute/restore.go @@ -2,30 +2,25 @@ package compute import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore compute type RestoreRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq RestoreRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Restore restore compute from recycle bin func (c Compute) Restore(ctx context.Context, req RestoreRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/compute/restore" diff --git a/pkg/cloudapi/compute/resume.go b/pkg/cloudapi/compute/resume.go index 2a34a36..123e245 100644 --- a/pkg/cloudapi/compute/resume.go +++ b/pkg/cloudapi/compute/resume.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for resume compute type ResumeRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq ResumeRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Resume resume Compute from paused state func (c Compute) Resume(ctx context.Context, req ResumeRequest) (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/compute/resume" diff --git a/pkg/cloudapi/compute/serialize.go b/pkg/cloudapi/compute/serialize.go index fdd767b..31af2f2 100644 --- a/pkg/cloudapi/compute/serialize.go +++ b/pkg/cloudapi/compute/serialize.go @@ -3,7 +3,7 @@ package compute 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. diff --git a/pkg/cloudapi/compute/snapshot_create.go b/pkg/cloudapi/compute/snapshot_create.go index 693b9fb..06a4118 100644 --- a/pkg/cloudapi/compute/snapshot_create.go +++ b/pkg/cloudapi/compute/snapshot_create.go @@ -2,39 +2,31 @@ package compute import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create snapshot type SnapshotCreateRequest struct { // ID of the compute instance to create snapshot for // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Text label for snapshot. // Must be unique among this compute snapshots // Required: true - Label string `url:"label" json:"label"` -} - -func (crq SnapshotCreateRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Label == "" { - return errors.New("validation-error: field Label can not be empty") - } - - return nil + Label string `url:"label" json:"label" validate:"required"` } // SnapshotCreate create compute snapshot func (c Compute) SnapshotCreate(ctx context.Context, req SnapshotCreateRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/compute/snapshotCreate" diff --git a/pkg/cloudapi/compute/snapshot_delete.go b/pkg/cloudapi/compute/snapshot_delete.go index 733b273..7124235 100644 --- a/pkg/cloudapi/compute/snapshot_delete.go +++ b/pkg/cloudapi/compute/snapshot_delete.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete snapshot type SnapshotDeleteRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Text label of snapshot to delete // Required: true - Label string `url:"label" json:"label"` -} - -func (crq SnapshotDeleteRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Label == "" { - return errors.New("validation-error: field Label can not be empty") - } - - return nil + Label string `url:"label" json:"label" validate:"required"` } // SnapshotDelete delete specified compute snapshot func (c Compute) SnapshotDelete(ctx context.Context, req SnapshotDeleteRequest) (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/compute/snapshotDelete" diff --git a/pkg/cloudapi/compute/snapshot_list.go b/pkg/cloudapi/compute/snapshot_list.go index b759e3f..48dcc2b 100644 --- a/pkg/cloudapi/compute/snapshot_list.go +++ b/pkg/cloudapi/compute/snapshot_list.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list snapshots type SnapshotListRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq SnapshotListRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // SnapshotList gets list compute snapshots func (c Compute) SnapshotList(ctx context.Context, req SnapshotListRequest) (ListSnapShots, 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/compute/snapshotList" diff --git a/pkg/cloudapi/compute/snapshot_rollback.go b/pkg/cloudapi/compute/snapshot_rollback.go index e51700d..047e91f 100644 --- a/pkg/cloudapi/compute/snapshot_rollback.go +++ b/pkg/cloudapi/compute/snapshot_rollback.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for rollback type SnapshotRollbackRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Text label of snapshot to rollback // Required: true - Label string `url:"label" json:"label"` -} - -func (crq SnapshotRollbackRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Label == "" { - return errors.New("validation-error: field Label can not be empty") - } - - return nil + Label string `url:"label" json:"label" validate:"required"` } // SnapshotRollback rollback specified compute snapshot func (c Compute) SnapshotRollback(ctx context.Context, req SnapshotRollbackRequest) (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/compute/snapshotRollback" diff --git a/pkg/cloudapi/compute/snapshot_usage.go b/pkg/cloudapi/compute/snapshot_usage.go index c31f999..8761b62 100644 --- a/pkg/cloudapi/compute/snapshot_usage.go +++ b/pkg/cloudapi/compute/snapshot_usage.go @@ -3,15 +3,16 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get compute snapshot real size on storage type SnapshotUsageRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Specify to show usage exact for this snapshot. // Leave empty for get usage for all compute snapshots @@ -19,21 +20,15 @@ type SnapshotUsageRequest struct { Label string `url:"label,omitempty" json:"label,omitempty"` } -func (crq SnapshotUsageRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // SnapshotUsage Get compute snapshot real size on storage. // Always returns list of json objects, and first json object contains summary about all related // snapshots. func (c Compute) SnapshotUsage(ctx context.Context, req SnapshotUsageRequest) (ListUsageSnapshots, 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/compute/snapshotUsage" diff --git a/pkg/cloudapi/compute/start.go b/pkg/cloudapi/compute/start.go index 9f87d4e..aba5ff7 100644 --- a/pkg/cloudapi/compute/start.go +++ b/pkg/cloudapi/compute/start.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start compute type StartRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of CD-ROM live image to boot // Required: false AltBootID uint64 `url:"altBootId,omitempty" json:"altBootId,omitempty"` } -func (crq StartRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // Start starts compute func (c Compute) Start(ctx context.Context, req StartRequest) (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/compute/start" diff --git a/pkg/cloudapi/compute/stop.go b/pkg/cloudapi/compute/stop.go index f9b5f0f..e973890 100644 --- a/pkg/cloudapi/compute/stop.go +++ b/pkg/cloudapi/compute/stop.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for stop compute type StopRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Force stop compute // Required: false Force bool `url:"force,omitempty" json:"force,omitempty"` } -func (crq StopRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // Stop stops compute func (c Compute) Stop(ctx context.Context, req StopRequest) (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/compute/stop" diff --git a/pkg/cloudapi/compute/tag_add.go b/pkg/cloudapi/compute/tag_add.go index 03677c6..cc6d786 100644 --- a/pkg/cloudapi/compute/tag_add.go +++ b/pkg/cloudapi/compute/tag_add.go @@ -2,45 +2,34 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add tag to compute type TagAddRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Tag key // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Tag value // Required: true - Value string `url:"value" json:"value"` -} - -func (crq TagAddRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Key == "" { - return errors.New("validation-error: field Key can not be empty") - } - if crq.Value == "" { - return errors.New("validation-error: field Value can not be empty") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // TagAdd add tag to compute tags dict func (c Compute) TagAdd(ctx context.Context, req TagAddRequest) (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/compute/tagAdd" diff --git a/pkg/cloudapi/compute/tag_remove.go b/pkg/cloudapi/compute/tag_remove.go index 1643809..e12496e 100644 --- a/pkg/cloudapi/compute/tag_remove.go +++ b/pkg/cloudapi/compute/tag_remove.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove tag from compute type TagRemoveRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Tag key // Required: true - Key string `url:"key" json:"key"` -} - -func (crq TagRemoveRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Key == "" { - return errors.New("validation-error: field Key can not be empty") - } - - return nil + Key string `url:"key" json:"key" validate:"required"` } // TagRemove removes tag from compute tags dict func (c Compute) TagRemove(ctx context.Context, req TagRemoveRequest) (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/compute/tagRemove" diff --git a/pkg/cloudapi/compute/unpin_from_stack.go b/pkg/cloudapi/compute/unpin_from_stack.go index c60b1ba..9907666 100644 --- a/pkg/cloudapi/compute/unpin_from_stack.go +++ b/pkg/cloudapi/compute/unpin_from_stack.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for unpin from stack type UnpinFromStackRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq UnpinFromStackRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // UnpinFromStack unpin compute from current stack func (c Compute) UnpinFromStack(ctx context.Context, req UnpinFromStackRequest) (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/compute/unpinFromStack" diff --git a/pkg/cloudapi/compute/update.go b/pkg/cloudapi/compute/update.go index 1ea6b33..67fd8dc 100644 --- a/pkg/cloudapi/compute/update.go +++ b/pkg/cloudapi/compute/update.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update compute type UpdateRequest struct { // ID of the compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // New name // Required: false @@ -22,19 +23,13 @@ type UpdateRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (crq UpdateRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // Update updates some properties of the compute func (c Compute) Update(ctx context.Context, req UpdateRequest) (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/compute/update" diff --git a/pkg/cloudapi/compute/user_grant.go b/pkg/cloudapi/compute/user_grant.go index 114accc..37152f9 100644 --- a/pkg/cloudapi/compute/user_grant.go +++ b/pkg/cloudapi/compute/user_grant.go @@ -2,22 +2,21 @@ package compute 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 grant access to compute type UserGrantRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name of the user to add // Required: true - Username string `url:"userName" json:"userName"` + Username string `url:"userName" json:"userName" validate:"required"` // Access type // Should be one of: @@ -25,32 +24,16 @@ type UserGrantRequest struct { // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype"` -} - -func (crq UserGrantRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Username == "" { - return errors.New("validation-error: field UserName can not be empty") - } - if crq.AccessType == "" { - return errors.New("validation-error: field AccessType can not be empty") - } - validator := validators.StringInSlice(crq.AccessType, []string{"R", "RCX", "ARCXDU"}) - if !validator { - return errors.New("validation-error: field AccessType can be only R, RCX or ARCXDU") - } - - return nil + AccessType string `url:"accesstype" json:"accesstype" validate:"accountAccessType"` } // UserGrant grant user access to the compute func (c Compute) UserGrant(ctx context.Context, req UserGrantRequest) (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/compute/userGrant" diff --git a/pkg/cloudapi/compute/user_list.go b/pkg/cloudapi/compute/user_list.go index a97cedc..5028f20 100644 --- a/pkg/cloudapi/compute/user_list.go +++ b/pkg/cloudapi/compute/user_list.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list users for compute type UserListRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq UserListRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // UserList gets users list for compute func (c Compute) UserList(ctx context.Context, req UserListRequest) (*RecordACL, 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/compute/userList" diff --git a/pkg/cloudapi/compute/user_revoke.go b/pkg/cloudapi/compute/user_revoke.go index 39d46cd..3cd1076 100644 --- a/pkg/cloudapi/compute/user_revoke.go +++ b/pkg/cloudapi/compute/user_revoke.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for revoke user access type UserRevokeRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name of the user to remove // Required: true - Username string `url:"userName" json:"userName"` -} - -func (crq UserRevokeRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Username == "" { - return errors.New("validation-error: field UserName can not be empty") - } - - return nil + Username string `url:"userName" json:"userName" validate:"required"` } // UserRevoke revokes user access to the compute func (c Compute) UserRevoke(ctx context.Context, req UserRevokeRequest) (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/compute/userRevoke" diff --git a/pkg/cloudapi/compute/user_update.go b/pkg/cloudapi/compute/user_update.go index 5097dc7..251e995 100644 --- a/pkg/cloudapi/compute/user_update.go +++ b/pkg/cloudapi/compute/user_update.go @@ -2,22 +2,21 @@ package compute 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 update user access type UserUpdateRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name of the user to update // Required: true - Username string `url:"userName" json:"userName"` + Username string `url:"userName" json:"userName" validate:"required"` // Access type // Should be one of: @@ -25,32 +24,16 @@ type UserUpdateRequest struct { // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype"` -} - -func (crq UserUpdateRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - if crq.Username == "" { - return errors.New("validation-error: field UserName can not be empty") - } - if crq.AccessType == "" { - return errors.New("validation-error: field AccessType can not be empty") - } - validator := validators.StringInSlice(crq.AccessType, []string{"R", "RCX", "ARCXDU"}) - if !validator { - return errors.New("validation-error: field AccessType can be only R, RCX or ARCXDU") - } - - return nil + AccessType string `url:"accesstype" json:"accesstype" validate:"accountAccessType"` } // UserUpdate updates user access to the compute func (c Compute) UserUpdate(ctx context.Context, req UserUpdateRequest) (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/compute/userUpdate" diff --git a/pkg/cloudapi/computeci.go b/pkg/cloudapi/computeci.go index f76a128..1dd17d0 100644 --- a/pkg/cloudapi/computeci.go +++ b/pkg/cloudapi/computeci.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/computeci" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/computeci" ) // Accessing the ComputeCI method group diff --git a/pkg/cloudapi/computeci/computeci.go b/pkg/cloudapi/computeci/computeci.go index 3f20988..09d0a4e 100644 --- a/pkg/cloudapi/computeci/computeci.go +++ b/pkg/cloudapi/computeci/computeci.go @@ -2,7 +2,7 @@ package computeci import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to computeci diff --git a/pkg/cloudapi/computeci/filter_test.go b/pkg/cloudapi/computeci/filter_test.go new file mode 100644 index 0000000..804d228 --- /dev/null +++ b/pkg/cloudapi/computeci/filter_test.go @@ -0,0 +1,95 @@ +package computeci + +import "testing" + +var computeciItems = ListComputeCI{ + { + CustomFields: map[string]interface{}{}, + Description: "", + Drivers: []string{ + "KVM_X86", + }, + GUID: 1, + ID: 1, + Name: "computeci_1", + Status: "ENABLED", + Template: "", + }, + { + CustomFields: map[string]interface{}{}, + Description: "", + Drivers: []string{ + "KVM_X86", + }, + GUID: 2, + ID: 2, + Name: "computeci_2", + Status: "ENABLED", + Template: "", + }, + { + CustomFields: map[string]interface{}{}, + Description: "", + Drivers: []string{ + "SVA_KVM_X86", + }, + GUID: 3, + ID: 3, + Name: "computeci_3", + Status: "DISABLED", + Template: "", + }, +} + +func TestFilterByID(t *testing.T) { + actual := computeciItems.FilterByID(2).FindOne() + + if actual.ID != 2 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := computeciItems.FilterByName("computeci_3").FindOne() + + if actual.Name != "computeci_3" { + t.Fatal("expected Name 'computeci_2', found: ", actual.Name) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := computeciItems.FilterByStatus("ENABLED") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.Status != "ENABLED" { + t.Fatal("expected Status 'ENABLED', found: ", item.Status) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := computeciItems.FilterFunc(func(icc ItemComputeCI) bool { + for _, item := range icc.Drivers { + if item == "KVM_X86" { + return true + } + } + return false + }) + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + for _, driver := range item.Drivers { + if driver != "KVM_X86" { + t.Fatal("expected 'KVM_X86' Driver, found: ", driver) + } + } + } +} diff --git a/pkg/cloudapi/computeci/get.go b/pkg/cloudapi/computeci/get.go index 668e480..acbdc2b 100644 --- a/pkg/cloudapi/computeci/get.go +++ b/pkg/cloudapi/computeci/get.go @@ -3,30 +3,25 @@ package computeci import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for information about computeci type GetRequest struct { // ID of the Compute CI // Required: true - ComputeCIID uint64 `url:"computeciId" json:"computeciId"` -} - -func (krq GetRequest) validate() error { - if krq.ComputeCIID == 0 { - return errors.New("field ComputeCIID can not be empty or equal to 0") - } - - return nil + ComputeCIID uint64 `url:"computeciId" json:"computeciId" validate:"required"` } // Get gets information about computeci by ID func (c ComputeCI) Get(ctx context.Context, req GetRequest) (*ItemComputeCI, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validatonError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validatonError) + } } url := "/cloudapi/computeci/get" diff --git a/pkg/cloudapi/computeci/serialize.go b/pkg/cloudapi/computeci/serialize.go index ba63b05..d7621eb 100644 --- a/pkg/cloudapi/computeci/serialize.go +++ b/pkg/cloudapi/computeci/serialize.go @@ -3,7 +3,7 @@ package computeci 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. diff --git a/pkg/cloudapi/disks.go b/pkg/cloudapi/disks.go index 16671bb..936e10a 100644 --- a/pkg/cloudapi/disks.go +++ b/pkg/cloudapi/disks.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/disks" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/disks" ) // Accessing the Disks method group diff --git a/pkg/cloudapi/disks/create.go b/pkg/cloudapi/disks/create.go index cc0ad48..2831273 100644 --- a/pkg/cloudapi/disks/create.go +++ b/pkg/cloudapi/disks/create.go @@ -6,7 +6,7 @@ import ( "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 disk diff --git a/pkg/cloudapi/disks/disks.go b/pkg/cloudapi/disks/disks.go index d7682bb..0c22009 100644 --- a/pkg/cloudapi/disks/disks.go +++ b/pkg/cloudapi/disks/disks.go @@ -2,7 +2,7 @@ package disks import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to disks diff --git a/pkg/cloudapi/disks/serialize.go b/pkg/cloudapi/disks/serialize.go index 48d6f9b..2de9476 100644 --- a/pkg/cloudapi/disks/serialize.go +++ b/pkg/cloudapi/disks/serialize.go @@ -3,7 +3,7 @@ package disks 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. diff --git a/pkg/cloudapi/extnet.go b/pkg/cloudapi/extnet.go index 2d3819b..6b3a807 100644 --- a/pkg/cloudapi/extnet.go +++ b/pkg/cloudapi/extnet.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/extnet" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/extnet" ) // Accessing the ExtNet method group diff --git a/pkg/cloudapi/extnet/extnet.go b/pkg/cloudapi/extnet/extnet.go index 2470c7d..54db525 100644 --- a/pkg/cloudapi/extnet/extnet.go +++ b/pkg/cloudapi/extnet/extnet.go @@ -2,7 +2,7 @@ package extnet import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to extnet diff --git a/pkg/cloudapi/extnet/serialize.go b/pkg/cloudapi/extnet/serialize.go index d00f440..2505180 100644 --- a/pkg/cloudapi/extnet/serialize.go +++ b/pkg/cloudapi/extnet/serialize.go @@ -3,7 +3,7 @@ package extnet 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. diff --git a/pkg/cloudapi/flipgroup.go b/pkg/cloudapi/flipgroup.go index dc285cc..7ceb9c5 100644 --- a/pkg/cloudapi/flipgroup.go +++ b/pkg/cloudapi/flipgroup.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/flipgroup" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/flipgroup" ) // Accessing the FLIPGroup method group diff --git a/pkg/cloudapi/flipgroup/create.go b/pkg/cloudapi/flipgroup/create.go index 619f767..47f72e0 100644 --- a/pkg/cloudapi/flipgroup/create.go +++ b/pkg/cloudapi/flipgroup/create.go @@ -6,7 +6,7 @@ import ( "errors" "net/http" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create FLIPGroup diff --git a/pkg/cloudapi/flipgroup/filter_test.go b/pkg/cloudapi/flipgroup/filter_test.go new file mode 100644 index 0000000..af2697b --- /dev/null +++ b/pkg/cloudapi/flipgroup/filter_test.go @@ -0,0 +1,189 @@ +package flipgroup + +import "testing" + +var flipgroups = ListFLIPGroups{ + { + AccountID: 1, + AccountName: "std_1", + ClientIDs: []uint64{ + 1, + }, + ClientNames: []string{ + "compute_1", + }, + ClientType: "compute", + ConnID: 1, + ConnType: "", + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1234456789, + DefaultGW: "", + DeletedBy: "", + DeletedTime: 0, + Description: "", + GID: 212, + GUID: 1, + ID: 1, + IP: "", + Milestones: 999001, + Name: "flipgroup_1", + NetID: 111, + NetType: "EXTNET", + Network: "", + RGID: 7971, + RGName: "rg_1", + Status: "CREATED", + UpdatedBy: "", + UpdatedTime: 0, + }, + { + AccountID: 2, + AccountName: "std_2", + ClientIDs: []uint64{ + 2, + }, + ClientNames: []string{ + "compute_2", + }, + ClientType: "compute", + ConnID: 2, + ConnType: "", + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1234456830, + DefaultGW: "", + DeletedBy: "sample_user_1@decs3o", + DeletedTime: 1234456879, + Description: "", + GID: 212, + GUID: 2, + ID: 2, + IP: "", + Milestones: 999002, + Name: "flipgroup_2", + NetID: 222, + NetType: "EXTNET", + Network: "", + RGID: 7972, + RGName: "rg_2", + Status: "DESTROYED", + UpdatedBy: "", + UpdatedTime: 0, + }, + { + AccountID: 3, + AccountName: "std_3", + ClientIDs: []uint64{ + 3, + }, + ClientNames: []string{ + "compute_3", + }, + ClientType: "compute", + ConnID: 3, + ConnType: "", + CreatedBy: "sample_user_2@decs3o", + CreatedTime: 1234456866, + DefaultGW: "", + DeletedBy: "", + DeletedTime: 0, + Description: "", + GID: 212, + GUID: 3, + ID: 3, + IP: "", + Milestones: 999003, + Name: "flipgroup_3", + NetID: 223, + NetType: "EXTNET", + Network: "", + RGID: 7973, + RGName: "rg_3", + Status: "CREATED", + UpdatedBy: "", + UpdatedTime: 0, + }, +} + +func TestFilterByID(t *testing.T) { + actual := flipgroups.FilterByID(3).FindOne() + + if actual.ID != 3 { + t.Fatal("expected ID 3, found: ", actual.ID) + } +} + +func TestFilterByAccountID(t *testing.T) { + actual := flipgroups.FilterByAccountID(1).FindOne() + + if actual.AccountID != 1 { + t.Fatal("expected AccountID 1, found: ", actual.AccountID) + } +} + +func TestFilterByRGID(t *testing.T) { + actual := flipgroups.FilterByRGID(7972).FindOne() + + if actual.RGID != 7972 { + t.Fatal("expected RGID 7972, found: ", actual.RGID) + } +} + +func TestFilterByName(t *testing.T) { + actual := flipgroups.FilterByName("flipgroup_1").FindOne() + + if actual.Name != "flipgroup_1" { + t.Fatal("expected Name 'flipgroup_1', found: ", actual.Name) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := flipgroups.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 TestFilterByCreatedBy(t *testing.T) { + actual := flipgroups.FilterByCreatedBy("sample_user_1@decs3o") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.CreatedBy != "sample_user_1@decs3o" { + t.Fatal("expected CreatedBy 'sample_user_1@decs3o', found: ", item.CreatedBy) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := flipgroups.FilterFunc(func(ifg ItemFLIPGroup) bool { + return ifg.NetType == "EXTNET" + }) + + if len(actual) != 3 { + t.Fatal("expected 3 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.NetType != "EXTNET" { + t.Fatal("expected NetType 'EXTNET', found: ", item.NetType) + } + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := flipgroups.SortByCreatedTime(false) + + if actual[0].CreatedTime != 1234456789 || actual[2].CreatedTime != 1234456866 { + t.Fatal("expected ascending order, found descending") + } +} diff --git a/pkg/cloudapi/flipgroup/flipgroup.go b/pkg/cloudapi/flipgroup/flipgroup.go index 89184bf..2a51f46 100644 --- a/pkg/cloudapi/flipgroup/flipgroup.go +++ b/pkg/cloudapi/flipgroup/flipgroup.go @@ -2,7 +2,7 @@ package flipgroup import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to FLIPGroup diff --git a/pkg/cloudapi/flipgroup/serialize.go b/pkg/cloudapi/flipgroup/serialize.go index e540525..f3e0399 100644 --- a/pkg/cloudapi/flipgroup/serialize.go +++ b/pkg/cloudapi/flipgroup/serialize.go @@ -3,7 +3,7 @@ package flipgroup 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. diff --git a/pkg/cloudapi/image.go b/pkg/cloudapi/image.go index bd9389d..2ab2805 100644 --- a/pkg/cloudapi/image.go +++ b/pkg/cloudapi/image.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/image" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/image" ) // Accessing the Image method group diff --git a/pkg/cloudapi/image/create.go b/pkg/cloudapi/image/create.go index 6ade77e..9b4e02b 100644 --- a/pkg/cloudapi/image/create.go +++ b/pkg/cloudapi/image/create.go @@ -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" diff --git a/pkg/cloudapi/image/create_virtual.go b/pkg/cloudapi/image/create_virtual.go index a77c929..7141f70 100644 --- a/pkg/cloudapi/image/create_virtual.go +++ b/pkg/cloudapi/image/create_virtual.go @@ -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" diff --git a/pkg/cloudapi/image/delete.go b/pkg/cloudapi/image/delete.go index c604791..1ce5104 100644 --- a/pkg/cloudapi/image/delete.go +++ b/pkg/cloudapi/image/delete.go @@ -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" diff --git a/pkg/cloudapi/image/filter_test.go b/pkg/cloudapi/image/filter_test.go new file mode 100644 index 0000000..0725368 --- /dev/null +++ b/pkg/cloudapi/image/filter_test.go @@ -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") + } +} diff --git a/pkg/cloudapi/image/get.go b/pkg/cloudapi/image/get.go index 958ddb2..63024d7 100644 --- a/pkg/cloudapi/image/get.go +++ b/pkg/cloudapi/image/get.go @@ -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" diff --git a/pkg/cloudapi/image/image.go b/pkg/cloudapi/image/image.go index b70b34d..e724eee 100644 --- a/pkg/cloudapi/image/image.go +++ b/pkg/cloudapi/image/image.go @@ -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 diff --git a/pkg/cloudapi/image/link.go b/pkg/cloudapi/image/link.go index 4362a6c..c260dc7 100644 --- a/pkg/cloudapi/image/link.go +++ b/pkg/cloudapi/image/link.go @@ -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" diff --git a/pkg/cloudapi/image/rename.go b/pkg/cloudapi/image/rename.go index 220a61e..a6f4b10 100644 --- a/pkg/cloudapi/image/rename.go +++ b/pkg/cloudapi/image/rename.go @@ -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" diff --git a/pkg/cloudapi/image/serialize.go b/pkg/cloudapi/image/serialize.go index 92c635b..2c6c081 100644 --- a/pkg/cloudapi/image/serialize.go +++ b/pkg/cloudapi/image/serialize.go @@ -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. diff --git a/pkg/cloudapi/k8ci.go b/pkg/cloudapi/k8ci.go index 5a74641..b89a4c4 100644 --- a/pkg/cloudapi/k8ci.go +++ b/pkg/cloudapi/k8ci.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/k8ci" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8ci" ) // Accessing the K8CI method group diff --git a/pkg/cloudapi/k8ci/filter_test.go b/pkg/cloudapi/k8ci/filter_test.go new file mode 100644 index 0000000..6207612 --- /dev/null +++ b/pkg/cloudapi/k8ci/filter_test.go @@ -0,0 +1,73 @@ +package k8ci + +import "testing" + +var k8ciItems = ListK8CI{ + ItemK8CI{ + CreatedTime: 123902139, + RecordK8CI: RecordK8CI{ + Description: "", + ID: 1, + Name: "purple_snake", + Version: "1", + }, + }, + ItemK8CI{ + CreatedTime: 123902232, + RecordK8CI: RecordK8CI{ + Description: "", + ID: 2, + Name: "green_giant", + Version: "2", + }, + }, + ItemK8CI{ + CreatedTime: 123902335, + RecordK8CI: RecordK8CI{ + Description: "", + ID: 3, + Name: "magenta_cloud", + Version: "3", + }, + }, +} + +func TestFilterByID(t *testing.T) { + actual := k8ciItems.FilterByID(2).FindOne() + + if actual.ID != 2 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := k8ciItems.FilterByName("magenta_cloud").FindOne() + + if actual.Name != "magenta_cloud" { + t.Fatal("expected Name 'magenta_cloud', found: ", actual.Name) + } +} + +func TestFilterFunc(t *testing.T) { + actual := k8ciItems.FilterFunc(func(ikc ItemK8CI) bool { + return ikc.CreatedTime > 123902139 + }) + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.CreatedTime < 123902139 { + t.Fatal("expected CreatedTime greater than 123902139, found: ", item.CreatedTime) + } + } +} + +func TestSortingByCreatedTime(t *testing.T) { + actual := k8ciItems.SortByCreatedTime(true) + + if actual[0].CreatedTime != 123902335 && actual[2].CreatedTime != 123902139 { + t.Fatal("expected inverse sort, found normal") + } +} diff --git a/pkg/cloudapi/k8ci/k8ci.go b/pkg/cloudapi/k8ci/k8ci.go index 7eed52d..6119dc9 100644 --- a/pkg/cloudapi/k8ci/k8ci.go +++ b/pkg/cloudapi/k8ci/k8ci.go @@ -2,7 +2,7 @@ package k8ci import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to K8CI diff --git a/pkg/cloudapi/k8ci/serialize.go b/pkg/cloudapi/k8ci/serialize.go index 0fc31dd..01fadfa 100644 --- a/pkg/cloudapi/k8ci/serialize.go +++ b/pkg/cloudapi/k8ci/serialize.go @@ -3,7 +3,7 @@ package k8ci 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. diff --git a/pkg/cloudapi/k8s.go b/pkg/cloudapi/k8s.go index 7c8ebd8..b9564b1 100644 --- a/pkg/cloudapi/k8s.go +++ b/pkg/cloudapi/k8s.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/k8s" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s" ) // Accessing the K8S method group diff --git a/pkg/cloudapi/k8s/k8s.go b/pkg/cloudapi/k8s/k8s.go index 5c2711e..0ad159e 100644 --- a/pkg/cloudapi/k8s/k8s.go +++ b/pkg/cloudapi/k8s/k8s.go @@ -2,7 +2,7 @@ package k8s import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to K8S diff --git a/pkg/cloudapi/k8s/serialize.go b/pkg/cloudapi/k8s/serialize.go index c2ab881..8f62cb4 100644 --- a/pkg/cloudapi/k8s/serialize.go +++ b/pkg/cloudapi/k8s/serialize.go @@ -3,7 +3,7 @@ package k8s 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. diff --git a/pkg/cloudapi/kvmppc.go b/pkg/cloudapi/kvmppc.go index 9acbd25..c8c4e44 100644 --- a/pkg/cloudapi/kvmppc.go +++ b/pkg/cloudapi/kvmppc.go @@ -1,6 +1,6 @@ package cloudapi -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/kvmppc" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/kvmppc" // Accessing the KVMPPC method group func (ca *CloudAPI) KVMPPC() *kvmppc.KVMPPC { diff --git a/pkg/cloudapi/kvmppc/kvmppc.go b/pkg/cloudapi/kvmppc/kvmppc.go index 4b598d9..ff959f2 100644 --- a/pkg/cloudapi/kvmppc/kvmppc.go +++ b/pkg/cloudapi/kvmppc/kvmppc.go @@ -2,7 +2,7 @@ package kvmppc import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to KVMPPC diff --git a/pkg/cloudapi/kvmx86.go b/pkg/cloudapi/kvmx86.go index 5f10bb8..f0d725a 100644 --- a/pkg/cloudapi/kvmx86.go +++ b/pkg/cloudapi/kvmx86.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/kvmx86" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/kvmx86" ) // Accessing the KVMX86 method group diff --git a/pkg/cloudapi/kvmx86/kvmx86.go b/pkg/cloudapi/kvmx86/kvmx86.go index 297a5ed..8bf8257 100644 --- a/pkg/cloudapi/kvmx86/kvmx86.go +++ b/pkg/cloudapi/kvmx86/kvmx86.go @@ -2,7 +2,7 @@ package kvmx86 import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to KVMX86 diff --git a/pkg/cloudapi/lb.go b/pkg/cloudapi/lb.go index 597aa80..30d7900 100644 --- a/pkg/cloudapi/lb.go +++ b/pkg/cloudapi/lb.go @@ -1,6 +1,6 @@ package cloudapi -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/lb" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/lb" // Accessing the LB method group func (ca *CloudAPI) LB() *lb.LB { diff --git a/pkg/cloudapi/lb/filter_test.go b/pkg/cloudapi/lb/filter_test.go new file mode 100644 index 0000000..af9dbd2 --- /dev/null +++ b/pkg/cloudapi/lb/filter_test.go @@ -0,0 +1,145 @@ +package lb + +import "testing" + +var lbs = ListLB{ + ItemLoadBalancer{ + DPAPIPassword: "0000", + RecordLB: RecordLB{ + HAMode: true, + ACL: []interface{}{}, + Backends: []ItemBackend{}, + CreatedBy: "test_user_1", + CreatedTime: 1636667448, + DeletedBy: "", + DeletedTime: 0, + Description: "", + DPAPIUser: "api_user", + ExtNetID: 2522, + Frontends: []ItemFrontend{}, + GID: 212, + GUID: 1, + ID: 1, + ImageID: 2121, + Milestones: 129000, + Name: "k8s-lb-test-1", + RGID: 25090, + RGName: "", + Status: "ENABLED", + TechStatus: "STARTED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 101, + }, + }, + ItemLoadBalancer{ + DPAPIPassword: "0000", + RecordLB: RecordLB{ + HAMode: false, + ACL: []interface{}{}, + Backends: []ItemBackend{}, + CreatedBy: "test_user_2", + CreatedTime: 1636667506, + DeletedBy: "", + DeletedTime: 0, + Description: "", + DPAPIUser: "api_user_2", + ExtNetID: 2524, + Frontends: []ItemFrontend{}, + GID: 212, + GUID: 2, + ID: 2, + ImageID: 2129, + Milestones: 129013, + Name: "k8s-lb-test-2", + RGID: 25092, + RGName: "", + Status: "ENABLED", + TechStatus: "STOPPED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 102, + }, + }, + ItemLoadBalancer{ + DPAPIPassword: "0000", + RecordLB: RecordLB{ + HAMode: true, + ACL: []interface{}{}, + Backends: []ItemBackend{}, + CreatedBy: "te2t_user_3", + CreatedTime: 1636667534, + DeletedBy: "", + DeletedTime: 0, + Description: "", + DPAPIUser: "api_user_3", + ExtNetID: 2536, + Frontends: []ItemFrontend{}, + GID: 212, + GUID: 3, + ID: 3, + ImageID: 2139, + Milestones: 129025, + Name: "k8s-lb-test-3", + RGID: 25106, + RGName: "", + Status: "DISABLED", + TechStatus: "STOPPED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 118, + }, + }, +} + +func TestFilterByID(t *testing.T) { + actual := lbs.FilterByID(2).FindOne() + + if actual.ID != 2 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := lbs.FilterByName("k8s-lb-test-3").FindOne() + + if actual.Name != "k8s-lb-test-3" { + t.Fatal("expected Name 'k8s-lb-test-3', found: ", actual.Name) + } +} + +func TestFilterByExtNetID(t *testing.T) { + actual := lbs.FilterByExtNetID(2522).FindOne() + + if actual.ExtNetID != 2522 { + t.Fatal("expected ExtNetID 2522, found: ", actual.ExtNetID) + } +} + +func TestFilterByImageID(t *testing.T) { + actual := lbs.FilterByImageID(2139).FindOne() + + if actual.ImageID != 2139 { + t.Fatal("expected ImageID 2139, found: ", actual.ImageID) + } +} + +func TestFilterFunc(t *testing.T) { + actual := lbs.FilterFunc(func(rl ItemLoadBalancer) bool { + return rl.Status == "DISABLED" + }) + + for _, item := range actual { + if item.Status != "DISABLED" { + t.Fatal("expected Status 'DISABLED', found: ", item.Status) + } + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := lbs.SortByCreatedTime(true) + + if actual[0].CreatedTime != 1636667534 || actual[2].CreatedTime != 1636667448 { + t.Fatal("expected descending order, found ascending") + } +} diff --git a/pkg/cloudapi/lb/lb.go b/pkg/cloudapi/lb/lb.go index 0acdb21..cfe4917 100644 --- a/pkg/cloudapi/lb/lb.go +++ b/pkg/cloudapi/lb/lb.go @@ -2,7 +2,7 @@ package lb import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to load balancer diff --git a/pkg/cloudapi/lb/serialize.go b/pkg/cloudapi/lb/serialize.go index f1affb8..a4a8bee 100644 --- a/pkg/cloudapi/lb/serialize.go +++ b/pkg/cloudapi/lb/serialize.go @@ -3,7 +3,7 @@ package lb 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. diff --git a/pkg/cloudapi/locations/filter_test.go b/pkg/cloudapi/locations/filter_test.go new file mode 100644 index 0000000..a8d253d --- /dev/null +++ b/pkg/cloudapi/locations/filter_test.go @@ -0,0 +1,75 @@ +package locations + +import "testing" + +var locationItems = ListLocations{ + { + GID: 212, + ID: 1, + GUID: 1, + LocationCode: "alfa", + Name: "alfa", + Flag: "", + Meta: []interface{}{ + "cloudbroker", + "location", + 1, + }, + CKey: "", + }, + { + GID: 222, + ID: 2, + GUID: 2, + LocationCode: "beta", + Name: "beta", + Flag: "", + Meta: []interface{}{ + "cloudbroker", + "location", + 1, + }, + CKey: "", + }, + { + GID: 232, + ID: 3, + GUID: 3, + LocationCode: "gamma", + Name: "gamma", + Flag: "", + Meta: []interface{}{ + "cloudbroker", + "location", + 1, + }, + CKey: "", + }, +} + +func TestFilterByID(t *testing.T) { + actual := locationItems.FilterByID(1).FindOne() + + if actual.ID != 1 { + t.Fatal("expected ID 1, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := locationItems.FilterByName("gamma").FindOne() + + if actual.Name != "gamma" { + t.Fatal("expected Name 'gamma', found: ", actual.Name) + } +} + +func TestFilterFunc(t *testing.T) { + actual := locationItems.FilterFunc(func(il ItemLocation) bool { + return il.GID == 212 + }). + FindOne() + + if actual.GID != 212 { + t.Fatal("expected GID 212, found: ", actual.GID) + } +} diff --git a/pkg/cloudapi/locations/locations.go b/pkg/cloudapi/locations/locations.go index fca0904..556da43 100644 --- a/pkg/cloudapi/locations/locations.go +++ b/pkg/cloudapi/locations/locations.go @@ -2,7 +2,7 @@ package locations import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to locations diff --git a/pkg/cloudapi/locations/serialize.go b/pkg/cloudapi/locations/serialize.go index 49ce834..0ab0501 100644 --- a/pkg/cloudapi/locations/serialize.go +++ b/pkg/cloudapi/locations/serialize.go @@ -3,7 +3,7 @@ package locations 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. diff --git a/pkg/cloudapi/locatons.go b/pkg/cloudapi/locatons.go index 41c79d2..d97f7a5 100644 --- a/pkg/cloudapi/locatons.go +++ b/pkg/cloudapi/locatons.go @@ -1,6 +1,6 @@ package cloudapi -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/locations" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/locations" // Accessing the Locations method group func (ca *CloudAPI) Locations() *locations.Locations { diff --git a/pkg/cloudapi/rg.go b/pkg/cloudapi/rg.go index 9dbe177..c91aded 100644 --- a/pkg/cloudapi/rg.go +++ b/pkg/cloudapi/rg.go @@ -1,6 +1,6 @@ package cloudapi -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/rg" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/rg" // Accessing the RG method group func (ca *CloudAPI) RG() *rg.RG { diff --git a/pkg/cloudapi/rg/access_grant.go b/pkg/cloudapi/rg/access_grant.go index c3867f3..d0c5791 100644 --- a/pkg/cloudapi/rg/access_grant.go +++ b/pkg/cloudapi/rg/access_grant.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for grant access to resource group diff --git a/pkg/cloudapi/rg/filter_test.go b/pkg/cloudapi/rg/filter_test.go new file mode 100644 index 0000000..d2ba496 --- /dev/null +++ b/pkg/cloudapi/rg/filter_test.go @@ -0,0 +1,233 @@ +package rg + +import "testing" + +var rgs = ListResourceGroups{ + { + AccountID: 1, + AccountName: "std", + ACL: ListACL{ + { + Explicit: true, + GUID: "", + Right: "ARCXDU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "sample_user_1@decs3o", + }, + }, + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1676645305, + DefNetID: 1, + DefNetType: "NONE", + DeletedBy: "", + DeletedTime: 0, + Description: "", + GID: 212, + GUID: 7971, + ID: 7971, + LockStatus: "UNLOCKED", + Milestones: 363459, + Name: "rg_1", + RegisterComputes: false, + ResourceLimits: ResourceLimits{ + CUC: -1, + CUI: -1, + CUM: -1, + CUNP: -1, + GPUUnits: -1, + }, + Secret: "", + Status: "CREATED", + UpdatedBy: "", + UpdatedTime: 0, + VINS: []uint64{}, + Computes: []uint64{}, + ResTypes: []string{}, + UniqPools: []string{}, + }, + { + AccountID: 2, + AccountName: "std_2", + ACL: ListACL{ + { + Explicit: true, + GUID: "", + Right: "ARCXDU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "sample_user_1@decs3o", + }, + }, + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1676645461, + DefNetID: 2, + DefNetType: "NONE", + DeletedBy: "", + DeletedTime: 0, + Description: "", + GID: 212, + GUID: 7972, + ID: 7972, + LockStatus: "UNLOCKED", + Milestones: 363468, + Name: "rg_2", + RegisterComputes: false, + ResourceLimits: ResourceLimits{ + CUC: -1, + CUI: -1, + CUM: -1, + CUNP: -1, + GPUUnits: -1, + }, + Secret: "", + Status: "CREATED", + UpdatedBy: "", + UpdatedTime: 0, + VINS: []uint64{}, + Computes: []uint64{}, + ResTypes: []string{}, + UniqPools: []string{}, + }, + { + AccountID: 3, + AccountName: "std_3", + ACL: ListACL{ + { + Explicit: true, + GUID: "", + Right: "ARCXDU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "sample_user_2@decs3o", + }, + }, + CreatedBy: "sample_user_2@decs3o", + CreatedTime: 1676645548, + DefNetID: 3, + DefNetType: "NONE", + DeletedBy: "", + DeletedTime: 0, + Description: "", + GID: 212, + GUID: 7973, + ID: 7973, + LockStatus: "kjLOCKED", + Milestones: 363471, + Name: "rg_3", + RegisterComputes: false, + ResourceLimits: ResourceLimits{ + CUC: -1, + CUI: -1, + CUM: -1, + CUNP: -1, + GPUUnits: -1, + }, + Secret: "", + Status: "DISABLED", + UpdatedBy: "", + UpdatedTime: 0, + VINS: []uint64{}, + Computes: []uint64{ + 48500, + }, + ResTypes: []string{}, + UniqPools: []string{}, + }, +} + +func TestFilterByID(t *testing.T) { + actual := rgs.FilterByID(7972).FindOne() + + if actual.ID != 7972 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := rgs.FilterByName("rg_1").FindOne() + + if actual.Name != "rg_1" { + t.Fatal("expected Name 'rg_1', found: ", actual.Name) + } +} + +func TestFilterByCreatedBy(t *testing.T) { + actual := rgs.FilterByCreatedBy("sample_user_1@decs3o") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.CreatedBy != "sample_user_1@decs3o" { + t.Fatal("expected CreatedBy 'sample_user_1@decs3o', found: ", item.CreatedBy) + } + } +} + +func TestFilterByStatus(t *testing.T) { + actual := rgs.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 'ENABLED', found: ", item.Status) + } + } +} + +func TestFilterByLockStatus(t *testing.T) { + actual := rgs.FilterByLockStatus("UNLOCKED") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.LockStatus != "UNLOCKED" { + t.Fatal("expected LockStatus 'UNLOCKED', found: ", item.LockStatus) + } + } +} + +func TestFilterByDefNetType(t *testing.T) { + actual := rgs.FilterByDefNetType("NONE") + + if len(actual) != 3 { + t.Fatal("expected 3 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.DefNetType != "NONE" { + t.Fatal("expected DefNetType 'NONE', found: ", item.DefNetType) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := rgs.FilterFunc(func(ir ItemResourceGroup) bool { + return len(ir.Computes) > 0 + }) + + if len(actual) < 1 { + t.Fatal("expected 1 found, actual: ", len(actual)) + } + + for _, item := range actual { + if len(item.Computes) < 1 { + t.Fatal("expected VMs to contain at least 1 element, found empty") + } + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := rgs.SortByCreatedTime(true) + + if actual[0].CreatedTime != 1676645548 || actual[2].CreatedTime != 1676645305 { + t.Fatal("expected descending order, found ascending") + } +} diff --git a/pkg/cloudapi/rg/rg.go b/pkg/cloudapi/rg/rg.go index c247edf..03cb67e 100644 --- a/pkg/cloudapi/rg/rg.go +++ b/pkg/cloudapi/rg/rg.go @@ -2,7 +2,7 @@ package rg import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to resource group diff --git a/pkg/cloudapi/rg/serialize.go b/pkg/cloudapi/rg/serialize.go index 6291a27..070b022 100644 --- a/pkg/cloudapi/rg/serialize.go +++ b/pkg/cloudapi/rg/serialize.go @@ -3,7 +3,7 @@ package rg 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. diff --git a/pkg/cloudapi/rg/set_def_net.go b/pkg/cloudapi/rg/set_def_net.go index 32ad820..6702f07 100644 --- a/pkg/cloudapi/rg/set_def_net.go +++ b/pkg/cloudapi/rg/set_def_net.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set default network diff --git a/pkg/cloudapi/sizes.go b/pkg/cloudapi/sizes.go index 9e9e82b..945825c 100644 --- a/pkg/cloudapi/sizes.go +++ b/pkg/cloudapi/sizes.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/sizes" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/sizes" ) // Accessing the Sizes method group diff --git a/pkg/cloudapi/sizes/filter_test.go b/pkg/cloudapi/sizes/filter_test.go new file mode 100644 index 0000000..3a3682e --- /dev/null +++ b/pkg/cloudapi/sizes/filter_test.go @@ -0,0 +1,62 @@ +package sizes + +import "testing" + +var sizeItems = ListSizes{ + { + Description: "", + Disks: []uint64{}, + ID: 1, + Memory: 512, + Name: "size_1", + VCPUs: 2, + }, + { + Description: "", + Disks: []uint64{}, + ID: 2, + Memory: 1024, + Name: "size_2", + VCPUs: 4, + }, + { + Description: "", + Disks: []uint64{}, + ID: 2, + Memory: 2048, + Name: "size_3", + VCPUs: 6, + }, +} + +func TestFilterByID(t *testing.T) { + actual := sizeItems.FilterByID(1).FindOne() + + if actual.ID != 1 { + t.Fatal("expected ID 1, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := sizeItems.FilterByName("size_2").FindOne() + + if actual.Name != "size_2" { + t.Fatal("expected Name 'size_2', found: ", actual.Name) + } +} + +func TestFilterFunc(t *testing.T) { + actual := sizeItems.FilterFunc(func(is ItemSize) bool { + return is.Memory > 512 + }) + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.Memory <= 512 { + t.Fatal("expected Memory greater than 512, found: ", item.Memory) + } + } +} diff --git a/pkg/cloudapi/sizes/serialize.go b/pkg/cloudapi/sizes/serialize.go index dc791f3..6de747a 100644 --- a/pkg/cloudapi/sizes/serialize.go +++ b/pkg/cloudapi/sizes/serialize.go @@ -3,7 +3,7 @@ package sizes 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. diff --git a/pkg/cloudapi/sizes/sizes.go b/pkg/cloudapi/sizes/sizes.go index 5947c16..27b9750 100644 --- a/pkg/cloudapi/sizes/sizes.go +++ b/pkg/cloudapi/sizes/sizes.go @@ -3,7 +3,7 @@ package sizes import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creatig request to sizes diff --git a/pkg/cloudapi/tasks.go b/pkg/cloudapi/tasks.go index 92c808d..0058fc8 100644 --- a/pkg/cloudapi/tasks.go +++ b/pkg/cloudapi/tasks.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/tasks" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/tasks" ) // Accessing the Tasks method group diff --git a/pkg/cloudapi/tasks/tasks.go b/pkg/cloudapi/tasks/tasks.go index 805a1ee..1bd13fe 100644 --- a/pkg/cloudapi/tasks/tasks.go +++ b/pkg/cloudapi/tasks/tasks.go @@ -2,7 +2,7 @@ package tasks import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to tasks diff --git a/pkg/cloudapi/vins.go b/pkg/cloudapi/vins.go index 7435d34..d8eb1ad 100644 --- a/pkg/cloudapi/vins.go +++ b/pkg/cloudapi/vins.go @@ -1,7 +1,7 @@ package cloudapi import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/vins" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/vins" ) // Accessing the VINS method group diff --git a/pkg/cloudapi/vins/filter_test.go b/pkg/cloudapi/vins/filter_test.go new file mode 100644 index 0000000..579e463 --- /dev/null +++ b/pkg/cloudapi/vins/filter_test.go @@ -0,0 +1,117 @@ +package vins + +import "testing" + +var vinsItems = ListVINS{ + { + AccountID: 1, + AccountName: "std", + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1676898844, + DeletedBy: "", + DeletedTime: 0, + ExternalIP: "", + ID: 1, + Name: "vins01", + Network: "192.168.1.0/24", + RGID: 7971, + RGName: "rg_01", + Status: "ENABLED", + UpdatedBy: "", + UpdatedTime: 0, + VXLANID: 3544, + }, + { + AccountID: 2, + AccountName: "std2", + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1676898948, + DeletedBy: "", + DeletedTime: 0, + ExternalIP: "", + ID: 2, + Name: "vins02", + Network: "192.168.2.0/24", + RGID: 7972, + RGName: "rg_02", + Status: "ENABLED", + UpdatedBy: "", + UpdatedTime: 0, + VXLANID: 3545, + }, + { + AccountID: 3, + AccountName: "std3", + CreatedBy: "sample_user_2@decs3o", + CreatedTime: 1676899026, + DeletedBy: "", + DeletedTime: 0, + ExternalIP: "", + ID: 3, + Name: "vins03", + Network: "192.168.3.0/24", + RGID: 7973, + RGName: "rg_03", + Status: "DISABLED", + UpdatedBy: "", + UpdatedTime: 0, + VXLANID: 3546, + }, +} + +func TestFilterByID(t *testing.T) { + actual := vinsItems.FilterByID(2).FindOne() + + if actual.ID != 2 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := vinsItems.FilterByName("vins01").FindOne() + + if actual.Name != "vins01" { + t.Fatal("expected Name 'vins01', found: ", actual.Name) + } +} + +func TestFilterByAccountID(t *testing.T) { + actual := vinsItems.FilterByAccountID(3).FindOne() + + if actual.AccountID != 3 { + t.Fatal("expected AccountID 3, found: ", actual.AccountID) + } +} + +func TestFilterByCreatedBy(t *testing.T) { + actual := vinsItems.FilterByCreatedBy("sample_user_1@decs3o") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.CreatedBy != "sample_user_1@decs3o" { + t.Fatal("expected CreatedBy 'sample_user_1@decs3o', found: ", item.CreatedBy) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := vinsItems.FilterFunc(func(iv ItemVINS) bool { + return iv.RGID == 7971 + }). + FindOne() + + if actual.RGID != 7971 { + t.Fatal("expected RGID 7971, found: ", actual.RGID) + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := vinsItems.SortByCreatedTime(false) + + if actual[0].CreatedTime != 1676898844 || actual[2].CreatedTime != 1676899026 { + t.Fatal("expected ascending order, found descending") + } +} diff --git a/pkg/cloudapi/vins/ip_reserve.go b/pkg/cloudapi/vins/ip_reserve.go index da4486b..220cf45 100644 --- a/pkg/cloudapi/vins/ip_reserve.go +++ b/pkg/cloudapi/vins/ip_reserve.go @@ -5,7 +5,7 @@ import ( "errors" "net/http" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for IP reserve diff --git a/pkg/cloudapi/vins/serialize.go b/pkg/cloudapi/vins/serialize.go index 0a1c44e..7ff8242 100644 --- a/pkg/cloudapi/vins/serialize.go +++ b/pkg/cloudapi/vins/serialize.go @@ -3,7 +3,7 @@ package vins 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. diff --git a/pkg/cloudapi/vins/vins.go b/pkg/cloudapi/vins/vins.go index 0a52aa0..9ac4cb3 100644 --- a/pkg/cloudapi/vins/vins.go +++ b/pkg/cloudapi/vins/vins.go @@ -2,7 +2,7 @@ package vins import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to VINS diff --git a/pkg/cloudbroker/account.go b/pkg/cloudbroker/account.go index 404cf11..03efd4b 100644 --- a/pkg/cloudbroker/account.go +++ b/pkg/cloudbroker/account.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/account" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/account" ) // Accessing the Account method group diff --git a/pkg/cloudbroker/account/account.go b/pkg/cloudbroker/account/account.go index 41f9db6..de373a5 100644 --- a/pkg/cloudbroker/account/account.go +++ b/pkg/cloudbroker/account/account.go @@ -1,7 +1,7 @@ // API Actor API for managing account package account -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to account type Account struct { diff --git a/pkg/cloudbroker/account/add_user.go b/pkg/cloudbroker/account/add_user.go index 3030e0b..eb93eb1 100644 --- a/pkg/cloudbroker/account/add_user.go +++ b/pkg/cloudbroker/account/add_user.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for adding permission to access to account for a user diff --git a/pkg/cloudbroker/account/serialize.go b/pkg/cloudbroker/account/serialize.go index 6c2dac5..8ee25ca 100644 --- a/pkg/cloudbroker/account/serialize.go +++ b/pkg/cloudbroker/account/serialize.go @@ -3,7 +3,7 @@ package account 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. diff --git a/pkg/cloudbroker/account/update_resource_types.go b/pkg/cloudbroker/account/update_resource_types.go index b54d80c..57ca3ca 100644 --- a/pkg/cloudbroker/account/update_resource_types.go +++ b/pkg/cloudbroker/account/update_resource_types.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update resource types in account diff --git a/pkg/cloudbroker/cloudbroker.go b/pkg/cloudbroker/cloudbroker.go index 8799efe..b17eb90 100644 --- a/pkg/cloudbroker/cloudbroker.go +++ b/pkg/cloudbroker/cloudbroker.go @@ -1,7 +1,7 @@ // List of method groups for the admin package cloudbroker -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to CloudBroker groups type CloudBroker struct { diff --git a/pkg/cloudbroker/compute.go b/pkg/cloudbroker/compute.go index e86d5e5..e6171a5 100644 --- a/pkg/cloudbroker/compute.go +++ b/pkg/cloudbroker/compute.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/compute" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/compute" ) // Accessing the Compute method group diff --git a/pkg/cloudbroker/compute/affinity_rule_add.go b/pkg/cloudbroker/compute/affinity_rule_add.go index d63f0bd..d64efd1 100644 --- a/pkg/cloudbroker/compute/affinity_rule_add.go +++ b/pkg/cloudbroker/compute/affinity_rule_add.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add affinity rule diff --git a/pkg/cloudbroker/compute/affinity_rule_remove.go b/pkg/cloudbroker/compute/affinity_rule_remove.go index d55ef17..64edc11 100644 --- a/pkg/cloudbroker/compute/affinity_rule_remove.go +++ b/pkg/cloudbroker/compute/affinity_rule_remove.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove affinity rule diff --git a/pkg/cloudbroker/compute/anti_affinity_rule_add.go b/pkg/cloudbroker/compute/anti_affinity_rule_add.go index 9242fdf..0f9d290 100644 --- a/pkg/cloudbroker/compute/anti_affinity_rule_add.go +++ b/pkg/cloudbroker/compute/anti_affinity_rule_add.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add anti affinity rule diff --git a/pkg/cloudbroker/compute/anti_affinity_rule_remove.go b/pkg/cloudbroker/compute/anti_affinity_rule_remove.go index 17e1d69..e7a52d0 100644 --- a/pkg/cloudbroker/compute/anti_affinity_rule_remove.go +++ b/pkg/cloudbroker/compute/anti_affinity_rule_remove.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove anti affinity rule diff --git a/pkg/cloudbroker/compute/boot_order_set.go b/pkg/cloudbroker/compute/boot_order_set.go index bc6a2c7..2f1166f 100644 --- a/pkg/cloudbroker/compute/boot_order_set.go +++ b/pkg/cloudbroker/compute/boot_order_set.go @@ -6,7 +6,7 @@ import ( "errors" "net/http" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set boot order diff --git a/pkg/cloudbroker/compute/compute.go b/pkg/cloudbroker/compute/compute.go index d44d10f..e907336 100644 --- a/pkg/cloudbroker/compute/compute.go +++ b/pkg/cloudbroker/compute/compute.go @@ -1,7 +1,7 @@ // API Actor for managing Compute. This actor is a final API for admin to manage Compute package compute -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to compute type Compute struct { diff --git a/pkg/cloudbroker/compute/net_attach.go b/pkg/cloudbroker/compute/net_attach.go index a211e6e..75c611a 100644 --- a/pkg/cloudbroker/compute/net_attach.go +++ b/pkg/cloudbroker/compute/net_attach.go @@ -6,7 +6,7 @@ import ( "errors" "net/http" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for attach network diff --git a/pkg/cloudbroker/compute/net_qos.go b/pkg/cloudbroker/compute/net_qos.go index 350b50a..265c058 100644 --- a/pkg/cloudbroker/compute/net_qos.go +++ b/pkg/cloudbroker/compute/net_qos.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update QOS diff --git a/pkg/cloudbroker/compute/pfw_add.go b/pkg/cloudbroker/compute/pfw_add.go index 39ab6e7..ccbea22 100644 --- a/pkg/cloudbroker/compute/pfw_add.go +++ b/pkg/cloudbroker/compute/pfw_add.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add port forward rule diff --git a/pkg/cloudbroker/compute/serialize.go b/pkg/cloudbroker/compute/serialize.go index fdd767b..31af2f2 100644 --- a/pkg/cloudbroker/compute/serialize.go +++ b/pkg/cloudbroker/compute/serialize.go @@ -3,7 +3,7 @@ package compute 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. diff --git a/pkg/cloudbroker/compute/user_grant.go b/pkg/cloudbroker/compute/user_grant.go index 21cb566..f0eb976 100644 --- a/pkg/cloudbroker/compute/user_grant.go +++ b/pkg/cloudbroker/compute/user_grant.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for grant access to compute diff --git a/pkg/cloudbroker/compute/user_update.go b/pkg/cloudbroker/compute/user_update.go index 4e4b8dc..2f4c4d0 100644 --- a/pkg/cloudbroker/compute/user_update.go +++ b/pkg/cloudbroker/compute/user_update.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update user access diff --git a/pkg/cloudbroker/disks.go b/pkg/cloudbroker/disks.go index b42928c..cc17b16 100644 --- a/pkg/cloudbroker/disks.go +++ b/pkg/cloudbroker/disks.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/disks" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/disks" ) // Accessing the Disks method group diff --git a/pkg/cloudbroker/disks/create.go b/pkg/cloudbroker/disks/create.go index e963dc9..0f2df8f 100644 --- a/pkg/cloudbroker/disks/create.go +++ b/pkg/cloudbroker/disks/create.go @@ -6,7 +6,7 @@ import ( "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 disk diff --git a/pkg/cloudbroker/disks/disks.go b/pkg/cloudbroker/disks/disks.go index 7b12825..3624cf2 100644 --- a/pkg/cloudbroker/disks/disks.go +++ b/pkg/cloudbroker/disks/disks.go @@ -1,7 +1,7 @@ // API Actor for managing Disk. This actor is a final API for admin to manage Disk package disks -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to disks type Disks struct { diff --git a/pkg/cloudbroker/disks/serialize.go b/pkg/cloudbroker/disks/serialize.go index 48d6f9b..2de9476 100644 --- a/pkg/cloudbroker/disks/serialize.go +++ b/pkg/cloudbroker/disks/serialize.go @@ -3,7 +3,7 @@ package disks 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. diff --git a/pkg/cloudbroker/extnet.go b/pkg/cloudbroker/extnet.go index b6f7678..38e8e45 100644 --- a/pkg/cloudbroker/extnet.go +++ b/pkg/cloudbroker/extnet.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/extnet" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/extnet" ) // Accessing the ExtNet method group diff --git a/pkg/cloudbroker/extnet/extnet.go b/pkg/cloudbroker/extnet/extnet.go index b3a3b09..c114cba 100644 --- a/pkg/cloudbroker/extnet/extnet.go +++ b/pkg/cloudbroker/extnet/extnet.go @@ -1,7 +1,7 @@ // API Actor for configure and use external networks package extnet -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to extnet type ExtNet struct { diff --git a/pkg/cloudbroker/extnet/serialize.go b/pkg/cloudbroker/extnet/serialize.go index 1157e51..a80dd53 100644 --- a/pkg/cloudbroker/extnet/serialize.go +++ b/pkg/cloudbroker/extnet/serialize.go @@ -3,7 +3,7 @@ package extnet 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. diff --git a/pkg/cloudbroker/grid.go b/pkg/cloudbroker/grid.go index 68a5b73..a1c7a29 100644 --- a/pkg/cloudbroker/grid.go +++ b/pkg/cloudbroker/grid.go @@ -1,6 +1,6 @@ package cloudbroker -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/grid" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/grid" // Accessing the Grid method group func (cb *CloudBroker) Grid() *grid.Grid { diff --git a/pkg/cloudbroker/grid/filter_test.go b/pkg/cloudbroker/grid/filter_test.go new file mode 100644 index 0000000..b1f3c53 --- /dev/null +++ b/pkg/cloudbroker/grid/filter_test.go @@ -0,0 +1,131 @@ +package grid + +import "testing" + +var grids = ListGrids{ + { + Resources: Resources{ + Current: RecordResource{ + CPU: 84, + DiskSize: 976, + DiskSizeMax: 1200, + ExtIPs: 132, + ExtTraffic: 0, + GPU: 79500, + RAM: 0, + SEPs: map[string]map[string]DiskUsage{}, + }, + Reserved: RecordResource{ + CPU: 123, + DiskSize: 976, + DiskSizeMax: 1200, + ExtIPs: 132, + ExtTraffic: 0, + GPU: 0, + RAM: 152600, + SEPs: map[string]map[string]DiskUsage{}, + }, + }, + Flag: "", + GID: 212, + GUID: 1, + ID: 1, + LocationCode: "alfa", + Name: "alfa", + }, + { + Resources: Resources{ + Current: RecordResource{ + CPU: 84, + DiskSize: 976, + DiskSizeMax: 1200, + ExtIPs: 132, + ExtTraffic: 0, + GPU: 79500, + RAM: 0, + SEPs: map[string]map[string]DiskUsage{}, + }, + Reserved: RecordResource{ + CPU: 123, + DiskSize: 976, + DiskSizeMax: 1200, + ExtIPs: 132, + ExtTraffic: 0, + GPU: 0, + RAM: 152600, + SEPs: map[string]map[string]DiskUsage{}, + }, + }, + Flag: "", + GID: 666, + GUID: 2, + ID: 2, + LocationCode: "beta", + Name: "beta", + }, + { + Resources: Resources{ + Current: RecordResource{ + CPU: 84, + DiskSize: 976, + DiskSizeMax: 1200, + ExtIPs: 132, + ExtTraffic: 0, + GPU: 79500, + RAM: 0, + SEPs: map[string]map[string]DiskUsage{}, + }, + Reserved: RecordResource{ + CPU: 123, + DiskSize: 976, + DiskSizeMax: 1200, + ExtIPs: 132, + ExtTraffic: 0, + GPU: 0, + RAM: 152600, + SEPs: map[string]map[string]DiskUsage{}, + }, + }, + Flag: "", + GID: 777, + GUID: 3, + ID: 3, + LocationCode: "gamma", + Name: "gamma", + }, +} + +func TestFilterByID(t *testing.T) { + actual := grids.FilterByID(2).FindOne() + + if actual.ID != 2 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := grids.FilterByName("gamma").FindOne() + + if actual.Name != "gamma" { + t.Fatal("expected Name 'gamma', found: ", actual.Name) + } +} + +func TestFilterByLocationCode(t *testing.T) { + actual := grids.FilterByLocationCode("alfa").FindOne() + + if actual.LocationCode != "alfa" { + t.Fatal("expected LocationCode 'alfa', found: ", actual.LocationCode) + } +} + +func TestFilterFunc(t *testing.T) { + actual := grids.FilterFunc(func(rg RecordGrid) bool { + return rg.GID == 777 + }). + FindOne() + + if actual.GID != 777 { + t.Fatal("expected GID 777, found: ", actual.GID) + } +} diff --git a/pkg/cloudbroker/grid/grid.go b/pkg/cloudbroker/grid/grid.go index bb452a3..2f305c8 100644 --- a/pkg/cloudbroker/grid/grid.go +++ b/pkg/cloudbroker/grid/grid.go @@ -2,7 +2,7 @@ package grid import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to grid diff --git a/pkg/cloudbroker/grid/serialize.go b/pkg/cloudbroker/grid/serialize.go index 8a14b0c..5210435 100644 --- a/pkg/cloudbroker/grid/serialize.go +++ b/pkg/cloudbroker/grid/serialize.go @@ -3,7 +3,7 @@ package grid 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. diff --git a/pkg/cloudbroker/image.go b/pkg/cloudbroker/image.go index a49ef79..b48ce0c 100644 --- a/pkg/cloudbroker/image.go +++ b/pkg/cloudbroker/image.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/image" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/image" ) // Accessing the Image method group diff --git a/pkg/cloudbroker/image/create_cdrom_image.go b/pkg/cloudbroker/image/create_cdrom_image.go index 9c37ef0..db4227d 100644 --- a/pkg/cloudbroker/image/create_cdrom_image.go +++ b/pkg/cloudbroker/image/create_cdrom_image.go @@ -6,7 +6,7 @@ import ( "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 CD-ROM image diff --git a/pkg/cloudbroker/image/create_image.go b/pkg/cloudbroker/image/create_image.go index c8bf2b8..9b875f7 100644 --- a/pkg/cloudbroker/image/create_image.go +++ b/pkg/cloudbroker/image/create_image.go @@ -6,7 +6,7 @@ import ( "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 diff --git a/pkg/cloudbroker/image/filter.go b/pkg/cloudbroker/image/filter.go index 77eb0a6..55b3f40 100644 --- a/pkg/cloudbroker/image/filter.go +++ b/pkg/cloudbroker/image/filter.go @@ -1,7 +1,7 @@ package image // FilterById returns ListImages with specified ID. -func (li ListImages) FilterById(id uint64) ListImages { +func (li ListImages) FilterByID(id uint64) ListImages { predicate := func(ri RecordImage) bool { return ri.ID == id } diff --git a/pkg/cloudbroker/image/filter_test.go b/pkg/cloudbroker/image/filter_test.go new file mode 100644 index 0000000..64c9e50 --- /dev/null +++ b/pkg/cloudbroker/image/filter_test.go @@ -0,0 +1,214 @@ +package image + +import "testing" + +var images = ListImages{ + RecordImage{ + UNCPath: "", + CKey: "", + Meta: []interface{}{ + "osismodel", + "cloudbroker", + "image", + 1, + }, + AccountID: 0, + ACL: []ACL{}, + Architecture: "X86_64", + BootType: "bios", + Bootable: true, + ComputeCIID: 0, + DeletedTime: 0, + Description: "", + Drivers: []string{ + "KVM_X86", + }, + Enabled: true, + GID: 212, + GUID: 9882, + History: []History{}, + HotResize: true, + ID: 9882, + LastModified: 0, + LinkTo: 0, + Milestones: 363491, + Name: "u16", + Password: "", + Pool: "vmstor", + PresentTo: []uint64{}, + ProviderName: "", + PurgeAttempts: 0, + ReferenceID: "sample_reference_id_u16", + ResID: "b321318-3214as-324-213-fdas", + ResName: "templates/image_9882", + RescueCD: false, + SEPID: 2504, + SharedWith: []uint64{}, + Size: 5, + Status: "CREATED", + TechStatus: "ALLOCATED", + Type: "linux", + URL: "http://sample_url:8000/u16", + Username: "", + Version: "", + Virtual: false, + }, + RecordImage{ + UNCPath: "", + CKey: "", + Meta: []interface{}{ + "osismodel", + "cloudbroker", + "image", + 1, + }, + AccountID: 0, + ACL: []ACL{}, + Architecture: "X86_64", + BootType: "bois", + Bootable: true, + ComputeCIID: 0, + DeletedTime: 0, + Description: "", + Drivers: []string{ + "KVM_X86", + }, + Enabled: false, + GID: 212, + GUID: 9884, + History: []History{}, + HotResize: false, + ID: 9884, + LastModified: 0, + LinkTo: 0, + Milestones: 363499, + Name: "alpine-virt-3.17", + Password: "", + Pool: "vmstor", + PresentTo: []uint64{}, + ProviderName: "", + PurgeAttempts: 0, + ReferenceID: "sample_reference_id_alpine", + ResID: "31d1d410-74f1-4e09-866b-046a5a8433c3", + ResName: "templates/image_9884", + RescueCD: false, + SEPID: 2504, + SharedWith: []uint64{}, + Size: 1, + Status: "CREATED", + TechStatus: "ALLOCATED", + Type: "linux", + URL: "http://sample_url:8000/alpine-virt-3", + Username: "", + Version: "", + Virtual: true, + }, + RecordImage{ + UNCPath: "", + CKey: "", + Meta: []interface{}{ + "osismodel", + "cloudbroker", + "image", + 1, + }, + AccountID: 1, + ACL: []ACL{}, + Architecture: "X86_64", + BootType: "bios", + Bootable: true, + ComputeCIID: 0, + DeletedTime: 0, + Description: "", + Drivers: []string{ + "KVM_X86", + }, + Enabled: true, + GID: 212, + GUID: 9885, + History: []History{}, + HotResize: true, + ID: 9885, + LastModified: 0, + LinkTo: 0, + Milestones: 363513, + Name: "test", + Password: "", + Pool: "vmstor", + PresentTo: []uint64{}, + ProviderName: "", + PurgeAttempts: 0, + ReferenceID: "sample_reference_id_test", + ResID: "1f53b815-1ac9-4a4b-af98-a0a3b69a34bb", + ResName: "templates/image_9885", + RescueCD: false, + SEPID: 2505, + SharedWith: []uint64{}, + Size: 4, + Status: "DESTROYED", + TechStatus: "ALLOCATED", + Type: "linux", + URL: "http://sample_url:8000/test", + Username: "", + Version: "", + 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(ri RecordImage) bool { + return ri.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") + } +} diff --git a/pkg/cloudbroker/image/image.go b/pkg/cloudbroker/image/image.go index f0e0020..5af3e65 100644 --- a/pkg/cloudbroker/image/image.go +++ b/pkg/cloudbroker/image/image.go @@ -1,7 +1,7 @@ // Lists all the images. A image is a template which can be used to deploy machines package image -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to image type Image struct { diff --git a/pkg/cloudbroker/image/serialize.go b/pkg/cloudbroker/image/serialize.go index 60f05c9..3b962b4 100644 --- a/pkg/cloudbroker/image/serialize.go +++ b/pkg/cloudbroker/image/serialize.go @@ -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. diff --git a/pkg/cloudbroker/image/sync_create_image.go b/pkg/cloudbroker/image/sync_create_image.go index 44075d8..0815d66 100644 --- a/pkg/cloudbroker/image/sync_create_image.go +++ b/pkg/cloudbroker/image/sync_create_image.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for sync create image diff --git a/pkg/cloudbroker/k8ci.go b/pkg/cloudbroker/k8ci.go index 69c849b..3a32b0f 100644 --- a/pkg/cloudbroker/k8ci.go +++ b/pkg/cloudbroker/k8ci.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/k8ci" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/k8ci" ) // Accessing the K8CI method group diff --git a/pkg/cloudbroker/k8ci/create.go b/pkg/cloudbroker/k8ci/create.go index 0bb0cf8..722d1bb 100644 --- a/pkg/cloudbroker/k8ci/create.go +++ b/pkg/cloudbroker/k8ci/create.go @@ -6,7 +6,7 @@ import ( "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 K8CI instance diff --git a/pkg/cloudbroker/k8ci/filter_test.go b/pkg/cloudbroker/k8ci/filter_test.go new file mode 100644 index 0000000..8afbd47 --- /dev/null +++ b/pkg/cloudbroker/k8ci/filter_test.go @@ -0,0 +1,144 @@ +package k8ci + +import "testing" + +var k8ciItems = ListK8CI{ + ItemK8CI{ + CreatedTime: 123902139, + RecordK8CI: RecordK8CI{ + Description: "", + GID: 0, + GUID: 1, + ID: 1, + LBImageID: 5, + MasterDriver: "KVM_X86", + MasterImageID: 120, + MaxMasterCount: 2, + MaxWorkerCount: 3, + Name: "purple_snake", + SharedWith: []interface{}{}, + Status: "ENABLED", + Version: "1", + WorkerDriver: "KVM_X86", + WorkerImageID: 120, + }, + }, + ItemK8CI{ + CreatedTime: 123902232, + RecordK8CI: RecordK8CI{ + Description: "", + GID: 0, + GUID: 2, + ID: 2, + LBImageID: 10, + MasterDriver: "KVM_X86", + MasterImageID: 121, + MaxMasterCount: 3, + MaxWorkerCount: 5, + Name: "green_giant", + SharedWith: []interface{}{}, + Status: "DISABLED", + Version: "2", + WorkerDriver: "KVM_X86", + WorkerImageID: 121, + }, + }, + ItemK8CI{ + CreatedTime: 123902335, + RecordK8CI: RecordK8CI{ + Description: "", + GID: 0, + GUID: 3, + ID: 3, + LBImageID: 12, + MasterDriver: "KVM_X86", + MasterImageID: 98, + MaxMasterCount: 5, + MaxWorkerCount: 9, + Name: "magenta_cloud", + SharedWith: []interface{}{}, + Status: "ENABLED", + Version: "3", + WorkerDriver: "KVM_X86", + WorkerImageID: 98, + }, + }, +} + +func TestFilterByID(t *testing.T) { + actual := k8ciItems.FilterByID(2).FindOne() + + if actual.ID != 2 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := k8ciItems.FilterByName("magenta_cloud").FindOne() + + if actual.Name != "magenta_cloud" { + t.Fatal("expected Name 'magenta_cloud', found: ", actual.Name) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := k8ciItems.FilterByStatus("ENABLED") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.Status != "ENABLED" { + t.Fatal("expected Status 'ENABLED', found: ", item.Status) + } + } +} + +func TestFilterByWorkerImageID(t *testing.T) { + actual := k8ciItems.FilterByWorkerImageID(98).FindOne() + + if actual.WorkerImageID != 98 { + t.Fatal("expected WorkerImageID 98, found: ", actual.WorkerImageID) + } +} + +func TestFilterByLBImageID(t *testing.T) { + actual := k8ciItems.FilterByLBImageID(10).FindOne() + + if actual.LBImageID != 10 { + t.Fatal("expected LBImageID 10, found: ", actual.LBImageID) + } +} + +func TestFilterByMasterImageID(t *testing.T) { + actual := k8ciItems.FilterByMasterImageID(120).FindOne() + + if actual.MasterImageID != 120 { + t.Fatal("expected MasterImageID 120, found: ", actual.MasterImageID) + } +} + +func TestFilterFunc(t *testing.T) { + actual := k8ciItems.FilterFunc(func(ikc ItemK8CI) bool { + return ikc.CreatedTime > 123902139 + }) + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.CreatedTime < 123902139 { + t.Fatal("expected CreatedTime greater than 123902139, found: ", item.CreatedTime) + } + } +} + +func TestSortingByCreatedTime(t *testing.T) { + actual := k8ciItems.SortByCreatedTime(true) + + if actual[0].CreatedTime != 123902335 && actual[2].CreatedTime != 123902139 { + t.Fatal("expected inverse sort, found normal") + } +} diff --git a/pkg/cloudbroker/k8ci/k8ci.go b/pkg/cloudbroker/k8ci/k8ci.go index 837f401..6e45e95 100644 --- a/pkg/cloudbroker/k8ci/k8ci.go +++ b/pkg/cloudbroker/k8ci/k8ci.go @@ -2,7 +2,7 @@ package k8ci import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to K8CI diff --git a/pkg/cloudbroker/k8ci/serialize.go b/pkg/cloudbroker/k8ci/serialize.go index 0fc31dd..01fadfa 100644 --- a/pkg/cloudbroker/k8ci/serialize.go +++ b/pkg/cloudbroker/k8ci/serialize.go @@ -3,7 +3,7 @@ package k8ci 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. diff --git a/pkg/cloudbroker/k8s.go b/pkg/cloudbroker/k8s.go index 29bbab1..d64941c 100644 --- a/pkg/cloudbroker/k8s.go +++ b/pkg/cloudbroker/k8s.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/k8s" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/k8s" ) // Accessing the K8S method group diff --git a/pkg/cloudbroker/k8s/k8s.go b/pkg/cloudbroker/k8s/k8s.go index 93959f0..bfd50d0 100644 --- a/pkg/cloudbroker/k8s/k8s.go +++ b/pkg/cloudbroker/k8s/k8s.go @@ -2,7 +2,7 @@ package k8s import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to K8S diff --git a/pkg/cloudbroker/k8s/serialize.go b/pkg/cloudbroker/k8s/serialize.go index a06eeac..f95315f 100644 --- a/pkg/cloudbroker/k8s/serialize.go +++ b/pkg/cloudbroker/k8s/serialize.go @@ -3,7 +3,7 @@ package k8s 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. diff --git a/pkg/cloudbroker/kvmppc.go b/pkg/cloudbroker/kvmppc.go index 73fb584..34d2fcc 100644 --- a/pkg/cloudbroker/kvmppc.go +++ b/pkg/cloudbroker/kvmppc.go @@ -1,6 +1,6 @@ package cloudbroker -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/kvmppc" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/kvmppc" // Accessing the KVMPPC method group func (cb *CloudBroker) KVMPPC() *kvmppc.KVMPPC { diff --git a/pkg/cloudbroker/kvmppc/kvmppc.go b/pkg/cloudbroker/kvmppc/kvmppc.go index 9e7e6cc..ecd838f 100644 --- a/pkg/cloudbroker/kvmppc/kvmppc.go +++ b/pkg/cloudbroker/kvmppc/kvmppc.go @@ -1,7 +1,7 @@ // API to manage KVM PowerPC compute instances (PPC VMs) package kvmppc -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to KVMPPC type KVMPPC struct { diff --git a/pkg/cloudbroker/kvmx86.go b/pkg/cloudbroker/kvmx86.go index 6368590..0f53c86 100644 --- a/pkg/cloudbroker/kvmx86.go +++ b/pkg/cloudbroker/kvmx86.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/kvmx86" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/kvmx86" ) // Accessing the KVMX86 method group diff --git a/pkg/cloudbroker/kvmx86/kvmx86.go b/pkg/cloudbroker/kvmx86/kvmx86.go index 7f47fc5..b46fdde 100644 --- a/pkg/cloudbroker/kvmx86/kvmx86.go +++ b/pkg/cloudbroker/kvmx86/kvmx86.go @@ -1,7 +1,7 @@ // API to manage KVM x86 compute instances (x86 VMs) package kvmx86 -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to KVMX86 type KVMX86 struct { diff --git a/pkg/cloudbroker/lb.go b/pkg/cloudbroker/lb.go index 17f4839..be0d7e9 100644 --- a/pkg/cloudbroker/lb.go +++ b/pkg/cloudbroker/lb.go @@ -1,6 +1,6 @@ package cloudbroker -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/lb" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb" // Accessing the LB method group func (cb *CloudBroker) LB() *lb.LB { diff --git a/pkg/cloudbroker/lb/backend_create.go b/pkg/cloudbroker/lb/backend_create.go index fb76676..4334fa9 100644 --- a/pkg/cloudbroker/lb/backend_create.go +++ b/pkg/cloudbroker/lb/backend_create.go @@ -6,7 +6,7 @@ import ( "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 backend diff --git a/pkg/cloudbroker/lb/filter_test.go b/pkg/cloudbroker/lb/filter_test.go new file mode 100644 index 0000000..c225ef3 --- /dev/null +++ b/pkg/cloudbroker/lb/filter_test.go @@ -0,0 +1,151 @@ +package lb + +import "testing" + +var lbs = ListLB{ + RecordLB{ + HAMode: true, + CKey: "", + Meta: []interface{}{}, + ACL: []interface{}{}, + Backends: []ItemBackend{}, + CreatedBy: "test_user_1", + CreatedTime: 1636667448, + DeletedBy: "", + DeletedTime: 0, + Description: "", + DPAPIPassword: "0000", + DPAPIUser: "api_user", + ExtNetID: 2522, + Frontends: []ItemFrontend{}, + GID: 212, + GUID: 1, + ID: 1, + ImageID: 2121, + Milestones: 129000, + Name: "k8s-lb-test-1", + PrimaryNode: Node{}, + RGID: 25090, + RGName: "", + SecondaryNode: Node{}, + Status: "ENABLED", + TechStatus: "STARTED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 101, + }, + RecordLB{ + HAMode: false, + CKey: "", + Meta: []interface{}{}, + ACL: []interface{}{}, + Backends: []ItemBackend{}, + CreatedBy: "test_user_2", + CreatedTime: 1636667506, + DeletedBy: "", + DeletedTime: 0, + Description: "", + DPAPIPassword: "0000", + DPAPIUser: "api_user_2", + ExtNetID: 2524, + Frontends: []ItemFrontend{}, + GID: 212, + GUID: 2, + ID: 2, + ImageID: 2129, + Milestones: 129013, + Name: "k8s-lb-test-2", + PrimaryNode: Node{}, + RGID: 25092, + RGName: "", + SecondaryNode: Node{}, + Status: "ENABLED", + TechStatus: "STOPPED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 102, + }, + RecordLB{ + HAMode: true, + CKey: "", + Meta: []interface{}{}, + ACL: []interface{}{}, + Backends: []ItemBackend{}, + CreatedBy: "te2t_user_3", + CreatedTime: 1636667534, + DeletedBy: "", + DeletedTime: 0, + Description: "", + DPAPIPassword: "0000", + DPAPIUser: "api_user_3", + ExtNetID: 2536, + Frontends: []ItemFrontend{}, + GID: 212, + GUID: 3, + ID: 3, + ImageID: 2139, + Milestones: 129025, + Name: "k8s-lb-test-3", + PrimaryNode: Node{}, + RGID: 25106, + RGName: "", + SecondaryNode: Node{}, + Status: "DISABLED", + TechStatus: "STOPPED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 118, + }, +} + +func TestFilterByID(t *testing.T) { + actual := lbs.FilterByID(2).FindOne() + + if actual.ID != 2 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := lbs.FilterByName("k8s-lb-test-3").FindOne() + + if actual.Name != "k8s-lb-test-3" { + t.Fatal("expected Name 'k8s-lb-test-3', found: ", actual.Name) + } +} + +func TestFilterByExtNetID(t *testing.T) { + actual := lbs.FilterByExtNetID(2522).FindOne() + + if actual.ExtNetID != 2522 { + t.Fatal("expected ExtNetID 2522, found: ", actual.ExtNetID) + } +} + +func TestFilterByImageID(t *testing.T) { + actual := lbs.FilterByImageID(2139).FindOne() + + if actual.ImageID != 2139 { + t.Fatal("expected ImageID 2139, found: ", actual.ImageID) + } +} + +func TestFilterFunc(t *testing.T) { + actual := lbs.FilterFunc(func(rl RecordLB) bool { + return rl.Status == "DISABLED" + }) + + for _, item := range actual { + if item.Status != "DISABLED" { + t.Fatal("expected Status 'DISABLED', found: ", item.Status) + } + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := lbs.SortByCreatedTime(true) + + if actual[0].CreatedTime != 1636667534 || actual[2].CreatedTime != 1636667448 { + t.Fatal("expected descending order, found ascending") + } +} diff --git a/pkg/cloudbroker/lb/lb.go b/pkg/cloudbroker/lb/lb.go index b4e5f48..3e83b41 100644 --- a/pkg/cloudbroker/lb/lb.go +++ b/pkg/cloudbroker/lb/lb.go @@ -1,7 +1,7 @@ // API to manage load balancer instance package lb -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to load balancer type LB struct { diff --git a/pkg/cloudbroker/lb/serialize.go b/pkg/cloudbroker/lb/serialize.go index c91373b..549a310 100644 --- a/pkg/cloudbroker/lb/serialize.go +++ b/pkg/cloudbroker/lb/serialize.go @@ -3,7 +3,7 @@ package lb 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. diff --git a/pkg/cloudbroker/rg.go b/pkg/cloudbroker/rg.go index 4ba5a9c..8cb4220 100644 --- a/pkg/cloudbroker/rg.go +++ b/pkg/cloudbroker/rg.go @@ -1,6 +1,6 @@ package cloudbroker -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/rg" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/rg" // Accessing the RG method group func (cb *CloudBroker) RG() *rg.RG { diff --git a/pkg/cloudbroker/rg/access_grant.go b/pkg/cloudbroker/rg/access_grant.go index 8a67c3f..600c5c8 100644 --- a/pkg/cloudbroker/rg/access_grant.go +++ b/pkg/cloudbroker/rg/access_grant.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for grant access to resource group diff --git a/pkg/cloudbroker/rg/filter_test.go b/pkg/cloudbroker/rg/filter_test.go new file mode 100644 index 0000000..36756e7 --- /dev/null +++ b/pkg/cloudbroker/rg/filter_test.go @@ -0,0 +1,244 @@ +package rg + +import "testing" + +var rgs = ListRG{ + { + AccountID: 1, + AccountName: "std", + ACL: []ACL{ + { + Explicit: true, + GUID: "", + Right: "ARCXDU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "sample_user_1@decs3o", + }, + }, + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1676645305, + DefNetID: 1, + DefNetType: "NONE", + DeletedBy: "", + DeletedTime: 0, + Description: "", + GID: 212, + GUID: 7971, + ID: 7971, + LockStatus: "UNLOCKED", + Milestones: 363459, + Name: "rg_1", + RegisterComputes: false, + ResourceLimits: ResourceLimits{ + CUC: -1, + CuD: -1, + CUI: -1, + CUM: -1, + CUNP: -1, + GPUUnits: -1, + }, + Secret: "", + Status: "CREATED", + UpdatedBy: "", + UpdatedTime: 0, + VINS: []uint64{}, + VMs: []uint64{}, + ResTypes: []string{}, + UniqPools: []string{}, + }, + { + AccountID: 2, + AccountName: "std_2", + ACL: []ACL{ + { + Explicit: true, + GUID: "", + Right: "ARCXDU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "sample_user_1@decs3o", + }, + }, + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1676645461, + DefNetID: 2, + DefNetType: "NONE", + DeletedBy: "", + DeletedTime: 0, + Description: "", + GID: 212, + GUID: 7972, + ID: 7972, + LockStatus: "UNLOCKED", + Milestones: 363468, + Name: "rg_2", + RegisterComputes: false, + ResourceLimits: ResourceLimits{ + CUC: -1, + CuD: -1, + CUI: -1, + CUM: -1, + CUNP: -1, + GPUUnits: -1, + }, + Secret: "", + Status: "CREATED", + UpdatedBy: "", + UpdatedTime: 0, + VINS: []uint64{}, + VMs: []uint64{}, + ResTypes: []string{}, + UniqPools: []string{}, + }, + { + AccountID: 3, + AccountName: "std_3", + ACL: []ACL{ + { + Explicit: true, + GUID: "", + Right: "ARCXDU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "sample_user_2@decs3o", + }, + }, + CreatedBy: "sample_user_2@decs3o", + CreatedTime: 1676645548, + DefNetID: 3, + DefNetType: "NONE", + DeletedBy: "", + DeletedTime: 0, + Description: "", + GID: 212, + GUID: 7973, + ID: 7973, + LockStatus: "kjLOCKED", + Milestones: 363471, + Name: "rg_3", + RegisterComputes: false, + ResourceLimits: ResourceLimits{ + CUC: -1, + CuD: -1, + CUI: -1, + CUM: -1, + CUNP: -1, + GPUUnits: -1, + }, + Secret: "", + Status: "DISABLED", + UpdatedBy: "", + UpdatedTime: 0, + VINS: []uint64{}, + VMs: []uint64{ + 48500, + }, + ResTypes: []string{}, + UniqPools: []string{}, + }, +} + +func TestFilterByID(t *testing.T) { + actual := rgs.FilterByID(7972).FindOne() + + if actual.ID != 7972 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := rgs.FilterByName("rg_1").FindOne() + + if actual.Name != "rg_1" { + t.Fatal("expected Name 'rg_1', found: ", actual.Name) + } +} + +func TestFilterByCreatedBy(t *testing.T) { + actual := rgs.FilterByCreatedBy("sample_user_1@decs3o") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.CreatedBy != "sample_user_1@decs3o" { + t.Fatal("expected CreatedBy 'sample_user_1@decs3o', found: ", item.CreatedBy) + } + } +} + +func TestFilterByStatus(t *testing.T) { + actual := rgs.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 'ENABLED', found: ", item.Status) + } + } +} + +func TestFilterByLockStatus(t *testing.T) { + actual := rgs.FilterByLockStatus("UNLOCKED") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.LockStatus != "UNLOCKED" { + t.Fatal("expected LockStatus 'UNLOCKED', found: ", item.LockStatus) + } + } +} + +func TestFilterByDefNetType(t *testing.T) { + actual := rgs.FilterByDefNetType("NONE") + + if len(actual) != 3 { + t.Fatal("expected 3 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.DefNetType != "NONE" { + t.Fatal("expected DefNetType 'NONE', found: ", item.DefNetType) + } + } +} + +func TestFilterByDefNetID(t *testing.T) { + actual := rgs.FilterByDefNetID(1).FindOne() + + if actual.DefNetID != 1 { + t.Fatal("expected DefNetID 1, found: ", actual.DefNetID) + } +} + +func TestFilterFunc(t *testing.T) { + actual := rgs.FilterFunc(func(ir ItemRG) bool { + return len(ir.VMs) > 0 + }) + + if len(actual) < 1 { + t.Fatal("expected 1 found, actual: ", len(actual)) + } + + for _, item := range actual { + if len(item.VMs) < 1 { + t.Fatal("expected VMs to contain at least 1 element, found empty") + } + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := rgs.SortByCreatedTime(true) + + if actual[0].CreatedTime != 1676645548 || actual[2].CreatedTime != 1676645305 { + t.Fatal("expected descending order, found ascending") + } +} diff --git a/pkg/cloudbroker/rg/rg.go b/pkg/cloudbroker/rg/rg.go index 3191aad..bc936d1 100644 --- a/pkg/cloudbroker/rg/rg.go +++ b/pkg/cloudbroker/rg/rg.go @@ -2,7 +2,7 @@ package rg import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to resource group diff --git a/pkg/cloudbroker/rg/serialize.go b/pkg/cloudbroker/rg/serialize.go index fcaecbc..d56a0d7 100644 --- a/pkg/cloudbroker/rg/serialize.go +++ b/pkg/cloudbroker/rg/serialize.go @@ -3,7 +3,7 @@ package rg 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. diff --git a/pkg/cloudbroker/rg/set_def_net.go b/pkg/cloudbroker/rg/set_def_net.go index ee81533..675de7b 100644 --- a/pkg/cloudbroker/rg/set_def_net.go +++ b/pkg/cloudbroker/rg/set_def_net.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set default network diff --git a/pkg/cloudbroker/rg/update_resource_types.go b/pkg/cloudbroker/rg/update_resource_types.go index acffea6..376274b 100644 --- a/pkg/cloudbroker/rg/update_resource_types.go +++ b/pkg/cloudbroker/rg/update_resource_types.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update resource types in account diff --git a/pkg/cloudbroker/sep.go b/pkg/cloudbroker/sep.go index f2e6319..c2596a1 100644 --- a/pkg/cloudbroker/sep.go +++ b/pkg/cloudbroker/sep.go @@ -1,6 +1,6 @@ package cloudbroker -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/sep" +import "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/sep" // Accessing the SEP method group func (cb *CloudBroker) SEP() *sep.SEP { diff --git a/pkg/cloudbroker/sep/config_field_edit.go b/pkg/cloudbroker/sep/config_field_edit.go index 2f770cf..472079d 100644 --- a/pkg/cloudbroker/sep/config_field_edit.go +++ b/pkg/cloudbroker/sep/config_field_edit.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for edit config fields diff --git a/pkg/cloudbroker/sep/filter_test.go b/pkg/cloudbroker/sep/filter_test.go new file mode 100644 index 0000000..d3f4fc5 --- /dev/null +++ b/pkg/cloudbroker/sep/filter_test.go @@ -0,0 +1,184 @@ +package sep + +import "testing" + +var seps = ListSEP{ + { + CKey: "", + Meta: []interface{}{ + "osismodel", + "cloudbroker", + "sep", + 1, + }, + Config: map[string]interface{}{ + "API_IPs": []string{ + "10.212.3.61", + "10.212.3.62", + "10.212.3.63", + }, + }, + ConsumedBy: []uint64{ + 27, + }, + Description: "", + GID: 212, + GUID: 1, + ID: 1, + Milestones: 278329, + Name: "sep_1", + ObjStatus: "CREATED", + ProvidedBy: []uint64{ + 24, + 35, + 29, + }, + SharedWith: []uint64{}, + TechStatus: "ENABLED", + Type: "DES", + }, + { + CKey: "", + Meta: []interface{}{ + "osismodel", + "cloudbroker", + "sep", + 1, + }, + Config: map[string]interface{}{ + "API_IPs": []string{ + "10.212.3.64", + "10.212.3.65", + "10.212.3.66", + }, + }, + ConsumedBy: []uint64{ + 32, + 26, + }, + Description: "", + GID: 212, + GUID: 2, + ID: 2, + Milestones: 278337, + Name: "sep_2", + ObjStatus: "CREATED", + ProvidedBy: []uint64{ + 36, + 42, + 35, + }, + SharedWith: []uint64{}, + TechStatus: "ENABLED", + Type: "DES", + }, + { + CKey: "", + Meta: []interface{}{ + "osismodel", + "cloudbroker", + "sep", + 1, + }, + Config: map[string]interface{}{ + "API_IPs": []string{ + "10.212.3.67", + "10.212.3.68", + "10.212.3.69", + }, + }, + ConsumedBy: []uint64{ + 38, + 28, + }, + Description: "", + GID: 212, + GUID: 3, + ID: 3, + Milestones: 278345, + Name: "sep_3", + ObjStatus: "DESTROYED", + ProvidedBy: []uint64{ + 49, + 48, + 41, + }, + SharedWith: []uint64{}, + TechStatus: "DISABLED", + Type: "DES", + }, +} + +func TestFilterByID(t *testing.T) { + actual := seps.FilterByID(1).FindOne() + + if actual.ID != 1 { + t.Fatal("expected ID 1, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := seps.FilterByName("sep_2").FindOne() + + if actual.Name != "sep_2" { + t.Fatal("expected Name 'sep_2', found: ", actual.Name) + } +} + +func TestFilterByObjStatus(t *testing.T) { + actual := seps.FilterByObjStatus("CREATED") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.ObjStatus != "CREATED" { + t.Fatal("expected ObjStatus 'CREATED', found: ", item.ObjStatus) + } + } +} + +func TestFilterByTechStatus(t *testing.T) { + actual := seps.FilterByTechStatus("ENABLED") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.TechStatus != "ENABLED" { + t.Fatal("expected TechStatus 'ENABLED', found: ", item.TechStatus) + } + } +} + +func TestFilterByType(t *testing.T) { + actual := seps.FilterByType("DES") + + if len(actual) != 3 { + t.Fatal("expected 3 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.Type != "DES" { + t.Fatal("expected Type 'DES', found: ", item.Type) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := seps.FilterFunc(func(rs RecordSEP) bool { + return len(rs.ConsumedBy) > 1 + }) + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if len(item.ConsumedBy) <= 1 { + t.Fatal("expected ConsumedBy to contain more than 1 element, found: ", len(item.ConsumedBy)) + } + } +} diff --git a/pkg/cloudbroker/sep/sep.go b/pkg/cloudbroker/sep/sep.go index 20b7faa..aca77ef 100644 --- a/pkg/cloudbroker/sep/sep.go +++ b/pkg/cloudbroker/sep/sep.go @@ -2,7 +2,7 @@ package sep import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to storage endpoint provider diff --git a/pkg/cloudbroker/sep/serialize.go b/pkg/cloudbroker/sep/serialize.go index 75542d4..8e6b638 100644 --- a/pkg/cloudbroker/sep/serialize.go +++ b/pkg/cloudbroker/sep/serialize.go @@ -3,7 +3,7 @@ package sep 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. diff --git a/pkg/cloudbroker/tasks.go b/pkg/cloudbroker/tasks.go index 5c77622..5edb31f 100644 --- a/pkg/cloudbroker/tasks.go +++ b/pkg/cloudbroker/tasks.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/tasks" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/tasks" ) // Accessing the tasks method group diff --git a/pkg/cloudbroker/tasks/tasks.go b/pkg/cloudbroker/tasks/tasks.go index 503f722..2f5afbc 100644 --- a/pkg/cloudbroker/tasks/tasks.go +++ b/pkg/cloudbroker/tasks/tasks.go @@ -1,7 +1,7 @@ // User API tasks interface package tasks -import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" +import "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to tasks type Tasks struct { diff --git a/pkg/cloudbroker/vins.go b/pkg/cloudbroker/vins.go index 33c96ed..9998697 100644 --- a/pkg/cloudbroker/vins.go +++ b/pkg/cloudbroker/vins.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/vins" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/vins" ) // Accessing the VINS method group diff --git a/pkg/cloudbroker/vins/filter_test.go b/pkg/cloudbroker/vins/filter_test.go new file mode 100644 index 0000000..1ebe3bc --- /dev/null +++ b/pkg/cloudbroker/vins/filter_test.go @@ -0,0 +1,201 @@ +package vins + +import "testing" + +var vinsItems = ListVINS{ + { + AccountID: 1, + AccountName: "std", + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1676898844, + DefaultGW: "", + DefaultQOS: QOS{ + ERate: 0, + GUID: "", + InBurst: 0, + InRate: 0, + }, + DeletedBy: "", + DeletedTime: 0, + Description: "", + ExternalIP: "", + GID: 212, + GUID: 1, + ID: 1, + LockStatus: "UNLOCKED", + ManagerID: 0, + ManagerType: "", + Milestones: 363485, + Name: "vins01", + NetMask: 24, + Network: "192.168.1.0/24", + PreReservationsNum: 32, + PriVNFDevID: 29557, + Redundant: false, + RGID: 7971, + RGName: "rg_01", + SecVNFDevID: 0, + Status: "ENABLED", + UpdatedBy: "", + UpdatedTime: 0, + UserManaged: true, + VNFs: ItemVNFs{ + DHCP: 51997, + DNS: 0, + FW: 0, + GW: 0, + NAT: 0, + VPN: 0, + }, + VXLANID: 3544, + }, + { + AccountID: 2, + AccountName: "std2", + CreatedBy: "sample_user_1@decs3o", + CreatedTime: 1676898948, + DefaultGW: "", + DefaultQOS: QOS{ + ERate: 0, + GUID: "", + InBurst: 0, + InRate: 0, + }, + DeletedBy: "", + DeletedTime: 0, + Description: "", + ExternalIP: "", + GID: 212, + GUID: 2, + ID: 2, + LockStatus: "LOCKED", + ManagerID: 0, + ManagerType: "", + Milestones: 363508, + Name: "vins02", + NetMask: 24, + Network: "192.168.2.0/24", + PreReservationsNum: 32, + PriVNFDevID: 29558, + Redundant: false, + RGID: 7972, + RGName: "rg_02", + SecVNFDevID: 0, + Status: "ENABLED", + UpdatedBy: "", + UpdatedTime: 0, + UserManaged: true, + VNFs: ItemVNFs{ + DHCP: 51998, + DNS: 0, + FW: 0, + GW: 0, + NAT: 0, + VPN: 0, + }, + VXLANID: 3545, + }, + { + AccountID: 3, + AccountName: "std3", + CreatedBy: "sample_user_2@decs3o", + CreatedTime: 1676899026, + DefaultGW: "", + DefaultQOS: QOS{ + ERate: 0, + GUID: "", + InBurst: 0, + InRate: 0, + }, + DeletedBy: "", + DeletedTime: 0, + Description: "", + ExternalIP: "", + GID: 212, + GUID: 3, + ID: 3, + LockStatus: "UNLOCKED", + ManagerID: 0, + ManagerType: "", + Milestones: 363549, + Name: "vins03", + NetMask: 24, + Network: "192.168.3.0/24", + PreReservationsNum: 32, + PriVNFDevID: 29559, + Redundant: false, + RGID: 7973, + RGName: "rg_03", + SecVNFDevID: 0, + Status: "DISABLED", + UpdatedBy: "", + UpdatedTime: 0, + UserManaged: true, + VNFs: ItemVNFs{ + DHCP: 51999, + DNS: 0, + FW: 0, + GW: 0, + NAT: 0, + VPN: 0, + }, + VXLANID: 3546, + }, +} + +func TestFilterByID(t *testing.T) { + actual := vinsItems.FilterByID(2).FindOne() + + if actual.ID != 2 { + t.Fatal("expected ID 2, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := vinsItems.FilterByName("vins01").FindOne() + + if actual.Name != "vins01" { + t.Fatal("expected Name 'vins01', found: ", actual.Name) + } +} + +func TestFilterByAccountID(t *testing.T) { + actual := vinsItems.FilterByAccountID(3).FindOne() + + if actual.AccountID != 3 { + t.Fatal("expected AccountID 3, found: ", actual.AccountID) + } +} + +func TestFilterByCreatedBy(t *testing.T) { + actual := vinsItems.FilterByCreatedBy("sample_user_1@decs3o") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.CreatedBy != "sample_user_1@decs3o" { + t.Fatal("expected CreatedBy 'sample_user_1@decs3o', found: ", item.CreatedBy) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := vinsItems.FilterFunc(func(iv ItemVINS) bool { + return iv.RGID == 7971 + }). + FindOne() + + if actual.RGID != 7971 { + t.Fatal("expected RGID 7971, found: ", actual.RGID) + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := vinsItems.SortByCreatedTime(false) + + if actual[0].CreatedTime != 1676898844 || actual[2].CreatedTime != 1676899026 { + t.Fatal("expected ascending order, found descending") + } +} diff --git a/pkg/cloudbroker/vins/ip_reserve.go b/pkg/cloudbroker/vins/ip_reserve.go index 604b37f..cab686d 100644 --- a/pkg/cloudbroker/vins/ip_reserve.go +++ b/pkg/cloudbroker/vins/ip_reserve.go @@ -6,7 +6,7 @@ import ( "net/http" "strings" - "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for IP reserve diff --git a/pkg/cloudbroker/vins/serialize.go b/pkg/cloudbroker/vins/serialize.go index 0a1c44e..7ff8242 100644 --- a/pkg/cloudbroker/vins/serialize.go +++ b/pkg/cloudbroker/vins/serialize.go @@ -3,7 +3,7 @@ package vins 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. diff --git a/pkg/cloudbroker/vins/vins.go b/pkg/cloudbroker/vins/vins.go index 7c063c7..eebb6a0 100644 --- a/pkg/cloudbroker/vins/vins.go +++ b/pkg/cloudbroker/vins/vins.go @@ -2,7 +2,7 @@ package vins import ( - "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to VINS