parent
0be4d8fb0c
commit
8f152a2f63
@ -0,0 +1,40 @@
|
|||||||
|
package compute
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Request struct for deleting compute's custome fields
|
||||||
|
type DeleteCustomFieldsRequest struct {
|
||||||
|
// ID of the compute
|
||||||
|
// Required: true
|
||||||
|
ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteCustomFields deletes computes custom fields
|
||||||
|
func (c Compute) DeleteCustomFields(ctx context.Context, req DeleteCustomFieldsRequest) (bool, error) {
|
||||||
|
err := validators.ValidateRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
for _, validationError := range validators.GetErrors(err) {
|
||||||
|
return false, validators.ValidationError(validationError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
url := "/cloudapi/compute/deleteCustomFields"
|
||||||
|
|
||||||
|
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := strconv.ParseBool(string(res))
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package compute
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Request struct for getting Compute's customFields
|
||||||
|
type GetCustomFieldsRequest struct {
|
||||||
|
// Compute ID
|
||||||
|
// Required: true
|
||||||
|
ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCustomFields gets Compute's customFields
|
||||||
|
func (c Compute) GetCustomFields(ctx context.Context, req GetCustomFieldsRequest) (interface{}, error) {
|
||||||
|
err := validators.ValidateRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
for _, validationError := range validators.GetErrors(err) {
|
||||||
|
return nil, validators.ValidationError(validationError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
url := "/cloudapi/compute/getCustomFields"
|
||||||
|
|
||||||
|
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var info interface{}
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &info)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &info, nil
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package compute
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||||
|
)
|
||||||
|
// Request struct for setting customFields values for the Compute
|
||||||
|
type SetCustomFieldsRequest struct {
|
||||||
|
// ID of the compute
|
||||||
|
// Required: true
|
||||||
|
ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`
|
||||||
|
|
||||||
|
// Custom fields for Compute. Must be dict.
|
||||||
|
// Required: true
|
||||||
|
CustomFields string `url:"customFields" json:"customFields" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCustomFields sets customFields values for the Compute
|
||||||
|
func (c Compute) SetCustomFields(ctx context.Context, req SetCustomFieldsRequest) (bool, error) {
|
||||||
|
err := validators.ValidateRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
for _, validationError := range validators.GetErrors(err) {
|
||||||
|
return false, validators.ValidationError(validationError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
url := "/cloudapi/compute/setCustomFields"
|
||||||
|
|
||||||
|
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := strconv.ParseBool(string(res))
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
package cloudapi
|
|
||||||
|
|
||||||
import (
|
|
||||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/computeci"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Accessing the ComputeCI method group
|
|
||||||
func (ca *CloudAPI) ComputeCI() *computeci.ComputeCI {
|
|
||||||
return computeci.New(ca.client)
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
// API Actor for managing ComputeCI. This actor is a final API for admin to manage ComputeCI
|
|
||||||
package computeci
|
|
||||||
|
|
||||||
import (
|
|
||||||
"repository.basistech.ru/BASIS/decort-golang-sdk/interfaces"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Structure for creating request to computeci
|
|
||||||
type ComputeCI struct {
|
|
||||||
client interfaces.Caller
|
|
||||||
}
|
|
||||||
|
|
||||||
// Builder for computeci endpoints
|
|
||||||
func New(client interfaces.Caller) *ComputeCI {
|
|
||||||
return &ComputeCI{
|
|
||||||
client,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
package computeci
|
|
||||||
|
|
||||||
// FilterByID returns ListComputeCI with specified ID.
|
|
||||||
func (lci ListComputeCI) FilterByID(id uint64) ListComputeCI {
|
|
||||||
predicate := func(ic ItemComputeCI) bool {
|
|
||||||
return ic.ID == id
|
|
||||||
}
|
|
||||||
|
|
||||||
return lci.FilterFunc(predicate)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FilterByName returns ListComputeCI with specified Name.
|
|
||||||
func (lci ListComputeCI) FilterByName(name string) ListComputeCI {
|
|
||||||
predicate := func(ic ItemComputeCI) bool {
|
|
||||||
return ic.Name == name
|
|
||||||
}
|
|
||||||
|
|
||||||
return lci.FilterFunc(predicate)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FilterByStatus returns ListComputeCI with specified Status.
|
|
||||||
func (lci ListComputeCI) FilterByStatus(status string) ListComputeCI {
|
|
||||||
predicate := func(ic ItemComputeCI) bool {
|
|
||||||
return ic.Status == status
|
|
||||||
}
|
|
||||||
|
|
||||||
return lci.FilterFunc(predicate)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FilterFunc allows filtering ListComputeCI based on a user-specified predicate.
|
|
||||||
func (lci ListComputeCI) FilterFunc(predicate func(ItemComputeCI) bool) ListComputeCI {
|
|
||||||
var result ListComputeCI
|
|
||||||
|
|
||||||
for _, item := range lci.Data {
|
|
||||||
if predicate(item) {
|
|
||||||
result.Data = append(result.Data, item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result.EntryCount = uint64(len(result.Data))
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindOne returns first found ItemComputeCI
|
|
||||||
// If none was found, returns an empty struct.
|
|
||||||
func (lci ListComputeCI) FindOne() ItemComputeCI {
|
|
||||||
if lci.EntryCount == 0 {
|
|
||||||
return ItemComputeCI{}
|
|
||||||
}
|
|
||||||
|
|
||||||
return lci.Data[0]
|
|
||||||
}
|
|
@ -1,98 +0,0 @@
|
|||||||
package computeci
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
var computeciItems = ListComputeCI{
|
|
||||||
Data: []ItemComputeCI{
|
|
||||||
{
|
|
||||||
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: "",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
EntryCount: 3,
|
|
||||||
}
|
|
||||||
|
|
||||||
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.Data) != 2 {
|
|
||||||
t.Fatal("expected 2 found, actual: ", len(actual.Data))
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range actual.Data {
|
|
||||||
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.Data) != 2 {
|
|
||||||
t.Fatal("expected 2 found, actual: ", len(actual.Data))
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range actual.Data {
|
|
||||||
for _, driver := range item.Drivers {
|
|
||||||
if driver != "KVM_X86" {
|
|
||||||
t.Fatal("expected 'KVM_X86' Driver, found: ", driver)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
package computeci
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"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" validate:"required"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get gets information about computeci by ID
|
|
||||||
func (c ComputeCI) Get(ctx context.Context, req GetRequest) (*ItemComputeCI, error) {
|
|
||||||
err := validators.ValidateRequest(req)
|
|
||||||
if err != nil {
|
|
||||||
for _, validatonError := range validators.GetErrors(err) {
|
|
||||||
return nil, validators.ValidationError(validatonError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
url := "/cloudapi/computeci/get"
|
|
||||||
|
|
||||||
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
info := ItemComputeCI{}
|
|
||||||
|
|
||||||
err = json.Unmarshal(res, &info)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &info, nil
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
package computeci
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Request struct for get list of computeci
|
|
||||||
type ListRequest struct {
|
|
||||||
// Find by name
|
|
||||||
// Required: false
|
|
||||||
Name string `url:"name,omitempty" json:"name,omitempty"`
|
|
||||||
|
|
||||||
// Find by computeci ID
|
|
||||||
// Required: false
|
|
||||||
ByID uint64 `url:"by_id,omitempty" json:"by_id,omitempty"`
|
|
||||||
|
|
||||||
// Find by drivers
|
|
||||||
// Find by computeci ID
|
|
||||||
Drivers []string `url:"drivers,omitempty" json:"drivers,omitempty"`
|
|
||||||
|
|
||||||
// If true list deleted instances as well
|
|
||||||
// Required: false
|
|
||||||
IncludeDeleted bool `url:"includeDeleted,omitempty" json:"includeDeleted,omitempty"`
|
|
||||||
|
|
||||||
// Page number
|
|
||||||
// Required: false
|
|
||||||
Page uint64 `url:"page,omitempty" json:"page,omitempty"`
|
|
||||||
|
|
||||||
// Page size
|
|
||||||
// Required: false
|
|
||||||
Size uint64 `url:"size,omitempty" json:"size,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// List gets list of computeci instances
|
|
||||||
func (c ComputeCI) List(ctx context.Context, req ListRequest) (*ListComputeCI, error) {
|
|
||||||
url := "/cloudapi/computeci/list"
|
|
||||||
|
|
||||||
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
list := ListComputeCI{}
|
|
||||||
|
|
||||||
err = json.Unmarshal(res, &list)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &list, nil
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package computeci
|
|
||||||
|
|
||||||
// Main information about computeci
|
|
||||||
type ItemComputeCI struct {
|
|
||||||
// Custom fields
|
|
||||||
CustomFields map[string]interface{} `json:"customFields"`
|
|
||||||
|
|
||||||
// Description
|
|
||||||
Description string `json:"desc"`
|
|
||||||
|
|
||||||
// List drivers
|
|
||||||
Drivers []string `json:"drivers"`
|
|
||||||
|
|
||||||
// GUID
|
|
||||||
GUID uint64 `json:"guid"`
|
|
||||||
|
|
||||||
// ID
|
|
||||||
ID uint64 `json:"id"`
|
|
||||||
|
|
||||||
// Name
|
|
||||||
Name string `json:"name"`
|
|
||||||
|
|
||||||
// Status
|
|
||||||
Status string `json:"status"`
|
|
||||||
|
|
||||||
// Template
|
|
||||||
Template string `json:"template"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// List of computeci instances
|
|
||||||
type ListComputeCI struct {
|
|
||||||
Data []ItemComputeCI `json:"data"`
|
|
||||||
|
|
||||||
EntryCount uint64 `json:"entryCount"`
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package computeci
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"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.
|
|
||||||
//
|
|
||||||
// In order to serialize with indent make sure to follow these guidelines:
|
|
||||||
// - First argument -> prefix
|
|
||||||
// - Second argument -> indent
|
|
||||||
func (lci ListComputeCI) Serialize(params ...string) (serialization.Serialized, error) {
|
|
||||||
if lci.EntryCount == 0 {
|
|
||||||
return []byte{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(params) > 1 {
|
|
||||||
prefix := params[0]
|
|
||||||
indent := params[1]
|
|
||||||
|
|
||||||
return json.MarshalIndent(lci, prefix, indent)
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.Marshal(lci)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serialize returns JSON-serialized []byte. Used as a wrapper over json.Marshal and json.MarshalIndent functions.
|
|
||||||
//
|
|
||||||
// In order to serialize with indent make sure to follow these guidelines:
|
|
||||||
// - First argument -> prefix
|
|
||||||
// - Second argument -> indent
|
|
||||||
func (ic ItemComputeCI) Serialize(params ...string) (serialization.Serialized, error) {
|
|
||||||
if len(params) > 1 {
|
|
||||||
prefix := params[0]
|
|
||||||
indent := params[1]
|
|
||||||
|
|
||||||
return json.MarshalIndent(ic, prefix, indent)
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.Marshal(ic)
|
|
||||||
}
|
|
Loading…
Reference in new issue