diff --git a/CHANGELOG.md b/CHANGELOG.md index b500526..86fa395 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ -## Version 1.2.0 +## Version 1.2.1 ### Bug fixes -#### Client +#### Legacy Client -- Added legacy client authorization support +- Fixed password and username encoding +- Fixed request params absence in HTTP Transport #### All -- Add json tags for requests +- Updated module to new repository diff --git a/Makefile b/Makefile index 505d0e0..544f131 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,10 @@ -.PHONY: lint -lint: - golangci-lint run --timeout 600s - -.DEFAULT_GOAL := lint \ No newline at end of file +all: lint test +.PHONY: all + +lint: + golangci-lint run --timeout 600s +test: + go test -v -failfast -timeout 600s ./... + +.DEFAULT_GOAL := lint + diff --git a/README.md b/README.md index 1e7b15c..a3f9d72 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ Decort SDK - это библиотека, написанная на языке G - [Создание клиента](#создание-клиента) - [Создание структуры запроса](#cоздание-структуры-запроса) - [Выполнение запроса](#выполнение-запроса) + - [Фильтрация](#фильтрация) + - [Сортировка](#сортировка) + - [Сериализация](#сериализация) - [Работа с legacy клиентом](#работа-с-legacy-клиентом) - [Настройка конфигурации legacy клиента](#настройка-конфигурации-legacy-клиента) - [Создание legacy клиента](#создание-legacy-клиента) @@ -205,44 +208,44 @@ func main() { - Обязательный или нет - поле required в комментариях; - Доп. информация (допустимые значения, значения по умолчанию). -#### Пример комментарие структуры +#### Пример комментариев структуры ```go type CreateRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId"` + RGID uint64 `url:"rgId" json:"rgId"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name"` + Name string `url:"name" json:"name"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu"` + CPU uint64 `url:"cpu" json:"cpu"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram"` + RAM uint64 `url:"ram" json:"ram"` // ID of the OS image to base this VM on; // Could be boot disk image or CD-ROM image // Required: true - ImageID uint64 `url:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId"` // Size of the boot disk in GB // Required: false - BootDisk uint64 `url:"bootDisk,omitempty"` + BootDisk uint64 `url:"bootDisk,omitempty" json:"bootDisk,omitempty"` // ID of SEP to create boot disk on. // Uses images SEP ID if not set // Required: false - SEPID uint64 `url:"sepId,omitempty"` + SEPID uint64 `url:"sepId,omitempty" json:"sepId,omitempty"` // Pool to use if SEP ID is set, can be also empty if needed to be chosen by system // Required: false - Pool string `url:"pool,omitempty"` + Pool string `url:"pool,omitempty" json:"pool,omitempty"` // Network type // Should be one of: @@ -250,46 +253,46 @@ type CreateRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty"` // Network ID for connect to, // for EXTNET - external network ID, // for VINS - VINS ID, // when network type is not "NONE" // Required: false - NetID uint64 `url:"netId,omitempty"` + NetID uint64 `url:"netId,omitempty" json:"netId,omitempty"` // IP address to assign to this VM when connecting to the specified network // Required: false - IPAddr string `url:"ipAddr,omitempty"` + IPAddr string `url:"ipAddr,omitempty" json:"ipAddr,omitempty"` // Input data for cloud-init facility // Required: false - Userdata string `url:"userdata,omitempty"` + Userdata string `url:"userdata,omitempty" json:"userdata,omitempty"` // Text description of this VM // Required: false - Description string `url:"desc,omitempty"` + Description string `url:"desc,omitempty" json:"desc,omitempty"` // Start VM upon success // Required: false - Start bool `url:"start,omitempty"` + Start bool `url:"start,omitempty" json:"start,omitempty"` // Stack ID // Required: false - StackID uint64 `url:"stackId,omitempty"` + StackID uint64 `url:"stackId,omitempty" json:"stackId,omitempty"` // System name // Required: false - IS string `url:"IS,omitempty"` + IS string `url:"IS,omitempty" json:"IS,omitempty"` // Compute purpose // Required: false - IPAType string `url:"ipaType,omitempty"` + IPAType string `url:"ipaType,omitempty" json:"ipaType,omitempty"` // Reason for action // Required: false - Reason string `url:"reason,omitempty"` + Reason string `url:"reason,omitempty" json:"reason,omitempty"` } ``` @@ -437,6 +440,147 @@ func main() { } ``` +### Фильтрация + +Для каждого `ListRequest` в SDK есть группа функций для фильтрации ответа платформы. Для того чтобы произвести фильтрацию по заданным полям, достаточно описать анонимную функцию (предикат) в `.FilterFunc()`, например: + +```go +// Создание запроса account/list +req := account.ListRequest{} + +resp, err := client.CloudAPI().Account().List(context.Background(), req) +if err != nil { + log.Fatal(err) +} + +// Тип filtered - ListAccount, тот же, что и у ответа платформы. +filtered := resp. + FilterFunc(func(ia account.ItemAccount) bool { + // ItemAccount открывает доступ к полям модели ответа платформы. + for _, itemAcl := range ia.ACL { + if itemAcl.UgroupID == "" { // Фильтр по ACL/UgroupID + return true + } + } + return false + }) +``` + +Для удобства пользователей, особенно важные фильтры вынесены в отдельные функции, что облегчает фильтрацию сразу по нескольким полям: + +```go +// Создание запроса account/list +req := account.ListRequest{} + +resp, err := client.CloudAPI().Account().List(context.Background(), req) +if err != nil { + log.Fatal(err) +} + +// Несколько фильтров объединены в конвейер +filtered := resp. + FilterByName(""). + FilterByStatus("") + // .... +``` + +### Сортировка + +Функции сортировки так же могут быть объединены в конвейер: + +```go +// Создание запроса compute/list +req := compute.ListRequest{} + +resp, err := client.CloudAPI().Compute().List(context.Background(), req) +if err != nil { + log.Fatal(err) +} + +// Функции сортировки имеют параметр inverse (bool): +// При значении false -> сортировка по возрастанию +// При true -> сортировка по убыванию +sorted := resp. + SortByCPU(false). + SortByRAM(false). + SortByCreatedTime(true) + // .... +``` + +### Сериализация + +Результат преобразований легко сериализовать в JSON при помощи функции `.Serialize()`. Она принимает в себя 2 необязательных аргумента: префикс (prefix) и отступ (Indent). + +В случае если функция вызывается без аргументов, то маршализация пройдет успешно, но без отступов и префиксов. + +```go +// Сериализация данных с префиксом "" и отступом "\t" +serialized, err := filtered.Serialize("", "\t") +if err != nil { + log.Fatal(err) +} + +// Запись сериализованных данных в файл +err = serialized.WriteToFile("") +if err != nil { + log.Fatal(err) +} +``` + +#### Комплексный пример + +```go +package main + +import ( + "context" + "log" + + decort "github.com/rudecs/decort-sdk" + "github.com/rudecs/decort-sdk/config" + "github.com/rudecs/decort-sdk/pkg/cloudbroker/compute" +) + +func main() { + cfg := config.Config{ + AppID: "", + AppSecret: "", + SSOURL: "", + DecortURL: "", + Retries: 5, + } + + // Создание клиента + client := decort.New(cfg) + + // Создание запроса compute/list + req := compute.ListRequest{} + + resp, err := client.CloudBroker().Compute().List(context.Background(), req) + if err != nil { + log.Fatal(err) + } + + // Цепь преобразований. + filtered, _ := resp. + FilterFunc(func(ic compute.ItemCompute) bool { + return ic.GUID == 123 // Фильтр по GUID + }). + FilterByName(""). // Фильтр по имени + SortByCPU(false). // Сортировка по кол-ву ядер CPU по возрастанию + SortByCreatedTime(true). // Сортировка по времени создания по убыванию + Serialize("", "\t") // Сериализация с префиксом "" и отступом "\t" + // Serialize - терминальная функция, возвращает Serialized (обертку над []byte) + + // Запись данных в файл + err = filtered.WriteToFile("") + if err != nil { + log.Fatal(err) + } +} + +``` + ## Работа с legacy клиентом Работа с legacy клиентом применяется для пользователей, которые не используют для авторизации decs3o. @@ -475,7 +619,7 @@ func main(){ Создание клиента происходит с помощью функции-строителя `NewLegacy` из основного пакета `decort-sdk`, для избежания проблем с именами, пакету можно присвоить алиас `decort`. Функция принимает конфигурацию, возвращает структуру `DecortClient`, с помощью которой можно взаимодействовать с платформой. -### Пример +#### Пример создания legacy клиента ```go package main @@ -498,3 +642,47 @@ func main() { legacyClient := decort.NewLegacy(cfg) } ``` + +#### Пример выполнения запроса + +```go +package main + +import ( + "fmt" + + "github.com/rudecs/decort-sdk/config" + decort "github.com/rudecs/decort-sdk" +) + +func main() { + // Настройка конфигурации + legacyCfg := config.LegacyConfig{ + Username: "", + Password: "", + DecortURL: "https://mr4.digitalenergy.online", + Retries: 5, + } + + // Создание клиента + legacyClient := decort.NewLegacy(cfg) + + // Создание структуры запроса + // CreateRequest - реквест на создание виртуальной машины + req := kvmx86.CreateRequest{ + RGID: 123, + Name: "compute", + CPU: 4, + RAM: 4096, + ImageID: 321, + } + + // Выполнение запроса + res, err := client.CloudAPI().KVMX86().Create(context.Background(), req) + if err != nil { + log.Fatal(err) + } + + fmt.Println(res) +} +``` \ No newline at end of file diff --git a/client.go b/client.go index 3407585..bcb813e 100644 --- a/client.go +++ b/client.go @@ -7,12 +7,12 @@ import ( "net/http" "strings" - "github.com/rudecs/decort-sdk/pkg/cloudapi" - "github.com/rudecs/decort-sdk/pkg/cloudbroker" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker" "github.com/google/go-querystring/query" - "github.com/rudecs/decort-sdk/config" - "github.com/rudecs/decort-sdk/internal/client" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/config" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/client" ) // HTTP-client for platform diff --git a/go.mod b/go.mod index ee993d1..65b9669 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ -module github.com/rudecs/decort-sdk +module repos.digitalenergy.online/BASIS/decort-golang-sdk -go 1.19 +go 1.20 require github.com/google/go-querystring v1.1.0 diff --git a/internal/client/http-client.go b/internal/client/http-client.go index 855a49e..78d5717 100644 --- a/internal/client/http-client.go +++ b/internal/client/http-client.go @@ -5,7 +5,7 @@ import ( "net/http" "time" - "github.com/rudecs/decort-sdk/config" + "repos.digitalenergy.online/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 9514b0a..8d3c4b7 100644 --- a/internal/client/legacy-http-client.go +++ b/internal/client/legacy-http-client.go @@ -3,9 +3,10 @@ package client import ( "crypto/tls" "net/http" + "net/url" "time" - "github.com/rudecs/decort-sdk/config" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/config" ) // NewLegacyHttpClient creates legacy HTTP Client @@ -20,8 +21,8 @@ func NewLegacyHttpClient(cfg config.LegacyConfig) *http.Client { return &http.Client{ Transport: &transportLegacy{ base: transCfg, - username: cfg.Username, - password: cfg.Password, + username: url.QueryEscape(cfg.Username), + password: url.QueryEscape(cfg.Password), retries: cfg.Retries, token: cfg.Token, decortURL: cfg.DecortURL, diff --git a/internal/client/legacy-transport.go b/internal/client/legacy-transport.go index e28029a..5c4e36f 100644 --- a/internal/client/legacy-transport.go +++ b/internal/client/legacy-transport.go @@ -41,10 +41,12 @@ func (t *transportLegacy) RoundTrip(request *http.Request) (*http.Response, erro t.token = token } - tokenValue := fmt.Sprintf("authkey=%s", t.token) + tokenValue := fmt.Sprintf("&authkey=%s", t.token) tokenReader := strings.NewReader(tokenValue) - req, _ := http.NewRequestWithContext(request.Context(), request.Method, request.URL.String(), tokenReader) + newBody := io.MultiReader(request.Body, tokenReader) + + req, _ := http.NewRequestWithContext(request.Context(), request.Method, request.URL.String(), newBody) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Accept", "application/json") diff --git a/internal/serialization/serialize.go b/internal/serialization/serialize.go new file mode 100644 index 0000000..9e3cc65 --- /dev/null +++ b/internal/serialization/serialize.go @@ -0,0 +1,18 @@ +package serialization + +import ( + "os" +) + +type Writable interface { + WriteToFile(string) error +} + +type Serialized []byte + +// WriteToFile writes serialized data to specified file. +// +// Make sure to use .json extension for best compatibility. +func (s Serialized) WriteToFile(path string) error { + return os.WriteFile(path, s, 0600) +} diff --git a/legacy-client.go b/legacy-client.go index c15b1f8..a2f83b6 100644 --- a/legacy-client.go +++ b/legacy-client.go @@ -8,10 +8,10 @@ import ( "strings" "github.com/google/go-querystring/query" - "github.com/rudecs/decort-sdk/config" - "github.com/rudecs/decort-sdk/internal/client" - "github.com/rudecs/decort-sdk/pkg/cloudapi" - "github.com/rudecs/decort-sdk/pkg/cloudbroker" + "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" ) // Legacy HTTP-client for platform diff --git a/pkg/cloudapi/account.go b/pkg/cloudapi/account.go index b8caa47..55963c8 100644 --- a/pkg/cloudapi/account.go +++ b/pkg/cloudapi/account.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/account" + "repos.digitalenergy.online/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 f39188a..3e05011 100644 --- a/pkg/cloudapi/account/account.go +++ b/pkg/cloudapi/account/account.go @@ -2,7 +2,7 @@ package account import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 bb1179d..9d50aab 100644 --- a/pkg/cloudapi/account/add_user.go +++ b/pkg/cloudapi/account/add_user.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for adding permission to access to account for a user diff --git a/pkg/cloudapi/account/filter.go b/pkg/cloudapi/account/filter.go new file mode 100644 index 0000000..be18360 --- /dev/null +++ b/pkg/cloudapi/account/filter.go @@ -0,0 +1,68 @@ +package account + +// FilterByID returns ListAccounts with specified ID. +func (la ListAccounts) FilterByID(id uint64) ListAccounts { + predicate := func(ia ItemAccount) bool { + return ia.ID == id + } + + return la.FilterFunc(predicate) +} + +// FilterByName returns ListAccounts with specified Name. +func (la ListAccounts) FilterByName(name string) ListAccounts { + predicate := func(ia ItemAccount) bool { + return ia.Name == name + } + + return la.FilterFunc(predicate) +} + +// FilterByStatus returns ListAccounts with specified Status. +func (la ListAccounts) FilterByStatus(status string) ListAccounts { + predicate := func(ia ItemAccount) bool { + return ia.Status == status + } + + return la.FilterFunc(predicate) +} + +// FilterByUserGroupID returns ListAccounts with specified UserGroupID. +func (la ListAccounts) FilterByUserGroupID(userGroupID string) ListAccounts { + predicate := func(ia ItemAccount) bool { + acl := ia.ACL + + for _, item := range acl { + if item.UgroupID == userGroupID { + return true + } + } + + return false + } + + return la.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListAccounts based on a user-specified predicate. +func (la ListAccounts) FilterFunc(predicate func(ItemAccount) bool) ListAccounts { + var result ListAccounts + + for _, acc := range la { + if predicate(acc) { + result = append(result, acc) + } + } + + return result +} + +// FindOne returns first found ItemAccount. +// If none was found, returns an empty struct. +func (la ListAccounts) FindOne() ItemAccount { + if len(la) == 0 { + return ItemAccount{} + } + + return la[0] +} diff --git a/pkg/cloudapi/account/filter_test.go b/pkg/cloudapi/account/filter_test.go new file mode 100644 index 0000000..0e98cc2 --- /dev/null +++ b/pkg/cloudapi/account/filter_test.go @@ -0,0 +1,146 @@ +package account + +import ( + "testing" +) + +var accounts = ListAccounts{ + ItemAccount{ + ACL: []RecordACL{ + { + IsExplicit: true, + GUID: "", + Rights: "CXDRAU", + Status: "CONFIRMED", + Type: "U", + UgroupID: "timofey_tkachev_1@decs3o", + }, + }, + CreatedTime: 1676645275, + DeletedTime: 0, + ID: 132846, + Name: "std", + Status: "CONFIRMED", + UpdatedTime: 1676645275, + }, + ItemAccount{ + ACL: []RecordACL{ + { + IsExplicit: true, + GUID: "", + Rights: "CXDRAU", + Status: "CONFIRMED", + Type: "U", + UgroupID: "not_really_timofey_tkachev_1@decs3o", + }, + }, + CreatedTime: 1676878820, + DeletedTime: 0, + ID: 132847, + Name: "std_2", + Status: "CONFIRMED", + UpdatedTime: 1676645275, + }, + ItemAccount{ + ACL: []RecordACL{ + { + IsExplicit: true, + GUID: "", + Rights: "CXDRAU", + Status: "CONFIRMED", + Type: "U", + UgroupID: "timofey_tkachev_1@decs3o", + }, + { + IsExplicit: true, + GUID: "", + Rights: "CXDRAU", + Status: "CONFIRMED", + Type: "U", + UgroupID: "second_account@decs3o", + }, + }, + CreatedTime: 1676883850, + DeletedTime: 1676883899, + ID: 132848, + Name: "std_broker", + Status: "DELETED", + UpdatedTime: 1676878820, + }, +} + +func TestFilterByID(t *testing.T) { + actual := accounts.FilterByID(132846).FindOne() + + if actual.ID != 132846 { + t.Fatal("actual: ", actual.ID, " > expected: 132846") + } +} + +func TestFilterByUserGroupId(t *testing.T) { + actual := accounts.FilterByUserGroupID("second_account@decs3o").FindOne() + + for _, item := range actual.ACL { + if item.UgroupID == "second_account@decs3o" { + return + } + } + + t.Fatal("second_account@decs3o has not been found. expected 1 found") +} + +func TestFilterByName(t *testing.T) { + actual := accounts.FilterByName("std_broker").FindOne() + + if actual.Name != "std_broker" { + t.Fatal("actual: ", actual.Name, " >> expected: std_broker") + } +} + +func TestFilterByStatus(t *testing.T) { + actual := accounts.FilterByStatus("CONFIRMED") + + if len(actual) != 2 { + t.Fatal("Expected 2 elements in slice, found: ", len(actual)) + } + + for _, item := range actual { + if item.Status != "CONFIRMED" { + t.Fatal("expected CONFIRMED, found: ", item.Status) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := accounts.FilterFunc(func(ia ItemAccount) bool { + return ia.DeletedTime == 0 + }) + + for _, item := range actual { + if item.DeletedTime != 0 { + t.Fatal("Expected DeletedTime = 0, found: ", item.DeletedTime) + } + } +} + +func TestSortingByCreatedTime(t *testing.T) { + actual := accounts.SortByCreatedTime(false) + + if actual[0].Name != "std" { + t.Fatal("Expected account std as earliest, found: ", actual[0].Name) + } + + actual = accounts.SortByCreatedTime(true) + + if actual[0].Name != "std_broker" { + t.Fatal("Expected account std_broker as latest, found: ", actual[0].Name) + } +} + +func TestFilterEmpty(t *testing.T) { + actual := accounts.FilterByID(0) + + if len(actual) != 0 { + t.Fatal("Expected 0 found, actual: ", len(actual)) + } +} 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 0707128..5021542 100644 --- a/pkg/cloudapi/account/get_consumed_cloud_units_by_type.go +++ b/pkg/cloudapi/account/get_consumed_cloud_units_by_type.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 diff --git a/pkg/cloudapi/account/serialize.go b/pkg/cloudapi/account/serialize.go new file mode 100644 index 0000000..6c2dac5 --- /dev/null +++ b/pkg/cloudapi/account/serialize.go @@ -0,0 +1,43 @@ +package account + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (la ListAccounts) Serialize(params ...string) (serialization.Serialized, error) { + if len(la) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(la, prefix, indent) + } + + return json.Marshal(la) +} + +// 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 (ia ItemAccount) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ia, prefix, indent) + } + + return json.Marshal(ia) +} diff --git a/pkg/cloudapi/account/sorting.go b/pkg/cloudapi/account/sorting.go new file mode 100644 index 0000000..daf5ffd --- /dev/null +++ b/pkg/cloudapi/account/sorting.go @@ -0,0 +1,60 @@ +package account + +import "sort" + +// SortByCreatedTime sorts ListAccounts by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (la ListAccounts) SortByCreatedTime(inverse bool) ListAccounts { + if len(la) < 2 { + return la + } + + sort.Slice(la, func(i, j int) bool { + if inverse { + return la[i].CreatedTime > la[j].CreatedTime + } + + return la[i].CreatedTime < la[j].CreatedTime + }) + + return la +} + +// SortByUpdatedTime sorts ListAccounts by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (la ListAccounts) SortByUpdatedTime(inverse bool) ListAccounts { + if len(la) < 2 { + return la + } + + sort.Slice(la, func(i, j int) bool { + if inverse { + return la[i].UpdatedTime > la[j].UpdatedTime + } + + return la[i].UpdatedTime < la[j].UpdatedTime + }) + + return la +} + +// SortByDeletedTime sorts ListAccounts by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (la ListAccounts) SortByDeletedTime(inverse bool) ListAccounts { + if len(la) < 2 { + return la + } + + sort.Slice(la, func(i, j int) bool { + if inverse { + return la[i].DeletedTime > la[j].DeletedTime + } + + return la[i].DeletedTime < la[j].DeletedTime + }) + + return la +} diff --git a/pkg/cloudapi/account/update_user.go b/pkg/cloudapi/account/update_user.go index 720d8b3..f33e99b 100644 --- a/pkg/cloudapi/account/update_user.go +++ b/pkg/cloudapi/account/update_user.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update user access rights diff --git a/pkg/cloudapi/bservice.go b/pkg/cloudapi/bservice.go index e21d011..7372528 100644 --- a/pkg/cloudapi/bservice.go +++ b/pkg/cloudapi/bservice.go @@ -1,6 +1,6 @@ package cloudapi -import "github.com/rudecs/decort-sdk/pkg/cloudapi/bservice" +import "repos.digitalenergy.online/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 401df71..ad80d7b 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to bservice type BService struct { diff --git a/pkg/cloudapi/bservice/filter.go b/pkg/cloudapi/bservice/filter.go new file mode 100644 index 0000000..311f46b --- /dev/null +++ b/pkg/cloudapi/bservice/filter.go @@ -0,0 +1,69 @@ +package bservice + +// FilterByID returns ListBasicServices with specified ID. +func (lbs ListBasicServices) FilterByID(id uint64) ListBasicServices { + predicate := func(ibs ItemBasicService) bool { + return ibs.ID == id + } + + return lbs.FilterFunc(predicate) +} + +// FilterByName returns ListBasicServices with specified Name. +func (lbs ListBasicServices) FilterByName(name string) ListBasicServices { + predicate := func(ibs ItemBasicService) bool { + return ibs.Name == name + } + + return lbs.FilterFunc(predicate) +} + +// FilterByRGID returns ListBasicServices with specified RGID. +func (lbs ListBasicServices) FilterByRGID(rgID uint64) ListBasicServices { + predicate := func(ibs ItemBasicService) bool { + return ibs.RGID == rgID + } + + return lbs.FilterFunc(predicate) +} + +// FilterByStatus returns ListBasicServices with specified Status. +func (lbs ListBasicServices) FilterByStatus(status string) ListBasicServices { + predicate := func(ibs ItemBasicService) bool { + return ibs.Status == status + } + + return lbs.FilterFunc(predicate) +} + +// FilterByTechStatus returns ListBasicServices with specified TechStatus. +func (lbs ListBasicServices) FilterByTechStatus(techStatus string) ListBasicServices { + predicate := func(ibs ItemBasicService) bool { + return ibs.TechStatus == techStatus + } + + return lbs.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListResourceGroups based on a user-specified predicate. +func (lbs ListBasicServices) FilterFunc(predicate func(ItemBasicService) bool) ListBasicServices { + var result ListBasicServices + + for _, item := range lbs { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemBasicService +// If none was found, returns an empty struct. +func (lbs ListBasicServices) FindOne() ItemBasicService { + if len(lbs) == 0 { + return ItemBasicService{} + } + + return lbs[0] +} diff --git a/pkg/cloudapi/bservice/group_resize.go b/pkg/cloudapi/bservice/group_resize.go index a62e017..70d3fec 100644 --- a/pkg/cloudapi/bservice/group_resize.go +++ b/pkg/cloudapi/bservice/group_resize.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for resize the group diff --git a/pkg/cloudapi/bservice/serialize.go b/pkg/cloudapi/bservice/serialize.go new file mode 100644 index 0000000..a2e221a --- /dev/null +++ b/pkg/cloudapi/bservice/serialize.go @@ -0,0 +1,43 @@ +package bservice + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lbs ListBasicServices) Serialize(params ...string) (serialization.Serialized, error) { + if len(lbs) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lbs, prefix, indent) + } + + return json.Marshal(lbs) +} + +// 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 (ibs ItemBasicService) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ibs, prefix, indent) + } + + return json.Marshal(ibs) +} diff --git a/pkg/cloudapi/bservice/sorting.go b/pkg/cloudapi/bservice/sorting.go new file mode 100644 index 0000000..93ab69f --- /dev/null +++ b/pkg/cloudapi/bservice/sorting.go @@ -0,0 +1,60 @@ +package bservice + +import "sort" + +// SortByCreatedTime sorts ListBasicServices by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lbs ListBasicServices) SortByCreatedTime(inverse bool) ListBasicServices { + if len(lbs) < 2 { + return lbs + } + + sort.Slice(lbs, func(i, j int) bool { + if inverse { + return lbs[i].CreatedTime > lbs[j].CreatedTime + } + + return lbs[i].CreatedTime < lbs[j].CreatedTime + }) + + return lbs +} + +// SortByUpdatedTime sorts ListBasicServices by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lbs ListBasicServices) SortByUpdatedTime(inverse bool) ListBasicServices { + if len(lbs) < 2 { + return lbs + } + + sort.Slice(lbs, func(i, j int) bool { + if inverse { + return lbs[i].UpdatedTime > lbs[j].UpdatedTime + } + + return lbs[i].UpdatedTime < lbs[j].UpdatedTime + }) + + return lbs +} + +// SortByDeletedTime sorts ListBasicServices by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lbs ListBasicServices) SortByDeletedTime(inverse bool) ListBasicServices { + if len(lbs) < 2 { + return lbs + } + + sort.Slice(lbs, func(i, j int) bool { + if inverse { + return lbs[i].DeletedTime > lbs[j].DeletedTime + } + + return lbs[i].DeletedTime < lbs[j].DeletedTime + }) + + return lbs +} diff --git a/pkg/cloudapi/cloudapi.go b/pkg/cloudapi/cloudapi.go index e1f8968..83f3e57 100644 --- a/pkg/cloudapi/cloudapi.go +++ b/pkg/cloudapi/cloudapi.go @@ -2,7 +2,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 a3a0b55..8cdcb51 100644 --- a/pkg/cloudapi/compute.go +++ b/pkg/cloudapi/compute.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/compute" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/compute" ) // Accessing the Compute method group diff --git a/pkg/cloudapi/compute/affinity_rule_add.go b/pkg/cloudapi/compute/affinity_rule_add.go index e064537..d7fa5ca 100644 --- a/pkg/cloudapi/compute/affinity_rule_add.go +++ b/pkg/cloudapi/compute/affinity_rule_add.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add affinity rule diff --git a/pkg/cloudapi/compute/affinity_rule_remove.go b/pkg/cloudapi/compute/affinity_rule_remove.go index 60d519b..8cfa915 100644 --- a/pkg/cloudapi/compute/affinity_rule_remove.go +++ b/pkg/cloudapi/compute/affinity_rule_remove.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove affinity rule diff --git a/pkg/cloudapi/compute/anti_affinity_rule_add.go b/pkg/cloudapi/compute/anti_affinity_rule_add.go index 7013e5f..776f4c6 100644 --- a/pkg/cloudapi/compute/anti_affinity_rule_add.go +++ b/pkg/cloudapi/compute/anti_affinity_rule_add.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add anti affinity rule diff --git a/pkg/cloudapi/compute/anti_affinity_rule_remove.go b/pkg/cloudapi/compute/anti_affinity_rule_remove.go index fc352fa..1b84244 100644 --- a/pkg/cloudapi/compute/anti_affinity_rule_remove.go +++ b/pkg/cloudapi/compute/anti_affinity_rule_remove.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove anti affinity rule diff --git a/pkg/cloudapi/compute/compute.go b/pkg/cloudapi/compute/compute.go index 17974ad..e0fcc9a 100644 --- a/pkg/cloudapi/compute/compute.go +++ b/pkg/cloudapi/compute/compute.go @@ -2,7 +2,7 @@ package compute import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to compute diff --git a/pkg/cloudapi/compute/filter.go b/pkg/cloudapi/compute/filter.go new file mode 100644 index 0000000..1402123 --- /dev/null +++ b/pkg/cloudapi/compute/filter.go @@ -0,0 +1,74 @@ +package compute + +// FilterByID returns ListComputes with specified ID. +func (lc ListComputes) FilterByID(id uint64) ListComputes { + predicate := func(ic ItemCompute) bool { + return ic.ID == id + } + + return lc.FilterFunc(predicate) +} + +// FilterByName returns ListComputes with specified Name. +func (lc ListComputes) FilterByName(name string) ListComputes { + predicate := func(ic ItemCompute) bool { + return ic.Name == name + } + + return lc.FilterFunc(predicate) +} + +// FilterByStatus returns ListComputes with specified Status. +func (lc ListComputes) FilterByStatus(status string) ListComputes { + predicate := func(ic ItemCompute) bool { + return ic.Status == status + } + + return lc.FilterFunc(predicate) +} + +// FilterByTechStatus returns ListComputes with specified TechStatus. +func (lc ListComputes) FilterByTechStatus(techStatus string) ListComputes { + predicate := func(ic ItemCompute) bool { + return ic.TechStatus == techStatus + } + + return lc.FilterFunc(predicate) +} + +// FilterByDiskID return ListComputes with specified DiskID. +func (lc ListComputes) FilterByDiskID(diskID uint64) ListComputes { + predicate := func(ic ItemCompute) bool { + for _, disk := range ic.Disks { + if disk.ID == diskID { + return true + } + } + return false + } + + return lc.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListComputes based on a user-specified predicate. +func (lc ListComputes) FilterFunc(predicate func(ItemCompute) bool) ListComputes { + var result ListComputes + + for _, item := range lc { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemCompute +// If none was found, returns an empty struct. +func (lc ListComputes) FindOne() ItemCompute { + if len(lc) == 0 { + return ItemCompute{} + } + + return lc[0] +} diff --git a/pkg/cloudapi/compute/filter_test.go b/pkg/cloudapi/compute/filter_test.go new file mode 100644 index 0000000..3d27871 --- /dev/null +++ b/pkg/cloudapi/compute/filter_test.go @@ -0,0 +1,241 @@ +package compute + +import "testing" + +var computes = ListComputes{ + ItemCompute{ + ACL: []interface{}{}, + AccountID: 132847, + AccountName: "std_2", + AffinityLabel: "", + AffinityRules: []ItemRule{ + { + GUID: "", + Key: "aff_key", + Mode: "ANY", + Policy: "RECOMMENDED", + Topology: "compute", + Value: "aff_val", + }, + }, + AffinityWeight: 0, + AntiAffinityRules: []ItemRule{ + { + GUID: "", + Key: "antiaff_key", + Mode: "ANY", + Policy: "RECOMMENDED", + Topology: "compute", + Value: "antiaff_val", + }, + }, + Architecture: "X86_64", + BootOrder: []string{ + "hd", "cdrom", + }, + BootDiskSize: 0, + CloneReference: 0, + Clones: []uint64{}, + ComputeCIID: 0, + CPU: 4, + CreatedBy: "timofey_tkachev_1@decs3o", + CreatedTime: 1676975175, + CustomFields: map[string]interface{}{}, + DeletedBy: "", + DeletedTime: 0, + Description: "", + Devices: nil, + Disks: []InfoDisk{ + { + ID: 65191, + PCISlot: 6, + }, + }, + Driver: "KVM_X86", + GID: 212, + GUID: 48500, + ID: 48500, + ImageID: 9884, + Interfaces: []ItemVNFInterface{}, + LockStatus: "UNLOCKED", + ManagerID: 0, + ManagerType: "", + MigrationJob: 0, + Milestones: 363500, + Name: "test", + Pinned: false, + RAM: 4096, + ReferenceID: "c7cb19ac-af4a-4067-852f-c5572949207e", + Registered: true, + ResName: "compute-48500", + RGID: 79724, + RGName: "std_broker2", + SnapSets: []ItemSnapSet{}, + StatelessSepID: 0, + StatelessSepType: "", + Status: "ENABLED", + Tags: map[string]string{}, + TechStatus: "STOPPED", + TotalDiskSize: 2, + UpdatedBy: "", + UpdatedTime: 1677058904, + UserManaged: true, + VGPUs: []uint64{}, + VINSConnected: 0, + VirtualImageID: 0, + }, + ItemCompute{ + ACL: []interface{}{}, + AccountID: 132848, + AccountName: "std_broker", + AffinityLabel: "", + AffinityRules: []ItemRule{}, + AffinityWeight: 0, + AntiAffinityRules: []ItemRule{}, + Architecture: "X86_64", + BootOrder: []string{ + "hd", "cdrom", + }, + BootDiskSize: 0, + CloneReference: 0, + Clones: []uint64{}, + ComputeCIID: 0, + CPU: 6, + CreatedBy: "timofey_tkachev_1@decs3o", + CreatedTime: 1677579436, + CustomFields: map[string]interface{}{}, + DeletedBy: "", + DeletedTime: 0, + Description: "", + Devices: nil, + Disks: []InfoDisk{ + { + ID: 65248, + PCISlot: 6, + }, + }, + Driver: "KVM_X86", + GID: 212, + GUID: 48556, + ID: 48556, + ImageID: 9884, + Interfaces: []ItemVNFInterface{}, + LockStatus: "UNLOCKED", + ManagerID: 0, + ManagerType: "", + MigrationJob: 0, + Milestones: 363853, + Name: "compute_2", + Pinned: false, + RAM: 4096, + ReferenceID: "a542c449-5b1c-4f90-88c5-7bb5f8ae68ff", + Registered: true, + ResName: "compute-48556", + RGID: 79727, + RGName: "sdk_negative_fields_test", + SnapSets: []ItemSnapSet{}, + StatelessSepID: 0, + StatelessSepType: "", + Status: "ENABLED", + Tags: map[string]string{}, + TechStatus: "STARTED", + TotalDiskSize: 1, + UpdatedBy: "", + UpdatedTime: 1677579436, + UserManaged: true, + VGPUs: []uint64{}, + VINSConnected: 0, + VirtualImageID: 0, + }, +} + +func TestFilterByID(t *testing.T) { + actual := computes.FilterByID(48500).FindOne() + + if actual.ID != 48500 { + t.Fatal("expected ID 48500, found: ", actual.ID) + } + + actualEmpty := computes.FilterByID(0) + + if len(actualEmpty) != 0 { + t.Fatal("expected empty, actual: ", len(actualEmpty)) + } +} + +func TestFilterByName(t *testing.T) { + actual := computes.FilterByName("test").FindOne() + + if actual.Name != "test" { + t.Fatal("expected compute with name 'test', found: ", actual.Name) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := computes.FilterByStatus("ENABLED") + + for _, item := range actual { + if item.Status != "ENABLED" { + t.Fatal("expected ENABLED status, found: ", item.Status) + } + } +} + +func TestFilterByTechStatus(t *testing.T) { + actual := computes.FilterByTechStatus("STARTED").FindOne() + + if actual.ID != 48556 { + t.Fatal("expected 48556 with STARTED techStatus, found: ", actual.ID) + } +} + +func TestFilterByDiskID(t *testing.T) { + actual := computes.FilterByDiskID(65248).FindOne() + + if actual.ID != 48556 { + t.Fatal("expected 48556 with DiskID 65248, found: ", actual.ID) + } +} + +func TestFilterFunc(t *testing.T) { + actual := computes.FilterFunc(func(ic ItemCompute) bool { + return ic.Registered == true + }) + + if len(actual) != 2 { + t.Fatal("expected 2 elements found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.Registered != true { + t.Fatal("expected Registered to be true, actual: ", item.Registered) + } + } +} + +func TestSortingByCreatedTime(t *testing.T) { + actual := computes.SortByCreatedTime(false) + + if actual[0].Name != "test" { + t.Fatal("expected 'test', found: ", actual[0].Name) + } + + actual = computes.SortByCreatedTime(true) + if actual[0].Name != "compute_2" { + t.Fatal("expected 'compute_2', found: ", actual[0].Name) + } +} + +func TestSortingByCPU(t *testing.T) { + actual := computes.SortByCPU(false) + + if actual[0].CPU != 4{ + t.Fatal("expected 4 CPU cores, found: ", actual[0].CPU) + } + + actual = computes.SortByCPU(true) + + if actual[0].CPU != 6 { + t.Fatal("expected 6 CPU cores, found: ", actual[0].CPU) + } +} diff --git a/pkg/cloudapi/compute/net_attach.go b/pkg/cloudapi/compute/net_attach.go index 638dbd5..0b6236e 100644 --- a/pkg/cloudapi/compute/net_attach.go +++ b/pkg/cloudapi/compute/net_attach.go @@ -6,7 +6,7 @@ import ( "errors" "net/http" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for attach network diff --git a/pkg/cloudapi/compute/pfw_add.go b/pkg/cloudapi/compute/pfw_add.go index 79de205..9c2b4f4 100644 --- a/pkg/cloudapi/compute/pfw_add.go +++ b/pkg/cloudapi/compute/pfw_add.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add port forward rule diff --git a/pkg/cloudapi/compute/serialize.go b/pkg/cloudapi/compute/serialize.go new file mode 100644 index 0000000..fdd767b --- /dev/null +++ b/pkg/cloudapi/compute/serialize.go @@ -0,0 +1,43 @@ +package compute + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lc ListComputes) Serialize(params ...string) (serialization.Serialized, error) { + if len(lc) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lc, prefix, indent) + } + + return json.Marshal(lc) +} + +// 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 ItemCompute) 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) +} diff --git a/pkg/cloudapi/compute/sorting.go b/pkg/cloudapi/compute/sorting.go new file mode 100644 index 0000000..248407b --- /dev/null +++ b/pkg/cloudapi/compute/sorting.go @@ -0,0 +1,98 @@ +package compute + +import "sort" + +// SortByCPU sorts ListComputes by the CPU core amount in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByCPU(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].CPU > lc[j].CPU + } + + return lc[i].CPU < lc[j].CPU + }) + + return lc +} + +// SortByRAM sorts ListComputes by the RAM amount in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByRAM(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].RAM > lc[j].RAM + } + + return lc[i].RAM < lc[j].RAM + }) + + return lc +} + +// SortByCreatedTime sorts ListComputes by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByCreatedTime(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].CreatedTime > lc[j].CreatedTime + } + + return lc[i].CreatedTime < lc[j].CreatedTime + }) + + return lc +} + +// SortByUpdatedTime sorts ListComputes by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByUpdatedTime(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].UpdatedTime > lc[j].UpdatedTime + } + + return lc[i].UpdatedTime < lc[j].UpdatedTime + }) + + return lc +} + +// SortByDeletedTime sorts ListComputes by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByDeletedTime(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].DeletedTime > lc[j].DeletedTime + } + + return lc[i].DeletedTime < lc[j].DeletedTime + }) + + return lc +} diff --git a/pkg/cloudapi/compute/user_grant.go b/pkg/cloudapi/compute/user_grant.go index 7cf43e3..114accc 100644 --- a/pkg/cloudapi/compute/user_grant.go +++ b/pkg/cloudapi/compute/user_grant.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for grant access to compute diff --git a/pkg/cloudapi/compute/user_update.go b/pkg/cloudapi/compute/user_update.go index 9046223..5097dc7 100644 --- a/pkg/cloudapi/compute/user_update.go +++ b/pkg/cloudapi/compute/user_update.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update user access diff --git a/pkg/cloudapi/computeci.go b/pkg/cloudapi/computeci.go index 86c5404..f76a128 100644 --- a/pkg/cloudapi/computeci.go +++ b/pkg/cloudapi/computeci.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/computeci" + "repos.digitalenergy.online/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 47c5873..3f20988 100644 --- a/pkg/cloudapi/computeci/computeci.go +++ b/pkg/cloudapi/computeci/computeci.go @@ -2,7 +2,7 @@ package computeci import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to computeci diff --git a/pkg/cloudapi/computeci/filter.go b/pkg/cloudapi/computeci/filter.go new file mode 100644 index 0000000..ebbf697 --- /dev/null +++ b/pkg/cloudapi/computeci/filter.go @@ -0,0 +1,51 @@ +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 { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemComputeCI +// If none was found, returns an empty struct. +func (lci ListComputeCI) FindOne() ItemComputeCI { + if len(lci) == 0 { + return ItemComputeCI{} + } + + return lci[0] +} diff --git a/pkg/cloudapi/computeci/serialize.go b/pkg/cloudapi/computeci/serialize.go new file mode 100644 index 0000000..ba63b05 --- /dev/null +++ b/pkg/cloudapi/computeci/serialize.go @@ -0,0 +1,43 @@ +package computeci + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 len(lci) == 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) +} diff --git a/pkg/cloudapi/disks.go b/pkg/cloudapi/disks.go index f67f5ee..16671bb 100644 --- a/pkg/cloudapi/disks.go +++ b/pkg/cloudapi/disks.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/disks" + "repos.digitalenergy.online/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 827d317..cc0ad48 100644 --- a/pkg/cloudapi/disks/create.go +++ b/pkg/cloudapi/disks/create.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 55c100d..d7682bb 100644 --- a/pkg/cloudapi/disks/disks.go +++ b/pkg/cloudapi/disks/disks.go @@ -2,7 +2,7 @@ package disks import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to disks diff --git a/pkg/cloudapi/disks/filter.go b/pkg/cloudapi/disks/filter.go new file mode 100644 index 0000000..73188ef --- /dev/null +++ b/pkg/cloudapi/disks/filter.go @@ -0,0 +1,60 @@ +package disks + +// FilterByID returns ListDisks with specified ID. +func (ld ListDisks) FilterByID(id uint64) ListDisks { + predicate := func(idisk ItemDisk) bool { + return idisk.ID == id + } + + return ld.FilterFunc(predicate) +} + +// FilterByName returns ListDisks with specified Name. +func (ld ListDisks) FilterByName(name string) ListDisks { + predicate := func(idisk ItemDisk) bool { + return idisk.Name == name + } + + return ld.FilterFunc(predicate) +} + +// FilterByStatus returns ListDisks with specified Status. +func (ld ListDisks) FilterByStatus(status string) ListDisks { + predicate := func(idisk ItemDisk) bool { + return idisk.Status == status + } + + return ld.FilterFunc(predicate) +} + +// FilterByTechStatus returns ListDisks with specified TechStatus. +func (ld ListDisks) FilterByTechStatus(techStatus string) ListDisks { + predicate := func(idisk ItemDisk) bool { + return idisk.TechStatus == techStatus + } + + return ld.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListDisks based on a user-specified predicate. +func (ld ListDisks) FilterFunc(predicate func(ItemDisk) bool) ListDisks { + var result ListDisks + + for _, item := range ld { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemDisk +// If none was found, returns an empty struct. +func (ld ListDisks) FindOne() ItemDisk { + if len(ld) == 0 { + return ItemDisk{} + } + + return ld[0] +} diff --git a/pkg/cloudapi/disks/filter_test.go b/pkg/cloudapi/disks/filter_test.go new file mode 100644 index 0000000..aea6141 --- /dev/null +++ b/pkg/cloudapi/disks/filter_test.go @@ -0,0 +1,177 @@ +package disks + +import "testing" + +var disks = ListDisks{ + ItemDisk{ + MachineID: 0, + MachineName: "", + DeviceName: "vda", + AccountID: 132847, + AccountName: "std_2", + ACL: map[string]interface{}{}, + Computes: map[string]string{ + "48500": "test", + }, + CreatedTime: 1676975177, + DeletedTime: 0, + Description: "", + DestructionTime: 0, + GID: 212, + ID: 65191, + ImageID: 9884, + Images: []uint64{}, + IOTune: IOTune{ + TotalIOPSSec: 2000, + }, + Name: "bootdisk", + Order: 0, + Params: "", + ParentID: 0, + PCISlot: 6, + Pool: "vmstor", + PresentTo: []uint64{ + 27, + }, + PurgeTime: 0, + ResID: "sample", + ResName: "sample", + Role: "", + Shareable: false, + SizeMax: 2, + SizeUsed: 2, + Snapshots: []ItemSnapshot{}, + Status: "ASSIGNED", + TechStatus: "ALLOCATED", + Type: "B", + VMID: 48500, + }, + ItemDisk{ + MachineID: 0, + MachineName: "", + DeviceName: "vda", + AccountID: 132852, + AccountName: "std", + ACL: map[string]interface{}{}, + Computes: map[string]string{ + "48502": "stdvm2", + }, + CreatedTime: 1676982606, + DeletedTime: 0, + Description: "", + DestructionTime: 0, + GID: 212, + ID: 65193, + ImageID: 9885, + Images: []uint64{}, + IOTune: IOTune{ + TotalIOPSSec: 2000, + }, + Name: "bootdisk", + Order: 0, + Params: "", + ParentID: 0, + PCISlot: 6, + Pool: "vmstor", + PresentTo: []uint64{ + 27, + 27, + }, + PurgeTime: 0, + ResID: "sample", + ResName: "sample", + Role: "", + Shareable: false, + SizeMax: 4, + SizeUsed: 4, + Snapshots: []ItemSnapshot{}, + Status: "ASSIGNED", + TechStatus: "ALLOCATED", + Type: "B", + VMID: 48502, + }, +} + +func TestFilterByID(t *testing.T) { + actual := disks.FilterByID(65193) + + if len(actual) == 0 { + t.Fatal("No elements were found") + } + + actualItem := actual.FindOne() + + if actualItem.ID != 65193 { + t.Fatal("expected ID 65193, found: ", actualItem.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := disks.FilterByName("bootdisk") + + if len(actual) != 2 { + t.Fatal("expected 2 elements, found: ", len(actual)) + } + + for _, item := range actual { + if item.Name != "bootdisk" { + t.Fatal("expected 'bootdisk' name, found: ", item.Name) + } + } +} + +func TestFilterByStatus(t *testing.T) { + actual := disks.FilterByStatus("ASSIGNED") + + if len(actual) == 0 { + t.Fatal("No elements were found") + } + + for _, item := range actual { + if item.Status != "ASSIGNED" { + t.Fatal("expected 'ASSIGNED' status, found: ", item.Status) + } + } +} + +func TestFilterByTechStatus(t *testing.T) { + actual := disks.FilterByTechStatus("ALLOCATED") + + if len(actual) == 0 { + t.Fatal("No elements were found") + } + + for _, item := range actual { + if item.TechStatus != "ALLOCATED" { + t.Fatal("expected 'ALLOCATED' techStatus, found: ", item.TechStatus) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := disks.FilterFunc(func(id ItemDisk) bool { + return len(id.PresentTo) == 2 + }) + + if len(actual) == 0 { + t.Fatal("No elements were found") + } + + if len(actual[0].PresentTo) != 2 { + t.Fatal("expected 2 elements in PresentTo, found: ", len(actual[0].PresentTo)) + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := disks.SortByCreatedTime(false) + + if actual[0].ID != 65191 { + t.Fatal("expected ID 65191, found: ", actual[0].ID) + } + + actual = disks.SortByCreatedTime(true) + + if actual[0].ID != 65193 { + t.Fatal("expected ID 65193, found: ", actual[0].ID) + } +} diff --git a/pkg/cloudapi/disks/serialize.go b/pkg/cloudapi/disks/serialize.go new file mode 100644 index 0000000..48d6f9b --- /dev/null +++ b/pkg/cloudapi/disks/serialize.go @@ -0,0 +1,43 @@ +package disks + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (ld ListDisks) Serialize(params ...string) (serialization.Serialized, error) { + if len(ld) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ld, prefix, indent) + } + + return json.Marshal(ld) +} + +// 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 (idisk ItemDisk) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(idisk, prefix, indent) + } + + return json.Marshal(idisk) +} diff --git a/pkg/cloudapi/disks/sorting.go b/pkg/cloudapi/disks/sorting.go new file mode 100644 index 0000000..56891f9 --- /dev/null +++ b/pkg/cloudapi/disks/sorting.go @@ -0,0 +1,60 @@ +package disks + +import "sort" + +// SortByCreatedTime sorts ListDisks by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ld ListDisks) SortByCreatedTime(inverse bool) ListDisks { + if len(ld) < 2 { + return ld + } + + sort.Slice(ld, func(i, j int) bool { + if inverse { + return ld[i].CreatedTime > ld[j].CreatedTime + } + + return ld[i].CreatedTime < ld[j].CreatedTime + }) + + return ld +} + +// SortByDestructionTime sorts ListDisks by the DestructionTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ld ListDisks) SortByDestructionTime(inverse bool) ListDisks { + if len(ld) < 2 { + return ld + } + + sort.Slice(ld, func(i, j int) bool { + if inverse { + return ld[i].DestructionTime > ld[j].DestructionTime + } + + return ld[i].DestructionTime < ld[j].DestructionTime + }) + + return ld +} + +// SortByDeletedTime sorts ListDisks by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ld ListDisks) SortByDeletedTime(inverse bool) ListDisks { + if len(ld) < 2 { + return ld + } + + sort.Slice(ld, func(i, j int) bool { + if inverse { + return ld[i].DeletedTime > ld[j].DeletedTime + } + + return ld[i].DeletedTime < ld[j].DeletedTime + }) + + return ld +} diff --git a/pkg/cloudapi/extnet.go b/pkg/cloudapi/extnet.go index 7c25c0e..2d3819b 100644 --- a/pkg/cloudapi/extnet.go +++ b/pkg/cloudapi/extnet.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/extnet" + "repos.digitalenergy.online/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 41d6699..2470c7d 100644 --- a/pkg/cloudapi/extnet/extnet.go +++ b/pkg/cloudapi/extnet/extnet.go @@ -2,7 +2,7 @@ package extnet import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to extnet diff --git a/pkg/cloudapi/extnet/filter.go b/pkg/cloudapi/extnet/filter.go new file mode 100644 index 0000000..8148d25 --- /dev/null +++ b/pkg/cloudapi/extnet/filter.go @@ -0,0 +1,51 @@ +package extnet + +// FilterByID returns ListExtNets with specified ID. +func (lenet ListExtNets) FilterByID(id uint64) ListExtNets { + predicate := func(iexnet ItemExtNet) bool { + return iexnet.ID == id + } + + return lenet.FilterFunc(predicate) +} + +// FilterByName returns ListExtNets with specified Name. +func (lenet ListExtNets) FilterByName(name string) ListExtNets { + predicate := func(iexnet ItemExtNet) bool { + return iexnet.Name == name + } + + return lenet.FilterFunc(predicate) +} + +// FilterByStatus returns ListExtNets with specified Status. +func (lenet ListExtNets) FilterByStatus(status string) ListExtNets { + predicate := func(iexnet ItemExtNet) bool { + return iexnet.Status == status + } + + return lenet.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListExtNets based on a user-specified predicate. +func (lenet ListExtNets) FilterFunc(predicate func(ItemExtNet) bool) ListExtNets { + var result ListExtNets + + for _, item := range lenet { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemExtNet +// If none was found, returns an empty struct. +func (lenet ListExtNets) FindOne() ItemExtNet { + if len(lenet) == 0 { + return ItemExtNet{} + } + + return lenet[0] +} diff --git a/pkg/cloudapi/extnet/filter_test.go b/pkg/cloudapi/extnet/filter_test.go new file mode 100644 index 0000000..a9a7759 --- /dev/null +++ b/pkg/cloudapi/extnet/filter_test.go @@ -0,0 +1,65 @@ +package extnet + +import "testing" + +var extnets = ListExtNets{ + ItemExtNet{ + ID: 3, + IPCIDR: "176.118.164.0/24", + Name: "176.118.164.0/24", + Status: "ENABLED", + }, + ItemExtNet{ + ID: 10, + IPCIDR: "45.134.255.0/24", + Name: "45.134.255.0/24", + Status: "ENABLED", + }, + ItemExtNet{ + ID: 13, + IPCIDR: "88.218.249.0/24", + Name: "88.218.249.0/24", + Status: "DISABLED", + }, +} + +func TestFilterByID(t *testing.T) { + actual := extnets.FilterByID(10).FindOne() + + if actual.ID != 10 { + t.Fatal("expected ID 10, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + name := "88.218.249.0/24" + actual := extnets.FilterByName(name).FindOne() + + if actual.Name != name { + t.Fatal("expected ", name, " found: ", actual.Name) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := extnets.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 := extnets.FilterFunc(func(ien ItemExtNet) bool { + return ien.IPCIDR == ien.Name + }) + + if len(actual) != 3 { + t.Fatal("expected 3 elements, found: ", len(actual)) + } +} diff --git a/pkg/cloudapi/extnet/serialize.go b/pkg/cloudapi/extnet/serialize.go new file mode 100644 index 0000000..d00f440 --- /dev/null +++ b/pkg/cloudapi/extnet/serialize.go @@ -0,0 +1,43 @@ +package extnet + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lenet ListExtNets) Serialize(params ...string) (serialization.Serialized, error) { + if len(lenet) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lenet, prefix, indent) + } + + return json.Marshal(lenet) +} + +// 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 (ienet ItemExtNet) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ienet, prefix, indent) + } + + return json.Marshal(ienet) +} diff --git a/pkg/cloudapi/flipgroup.go b/pkg/cloudapi/flipgroup.go index 412ed1c..dc285cc 100644 --- a/pkg/cloudapi/flipgroup.go +++ b/pkg/cloudapi/flipgroup.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/flipgroup" + "repos.digitalenergy.online/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 99ceec9..619f767 100644 --- a/pkg/cloudapi/flipgroup/create.go +++ b/pkg/cloudapi/flipgroup/create.go @@ -6,7 +6,7 @@ import ( "errors" "net/http" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create FLIPGroup diff --git a/pkg/cloudapi/flipgroup/filter.go b/pkg/cloudapi/flipgroup/filter.go new file mode 100644 index 0000000..f86a4b7 --- /dev/null +++ b/pkg/cloudapi/flipgroup/filter.go @@ -0,0 +1,96 @@ +package flipgroup + +// FilterByID returns ListFLIPGroups with specified ID. +func (lfg ListFLIPGroups) FilterByID(id uint64) ListFLIPGroups { + predicate := func(ifg ItemFLIPGroup) bool { + return ifg.ID == id + } + + return lfg.FilterFunc(predicate) +} + +// FilterByAccountID returns ListFLIPGroups with specified AccountID. +func (lfg ListFLIPGroups) FilterByAccountID(accountID uint64) ListFLIPGroups { + predicate := func(ifg ItemFLIPGroup) bool { + return ifg.AccountID == accountID + } + + return lfg.FilterFunc(predicate) +} + +// FilterByRGID returns ListFLIPGroups with specified RGID. +func (lfg ListFLIPGroups) FilterByRGID(rgID uint64) ListFLIPGroups { + predicate := func(ifg ItemFLIPGroup) bool { + return ifg.RGID == rgID + } + + return lfg.FilterFunc(predicate) +} + +// FilterByName returns ListFLIPGroups with specified Name. +func (lfg ListFLIPGroups) FilterByName(name string) ListFLIPGroups { + predicate := func(ifg ItemFLIPGroup) bool { + return ifg.Name == name + } + + return lfg.FilterFunc(predicate) +} + +// FilterByStatus returns ListFLIPGroups with specified Status. +func (lfg ListFLIPGroups) FilterByStatus(status string) ListFLIPGroups { + predicate := func(ifg ItemFLIPGroup) bool { + return ifg.Status == status + } + + return lfg.FilterFunc(predicate) +} + +// FilterByCreatedBy returns ListFLIPGroups created by specified user. +func (lfg ListFLIPGroups) FilterByCreatedBy(createdBy string) ListFLIPGroups { + predicate := func(ifg ItemFLIPGroup) bool { + return ifg.CreatedBy == createdBy + } + + return lfg.FilterFunc(predicate) +} + +// FilterByUpdatedBy returns ListFLIPGroups updated by specified user. +func (lfg ListFLIPGroups) FilterByUpdatedBy(updatedBy string) ListFLIPGroups { + predicate := func(ifg ItemFLIPGroup) bool { + return ifg.UpdatedBy == updatedBy + } + + return lfg.FilterFunc(predicate) +} + +// FilterByDeletedBy returns ListFLIPGroups deleted by specified user. +func (lfg ListFLIPGroups) FilterByDeletedBy(deletedBy string) ListFLIPGroups { + predicate := func(ifg ItemFLIPGroup) bool { + return ifg.DeletedBy == deletedBy + } + + return lfg.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListFLIPGroups based on a user-specified predicate. +func (lfg ListFLIPGroups) FilterFunc(predicate func(ItemFLIPGroup) bool) ListFLIPGroups { + var result ListFLIPGroups + + for _, item := range lfg { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemFLIPGroup +// If none was found, returns an empty struct. +func (lfg ListFLIPGroups) FindOne() ItemFLIPGroup { + if len(lfg) == 0 { + return ItemFLIPGroup{} + } + + return lfg[0] +} diff --git a/pkg/cloudapi/flipgroup/flipgroup.go b/pkg/cloudapi/flipgroup/flipgroup.go index ae168e6..89184bf 100644 --- a/pkg/cloudapi/flipgroup/flipgroup.go +++ b/pkg/cloudapi/flipgroup/flipgroup.go @@ -2,7 +2,7 @@ package flipgroup import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..e540525 --- /dev/null +++ b/pkg/cloudapi/flipgroup/serialize.go @@ -0,0 +1,43 @@ +package flipgroup + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lfg ListFLIPGroups) Serialize(params ...string) (serialization.Serialized, error) { + if len(lfg) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lfg, prefix, indent) + } + + return json.Marshal(lfg) +} + +// 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 (ifg ItemFLIPGroup) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ifg, prefix, indent) + } + + return json.Marshal(ifg) +} diff --git a/pkg/cloudapi/flipgroup/sorting.go b/pkg/cloudapi/flipgroup/sorting.go new file mode 100644 index 0000000..d7c75eb --- /dev/null +++ b/pkg/cloudapi/flipgroup/sorting.go @@ -0,0 +1,60 @@ +package flipgroup + +import "sort" + +// SortByCreatedTime sorts ListFLIPGroups by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lfg ListFLIPGroups) SortByCreatedTime(inverse bool) ListFLIPGroups { + if len(lfg) < 2 { + return lfg + } + + sort.Slice(lfg, func(i, j int) bool { + if inverse { + return lfg[i].CreatedTime > lfg[j].CreatedTime + } + + return lfg[i].CreatedTime < lfg[j].CreatedTime + }) + + return lfg +} + +// SortByUpdatedTime sorts ListFLIPGroups by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lfg ListFLIPGroups) SortByUpdatedTime(inverse bool) ListFLIPGroups { + if len(lfg) < 2 { + return lfg + } + + sort.Slice(lfg, func(i, j int) bool { + if inverse { + return lfg[i].UpdatedTime > lfg[j].UpdatedTime + } + + return lfg[i].UpdatedTime < lfg[j].UpdatedTime + }) + + return lfg +} + +// SortByDeletedTime sorts ListFLIPGroups by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lfg ListFLIPGroups) SortByDeletedTime(inverse bool) ListFLIPGroups { + if len(lfg) < 2 { + return lfg + } + + sort.Slice(lfg, func(i, j int) bool { + if inverse { + return lfg[i].DeletedTime > lfg[j].DeletedTime + } + + return lfg[i].DeletedTime < lfg[j].DeletedTime + }) + + return lfg +} diff --git a/pkg/cloudapi/image.go b/pkg/cloudapi/image.go index 2f713b3..bd9389d 100644 --- a/pkg/cloudapi/image.go +++ b/pkg/cloudapi/image.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/image" + "repos.digitalenergy.online/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 073bbbd..6ade77e 100644 --- a/pkg/cloudapi/image/create.go +++ b/pkg/cloudapi/image/create.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create image diff --git a/pkg/cloudapi/image/filter.go b/pkg/cloudapi/image/filter.go new file mode 100644 index 0000000..7726491 --- /dev/null +++ b/pkg/cloudapi/image/filter.go @@ -0,0 +1,60 @@ +package image + +// FilterByID returns ListImages with specified ID. +func (li ListImages) FilterByID(id uint64) ListImages { + predicate := func(ii ItemImage) bool { + return ii.ID == id + } + + return li.FilterFunc(predicate) +} + +// FilterByName returns ListImages with specified Name. +func (li ListImages) FilterByName(name string) ListImages { + predicate := func(ii ItemImage) bool { + return ii.Name == name + } + + return li.FilterFunc(predicate) +} + +// FilterByStatus returns ListImages with specified Status. +func (li ListImages) FilterByStatus(status string) ListImages { + predicate := func(ii ItemImage) bool { + return ii.Status == status + } + + return li.FilterFunc(predicate) +} + +// FilterByBootType returns ListImages with specified BootType. +func (li ListImages) FilterByBootType(bootType string) ListImages { + predicate := func(ii ItemImage) bool { + return ii.BootType == bootType + } + + return li.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListImages based on a user-specified predicate. +func (li ListImages) FilterFunc(predicate func(ItemImage) bool) ListImages { + var result ListImages + + for _, item := range li { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemImage +// If none was found, returns an empty struct. +func (li ListImages) FindOne() ItemImage { + if len(li) == 0 { + return ItemImage{} + } + + return li[0] +} diff --git a/pkg/cloudapi/image/image.go b/pkg/cloudapi/image/image.go index b09f5c8..b70b34d 100644 --- a/pkg/cloudapi/image/image.go +++ b/pkg/cloudapi/image/image.go @@ -2,7 +2,7 @@ package image import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to image diff --git a/pkg/cloudapi/image/serialize.go b/pkg/cloudapi/image/serialize.go new file mode 100644 index 0000000..92c635b --- /dev/null +++ b/pkg/cloudapi/image/serialize.go @@ -0,0 +1,43 @@ +package image + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (li ListImages) Serialize(params ...string) (serialization.Serialized, error) { + if len(li) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(li, prefix, indent) + } + + return json.Marshal(li) +} + +// 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 (ii ItemImage) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ii, prefix, indent) + } + + return json.Marshal(ii) +} diff --git a/pkg/cloudapi/k8ci.go b/pkg/cloudapi/k8ci.go index 4a7ec88..5a74641 100644 --- a/pkg/cloudapi/k8ci.go +++ b/pkg/cloudapi/k8ci.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/k8ci" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/k8ci" ) // Accessing the K8CI method group diff --git a/pkg/cloudapi/k8ci/filter.go b/pkg/cloudapi/k8ci/filter.go new file mode 100644 index 0000000..5043acd --- /dev/null +++ b/pkg/cloudapi/k8ci/filter.go @@ -0,0 +1,42 @@ +package k8ci + +// FilterByID returns ListK8CI with specified ID. +func (lkc ListK8CI) FilterByID(id uint64) ListK8CI { + predicate := func(ikc ItemK8CI) bool { + return ikc.RecordK8CI.ID == id + } + + return lkc.FilterFunc(predicate) +} + +// FilterByName returns ListK8CI with specified Name. +func (lkc ListK8CI) FilterByName(name string) ListK8CI { + predicate := func(ikc ItemK8CI) bool { + return ikc.RecordK8CI.Name == name + } + + return lkc.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListK8CI based on a user-specified predicate. +func (lkc ListK8CI) FilterFunc(predicate func(ItemK8CI) bool) ListK8CI { + var result ListK8CI + + for _, item := range lkc { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemK8CI +// If none was found, returns an empty struct. +func (lkc ListK8CI) FindOne() ItemK8CI { + if len(lkc) == 0 { + return ItemK8CI{} + } + + return lkc[0] +} diff --git a/pkg/cloudapi/k8ci/k8ci.go b/pkg/cloudapi/k8ci/k8ci.go index fdd2eab..7eed52d 100644 --- a/pkg/cloudapi/k8ci/k8ci.go +++ b/pkg/cloudapi/k8ci/k8ci.go @@ -2,7 +2,7 @@ package k8ci import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..0fc31dd --- /dev/null +++ b/pkg/cloudapi/k8ci/serialize.go @@ -0,0 +1,43 @@ +package k8ci + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lkc ListK8CI) Serialize(params ...string) (serialization.Serialized, error) { + if len(lkc) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lkc, prefix, indent) + } + + return json.Marshal(lkc) +} + +// 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 (ikc ItemK8CI) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ikc, prefix, indent) + } + + return json.Marshal(ikc) +} diff --git a/pkg/cloudapi/k8ci/sorting.go b/pkg/cloudapi/k8ci/sorting.go new file mode 100644 index 0000000..db8b328 --- /dev/null +++ b/pkg/cloudapi/k8ci/sorting.go @@ -0,0 +1,22 @@ +package k8ci + +import "sort" + +// SortByCreatedTime sorts ListK8CI by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lkc ListK8CI) SortByCreatedTime(inverse bool) ListK8CI { + if len(lkc) < 2 { + return lkc + } + + sort.Slice(lkc, func(i, j int) bool { + if inverse { + return lkc[i].CreatedTime > lkc[j].CreatedTime + } + + return lkc[i].CreatedTime < lkc[j].CreatedTime + }) + + return lkc +} diff --git a/pkg/cloudapi/k8s.go b/pkg/cloudapi/k8s.go index 1f5d060..7c8ebd8 100644 --- a/pkg/cloudapi/k8s.go +++ b/pkg/cloudapi/k8s.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/k8s" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/k8s" ) // Accessing the K8S method group diff --git a/pkg/cloudapi/k8s/filter.go b/pkg/cloudapi/k8s/filter.go new file mode 100644 index 0000000..00c3d2d --- /dev/null +++ b/pkg/cloudapi/k8s/filter.go @@ -0,0 +1,96 @@ +package k8s + +// FilterByID returns ListK8SClusters with specified ID. +func (lkc ListK8SClusters) FilterByID(id uint64) ListK8SClusters { + predicate := func(ikc ItemK8SCluster) bool { + return ikc.ID == id + } + + return lkc.FilterFunc(predicate) +} + +// FilterByName returns ListK8SClusters with specified Name. +func (lkc ListK8SClusters) FilterByName(name string) ListK8SClusters { + predicate := func(ikc ItemK8SCluster) bool { + return ikc.Name == name + } + + return lkc.FilterFunc(predicate) +} + +// FilterByAccountID returns ListK8SClusters with specified AccountID. +func (lkc ListK8SClusters) FilterByAccountID(accountID uint64) ListK8SClusters { + predicate := func(ikc ItemK8SCluster) bool { + return ikc.AccountID == accountID + } + + return lkc.FilterFunc(predicate) +} + +// FilterByRGID returns ListK8SClusters with specified RGID. +func (lkc ListK8SClusters) FilterByRGID(rgID uint64) ListK8SClusters { + predicate := func(ikc ItemK8SCluster) bool { + return ikc.RGID == rgID + } + + return lkc.FilterFunc(predicate) +} + +// FilterByStatus returns ListK8SClusters with specified Status. +func (lkc ListK8SClusters) FilterByStatus(status string) ListK8SClusters { + predicate := func(ikc ItemK8SCluster) bool { + return ikc.Status == status + } + + return lkc.FilterFunc(predicate) +} + +// FilterByTechStatus returns ListK8SClusters with specified TechStatus. +func (lkc ListK8SClusters) FilterByTechStatus(techStatus string) ListK8SClusters { + predicate := func(ikc ItemK8SCluster) bool { + return ikc.TechStatus == techStatus + } + + return lkc.FilterFunc(predicate) +} + +// FilterByCreatedBy returns ListK8SClusters created by specified user. +func (lkc ListK8SClusters) FilterByCreatedBy(createdBy string) ListK8SClusters { + predicate := func(ikc ItemK8SCluster) bool { + return ikc.CreatedBy == createdBy + } + + return lkc.FilterFunc(predicate) +} + +// FilterByDeletedBy returns ListK8SClusters deleted by specified user. +func (lkc ListK8SClusters) FilterByDeletedBy(deletedBy string) ListK8SClusters { + predicate := func(ikc ItemK8SCluster) bool { + return ikc.DeletedBy == deletedBy + } + + return lkc.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListK8SClusters based on a user-specified predicate. +func (lkc ListK8SClusters) FilterFunc(predicate func(ItemK8SCluster) bool) ListK8SClusters { + var result ListK8SClusters + + for _, item := range lkc { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemK8SCluster +// If none was found, returns an empty struct. +func (lkc ListK8SClusters) FindOne() ItemK8SCluster { + if len(lkc) == 0 { + return ItemK8SCluster{} + } + + return lkc[0] +} diff --git a/pkg/cloudapi/k8s/filter_test.go b/pkg/cloudapi/k8s/filter_test.go new file mode 100644 index 0000000..e7ced34 --- /dev/null +++ b/pkg/cloudapi/k8s/filter_test.go @@ -0,0 +1,188 @@ +package k8s + +import "testing" + +var k8sItems = ListK8SClusters{ + ItemK8SCluster{ + AccountID: 1, + AccountName: "test_1", + ACL: []interface{}{}, + BServiceID: 1, + CIID: 1, + Config: nil, + CreatedBy: "test_user", + CreatedTime: 132454563, + DeletedBy: "", + DeletedTime: 0, + Description: "", + ExtNetID: 1, + GID: 0, + GUID: 1, + ID: 1, + LBID: 1, + Milestones: 999999, + Name: "k8s_1", + RGID: 1, + RGName: "rg_1", + Status: "ENABLED", + TechStatus: "STARTED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 0, + }, + ItemK8SCluster{ + AccountID: 2, + AccountName: "test_2", + ACL: []interface{}{}, + BServiceID: 2, + CIID: 2, + Config: nil, + CreatedBy: "test_user", + CreatedTime: 132454638, + DeletedBy: "", + DeletedTime: 0, + Description: "", + ExtNetID: 2, + GID: 0, + GUID: 2, + ID: 2, + LBID: 2, + Milestones: 999999, + Name: "k8s_2", + RGID: 2, + RGName: "rg_2", + Status: "ENABLED", + TechStatus: "STARTED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 0, + }, + ItemK8SCluster{ + AccountID: 3, + AccountName: "test_3", + ACL: []interface{}{}, + BServiceID: 3, + CIID: 3, + Config: nil, + CreatedBy: "test_user", + CreatedTime: 132454682, + DeletedBy: "", + DeletedTime: 0, + Description: "", + ExtNetID: 3, + GID: 0, + GUID: 3, + ID: 3, + LBID: 3, + Milestones: 999999, + Name: "k8s_3", + RGID: 3, + RGName: "rg_3", + Status: "DISABLED", + TechStatus: "STOPPED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 0, + }, +} + +func TestFilterByID(t *testing.T) { + actual := k8sItems.FilterByID(1).FindOne() + + if actual.ID != 1 { + t.Fatal("expected 1 ID, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := k8sItems.FilterByName("k8s_3").FindOne() + + if actual.Name != "k8s_3" { + t.Fatal("expected Name 'k8s_3', found: ", actual.Name) + } +} + +func TestFilterByAccountID(t *testing.T) { + actual := k8sItems.FilterByAccountID(2).FindOne() + + if actual.AccountID != 2 { + t.Fatal("expected AccountID 2, found: ", actual.AccountID) + } +} + +func TestFilterByRGID(t *testing.T) { + actual := k8sItems.FilterByRGID(3).FindOne() + + if actual.RGID != 3 { + t.Fatal("expected RGID 3, found: ", actual.RGID) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := k8sItems.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 TestFilterByTechStatus(t *testing.T) { + actual := k8sItems.FilterByTechStatus("STARTED") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.TechStatus != "STARTED" { + t.Fatal("expected TechStatus 'STARTED', found: ", item.TechStatus) + } + } +} + +func TestFilterByCreatedBy(t *testing.T) { + actual := k8sItems.FilterByCreatedBy("test_user") + + if len(actual) != 3 { + t.Fatal("expected 3 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.CreatedBy != "test_user" { + t.Fatal("expected CreatedBy 'test_user', found: ", item.CreatedBy) + } + } +} + +func TestFilterByDeletedBy(t *testing.T) { + actual := k8sItems.FilterByDeletedBy("test_user") + + if len(actual) != 0 { + t.Fatal("expected 0 found, actual: ", len(actual)) + } +} + +func TestFilterFunc(t *testing.T) { + actual := k8sItems.FilterFunc(func(iks ItemK8SCluster) bool { + return iks.AccountName == "test_2" + }). + FindOne() + + if actual.AccountName != "test_2" { + t.Fatal("expected AccountName 'test_2', found: ", actual.AccountName) + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := k8sItems.SortByCreatedTime(false) + + if actual[0].CreatedTime != 132454563 || actual[2].CreatedTime != 132454682 { + t.Fatal("expected ascending sort, seems to be inversed") + } +} diff --git a/pkg/cloudapi/k8s/k8s.go b/pkg/cloudapi/k8s/k8s.go index 47d3cda..5c2711e 100644 --- a/pkg/cloudapi/k8s/k8s.go +++ b/pkg/cloudapi/k8s/k8s.go @@ -2,7 +2,7 @@ package k8s import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..c2ab881 --- /dev/null +++ b/pkg/cloudapi/k8s/serialize.go @@ -0,0 +1,43 @@ +package k8s + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lkc ListK8SClusters) Serialize(params ...string) (serialization.Serialized, error) { + if len(lkc) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lkc, prefix, indent) + } + + return json.Marshal(lkc) +} + +// 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 (ikc ItemK8SCluster) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ikc, prefix, indent) + } + + return json.Marshal(ikc) +} diff --git a/pkg/cloudapi/k8s/sorting.go b/pkg/cloudapi/k8s/sorting.go new file mode 100644 index 0000000..b5259ec --- /dev/null +++ b/pkg/cloudapi/k8s/sorting.go @@ -0,0 +1,60 @@ +package k8s + +import "sort" + +// SortByCreatedTime sorts ListK8SClusters by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lkc ListK8SClusters) SortByCreatedTime(inverse bool) ListK8SClusters { + if len(lkc) < 2 { + return lkc + } + + sort.Slice(lkc, func(i, j int) bool { + if inverse { + return lkc[i].CreatedTime > lkc[j].CreatedTime + } + + return lkc[i].CreatedTime < lkc[j].CreatedTime + }) + + return lkc +} + +// SortByUpdatedTime sorts ListK8SClusters by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lkc ListK8SClusters) SortByUpdatedTime(inverse bool) ListK8SClusters { + if len(lkc) < 2 { + return lkc + } + + sort.Slice(lkc, func(i, j int) bool { + if inverse { + return lkc[i].UpdatedTime > lkc[j].UpdatedTime + } + + return lkc[i].UpdatedTime < lkc[j].UpdatedTime + }) + + return lkc +} + +// SortByDeletedTime sorts ListK8SClusters by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lkc ListK8SClusters) SortByDeletedTime(inverse bool) ListK8SClusters { + if len(lkc) < 2 { + return lkc + } + + sort.Slice(lkc, func(i, j int) bool { + if inverse { + return lkc[i].DeletedTime > lkc[j].DeletedTime + } + + return lkc[i].DeletedTime < lkc[j].DeletedTime + }) + + return lkc +} diff --git a/pkg/cloudapi/kvmppc.go b/pkg/cloudapi/kvmppc.go index f189f62..9acbd25 100644 --- a/pkg/cloudapi/kvmppc.go +++ b/pkg/cloudapi/kvmppc.go @@ -1,6 +1,6 @@ package cloudapi -import "github.com/rudecs/decort-sdk/pkg/cloudapi/kvmppc" +import "repos.digitalenergy.online/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 bb7bfa5..4b598d9 100644 --- a/pkg/cloudapi/kvmppc/kvmppc.go +++ b/pkg/cloudapi/kvmppc/kvmppc.go @@ -2,7 +2,7 @@ package kvmppc import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to KVMPPC diff --git a/pkg/cloudapi/kvmx86.go b/pkg/cloudapi/kvmx86.go index 1b50d61..5f10bb8 100644 --- a/pkg/cloudapi/kvmx86.go +++ b/pkg/cloudapi/kvmx86.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/kvmx86" + "repos.digitalenergy.online/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 8b410a3..297a5ed 100644 --- a/pkg/cloudapi/kvmx86/kvmx86.go +++ b/pkg/cloudapi/kvmx86/kvmx86.go @@ -2,7 +2,7 @@ package kvmx86 import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to KVMX86 diff --git a/pkg/cloudapi/lb.go b/pkg/cloudapi/lb.go index c5e2c2a..597aa80 100644 --- a/pkg/cloudapi/lb.go +++ b/pkg/cloudapi/lb.go @@ -1,6 +1,6 @@ package cloudapi -import "github.com/rudecs/decort-sdk/pkg/cloudapi/lb" +import "repos.digitalenergy.online/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.go b/pkg/cloudapi/lb/filter.go new file mode 100644 index 0000000..59185b3 --- /dev/null +++ b/pkg/cloudapi/lb/filter.go @@ -0,0 +1,60 @@ +package lb + +// FilterByID returns ListLB with specified ID. +func (ll ListLB) FilterByID(id uint64) ListLB { + predicate := func(ill ItemLoadBalancer) bool { + return ill.ID == id + } + + return ll.FilterFunc(predicate) +} + +// FilterByName returns ListLB with specified Name. +func (ll ListLB) FilterByName(name string) ListLB { + predicate := func(ill ItemLoadBalancer) bool { + return ill.Name == name + } + + return ll.FilterFunc(predicate) +} + +// FilterByExtNetID returns ListLB with specified ExtNetID. +func (ll ListLB) FilterByExtNetID(extNetID uint64) ListLB { + predicate := func(ill ItemLoadBalancer) bool { + return ill.ExtNetID == extNetID + } + + return ll.FilterFunc(predicate) +} + +// FilterByImageID returns ListLB with specified ImageID. +func (ll ListLB) FilterByImageID(imageID uint64) ListLB { + predicate := func(ill ItemLoadBalancer) bool { + return ill.ImageID == imageID + } + + return ll.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListLB based on a user-specified predicate. +func (ll ListLB) FilterFunc(predicate func(ItemLoadBalancer) bool) ListLB { + var result ListLB + + for _, item := range ll { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemLoadBalancer +// If none was found, returns an empty struct. +func (ll ListLB) FindOne() ItemLoadBalancer { + if len(ll) == 0 { + return ItemLoadBalancer{} + } + + return ll[0] +} diff --git a/pkg/cloudapi/lb/lb.go b/pkg/cloudapi/lb/lb.go index 1718493..0acdb21 100644 --- a/pkg/cloudapi/lb/lb.go +++ b/pkg/cloudapi/lb/lb.go @@ -2,7 +2,7 @@ package lb import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..f1affb8 --- /dev/null +++ b/pkg/cloudapi/lb/serialize.go @@ -0,0 +1,43 @@ +package lb + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (ll ListLB) Serialize(params ...string) (serialization.Serialized, error) { + if len(ll) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ll, prefix, indent) + } + + return json.Marshal(ll) +} + +// 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 (ill ItemLoadBalancer) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ill, prefix, indent) + } + + return json.Marshal(ill) +} diff --git a/pkg/cloudapi/lb/sorting.go b/pkg/cloudapi/lb/sorting.go new file mode 100644 index 0000000..5ab873a --- /dev/null +++ b/pkg/cloudapi/lb/sorting.go @@ -0,0 +1,60 @@ +package lb + +import "sort" + +// SortByCreatedTime sorts ListLB by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ll ListLB) SortByCreatedTime(inverse bool) ListLB { + if len(ll) < 2 { + return ll + } + + sort.Slice(ll, func(i, j int) bool { + if inverse { + return ll[i].CreatedTime > ll[j].CreatedTime + } + + return ll[i].CreatedTime < ll[j].CreatedTime + }) + + return ll +} + +// SortByUpdatedTime sorts ListLB by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ll ListLB) SortByUpdatedTime(inverse bool) ListLB { + if len(ll) < 2 { + return ll + } + + sort.Slice(ll, func(i, j int) bool { + if inverse { + return ll[i].UpdatedTime > ll[j].UpdatedTime + } + + return ll[i].UpdatedTime < ll[j].UpdatedTime + }) + + return ll +} + +// SortByDeletedTime sorts ListLB by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ll ListLB) SortByDeletedTime(inverse bool) ListLB { + if len(ll) < 2 { + return ll + } + + sort.Slice(ll, func(i, j int) bool { + if inverse { + return ll[i].DeletedTime > ll[j].DeletedTime + } + + return ll[i].DeletedTime < ll[j].DeletedTime + }) + + return ll +} diff --git a/pkg/cloudapi/locations/filter.go b/pkg/cloudapi/locations/filter.go new file mode 100644 index 0000000..97f132d --- /dev/null +++ b/pkg/cloudapi/locations/filter.go @@ -0,0 +1,42 @@ +package locations + +// FilterByID returns ListLocations with specified ID. +func (ll ListLocations) FilterByID(id uint64) ListLocations { + predicate := func(il ItemLocation) bool { + return il.ID == id + } + + return ll.FilterFunc(predicate) +} + +// FilterByName returns ListLocations with specified Name. +func (ll ListLocations) FilterByName(name string) ListLocations { + predicate := func(il ItemLocation) bool { + return il.Name == name + } + + return ll.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListLocations based on a user-specified predicate. +func (ll ListLocations) FilterFunc(predicate func(ItemLocation) bool) ListLocations { + var result ListLocations + + for _, item := range ll { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemLocation +// If none was found, returns an empty struct. +func (ll ListLocations) FindOne() ItemLocation { + if len(ll) == 0 { + return ItemLocation{} + } + + return ll[0] +} diff --git a/pkg/cloudapi/locations/locations.go b/pkg/cloudapi/locations/locations.go index 165c5e7..fca0904 100644 --- a/pkg/cloudapi/locations/locations.go +++ b/pkg/cloudapi/locations/locations.go @@ -2,7 +2,7 @@ package locations import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..49ce834 --- /dev/null +++ b/pkg/cloudapi/locations/serialize.go @@ -0,0 +1,43 @@ +package locations + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (ll ListLocations) Serialize(params ...string) (serialization.Serialized, error) { + if len(ll) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ll, prefix, indent) + } + + return json.Marshal(ll) +} + +// 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 (il ItemLocation) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(il, prefix, indent) + } + + return json.Marshal(il) +} diff --git a/pkg/cloudapi/locatons.go b/pkg/cloudapi/locatons.go index 4c4f6be..41c79d2 100644 --- a/pkg/cloudapi/locatons.go +++ b/pkg/cloudapi/locatons.go @@ -1,6 +1,6 @@ package cloudapi -import "github.com/rudecs/decort-sdk/pkg/cloudapi/locations" +import "repos.digitalenergy.online/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 c34c59e..9dbe177 100644 --- a/pkg/cloudapi/rg.go +++ b/pkg/cloudapi/rg.go @@ -1,6 +1,6 @@ package cloudapi -import "github.com/rudecs/decort-sdk/pkg/cloudapi/rg" +import "repos.digitalenergy.online/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 f2363ea..c3867f3 100644 --- a/pkg/cloudapi/rg/access_grant.go +++ b/pkg/cloudapi/rg/access_grant.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for grant access to resource group diff --git a/pkg/cloudapi/rg/filter.go b/pkg/cloudapi/rg/filter.go new file mode 100644 index 0000000..af6b14a --- /dev/null +++ b/pkg/cloudapi/rg/filter.go @@ -0,0 +1,78 @@ +package rg + +// FilterByID returns ListResourceGroups with specified ID. +func (lrg ListResourceGroups) FilterByID(id uint64) ListResourceGroups { + predicate := func(irg ItemResourceGroup) bool { + return irg.ID == id + } + + return lrg.FilterFunc(predicate) +} + +// FilterByName returns ListResourceGroups with specified Name. +func (lrg ListResourceGroups) FilterByName(name string) ListResourceGroups { + predicate := func(irg ItemResourceGroup) bool { + return irg.Name == name + } + + return lrg.FilterFunc(predicate) +} + +// FilterByCreatedBy return ListResourceGroups created by specified user. +func (lrg ListResourceGroups) FilterByCreatedBy(createdBy string) ListResourceGroups { + predicate := func(irg ItemResourceGroup) bool { + return irg.CreatedBy == createdBy + } + + return lrg.FilterFunc(predicate) +} + +// FilterByStatus returns ListResourceGroups with specified Status. +func (lrg ListResourceGroups) FilterByStatus(status string) ListResourceGroups { + predicate := func(irg ItemResourceGroup) bool { + return irg.Status == status + } + + return lrg.FilterFunc(predicate) +} + +// FilterByLockStatus return ListResourceGroups with specified LockStatus. +func (lrg ListResourceGroups) FilterByLockStatus(lockStatus string) ListResourceGroups { + predicate := func(irg ItemResourceGroup) bool { + return irg.LockStatus == lockStatus + } + + return lrg.FilterFunc(predicate) +} + +// FilterByDefNetType returns ListResourceGroups with specified DefNetType. +func (lrg ListResourceGroups) FilterByDefNetType(defNetType string) ListResourceGroups { + predicate := func(irg ItemResourceGroup) bool { + return irg.DefNetType == defNetType + } + + return lrg.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListResourceGroups based on a user-specified predicate. +func (lrg ListResourceGroups) FilterFunc(predicate func(irg ItemResourceGroup) bool) ListResourceGroups { + var result ListResourceGroups + + for _, rgItem := range lrg { + if predicate(rgItem) { + result = append(result, rgItem) + } + } + + return result +} + +// FindOne returns first found ItemResourceGroup. +// If none was found, returns an empty struct. +func (lrg ListResourceGroups) FindOne() ItemResourceGroup { + if len(lrg) == 0 { + return ItemResourceGroup{} + } + + return lrg[0] +} diff --git a/pkg/cloudapi/rg/rg.go b/pkg/cloudapi/rg/rg.go index 0aef1cf..c247edf 100644 --- a/pkg/cloudapi/rg/rg.go +++ b/pkg/cloudapi/rg/rg.go @@ -2,7 +2,7 @@ package rg import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..6291a27 --- /dev/null +++ b/pkg/cloudapi/rg/serialize.go @@ -0,0 +1,43 @@ +package rg + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lrg ListResourceGroups) Serialize(params ...string) (serialization.Serialized, error) { + if len(lrg) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lrg, prefix, indent) + } + + return json.Marshal(lrg) +} + +// 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 (irg ItemResourceGroup) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(irg, prefix, indent) + } + + return json.Marshal(irg) +} diff --git a/pkg/cloudapi/rg/set_def_net.go b/pkg/cloudapi/rg/set_def_net.go index 150df3e..32ad820 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set default network diff --git a/pkg/cloudapi/rg/sorting.go b/pkg/cloudapi/rg/sorting.go new file mode 100644 index 0000000..3516660 --- /dev/null +++ b/pkg/cloudapi/rg/sorting.go @@ -0,0 +1,60 @@ +package rg + +import "sort" + +// SortByCreatedTime sorts ListResourceGroups by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lrg ListResourceGroups) SortByCreatedTime(inverse bool) ListResourceGroups { + if len(lrg) < 2 { + return lrg + } + + sort.Slice(lrg, func(i, j int) bool { + if inverse { + return lrg[i].CreatedTime > lrg[j].CreatedTime + } + + return lrg[i].CreatedTime < lrg[j].CreatedTime + }) + + return lrg +} + +// SortByUpdatedTime sorts ListResourceGroups by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lrg ListResourceGroups) SortByUpdatedTime(inverse bool) ListResourceGroups { + if len(lrg) < 2 { + return lrg + } + + sort.Slice(lrg, func(i, j int) bool { + if inverse { + return lrg[i].UpdatedTime > lrg[j].UpdatedTime + } + + return lrg[i].UpdatedTime < lrg[j].UpdatedTime + }) + + return lrg +} + +// SortByDeletedTime sorts ListResourceGroups by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lrg ListResourceGroups) SortByDeletedTime(inverse bool) ListResourceGroups { + if len(lrg) < 2 { + return lrg + } + + sort.Slice(lrg, func(i, j int) bool { + if inverse { + return lrg[i].DeletedTime > lrg[j].DeletedTime + } + + return lrg[i].DeletedTime < lrg[j].DeletedTime + }) + + return lrg +} diff --git a/pkg/cloudapi/sizes.go b/pkg/cloudapi/sizes.go index 25eb6b9..9e9e82b 100644 --- a/pkg/cloudapi/sizes.go +++ b/pkg/cloudapi/sizes.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/sizes" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/sizes" ) // Accessing the Sizes method group diff --git a/pkg/cloudapi/sizes/filter.go b/pkg/cloudapi/sizes/filter.go new file mode 100644 index 0000000..a87ebd8 --- /dev/null +++ b/pkg/cloudapi/sizes/filter.go @@ -0,0 +1,42 @@ +package sizes + +// FilterByID returns ListSizes with specified ID. +func (ls ListSizes) FilterByID(id uint64) ListSizes { + predicate := func(is ItemSize) bool { + return is.ID == id + } + + return ls.FilterFunc(predicate) +} + +// FilterByName returns ListSizes with specified Name. +func (ls ListSizes) FilterByName(name string) ListSizes { + predicate := func(is ItemSize) bool { + return is.Name == name + } + + return ls.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListSizes based on a user-specified predicate. +func (ls ListSizes) FilterFunc(predicate func(ItemSize) bool) ListSizes { + var result ListSizes + + for _, item := range ls { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemSize +// If none was found, returns an empty struct. +func (ls ListSizes) FindOne() ItemSize { + if len(ls) == 0 { + return ItemSize{} + } + + return ls[0] +} diff --git a/pkg/cloudapi/sizes/serialize.go b/pkg/cloudapi/sizes/serialize.go new file mode 100644 index 0000000..dc791f3 --- /dev/null +++ b/pkg/cloudapi/sizes/serialize.go @@ -0,0 +1,43 @@ +package sizes + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (ls ListSizes) Serialize(params ...string) (serialization.Serialized, error) { + if len(ls) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ls, prefix, indent) + } + + return json.Marshal(ls) +} + +// 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 (is ItemSize) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(is, prefix, indent) + } + + return json.Marshal(is) +} diff --git a/pkg/cloudapi/sizes/sizes.go b/pkg/cloudapi/sizes/sizes.go index 8f46aa8..5947c16 100644 --- a/pkg/cloudapi/sizes/sizes.go +++ b/pkg/cloudapi/sizes/sizes.go @@ -3,7 +3,7 @@ package sizes import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creatig request to sizes diff --git a/pkg/cloudapi/tasks.go b/pkg/cloudapi/tasks.go index 471ff86..92c808d 100644 --- a/pkg/cloudapi/tasks.go +++ b/pkg/cloudapi/tasks.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/tasks" + "repos.digitalenergy.online/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 9795b86..805a1ee 100644 --- a/pkg/cloudapi/tasks/tasks.go +++ b/pkg/cloudapi/tasks/tasks.go @@ -2,7 +2,7 @@ package tasks import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to tasks diff --git a/pkg/cloudapi/vins.go b/pkg/cloudapi/vins.go index 6ecc4d3..7435d34 100644 --- a/pkg/cloudapi/vins.go +++ b/pkg/cloudapi/vins.go @@ -1,7 +1,7 @@ package cloudapi import ( - "github.com/rudecs/decort-sdk/pkg/cloudapi/vins" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudapi/vins" ) // Accessing the VINS method group diff --git a/pkg/cloudapi/vins/filter.go b/pkg/cloudapi/vins/filter.go new file mode 100644 index 0000000..e46092f --- /dev/null +++ b/pkg/cloudapi/vins/filter.go @@ -0,0 +1,78 @@ +package vins + +// FilterByID returns ListVINS with specified ID. +func (lv ListVINS) FilterByID(id uint64) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.ID == id + } + + return lv.FilterFunc(predicate) +} + +// FilterByName returns ListVINS with specified Name. +func (lv ListVINS) FilterByName(name string) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.Name == name + } + + return lv.FilterFunc(predicate) +} + +// FilterByAccountID returns ListVINS with specified AccountID. +func (lv ListVINS) FilterByAccountID(accountID uint64) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.AccountID == accountID + } + + return lv.FilterFunc(predicate) +} + +// FilterByCreatedBy returns ListVINS created by specified user. +func (lv ListVINS) FilterByCreatedBy(createdBy string) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.CreatedBy == createdBy + } + + return lv.FilterFunc(predicate) +} + +// FilterByUpdatedBy returns ListVINS updated by specified user. +func (lv ListVINS) FilterByUpdatedBy(updatedBy string) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.UpdatedBy == updatedBy + } + + return lv.FilterFunc(predicate) +} + +// FilterByDeletedBy returns ListVINS deleted by specified user. +func (lv ListVINS) FilterByDeletedBy(deletedBy string) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.DeletedBy == deletedBy + } + + return lv.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListVINS based on a user-specified predicate. +func (lv ListVINS) FilterFunc(predicate func(ItemVINS) bool) ListVINS { + var result ListVINS + + for _, item := range lv { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemVINS +// If none was found, returns an empty struct. +func (lv ListVINS) FindOne() ItemVINS { + if len(lv) == 0 { + return ItemVINS{} + } + + return lv[0] +} diff --git a/pkg/cloudapi/vins/ip_reserve.go b/pkg/cloudapi/vins/ip_reserve.go index 09b953a..da4486b 100644 --- a/pkg/cloudapi/vins/ip_reserve.go +++ b/pkg/cloudapi/vins/ip_reserve.go @@ -5,7 +5,7 @@ import ( "errors" "net/http" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..0a1c44e --- /dev/null +++ b/pkg/cloudapi/vins/serialize.go @@ -0,0 +1,43 @@ +package vins + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lv ListVINS) Serialize(params ...string) (serialization.Serialized, error) { + if len(lv) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lv, prefix, indent) + } + + return json.Marshal(lv) +} + +// 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 (iv ItemVINS) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(iv, prefix, indent) + } + + return json.Marshal(iv) +} diff --git a/pkg/cloudapi/vins/sorting.go b/pkg/cloudapi/vins/sorting.go new file mode 100644 index 0000000..df7c7c0 --- /dev/null +++ b/pkg/cloudapi/vins/sorting.go @@ -0,0 +1,60 @@ +package vins + +import "sort" + +// SortByCreatedTime sorts ListVINS by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lv ListVINS) SortByCreatedTime(inverse bool) ListVINS { + if len(lv) < 2 { + return lv + } + + sort.Slice(lv, func(i, j int) bool { + if inverse { + return lv[i].CreatedTime > lv[j].CreatedTime + } + + return lv[i].CreatedTime < lv[j].CreatedTime + }) + + return lv +} + +// SortByUpdatedTime sorts ListVINS by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lv ListVINS) SortByUpdatedTime(inverse bool) ListVINS { + if len(lv) < 2 { + return lv + } + + sort.Slice(lv, func(i, j int) bool { + if inverse { + return lv[i].UpdatedTime > lv[j].UpdatedTime + } + + return lv[i].UpdatedTime < lv[j].UpdatedTime + }) + + return lv +} + +// SortByDeletedTime sorts ListVINS by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lv ListVINS) SortByDeletedTime(inverse bool) ListVINS { + if len(lv) < 2 { + return lv + } + + sort.Slice(lv, func(i, j int) bool { + if inverse { + return lv[i].DeletedTime > lv[j].DeletedTime + } + + return lv[i].DeletedTime < lv[j].DeletedTime + }) + + return lv +} diff --git a/pkg/cloudapi/vins/vins.go b/pkg/cloudapi/vins/vins.go index 0c59fc0..0a52aa0 100644 --- a/pkg/cloudapi/vins/vins.go +++ b/pkg/cloudapi/vins/vins.go @@ -2,7 +2,7 @@ package vins import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to VINS diff --git a/pkg/cloudbroker/account.go b/pkg/cloudbroker/account.go index 97579a9..404cf11 100644 --- a/pkg/cloudbroker/account.go +++ b/pkg/cloudbroker/account.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/account" + "repos.digitalenergy.online/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 1fab6d8..41f9db6 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/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 a44435a..3030e0b 100644 --- a/pkg/cloudbroker/account/add_user.go +++ b/pkg/cloudbroker/account/add_user.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for adding permission to access to account for a user diff --git a/pkg/cloudbroker/account/filter.go b/pkg/cloudbroker/account/filter.go new file mode 100644 index 0000000..a54f554 --- /dev/null +++ b/pkg/cloudbroker/account/filter.go @@ -0,0 +1,86 @@ +package account + +// FilterByID returns ListAccounts with specified ID. +func (la ListAccounts) FilterByID(id uint64) ListAccounts { + predicate := func(ia ItemAccount) bool { + return ia.ID == id + } + + return la.FilterFunc(predicate) +} + +// FilterByName returns ListAccounts with specified Name. +func (la ListAccounts) FilterByName(name string) ListAccounts { + predicate := func(ia ItemAccount) bool { + return ia.Name == name + } + + return la.FilterFunc(predicate) +} + +// FilterByStatus returns ListAccounts with specified Status. +func (la ListAccounts) FilterByStatus(status string) ListAccounts { + predicate := func(ia ItemAccount) bool { + return ia.Status == status + } + + return la.FilterFunc(predicate) +} + +// FilterByUserGroupID returns ListAccounts with specified UserGroupID. +func (la ListAccounts) FilterByUserGroupID(userGroupID string) ListAccounts { + predicate := func(ia ItemAccount) bool { + acl := ia.ACL + + for _, item := range acl { + if item.UserGroupID == userGroupID { + return true + } + } + + return false + } + + return la.FilterFunc(predicate) +} + +// FilterByCompany returns ListAccounts with specified Company. +func (la ListAccounts) FilterByCompany(company string) ListAccounts { + predicate := func(ia ItemAccount) bool { + return ia.Company == company + } + + return la.FilterFunc(predicate) +} + +// FilterByCreatedBy returns ListAccounts created by specified user. +func (la ListAccounts) FilterByCreatedBy(createdBy string) ListAccounts { + predicate := func(ia ItemAccount) bool { + return ia.CreatedBy == createdBy + } + + return la.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListAccounts based on a user-specified predicate. +func (la ListAccounts) FilterFunc(predicate func(ItemAccount) bool) ListAccounts { + var result ListAccounts + + for _, item := range la { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemAccount. +// If none was found, returns an empty struct. +func (la ListAccounts) FindOne() ItemAccount { + if len(la) == 0 { + return ItemAccount{} + } + + return la[0] +} diff --git a/pkg/cloudbroker/account/filter_test.go b/pkg/cloudbroker/account/filter_test.go new file mode 100644 index 0000000..f8ae94a --- /dev/null +++ b/pkg/cloudbroker/account/filter_test.go @@ -0,0 +1,155 @@ +package account + +import ( + "testing" +) + +var accounts = ListAccounts{ + ItemAccount{ + Meta: []interface{}{}, + InfoAccount: InfoAccount{ + ACL: []ACL{ + { + Explicit: true, + GUID: "", + Right: "CXDRAU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "not_really_timofey_tkachev_1@decs3o", + }, + }, + CreatedTime: 1676878820, + DeletedTime: 0, + ID: 132847, + Name: "std_2", + Status: "CONFIRMED", + UpdatedTime: 1676645275, + }, + }, + ItemAccount{ + Meta: []interface{}{}, + InfoAccount: InfoAccount{ + ACL: []ACL{ + { + Explicit: true, + GUID: "", + Right: "CXDRAU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "timofey_tkachev_1@decs3o", + }, + }, + CreatedTime: 1676645275, + DeletedTime: 1677723401, + ID: 132846, + Name: "std", + Status: "DELETED", + UpdatedTime: 1676645275, + }, + }, + ItemAccount{ + Meta: []interface{}{}, + InfoAccount: InfoAccount{ + ACL: []ACL{ + { + Explicit: true, + GUID: "", + Right: "CXDRAU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "timofey_tkachev_1@decs3o", + }, + { + Explicit: true, + GUID: "", + Right: "CXDRAU", + Status: "CONFIRMED", + Type: "U", + UserGroupID: "second_account@decs3o", + }, + }, + CreatedTime: 1676883850, + DeletedTime: 1676883899, + ID: 132848, + Name: "std_broker", + Status: "DELETED", + UpdatedTime: 1676878820, + }, + }, +} + +func TestFilterByID(t *testing.T) { + actual := accounts.FilterByID(132846).FindOne() + + if actual.ID != 132846 { + t.Fatal("actual: ", actual.ID, " > expected: 132846") + } +} + +func TestFilterByUserGroupId(t *testing.T) { + actual := accounts.FilterByUserGroupID("second_account@decs3o").FindOne() + + for _, item := range actual.ACL { + if item.UserGroupID == "second_account@decs3o" { + return + } + } + + t.Fatal("second_account@decs3o has not been found. expected 1 found") +} + +func TestFilterByName(t *testing.T) { + actual := accounts.FilterByName("std_broker").FindOne() + + if actual.Name != "std_broker" { + t.Fatal("actual: ", actual.Name, " >> expected: std_broker") + } +} + +func TestFilterByStatus(t *testing.T) { + actual := accounts.FilterByStatus("DELETED") + + if len(actual) != 2 { + t.Fatal("Expected 2 elements in slice, found: ", len(actual)) + } + + for _, item := range actual { + if item.Status != "DELETED" { + t.Fatal("expected DELETED, found: ", item.Status) + } + } +} + +func TestFilterFunc(t *testing.T) { + actual := accounts.FilterFunc(func(ia ItemAccount) bool { + return ia.DeletedTime == 0 + }) + + for _, item := range actual { + if item.DeletedTime != 0 { + t.Fatal("Expected DeletedTime = 0, found: ", item.DeletedTime) + } + } +} + +func TestSortingByCreatedTime(t *testing.T) { + actual := accounts.SortByCreatedTime(false) + + if actual[0].Name != "std" { + t.Fatal("Expected account std as earliest, found: ", actual[0].Name) + } + + actual = accounts.SortByCreatedTime(true) + + if actual[0].Name != "std_broker" { + t.Fatal("Expected account std_broker as latest, found: ", actual[0].Name) + } +} + +func TestFilterEmpty(t *testing.T) { + actual := accounts.FilterByID(0) + + if len(actual) != 0 { + t.Fatal("Expected 0 found, actual: ", len(actual)) + } +} diff --git a/pkg/cloudbroker/account/serialize.go b/pkg/cloudbroker/account/serialize.go new file mode 100644 index 0000000..6c2dac5 --- /dev/null +++ b/pkg/cloudbroker/account/serialize.go @@ -0,0 +1,43 @@ +package account + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (la ListAccounts) Serialize(params ...string) (serialization.Serialized, error) { + if len(la) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(la, prefix, indent) + } + + return json.Marshal(la) +} + +// 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 (ia ItemAccount) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ia, prefix, indent) + } + + return json.Marshal(ia) +} diff --git a/pkg/cloudbroker/account/sorting.go b/pkg/cloudbroker/account/sorting.go new file mode 100644 index 0000000..daf5ffd --- /dev/null +++ b/pkg/cloudbroker/account/sorting.go @@ -0,0 +1,60 @@ +package account + +import "sort" + +// SortByCreatedTime sorts ListAccounts by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (la ListAccounts) SortByCreatedTime(inverse bool) ListAccounts { + if len(la) < 2 { + return la + } + + sort.Slice(la, func(i, j int) bool { + if inverse { + return la[i].CreatedTime > la[j].CreatedTime + } + + return la[i].CreatedTime < la[j].CreatedTime + }) + + return la +} + +// SortByUpdatedTime sorts ListAccounts by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (la ListAccounts) SortByUpdatedTime(inverse bool) ListAccounts { + if len(la) < 2 { + return la + } + + sort.Slice(la, func(i, j int) bool { + if inverse { + return la[i].UpdatedTime > la[j].UpdatedTime + } + + return la[i].UpdatedTime < la[j].UpdatedTime + }) + + return la +} + +// SortByDeletedTime sorts ListAccounts by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (la ListAccounts) SortByDeletedTime(inverse bool) ListAccounts { + if len(la) < 2 { + return la + } + + sort.Slice(la, func(i, j int) bool { + if inverse { + return la[i].DeletedTime > la[j].DeletedTime + } + + return la[i].DeletedTime < la[j].DeletedTime + }) + + return la +} diff --git a/pkg/cloudbroker/account/update_resource_types.go b/pkg/cloudbroker/account/update_resource_types.go index 3c4930b..b54d80c 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 0810fdc..8799efe 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/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 7a6e745..e86d5e5 100644 --- a/pkg/cloudbroker/compute.go +++ b/pkg/cloudbroker/compute.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/compute" + "repos.digitalenergy.online/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 778bd9d..d63f0bd 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 fbe92e1..d55ef17 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 188c249..9242fdf 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 f700444..17e1d69 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 0c5c0fc..bc6a2c7 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 0f0e1c2..d44d10f 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to compute type Compute struct { diff --git a/pkg/cloudbroker/compute/filter.go b/pkg/cloudbroker/compute/filter.go new file mode 100644 index 0000000..1402123 --- /dev/null +++ b/pkg/cloudbroker/compute/filter.go @@ -0,0 +1,74 @@ +package compute + +// FilterByID returns ListComputes with specified ID. +func (lc ListComputes) FilterByID(id uint64) ListComputes { + predicate := func(ic ItemCompute) bool { + return ic.ID == id + } + + return lc.FilterFunc(predicate) +} + +// FilterByName returns ListComputes with specified Name. +func (lc ListComputes) FilterByName(name string) ListComputes { + predicate := func(ic ItemCompute) bool { + return ic.Name == name + } + + return lc.FilterFunc(predicate) +} + +// FilterByStatus returns ListComputes with specified Status. +func (lc ListComputes) FilterByStatus(status string) ListComputes { + predicate := func(ic ItemCompute) bool { + return ic.Status == status + } + + return lc.FilterFunc(predicate) +} + +// FilterByTechStatus returns ListComputes with specified TechStatus. +func (lc ListComputes) FilterByTechStatus(techStatus string) ListComputes { + predicate := func(ic ItemCompute) bool { + return ic.TechStatus == techStatus + } + + return lc.FilterFunc(predicate) +} + +// FilterByDiskID return ListComputes with specified DiskID. +func (lc ListComputes) FilterByDiskID(diskID uint64) ListComputes { + predicate := func(ic ItemCompute) bool { + for _, disk := range ic.Disks { + if disk.ID == diskID { + return true + } + } + return false + } + + return lc.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListComputes based on a user-specified predicate. +func (lc ListComputes) FilterFunc(predicate func(ItemCompute) bool) ListComputes { + var result ListComputes + + for _, item := range lc { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemCompute +// If none was found, returns an empty struct. +func (lc ListComputes) FindOne() ItemCompute { + if len(lc) == 0 { + return ItemCompute{} + } + + return lc[0] +} diff --git a/pkg/cloudbroker/compute/filter_test.go b/pkg/cloudbroker/compute/filter_test.go new file mode 100644 index 0000000..4c0df01 --- /dev/null +++ b/pkg/cloudbroker/compute/filter_test.go @@ -0,0 +1,247 @@ +package compute + +import ( + "testing" +) + +var computes = ListComputes{ + ItemCompute{ + Disks: []InfoDisk{ + { + ID: 65191, + PCISlot: 6, + }, + }, + InfoCompute: InfoCompute{ + ACL: []interface{}{}, + AccountID: 132847, + AccountName: "std_2", + AffinityLabel: "", + AffinityRules: []ItemRule{ + { + GUID: "", + Key: "aff_key", + Mode: "ANY", + Policy: "RECOMMENDED", + Topology: "compute", + Value: "aff_val", + }, + }, + AffinityWeight: 0, + AntiAffinityRules: []ItemRule{ + { + GUID: "", + Key: "antiaff_key", + Mode: "ANY", + Policy: "RECOMMENDED", + Topology: "compute", + Value: "antiaff_val", + }, + }, + Arch: "X86_64", + BootOrder: []string{ + "hd", "cdrom", + }, + BootDiskSize: 0, + CloneReference: 0, + Clones: []uint64{}, + ComputeCIID: 0, + CPUs: 4, + CreatedBy: "timofey_tkachev_1@decs3o", + CreatedTime: 1676975175, + CustomFields: map[string]interface{}{}, + DeletedBy: "", + DeletedTime: 0, + Description: "", + Devices: nil, + Driver: "KVM_X86", + GID: 212, + GUID: 48500, + ID: 48500, + ImageID: 9884, + Interfaces: ListInterfaces{}, + LockStatus: "UNLOCKED", + ManagerID: 0, + ManagerType: "", + MigrationJob: 0, + Milestones: 363500, + Name: "test", + Pinned: false, + RAM: 4096, + ReferenceID: "c7cb19ac-af4a-4067-852f-c5572949207e", + Registered: true, + ResName: "compute-48500", + RGID: 79724, + RGName: "std_broker2", + SnapSets: ListSnapshots{}, + StatelessSEPID: 0, + StatelessSEPType: "", + Status: "ENABLED", + Tags: map[string]interface{}{}, + TechStatus: "STOPPED", + TotalDiskSize: 2, + UpdatedBy: "", + UpdatedTime: 1677058904, + UserManaged: true, + VGPUs: []uint64{}, + VINSConnected: 0, + VirtualImageID: 0, + }, + }, + ItemCompute{ + Disks: []InfoDisk{ + { + ID: 65248, + PCISlot: 6, + }, + }, + InfoCompute: InfoCompute{ + ACL: []interface{}{}, + AccountID: 132848, + AccountName: "std_broker", + AffinityLabel: "", + AffinityRules: []ItemRule{}, + AffinityWeight: 0, + AntiAffinityRules: []ItemRule{}, + Arch: "X86_64", + BootOrder: []string{ + "hd", "cdrom", + }, + BootDiskSize: 0, + CloneReference: 0, + Clones: []uint64{}, + ComputeCIID: 0, + CPUs: 6, + CreatedBy: "timofey_tkachev_1@decs3o", + CreatedTime: 1677579436, + CustomFields: map[string]interface{}{}, + DeletedBy: "", + DeletedTime: 0, + Description: "", + Devices: nil, + Driver: "KVM_X86", + GID: 212, + GUID: 48556, + ID: 48556, + ImageID: 9884, + Interfaces: ListInterfaces{}, + LockStatus: "UNLOCKED", + ManagerID: 0, + ManagerType: "", + MigrationJob: 0, + Milestones: 363853, + Name: "compute_2", + Pinned: false, + RAM: 4096, + ReferenceID: "a542c449-5b1c-4f90-88c5-7bb5f8ae68ff", + Registered: true, + ResName: "compute-48556", + RGID: 79727, + RGName: "sdk_negative_fields_test", + SnapSets: ListSnapshots{}, + StatelessSEPID: 0, + StatelessSEPType: "", + Status: "ENABLED", + Tags: map[string]interface{}{}, + TechStatus: "STARTED", + TotalDiskSize: 1, + UpdatedBy: "", + UpdatedTime: 1677579436, + UserManaged: true, + VGPUs: []uint64{}, + VINSConnected: 0, + VirtualImageID: 0, + }, + }, +} + +func TestFilterByID(t *testing.T) { + actual := computes.FilterByID(48500).FindOne() + + if actual.ID != 48500 { + t.Fatal("expected ID 48500, found: ", actual.ID) + } + + actualEmpty := computes.FilterByID(0) + + if len(actualEmpty) != 0 { + t.Fatal("expected empty, actual: ", len(actualEmpty)) + } +} + +func TestFilterByName(t *testing.T) { + actual := computes.FilterByName("compute_2").FindOne() + + if actual.Name != "compute_2" { + t.Fatal("expected compute with name 'test', found: ", actual.Name) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := computes.FilterByStatus("ENABLED") + + for _, item := range actual { + if item.Status != "ENABLED" { + t.Fatal("expected ENABLED status, found: ", item.Status) + } + } +} + +func TestFilterByTechStatus(t *testing.T) { + actual := computes.FilterByTechStatus("STARTED").FindOne() + + if actual.ID != 48556 { + t.Fatal("expected 48556 with STARTED techStatus, found: ", actual.ID) + } +} + +func TestFilterByDiskID(t *testing.T) { + actual := computes.FilterByDiskID(65248).FindOne() + + if actual.ID != 48556 { + t.Fatal("expected 48556 with DiskID 65248, found: ", actual.ID) + } +} + +func TestFilterFunc(t *testing.T) { + actual := computes.FilterFunc(func(ic ItemCompute) bool { + return ic.Registered == true + }) + + if len(actual) != 2 { + t.Fatal("expected 2 elements found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.Registered != true { + t.Fatal("expected Registered to be true, actual: ", item.Registered) + } + } +} + +func TestSortingByCreatedTime(t *testing.T) { + actual := computes.SortByCreatedTime(false) + + if actual[0].Name != "test" { + t.Fatal("expected 'test', found: ", actual[0].Name) + } + + actual = computes.SortByCreatedTime(true) + if actual[0].Name != "compute_2" { + t.Fatal("expected 'compute_2', found: ", actual[0].Name) + } +} + +func TestSortingByCPU(t *testing.T) { + actual := computes.SortByCPU(false) + + if actual[0].CPUs != 4{ + t.Fatal("expected 4 CPU cores, found: ", actual[0].CPUs) + } + + actual = computes.SortByCPU(true) + + if actual[0].CPUs != 6 { + t.Fatal("expected 6 CPU cores, found: ", actual[0].CPUs) + } +} diff --git a/pkg/cloudbroker/compute/net_attach.go b/pkg/cloudbroker/compute/net_attach.go index 1a0f673..a211e6e 100644 --- a/pkg/cloudbroker/compute/net_attach.go +++ b/pkg/cloudbroker/compute/net_attach.go @@ -6,7 +6,7 @@ import ( "errors" "net/http" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 0f9c5cb..350b50a 100644 --- a/pkg/cloudbroker/compute/net_qos.go +++ b/pkg/cloudbroker/compute/net_qos.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 2cfaba8..39ab6e7 100644 --- a/pkg/cloudbroker/compute/pfw_add.go +++ b/pkg/cloudbroker/compute/pfw_add.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..fdd767b --- /dev/null +++ b/pkg/cloudbroker/compute/serialize.go @@ -0,0 +1,43 @@ +package compute + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lc ListComputes) Serialize(params ...string) (serialization.Serialized, error) { + if len(lc) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lc, prefix, indent) + } + + return json.Marshal(lc) +} + +// 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 ItemCompute) 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) +} diff --git a/pkg/cloudbroker/compute/sorting.go b/pkg/cloudbroker/compute/sorting.go new file mode 100644 index 0000000..a7ea27f --- /dev/null +++ b/pkg/cloudbroker/compute/sorting.go @@ -0,0 +1,98 @@ +package compute + +import "sort" + +// SortByCPU sorts ListComputes by the CPU core amount in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByCPU(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].CPUs > lc[j].CPUs + } + + return lc[i].CPUs < lc[j].CPUs + }) + + return lc +} + +// SortByRAM sorts ListComputes by the RAM amount in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByRAM(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].RAM > lc[j].RAM + } + + return lc[i].RAM < lc[j].RAM + }) + + return lc +} + +// SortByCreatedTime sorts ListComputes by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByCreatedTime(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].CreatedTime > lc[j].CreatedTime + } + + return lc[i].CreatedTime < lc[j].CreatedTime + }) + + return lc +} + +// SortByUpdatedTime sorts ListComputes by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByUpdatedTime(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].UpdatedTime > lc[j].UpdatedTime + } + + return lc[i].UpdatedTime < lc[j].UpdatedTime + }) + + return lc +} + +// SortByDeletedTime sorts ListComputes by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lc ListComputes) SortByDeletedTime(inverse bool) ListComputes { + if len(lc) < 2 { + return lc + } + + sort.Slice(lc, func(i, j int) bool { + if inverse { + return lc[i].DeletedTime > lc[j].DeletedTime + } + + return lc[i].DeletedTime < lc[j].DeletedTime + }) + + return lc +} diff --git a/pkg/cloudbroker/compute/user_grant.go b/pkg/cloudbroker/compute/user_grant.go index 96087dd..21cb566 100644 --- a/pkg/cloudbroker/compute/user_grant.go +++ b/pkg/cloudbroker/compute/user_grant.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 74c847d..4e4b8dc 100644 --- a/pkg/cloudbroker/compute/user_update.go +++ b/pkg/cloudbroker/compute/user_update.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 08f0901..b42928c 100644 --- a/pkg/cloudbroker/disks.go +++ b/pkg/cloudbroker/disks.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/disks" + "repos.digitalenergy.online/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 d7626c3..e963dc9 100644 --- a/pkg/cloudbroker/disks/create.go +++ b/pkg/cloudbroker/disks/create.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 f4a8708..7b12825 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to disks type Disks struct { diff --git a/pkg/cloudbroker/disks/filter.go b/pkg/cloudbroker/disks/filter.go new file mode 100644 index 0000000..74c7478 --- /dev/null +++ b/pkg/cloudbroker/disks/filter.go @@ -0,0 +1,69 @@ +package disks + +// FilterByID returns ListDisks with specified ID. +func (ld ListDisks) FilterByID(id uint64) ListDisks { + predicate := func(idisk ItemDisk) bool { + return idisk.ID == id + } + + return ld.FilterFunc(predicate) +} + +// FilterByName returns ListDisks with specified Name. +func (ld ListDisks) FilterByName(name string) ListDisks { + predicate := func(idisk ItemDisk) bool { + return idisk.Name == name + } + + return ld.FilterFunc(predicate) +} + +// FilterByStatus returns ListDisks with specified Status. +func (ld ListDisks) FilterByStatus(status string) ListDisks { + predicate := func(idisk ItemDisk) bool { + return idisk.Status == status + } + + return ld.FilterFunc(predicate) +} + +// FilterByTechStatus returns ListDisks with specified TechStatus. +func (ld ListDisks) FilterByTechStatus(techStatus string) ListDisks { + predicate := func(idisk ItemDisk) bool { + return idisk.TechStatus == techStatus + } + + return ld.FilterFunc(predicate) +} + +// FilterByImageID returns ListDisks with specified ImageID. +func (ld ListDisks) FilterByImageID(imageID uint64) ListDisks { + predicate := func(idisk ItemDisk) bool { + return idisk.ImageID == imageID + } + + return ld.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListDisks based on a user-specified predicate. +func (ld ListDisks) FilterFunc(predicate func(ItemDisk) bool) ListDisks { + var result ListDisks + + for _, item := range ld { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemDisk +// If none was found, returns an empty struct. +func (ld ListDisks) FindOne() ItemDisk { + if len(ld) == 0 { + return ItemDisk{} + } + + return ld[0] +} diff --git a/pkg/cloudbroker/disks/filter_test.go b/pkg/cloudbroker/disks/filter_test.go new file mode 100644 index 0000000..21811b1 --- /dev/null +++ b/pkg/cloudbroker/disks/filter_test.go @@ -0,0 +1,221 @@ +package disks + +import "testing" + +var disks = ListDisks{ + ItemDisk{ + MachineID: 0, + MachineName: "", + RecordDisk: RecordDisk{ + DeviceName: "vda", + SEPType: "", + InfoDisk: InfoDisk{ + AccountID: 132847, + AccountName: "std_2", + ACL: map[string]interface{}{}, + BootPartition: 0, + Computes: map[string]string{ + "48500": "test", + }, + CreatedTime: 1676975177, + DeletedTime: 0, + Description: "", + DestructionTime: 0, + DiskPath: "", + GID: 212, + GUID: 65191, + ID: 65191, + ImageID: 9884, + Images: []uint64{}, + IOTune: IOTune{ + TotalIOPSSec: 2000, + }, + IQN: "", + Login: "", + Milestones: 363501, + Name: "bootdisk", + Order: 0, + Params: "", + ParentID: 0, + Password: "", + PCISlot: 6, + Pool: "vmstor", + PresentTo: []uint64{ + 27, + }, + PurgeAttempts: 0, + PurgeTime: 0, + RealityDeviceNumber: 0, + ReferenceID: "sample", + ResID: "sample", + ResName: "sample", + Role: "", + SEPID: 2504, + Shareable: false, + SizeMax: 2, + SizeUsed: 2, + Snapshots: []ItemSnapshot{}, + Status: "ASSIGNED", + TechStatus: "ALLOCATED", + Type: "B", + VMID: 48500, + }, + }, + }, + ItemDisk{ + MachineID: 0, + MachineName: "", + RecordDisk: RecordDisk{ + DeviceName: "vda", + SEPType: "", + InfoDisk: InfoDisk{ + AccountID: 132852, + AccountName: "std", + ACL: map[string]interface{}{}, + BootPartition: 0, + Computes: map[string]string{ + "48502": "stdvm2", + }, + CreatedTime: 1676982606, + DeletedTime: 0, + Description: "", + DestructionTime: 0, + DiskPath: "", + GID: 212, + GUID: 65193, + ID: 65193, + ImageID: 9885, + Images: []uint64{}, + IOTune: IOTune{ + TotalIOPSSec: 2000, + }, + IQN: "", + Login: "", + Milestones: 363516, + Name: "bootdisk", + Order: 0, + Params: "", + ParentID: 0, + Password: "", + PCISlot: 6, + Pool: "vmstor", + PresentTo: []uint64{ + 27, + 27, + }, + PurgeAttempts: 0, + PurgeTime: 0, + RealityDeviceNumber: 0, + ReferenceID: "sample", + ResID: "sample", + ResName: "sample", + Role: "", + SEPID: 2504, + Shareable: false, + SizeMax: 4, + SizeUsed: 4, + Snapshots: []ItemSnapshot{}, + Status: "ASSIGNED", + TechStatus: "ALLOCATED", + Type: "B", + VMID: 48502, + }, + }, + }, +} + +func TestFilterByID(t *testing.T) { + actual := disks.FilterByID(65193) + + if len(actual) == 0 { + t.Fatal("No elements were found") + } + + actualItem := actual.FindOne() + + if actualItem.ID != 65193 { + t.Fatal("expected ID 65193, found: ", actualItem.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := disks.FilterByName("bootdisk") + + if len(actual) != 2 { + t.Fatal("expected 2 elements, found: ", len(actual)) + } + + for _, item := range actual { + if item.Name != "bootdisk" { + t.Fatal("expected 'bootdisk' name, found: ", item.Name) + } + } +} + +func TestFilterByStatus(t *testing.T) { + actual := disks.FilterByStatus("ASSIGNED") + + if len(actual) == 0 { + t.Fatal("No elements were found") + } + + for _, item := range actual { + if item.Status != "ASSIGNED" { + t.Fatal("expected 'ASSIGNED' status, found: ", item.Status) + } + } +} + +func TestFilterByTechStatus(t *testing.T) { + actual := disks.FilterByTechStatus("ALLOCATED") + + if len(actual) == 0 { + t.Fatal("No elements were found") + } + + for _, item := range actual { + if item.TechStatus != "ALLOCATED" { + t.Fatal("expected 'ALLOCATED' techStatus, found: ", item.TechStatus) + } + } +} + +func TestFilterByImageID(t *testing.T) { + actual := disks.FilterByImageID(9885) + + if len(actual) == 0 { + t.Fatal("No elements were found") + } + + if actual[0].ImageID != 9885 { + t.Fatal("expected 9885 ImageID, found: ", actual[0].ImageID) + } +} + +func TestFilterFunc(t *testing.T) { + actual := disks.FilterFunc(func(id ItemDisk) bool { + return len(id.PresentTo) == 2 + }) + + if len(actual) == 0 { + t.Fatal("No elements were found") + } + + if len(actual[0].PresentTo) != 2 { + t.Fatal("expected 2 elements in PresentTo, found: ", len(actual[0].PresentTo)) + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := disks.SortByCreatedTime(false) + + if actual[0].ID != 65191 { + t.Fatal("expected ID 65191, found: ", actual[0].ID) + } + + actual = disks.SortByCreatedTime(true) + + if actual[0].ID != 65193 { + t.Fatal("expected ID 65193, found: ", actual[0].ID) + } +} diff --git a/pkg/cloudbroker/disks/serialize.go b/pkg/cloudbroker/disks/serialize.go new file mode 100644 index 0000000..48d6f9b --- /dev/null +++ b/pkg/cloudbroker/disks/serialize.go @@ -0,0 +1,43 @@ +package disks + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (ld ListDisks) Serialize(params ...string) (serialization.Serialized, error) { + if len(ld) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ld, prefix, indent) + } + + return json.Marshal(ld) +} + +// 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 (idisk ItemDisk) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(idisk, prefix, indent) + } + + return json.Marshal(idisk) +} diff --git a/pkg/cloudbroker/disks/sorting.go b/pkg/cloudbroker/disks/sorting.go new file mode 100644 index 0000000..56891f9 --- /dev/null +++ b/pkg/cloudbroker/disks/sorting.go @@ -0,0 +1,60 @@ +package disks + +import "sort" + +// SortByCreatedTime sorts ListDisks by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ld ListDisks) SortByCreatedTime(inverse bool) ListDisks { + if len(ld) < 2 { + return ld + } + + sort.Slice(ld, func(i, j int) bool { + if inverse { + return ld[i].CreatedTime > ld[j].CreatedTime + } + + return ld[i].CreatedTime < ld[j].CreatedTime + }) + + return ld +} + +// SortByDestructionTime sorts ListDisks by the DestructionTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ld ListDisks) SortByDestructionTime(inverse bool) ListDisks { + if len(ld) < 2 { + return ld + } + + sort.Slice(ld, func(i, j int) bool { + if inverse { + return ld[i].DestructionTime > ld[j].DestructionTime + } + + return ld[i].DestructionTime < ld[j].DestructionTime + }) + + return ld +} + +// SortByDeletedTime sorts ListDisks by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ld ListDisks) SortByDeletedTime(inverse bool) ListDisks { + if len(ld) < 2 { + return ld + } + + sort.Slice(ld, func(i, j int) bool { + if inverse { + return ld[i].DeletedTime > ld[j].DeletedTime + } + + return ld[i].DeletedTime < ld[j].DeletedTime + }) + + return ld +} diff --git a/pkg/cloudbroker/extnet.go b/pkg/cloudbroker/extnet.go index eb360f9..b6f7678 100644 --- a/pkg/cloudbroker/extnet.go +++ b/pkg/cloudbroker/extnet.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/extnet" + "repos.digitalenergy.online/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 1187f35..b3a3b09 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" // Structure for creating request to extnet type ExtNet struct { diff --git a/pkg/cloudbroker/extnet/filter.go b/pkg/cloudbroker/extnet/filter.go new file mode 100644 index 0000000..56f13a3 --- /dev/null +++ b/pkg/cloudbroker/extnet/filter.go @@ -0,0 +1,51 @@ +package extnet + +// FilterByID returns ListExtNet with specified ID. +func (lenet ListExtNet) FilterByID(id uint64) ListExtNet { + predicate := func(iexnet ItemExtNet) bool { + return iexnet.ID == id + } + + return lenet.FilterFunc(predicate) +} + +// FilterByName returns ListExtNet with specified Name. +func (lenet ListExtNet) FilterByName(name string) ListExtNet { + predicate := func(iexnet ItemExtNet) bool { + return iexnet.Name == name + } + + return lenet.FilterFunc(predicate) +} + +// FilterByStatus returns ListExtNet with specified Status. +func (lenet ListExtNet) FilterByStatus(status string) ListExtNet { + predicate := func(iexnet ItemExtNet) bool { + return iexnet.Status == status + } + + return lenet.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListExtNet based on a user-specified predicate. +func (lenet ListExtNet) FilterFunc(predicate func(ItemExtNet) bool) ListExtNet { + var result ListExtNet + + for _, item := range lenet { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemExtNet +// If none was found, returns an empty struct. +func (lenet ListExtNet) FindOne() ItemExtNet { + if len(lenet) == 0 { + return ItemExtNet{} + } + + return lenet[0] +} diff --git a/pkg/cloudbroker/extnet/filter_test.go b/pkg/cloudbroker/extnet/filter_test.go new file mode 100644 index 0000000..5da2e34 --- /dev/null +++ b/pkg/cloudbroker/extnet/filter_test.go @@ -0,0 +1,116 @@ +package extnet + +import "testing" + +var extnets = ListExtNet{ + ItemExtNet{ + CKey: "", + Meta: []interface{}{}, + CheckIPs: []string{}, + Default: false, + DefaultQOS: QOS{}, + Description: "", + FreeIPs: 0, + GID: 212, + GUID: 3, + ID: 3, + IPCIDR: "176.118.164.0/24", + Milestones: 1355466, + Name: "176.118.164.0/24", + NetworkID: 0, + OVSBridge: "", + PreReservationsNum: 0, + PriVNFDevID: 0, + SharedWith: []interface{}{}, + Status: "ENABLED", + VLANID: 0, + VNFs: VNFs{}, + }, + ItemExtNet{ + CKey: "", + Meta: []interface{}{}, + CheckIPs: []string{}, + Default: false, + DefaultQOS: QOS{}, + Description: "", + FreeIPs: 0, + GID: 212, + GUID: 10, + ID: 10, + IPCIDR: "45.134.255.0/24", + Milestones: 2135543, + Name: "45.134.255.0/24", + NetworkID: 0, + OVSBridge: "", + PreReservationsNum: 0, + PriVNFDevID: 0, + SharedWith: []interface{}{}, + Status: "ENABLED", + VLANID: 0, + VNFs: VNFs{}, + }, + ItemExtNet{ + CKey: "", + Meta: []interface{}{}, + CheckIPs: []string{}, + Default: false, + DefaultQOS: QOS{}, + Description: "", + FreeIPs: 0, + GID: 212, + GUID: 13, + ID: 13, + IPCIDR: "88.218.249.0/24", + Milestones: 1232134, + Name: "88.218.249.0/24", + NetworkID: 0, + OVSBridge: "", + PreReservationsNum: 0, + PriVNFDevID: 0, + SharedWith: []interface{}{}, + Status: "DISABLED", + VLANID: 0, + VNFs: VNFs{}, + }, +} + +func TestFilterByID(t *testing.T) { + actual := extnets.FilterByID(10).FindOne() + + if actual.ID != 10 { + t.Fatal("expected ID 10, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + name := "88.218.249.0/24" + actual := extnets.FilterByName(name).FindOne() + + if actual.Name != name { + t.Fatal("expected ", name, " found: ", actual.Name) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := extnets.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 := extnets.FilterFunc(func(ien ItemExtNet) bool { + return ien.IPCIDR == ien.Name + }) + + if len(actual) != 3 { + t.Fatal("expected 3 elements, found: ", len(actual)) + } +} diff --git a/pkg/cloudbroker/extnet/serialize.go b/pkg/cloudbroker/extnet/serialize.go new file mode 100644 index 0000000..1157e51 --- /dev/null +++ b/pkg/cloudbroker/extnet/serialize.go @@ -0,0 +1,43 @@ +package extnet + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lenet ListExtNet) Serialize(params ...string) (serialization.Serialized, error) { + if len(lenet) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lenet, prefix, indent) + } + + return json.Marshal(lenet) +} + +// 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 (ienet ItemExtNet) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ienet, prefix, indent) + } + + return json.Marshal(ienet) +} diff --git a/pkg/cloudbroker/grid.go b/pkg/cloudbroker/grid.go index 739a416..68a5b73 100644 --- a/pkg/cloudbroker/grid.go +++ b/pkg/cloudbroker/grid.go @@ -1,6 +1,6 @@ package cloudbroker -import "github.com/rudecs/decort-sdk/pkg/cloudbroker/grid" +import "repos.digitalenergy.online/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.go b/pkg/cloudbroker/grid/filter.go new file mode 100644 index 0000000..de17d12 --- /dev/null +++ b/pkg/cloudbroker/grid/filter.go @@ -0,0 +1,51 @@ +package grid + +// FilterByID returns ListGrids with specified ID. +func (lg ListGrids) FilterByID(id uint64) ListGrids { + predicate := func(rg RecordGrid) bool { + return rg.ID == id + } + + return lg.FilterFunc(predicate) +} + +// FilterByName returns ListGrids with specified Name. +func (lg ListGrids) FilterByName(name string) ListGrids { + predicate := func(rg RecordGrid) bool { + return rg.Name == name + } + + return lg.FilterFunc(predicate) +} + +// FilterByLocationCode returns ListGrids with specified LocationCode. +func (lg ListGrids) FilterByLocationCode(locationCode string) ListGrids { + predicate := func(rg RecordGrid) bool { + return rg.LocationCode == locationCode + } + + return lg.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListGrids based on a user-specified predicate. +func (lg ListGrids) FilterFunc(predicate func(RecordGrid) bool) ListGrids { + var result ListGrids + + for _, item := range lg { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found RecordGrid. +// If none was found, returns an empty struct. +func (lg ListGrids) FindOne() RecordGrid { + if len(lg) == 0 { + return RecordGrid{} + } + + return lg[0] +} diff --git a/pkg/cloudbroker/grid/grid.go b/pkg/cloudbroker/grid/grid.go index 28c521c..bb452a3 100644 --- a/pkg/cloudbroker/grid/grid.go +++ b/pkg/cloudbroker/grid/grid.go @@ -2,7 +2,7 @@ package grid import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..8a14b0c --- /dev/null +++ b/pkg/cloudbroker/grid/serialize.go @@ -0,0 +1,43 @@ +package grid + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lg ListGrids) Serialize(params ...string) (serialization.Serialized, error) { + if len(lg) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lg, prefix, indent) + } + + return json.Marshal(lg) +} + +// 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 (rg RecordGrid) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(rg, prefix, indent) + } + + return json.Marshal(rg) +} diff --git a/pkg/cloudbroker/image.go b/pkg/cloudbroker/image.go index 050b4ad..a49ef79 100644 --- a/pkg/cloudbroker/image.go +++ b/pkg/cloudbroker/image.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/image" + "repos.digitalenergy.online/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 862cff5..9c37ef0 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 462929e..c8bf2b8 100644 --- a/pkg/cloudbroker/image/create_image.go +++ b/pkg/cloudbroker/image/create_image.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..77eb0a6 --- /dev/null +++ b/pkg/cloudbroker/image/filter.go @@ -0,0 +1,69 @@ +package image + +// FilterById returns ListImages with specified ID. +func (li ListImages) FilterById(id uint64) ListImages { + predicate := func(ri RecordImage) bool { + return ri.ID == id + } + + return li.FilterFunc(predicate) +} + +// FilterByName returns ListImages with specified Name. +func (li ListImages) FilterByName(name string) ListImages { + predicate := func(ri RecordImage) bool { + return ri.Name == name + } + + return li.FilterFunc(predicate) +} + +// FilterByStatus returns ListImages with specified Status. +func (li ListImages) FilterByStatus(status string) ListImages { + predicate := func(ri RecordImage) bool { + return ri.Status == status + } + + return li.FilterFunc(predicate) +} + +// FilterByTechStatus returns ListImages with specified TechStatus. +func (li ListImages) FilterByTechStatus(techStatus string) ListImages { + predicate := func(ri RecordImage) bool { + return ri.TechStatus == techStatus + } + + return li.FilterFunc(predicate) +} + +// FilterByBootType returns ListImages with specified BootType. +func (li ListImages) FilterByBootType(bootType string) ListImages { + predicate := func(ri RecordImage) bool { + return ri.BootType == bootType + } + + return li.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListImages based on a user-specified predicate. +func (li ListImages) FilterFunc(predicate func(RecordImage) bool) ListImages { + var result ListImages + + for _, item := range li { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found RecordImage +// If none was found, returns an empty struct. +func (li ListImages) FindOne() RecordImage { + if len(li) == 0 { + return RecordImage{} + } + + return li[0] +} diff --git a/pkg/cloudbroker/image/image.go b/pkg/cloudbroker/image/image.go index ec90d64..f0e0020 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/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 new file mode 100644 index 0000000..60f05c9 --- /dev/null +++ b/pkg/cloudbroker/image/serialize.go @@ -0,0 +1,43 @@ +package image + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (li ListImages) Serialize(params ...string) (serialization.Serialized, error) { + if len(li) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(li, prefix, indent) + } + + return json.Marshal(li) +} + +// 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 (ri RecordImage) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ri, prefix, indent) + } + + return json.Marshal(ri) +} diff --git a/pkg/cloudbroker/image/sync_create_image.go b/pkg/cloudbroker/image/sync_create_image.go index e20e9eb..44075d8 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 aacc3d9..69c849b 100644 --- a/pkg/cloudbroker/k8ci.go +++ b/pkg/cloudbroker/k8ci.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/k8ci" + "repos.digitalenergy.online/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 fa75c1f..0bb0cf8 100644 --- a/pkg/cloudbroker/k8ci/create.go +++ b/pkg/cloudbroker/k8ci/create.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create K8CI instance diff --git a/pkg/cloudbroker/k8ci/filter.go b/pkg/cloudbroker/k8ci/filter.go new file mode 100644 index 0000000..9fd58cb --- /dev/null +++ b/pkg/cloudbroker/k8ci/filter.go @@ -0,0 +1,78 @@ +package k8ci + +// FilterByID returns ListK8CI with specified ID. +func (lkc ListK8CI) FilterByID(id uint64) ListK8CI { + predicate := func(ikc ItemK8CI) bool { + return ikc.RecordK8CI.ID == id + } + + return lkc.FilterFunc(predicate) +} + +// FilterByName returns ListK8CI with specified Name. +func (lkc ListK8CI) FilterByName(name string) ListK8CI { + predicate := func(ikc ItemK8CI) bool { + return ikc.RecordK8CI.Name == name + } + + return lkc.FilterFunc(predicate) +} + +// FilterByStatus returns ListK8CI with specified Status. +func (lkc ListK8CI) FilterByStatus(status string) ListK8CI { + predicate := func(ikc ItemK8CI) bool { + return ikc.Status == status + } + + return lkc.FilterFunc(predicate) +} + +// FilterByWorkerImageID returns ListK8CI with specified WorkerImageID. +func (lkc ListK8CI) FilterByWorkerImageID(workerImageID uint64) ListK8CI { + predicate := func(ikc ItemK8CI) bool { + return ikc.WorkerImageID == workerImageID + } + + return lkc.FilterFunc(predicate) +} + +// FilterByLBImageID returns ListK8CI with specified LBImageID. +func (lkc ListK8CI) FilterByLBImageID(lbImageID uint64) ListK8CI { + predicate := func(ikc ItemK8CI) bool { + return ikc.LBImageID == lbImageID + } + + return lkc.FilterFunc(predicate) +} + +// FilterByMasterImageID returns ListK8CI with specified MasterImageID. +func (lkc ListK8CI) FilterByMasterImageID(masterImageID uint64) ListK8CI { + predicate := func(ikc ItemK8CI) bool { + return ikc.MasterImageID == masterImageID + } + + return lkc.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListK8CI based on a user-specified predicate. +func (lkc ListK8CI) FilterFunc(predicate func(ItemK8CI) bool) ListK8CI { + var result ListK8CI + + for _, item := range lkc { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemK8CI +// If none was found, returns an empty struct. +func (lkc ListK8CI) FindOne() ItemK8CI { + if len(lkc) == 0 { + return ItemK8CI{} + } + + return lkc[0] +} diff --git a/pkg/cloudbroker/k8ci/k8ci.go b/pkg/cloudbroker/k8ci/k8ci.go index 705278d..837f401 100644 --- a/pkg/cloudbroker/k8ci/k8ci.go +++ b/pkg/cloudbroker/k8ci/k8ci.go @@ -2,7 +2,7 @@ package k8ci import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..0fc31dd --- /dev/null +++ b/pkg/cloudbroker/k8ci/serialize.go @@ -0,0 +1,43 @@ +package k8ci + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lkc ListK8CI) Serialize(params ...string) (serialization.Serialized, error) { + if len(lkc) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lkc, prefix, indent) + } + + return json.Marshal(lkc) +} + +// 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 (ikc ItemK8CI) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ikc, prefix, indent) + } + + return json.Marshal(ikc) +} diff --git a/pkg/cloudbroker/k8ci/sorting.go b/pkg/cloudbroker/k8ci/sorting.go new file mode 100644 index 0000000..db8b328 --- /dev/null +++ b/pkg/cloudbroker/k8ci/sorting.go @@ -0,0 +1,22 @@ +package k8ci + +import "sort" + +// SortByCreatedTime sorts ListK8CI by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lkc ListK8CI) SortByCreatedTime(inverse bool) ListK8CI { + if len(lkc) < 2 { + return lkc + } + + sort.Slice(lkc, func(i, j int) bool { + if inverse { + return lkc[i].CreatedTime > lkc[j].CreatedTime + } + + return lkc[i].CreatedTime < lkc[j].CreatedTime + }) + + return lkc +} diff --git a/pkg/cloudbroker/k8s.go b/pkg/cloudbroker/k8s.go index fe6d2ea..29bbab1 100644 --- a/pkg/cloudbroker/k8s.go +++ b/pkg/cloudbroker/k8s.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/k8s" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/k8s" ) // Accessing the K8S method group diff --git a/pkg/cloudbroker/k8s/filter.go b/pkg/cloudbroker/k8s/filter.go new file mode 100644 index 0000000..d5d2c3c --- /dev/null +++ b/pkg/cloudbroker/k8s/filter.go @@ -0,0 +1,96 @@ +package k8s + +// FilterByID returns ListK8S with specified ID. +func (lkc ListK8S) FilterByID(id uint64) ListK8S { + predicate := func(ikc ItemK8S) bool { + return ikc.ID == id + } + + return lkc.FilterFunc(predicate) +} + +// FilterByName returns ListK8S with specified Name. +func (lkc ListK8S) FilterByName(name string) ListK8S { + predicate := func(ikc ItemK8S) bool { + return ikc.Name == name + } + + return lkc.FilterFunc(predicate) +} + +// FilterByAccountID returns ListK8S with specified AccountID. +func (lkc ListK8S) FilterByAccountID(accountID uint64) ListK8S { + predicate := func(ikc ItemK8S) bool { + return ikc.AccountID == accountID + } + + return lkc.FilterFunc(predicate) +} + +// FilterByRGID returns ListK8S with specified RGID. +func (lkc ListK8S) FilterByRGID(rgID uint64) ListK8S { + predicate := func(ikc ItemK8S) bool { + return ikc.RGID == rgID + } + + return lkc.FilterFunc(predicate) +} + +// FilterByStatus returns ListK8S with specified Status. +func (lkc ListK8S) FilterByStatus(status string) ListK8S { + predicate := func(ikc ItemK8S) bool { + return ikc.Status == status + } + + return lkc.FilterFunc(predicate) +} + +// FilterByTechStatus returns ListK8S with specified TechStatus. +func (lkc ListK8S) FilterByTechStatus(techStatus string) ListK8S { + predicate := func(ikc ItemK8S) bool { + return ikc.TechStatus == techStatus + } + + return lkc.FilterFunc(predicate) +} + +// FilterByCreatedBy returns ListK8S created by specified user. +func (lkc ListK8S) FilterByCreatedBy(createdBy string) ListK8S { + predicate := func(ikc ItemK8S) bool { + return ikc.CreatedBy == createdBy + } + + return lkc.FilterFunc(predicate) +} + +// FilterByDeletedBy returns ListK8S deleted by specified user. +func (lkc ListK8S) FilterByDeletedBy(deletedBy string) ListK8S { + predicate := func(ikc ItemK8S) bool { + return ikc.DeletedBy == deletedBy + } + + return lkc.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListK8S based on a user-specified predicate. +func (lkc ListK8S) FilterFunc(predicate func(ItemK8S) bool) ListK8S { + var result ListK8S + + for _, item := range lkc { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemK8S +// If none was found, returns an empty struct. +func (lkc ListK8S) FindOne() ItemK8S { + if len(lkc) == 0 { + return ItemK8S{} + } + + return lkc[0] +} diff --git a/pkg/cloudbroker/k8s/filter_test.go b/pkg/cloudbroker/k8s/filter_test.go new file mode 100644 index 0000000..e572de5 --- /dev/null +++ b/pkg/cloudbroker/k8s/filter_test.go @@ -0,0 +1,197 @@ +package k8s + +import "testing" + +var k8sItems = ListK8S{ + ItemK8S{ + AccountID: 1, + AccountName: "test_1", + ACL: []interface{}{}, + BServiceID: 1, + CIID: 1, + Config: nil, + CreatedBy: "test_user", + CreatedTime: 132454563, + DeletedBy: "", + DeletedTime: 0, + Description: "", + ExtNetID: 1, + GID: 0, + GUID: 1, + ID: 1, + LBID: 1, + Milestones: 999999, + Name: "k8s_1", + RGID: 1, + RGName: "rg_1", + ServiceAccount: ServiceAccount{}, + SSHKey: "sample_key", + Status: "ENABLED", + TechStatus: "STARTED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 0, + WorkersGroup: []RecordK8SGroup{}, + }, + ItemK8S{ + AccountID: 2, + AccountName: "test_2", + ACL: []interface{}{}, + BServiceID: 2, + CIID: 2, + Config: nil, + CreatedBy: "test_user", + CreatedTime: 132454638, + DeletedBy: "", + DeletedTime: 0, + Description: "", + ExtNetID: 2, + GID: 0, + GUID: 2, + ID: 2, + LBID: 2, + Milestones: 999999, + Name: "k8s_2", + RGID: 2, + RGName: "rg_2", + ServiceAccount: ServiceAccount{}, + SSHKey: "sample_key", + Status: "ENABLED", + TechStatus: "STARTED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 0, + WorkersGroup: []RecordK8SGroup{}, + }, + ItemK8S{ + AccountID: 3, + AccountName: "test_3", + ACL: []interface{}{}, + BServiceID: 3, + CIID: 3, + Config: nil, + CreatedBy: "test_user", + CreatedTime: 132454682, + DeletedBy: "", + DeletedTime: 0, + Description: "", + ExtNetID: 3, + GID: 0, + GUID: 3, + ID: 3, + LBID: 3, + Milestones: 999999, + Name: "k8s_3", + RGID: 3, + RGName: "rg_3", + ServiceAccount: ServiceAccount{}, + SSHKey: "sample_key", + Status: "DISABLED", + TechStatus: "STOPPED", + UpdatedBy: "", + UpdatedTime: 0, + VINSID: 0, + WorkersGroup: []RecordK8SGroup{}, + }, +} + +func TestFilterByID(t *testing.T) { + actual := k8sItems.FilterByID(1).FindOne() + + if actual.ID != 1 { + t.Fatal("expected 1 ID, found: ", actual.ID) + } +} + +func TestFilterByName(t *testing.T) { + actual := k8sItems.FilterByName("k8s_3").FindOne() + + if actual.Name != "k8s_3" { + t.Fatal("expected Name 'k8s_3', found: ", actual.Name) + } +} + +func TestFilterByAccountID(t *testing.T) { + actual := k8sItems.FilterByAccountID(2).FindOne() + + if actual.AccountID != 2 { + t.Fatal("expected AccountID 2, found: ", actual.AccountID) + } +} + +func TestFilterByRGID(t *testing.T) { + actual := k8sItems.FilterByRGID(3).FindOne() + + if actual.RGID != 3 { + t.Fatal("expected RGID 3, found: ", actual.RGID) + } +} + +func TestFilterByStatus(t *testing.T) { + actual := k8sItems.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 TestFilterByTechStatus(t *testing.T) { + actual := k8sItems.FilterByTechStatus("STARTED") + + if len(actual) != 2 { + t.Fatal("expected 2 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.TechStatus != "STARTED" { + t.Fatal("expected TechStatus 'STARTED', found: ", item.TechStatus) + } + } +} + +func TestFilterByCreatedBy(t *testing.T) { + actual := k8sItems.FilterByCreatedBy("test_user") + + if len(actual) != 3 { + t.Fatal("expected 3 found, actual: ", len(actual)) + } + + for _, item := range actual { + if item.CreatedBy != "test_user" { + t.Fatal("expected CreatedBy 'test_user', found: ", item.CreatedBy) + } + } +} + +func TestFilterByDeletedBy(t *testing.T) { + actual := k8sItems.FilterByDeletedBy("test_user") + + if len(actual) != 0 { + t.Fatal("expected 0 found, actual: ", len(actual)) + } +} + +func TestFilterFunc(t *testing.T) { + actual := k8sItems.FilterFunc(func(iks ItemK8S) bool { + return iks.AccountName == "test_2" + }). + FindOne() + + if actual.AccountName != "test_2" { + t.Fatal("expected AccountName 'test_2', found: ", actual.AccountName) + } +} + +func TestSortByCreatedTime(t *testing.T) { + actual := k8sItems.SortByCreatedTime(false) + + if actual[0].CreatedTime != 132454563 || actual[2].CreatedTime != 132454682 { + t.Fatal("expected ascending sort, seems to be inversed") + } +} diff --git a/pkg/cloudbroker/k8s/k8s.go b/pkg/cloudbroker/k8s/k8s.go index 0c5a03d..93959f0 100644 --- a/pkg/cloudbroker/k8s/k8s.go +++ b/pkg/cloudbroker/k8s/k8s.go @@ -2,7 +2,7 @@ package k8s import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..a06eeac --- /dev/null +++ b/pkg/cloudbroker/k8s/serialize.go @@ -0,0 +1,43 @@ +package k8s + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lkc ListK8S) Serialize(params ...string) (serialization.Serialized, error) { + if len(lkc) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lkc, prefix, indent) + } + + return json.Marshal(lkc) +} + +// 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 (ikc ItemK8S) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ikc, prefix, indent) + } + + return json.Marshal(ikc) +} diff --git a/pkg/cloudbroker/k8s/sorting.go b/pkg/cloudbroker/k8s/sorting.go new file mode 100644 index 0000000..a8f258b --- /dev/null +++ b/pkg/cloudbroker/k8s/sorting.go @@ -0,0 +1,60 @@ +package k8s + +import "sort" + +// SortByCreatedTime sorts ListK8S by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lkc ListK8S) SortByCreatedTime(inverse bool) ListK8S { + if len(lkc) < 2 { + return lkc + } + + sort.Slice(lkc, func(i, j int) bool { + if inverse { + return lkc[i].CreatedTime > lkc[j].CreatedTime + } + + return lkc[i].CreatedTime < lkc[j].CreatedTime + }) + + return lkc +} + +// SortByUpdatedTime sorts ListK8S by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lkc ListK8S) SortByUpdatedTime(inverse bool) ListK8S { + if len(lkc) < 2 { + return lkc + } + + sort.Slice(lkc, func(i, j int) bool { + if inverse { + return lkc[i].UpdatedTime > lkc[j].UpdatedTime + } + + return lkc[i].UpdatedTime < lkc[j].UpdatedTime + }) + + return lkc +} + +// SortByDeletedTime sorts ListK8S by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lkc ListK8S) SortByDeletedTime(inverse bool) ListK8S { + if len(lkc) < 2 { + return lkc + } + + sort.Slice(lkc, func(i, j int) bool { + if inverse { + return lkc[i].DeletedTime > lkc[j].DeletedTime + } + + return lkc[i].DeletedTime < lkc[j].DeletedTime + }) + + return lkc +} diff --git a/pkg/cloudbroker/kvmppc.go b/pkg/cloudbroker/kvmppc.go index abba4ce..73fb584 100644 --- a/pkg/cloudbroker/kvmppc.go +++ b/pkg/cloudbroker/kvmppc.go @@ -1,6 +1,6 @@ package cloudbroker -import "github.com/rudecs/decort-sdk/pkg/cloudbroker/kvmppc" +import "repos.digitalenergy.online/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 89ddebd..9e7e6cc 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/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 f9e7d50..6368590 100644 --- a/pkg/cloudbroker/kvmx86.go +++ b/pkg/cloudbroker/kvmx86.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/kvmx86" + "repos.digitalenergy.online/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 b19ec30..7f47fc5 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/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 3f811c3..17f4839 100644 --- a/pkg/cloudbroker/lb.go +++ b/pkg/cloudbroker/lb.go @@ -1,6 +1,6 @@ package cloudbroker -import "github.com/rudecs/decort-sdk/pkg/cloudbroker/lb" +import "repos.digitalenergy.online/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 1fead33..fb76676 100644 --- a/pkg/cloudbroker/lb/backend_create.go +++ b/pkg/cloudbroker/lb/backend_create.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create backend diff --git a/pkg/cloudbroker/lb/filter.go b/pkg/cloudbroker/lb/filter.go new file mode 100644 index 0000000..774f0a2 --- /dev/null +++ b/pkg/cloudbroker/lb/filter.go @@ -0,0 +1,60 @@ +package lb + +// FilterByID returns ListLB with specified ID. +func (ll ListLB) FilterByID(id uint64) ListLB { + predicate := func(rlb RecordLB) bool { + return rlb.ID == id + } + + return ll.FilterFunc(predicate) +} + +// FilterByName returns ListLB with specified Name. +func (ll ListLB) FilterByName(name string) ListLB { + predicate := func(rlb RecordLB) bool { + return rlb.Name == name + } + + return ll.FilterFunc(predicate) +} + +// FilterByExtNetID returns ListLB with specified ExtNetID. +func (ll ListLB) FilterByExtNetID(extNetID uint64) ListLB { + predicate := func(rlb RecordLB) bool { + return rlb.ExtNetID == extNetID + } + + return ll.FilterFunc(predicate) +} + +// FilterByImageID returns ListLB with specified ImageID. +func (ll ListLB) FilterByImageID(imageID uint64) ListLB { + predicate := func(rlb RecordLB) bool { + return rlb.ImageID == imageID + } + + return ll.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListLB based on a user-specified predicate. +func (ll ListLB) FilterFunc(predicate func(RecordLB) bool) ListLB { + var result ListLB + + for _, item := range ll { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found RecordLB +// If none was found, returns an empty struct. +func (ll ListLB) FindOne() RecordLB { + if len(ll) == 0 { + return RecordLB{} + } + + return ll[0] +} diff --git a/pkg/cloudbroker/lb/lb.go b/pkg/cloudbroker/lb/lb.go index 33fc220..b4e5f48 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 "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/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 new file mode 100644 index 0000000..c91373b --- /dev/null +++ b/pkg/cloudbroker/lb/serialize.go @@ -0,0 +1,43 @@ +package lb + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (ll ListLB) Serialize(params ...string) (serialization.Serialized, error) { + if len(ll) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(ll, prefix, indent) + } + + return json.Marshal(ll) +} + +// 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 (rlb RecordLB) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(rlb, prefix, indent) + } + + return json.Marshal(rlb) +} diff --git a/pkg/cloudbroker/lb/sorting.go b/pkg/cloudbroker/lb/sorting.go new file mode 100644 index 0000000..5ab873a --- /dev/null +++ b/pkg/cloudbroker/lb/sorting.go @@ -0,0 +1,60 @@ +package lb + +import "sort" + +// SortByCreatedTime sorts ListLB by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ll ListLB) SortByCreatedTime(inverse bool) ListLB { + if len(ll) < 2 { + return ll + } + + sort.Slice(ll, func(i, j int) bool { + if inverse { + return ll[i].CreatedTime > ll[j].CreatedTime + } + + return ll[i].CreatedTime < ll[j].CreatedTime + }) + + return ll +} + +// SortByUpdatedTime sorts ListLB by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ll ListLB) SortByUpdatedTime(inverse bool) ListLB { + if len(ll) < 2 { + return ll + } + + sort.Slice(ll, func(i, j int) bool { + if inverse { + return ll[i].UpdatedTime > ll[j].UpdatedTime + } + + return ll[i].UpdatedTime < ll[j].UpdatedTime + }) + + return ll +} + +// SortByDeletedTime sorts ListLB by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (ll ListLB) SortByDeletedTime(inverse bool) ListLB { + if len(ll) < 2 { + return ll + } + + sort.Slice(ll, func(i, j int) bool { + if inverse { + return ll[i].DeletedTime > ll[j].DeletedTime + } + + return ll[i].DeletedTime < ll[j].DeletedTime + }) + + return ll +} diff --git a/pkg/cloudbroker/rg.go b/pkg/cloudbroker/rg.go index 2198f03..4ba5a9c 100644 --- a/pkg/cloudbroker/rg.go +++ b/pkg/cloudbroker/rg.go @@ -1,6 +1,6 @@ package cloudbroker -import "github.com/rudecs/decort-sdk/pkg/cloudbroker/rg" +import "repos.digitalenergy.online/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 30e7764..8a67c3f 100644 --- a/pkg/cloudbroker/rg/access_grant.go +++ b/pkg/cloudbroker/rg/access_grant.go @@ -6,7 +6,7 @@ import ( "net/http" "strconv" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for grant access to resource group diff --git a/pkg/cloudbroker/rg/filter.go b/pkg/cloudbroker/rg/filter.go new file mode 100644 index 0000000..936d5ef --- /dev/null +++ b/pkg/cloudbroker/rg/filter.go @@ -0,0 +1,87 @@ +package rg + +// FilterByID returns ListRG with specified ID. +func (lrg ListRG) FilterByID(id uint64) ListRG { + predicate := func(irg ItemRG) bool { + return irg.ID == id + } + + return lrg.FilterFunc(predicate) +} + +// FilterByName returns ListRG with specified Name. +func (lrg ListRG) FilterByName(name string) ListRG { + predicate := func(irg ItemRG) bool { + return irg.Name == name + } + + return lrg.FilterFunc(predicate) +} + +// FilterByCreatedBy return ListRG created by specified user. +func (lrg ListRG) FilterByCreatedBy(createdBy string) ListRG { + predicate := func(irg ItemRG) bool { + return irg.CreatedBy == createdBy + } + + return lrg.FilterFunc(predicate) +} + +// FilterByStatus returns ListRG with specified Status. +func (lrg ListRG) FilterByStatus(status string) ListRG { + predicate := func(irg ItemRG) bool { + return irg.Status == status + } + + return lrg.FilterFunc(predicate) +} + +// FilterByLockStatus return ListRG with specified LockStatus. +func (lrg ListRG) FilterByLockStatus(lockStatus string) ListRG { + predicate := func(irg ItemRG) bool { + return irg.LockStatus == lockStatus + } + + return lrg.FilterFunc(predicate) +} + +// FilterByDefNetType returns ListRG with specified DefNetType. +func (lrg ListRG) FilterByDefNetType(defNetType string) ListRG { + predicate := func(irg ItemRG) bool { + return irg.DefNetType == defNetType + } + + return lrg.FilterFunc(predicate) +} + +// FilterByDefNetID returns ListRG with specified DefNetID. +func (lrg ListRG) FilterByDefNetID(defNetID int64) ListRG { + predicate := func(irg ItemRG) bool { + return irg.DefNetID == defNetID + } + + return lrg.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListRG based on a user-specified predicate. +func (lrg ListRG) FilterFunc(predicate func(ItemRG) bool) ListRG { + var result ListRG + + for _, item := range lrg { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemRG. +// If none was found, returns an empty struct. +func (lrg ListRG) FindOne() ItemRG { + if len(lrg) == 0 { + return ItemRG{} + } + + return lrg[0] +} diff --git a/pkg/cloudbroker/rg/models.go b/pkg/cloudbroker/rg/models.go index 9461d2b..ab965d6 100644 --- a/pkg/cloudbroker/rg/models.go +++ b/pkg/cloudbroker/rg/models.go @@ -138,7 +138,7 @@ type ItemRG struct { CreatedTime uint64 `json:"createdTime"` // DefNet ID - DefNetID uint64 `json:"def_net_id"` + DefNetID int64 `json:"def_net_id"` // DefNet type DefNetType string `json:"def_net_type"` diff --git a/pkg/cloudbroker/rg/rg.go b/pkg/cloudbroker/rg/rg.go index c842504..3191aad 100644 --- a/pkg/cloudbroker/rg/rg.go +++ b/pkg/cloudbroker/rg/rg.go @@ -2,7 +2,7 @@ package rg import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..fcaecbc --- /dev/null +++ b/pkg/cloudbroker/rg/serialize.go @@ -0,0 +1,43 @@ +package rg + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lrg ListRG) Serialize(params ...string) (serialization.Serialized, error) { + if len(lrg) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lrg, prefix, indent) + } + + return json.Marshal(lrg) +} + +// 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 (irg ItemRG) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(irg, prefix, indent) + } + + return json.Marshal(irg) +} diff --git a/pkg/cloudbroker/rg/set_def_net.go b/pkg/cloudbroker/rg/set_def_net.go index bebd4d2..ee81533 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set default network diff --git a/pkg/cloudbroker/rg/sorting.go b/pkg/cloudbroker/rg/sorting.go new file mode 100644 index 0000000..399753b --- /dev/null +++ b/pkg/cloudbroker/rg/sorting.go @@ -0,0 +1,60 @@ +package rg + +import "sort" + +// SortByCreatedTime sorts ListRG by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lrg ListRG) SortByCreatedTime(inverse bool) ListRG { + if len(lrg) < 2 { + return lrg + } + + sort.Slice(lrg, func(i, j int) bool { + if inverse { + return lrg[i].CreatedTime > lrg[j].CreatedTime + } + + return lrg[i].CreatedTime < lrg[j].CreatedTime + }) + + return lrg +} + +// SortByUpdatedTime sorts ListRG by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lrg ListRG) SortByUpdatedTime(inverse bool) ListRG { + if len(lrg) < 2 { + return lrg + } + + sort.Slice(lrg, func(i, j int) bool { + if inverse { + return lrg[i].UpdatedTime > lrg[j].UpdatedTime + } + + return lrg[i].UpdatedTime < lrg[j].UpdatedTime + }) + + return lrg +} + +// SortByDeletedTime sorts ListRG by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lrg ListRG) SortByDeletedTime(inverse bool) ListRG { + if len(lrg) < 2 { + return lrg + } + + sort.Slice(lrg, func(i, j int) bool { + if inverse { + return lrg[i].DeletedTime > lrg[j].DeletedTime + } + + return lrg[i].DeletedTime < lrg[j].DeletedTime + }) + + return lrg +} diff --git a/pkg/cloudbroker/rg/update_resource_types.go b/pkg/cloudbroker/rg/update_resource_types.go index 5cef627..acffea6 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 29ad412..f2e6319 100644 --- a/pkg/cloudbroker/sep.go +++ b/pkg/cloudbroker/sep.go @@ -1,6 +1,6 @@ package cloudbroker -import "github.com/rudecs/decort-sdk/pkg/cloudbroker/sep" +import "repos.digitalenergy.online/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 15c6faa..2f770cf 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" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for edit config fields diff --git a/pkg/cloudbroker/sep/filter.go b/pkg/cloudbroker/sep/filter.go new file mode 100644 index 0000000..b0c6058 --- /dev/null +++ b/pkg/cloudbroker/sep/filter.go @@ -0,0 +1,69 @@ +package sep + +// FilterByID returns ListSEP with specified ID. +func (lsep ListSEP) FilterByID(id uint64) ListSEP { + predicate := func(rsep RecordSEP) bool { + return rsep.ID == id + } + + return lsep.FilterFunc(predicate) +} + +// FilterByName returns ListSEP with specified Name. +func (lsep ListSEP) FilterByName(name string) ListSEP { + predicate := func(rsep RecordSEP) bool { + return rsep.Name == name + } + + return lsep.FilterFunc(predicate) +} + +// FilterByObjStatus returns ListSEP with specified ObjStatus. +func (lsep ListSEP) FilterByObjStatus(objStatus string) ListSEP { + predicate := func(rsep RecordSEP) bool { + return rsep.ObjStatus == objStatus + } + + return lsep.FilterFunc(predicate) +} + +// FilterByTechStatus returns ListSEP with specified TechStatus. +func (lsep ListSEP) FilterByTechStatus(techStatus string) ListSEP { + predicate := func(rsep RecordSEP) bool { + return rsep.TechStatus == techStatus + } + + return lsep.FilterFunc(predicate) +} + +// FilterByType returns ListSEP with specified Type. +func (lsep ListSEP) FilterByType(sepType string) ListSEP { + predicate := func(rsep RecordSEP) bool { + return rsep.Type == sepType + } + + return lsep.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListSEP based on user-specified predicate. +func (lsep ListSEP) FilterFunc(predicate func(RecordSEP) bool) ListSEP { + var result ListSEP + + for _, item := range lsep { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found RecordSEP +// If none was found, returns an empty struct. +func (lsep ListSEP) FindOne() RecordSEP { + if len(lsep) == 0 { + return RecordSEP{} + } + + return lsep[0] +} diff --git a/pkg/cloudbroker/sep/sep.go b/pkg/cloudbroker/sep/sep.go index dfc262e..20b7faa 100644 --- a/pkg/cloudbroker/sep/sep.go +++ b/pkg/cloudbroker/sep/sep.go @@ -2,7 +2,7 @@ package sep import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..75542d4 --- /dev/null +++ b/pkg/cloudbroker/sep/serialize.go @@ -0,0 +1,43 @@ +package sep + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lsep ListSEP) Serialize(params ...string) (serialization.Serialized, error) { + if len(lsep) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lsep, prefix, indent) + } + + return json.Marshal(lsep) +} + +// 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 (rsep RecordSEP) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(rsep, prefix, indent) + } + + return json.Marshal(rsep) +} diff --git a/pkg/cloudbroker/tasks.go b/pkg/cloudbroker/tasks.go index 8af8925..5c77622 100644 --- a/pkg/cloudbroker/tasks.go +++ b/pkg/cloudbroker/tasks.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/tasks" + "repos.digitalenergy.online/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 8466d7b..503f722 100644 --- a/pkg/cloudbroker/tasks/tasks.go +++ b/pkg/cloudbroker/tasks/tasks.go @@ -1,7 +1,7 @@ // User API tasks interface package tasks -import "github.com/rudecs/decort-sdk/interfaces" +import "repos.digitalenergy.online/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 dc84337..33c96ed 100644 --- a/pkg/cloudbroker/vins.go +++ b/pkg/cloudbroker/vins.go @@ -1,7 +1,7 @@ package cloudbroker import ( - "github.com/rudecs/decort-sdk/pkg/cloudbroker/vins" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/pkg/cloudbroker/vins" ) // Accessing the VINS method group diff --git a/pkg/cloudbroker/vins/filter.go b/pkg/cloudbroker/vins/filter.go new file mode 100644 index 0000000..e46092f --- /dev/null +++ b/pkg/cloudbroker/vins/filter.go @@ -0,0 +1,78 @@ +package vins + +// FilterByID returns ListVINS with specified ID. +func (lv ListVINS) FilterByID(id uint64) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.ID == id + } + + return lv.FilterFunc(predicate) +} + +// FilterByName returns ListVINS with specified Name. +func (lv ListVINS) FilterByName(name string) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.Name == name + } + + return lv.FilterFunc(predicate) +} + +// FilterByAccountID returns ListVINS with specified AccountID. +func (lv ListVINS) FilterByAccountID(accountID uint64) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.AccountID == accountID + } + + return lv.FilterFunc(predicate) +} + +// FilterByCreatedBy returns ListVINS created by specified user. +func (lv ListVINS) FilterByCreatedBy(createdBy string) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.CreatedBy == createdBy + } + + return lv.FilterFunc(predicate) +} + +// FilterByUpdatedBy returns ListVINS updated by specified user. +func (lv ListVINS) FilterByUpdatedBy(updatedBy string) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.UpdatedBy == updatedBy + } + + return lv.FilterFunc(predicate) +} + +// FilterByDeletedBy returns ListVINS deleted by specified user. +func (lv ListVINS) FilterByDeletedBy(deletedBy string) ListVINS { + predicate := func(iv ItemVINS) bool { + return iv.DeletedBy == deletedBy + } + + return lv.FilterFunc(predicate) +} + +// FilterFunc allows filtering ListVINS based on a user-specified predicate. +func (lv ListVINS) FilterFunc(predicate func(ItemVINS) bool) ListVINS { + var result ListVINS + + for _, item := range lv { + if predicate(item) { + result = append(result, item) + } + } + + return result +} + +// FindOne returns first found ItemVINS +// If none was found, returns an empty struct. +func (lv ListVINS) FindOne() ItemVINS { + if len(lv) == 0 { + return ItemVINS{} + } + + return lv[0] +} diff --git a/pkg/cloudbroker/vins/ip_reserve.go b/pkg/cloudbroker/vins/ip_reserve.go index 814d137..604b37f 100644 --- a/pkg/cloudbroker/vins/ip_reserve.go +++ b/pkg/cloudbroker/vins/ip_reserve.go @@ -6,7 +6,7 @@ import ( "net/http" "strings" - "github.com/rudecs/decort-sdk/internal/validators" + "repos.digitalenergy.online/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 new file mode 100644 index 0000000..0a1c44e --- /dev/null +++ b/pkg/cloudbroker/vins/serialize.go @@ -0,0 +1,43 @@ +package vins + +import ( + "encoding/json" + + "repos.digitalenergy.online/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 (lv ListVINS) Serialize(params ...string) (serialization.Serialized, error) { + if len(lv) == 0 { + return []byte{}, nil + } + + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(lv, prefix, indent) + } + + return json.Marshal(lv) +} + +// 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 (iv ItemVINS) Serialize(params ...string) (serialization.Serialized, error) { + if len(params) > 1 { + prefix := params[0] + indent := params[1] + + return json.MarshalIndent(iv, prefix, indent) + } + + return json.Marshal(iv) +} diff --git a/pkg/cloudbroker/vins/sorting.go b/pkg/cloudbroker/vins/sorting.go new file mode 100644 index 0000000..df7c7c0 --- /dev/null +++ b/pkg/cloudbroker/vins/sorting.go @@ -0,0 +1,60 @@ +package vins + +import "sort" + +// SortByCreatedTime sorts ListVINS by the CreatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lv ListVINS) SortByCreatedTime(inverse bool) ListVINS { + if len(lv) < 2 { + return lv + } + + sort.Slice(lv, func(i, j int) bool { + if inverse { + return lv[i].CreatedTime > lv[j].CreatedTime + } + + return lv[i].CreatedTime < lv[j].CreatedTime + }) + + return lv +} + +// SortByUpdatedTime sorts ListVINS by the UpdatedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lv ListVINS) SortByUpdatedTime(inverse bool) ListVINS { + if len(lv) < 2 { + return lv + } + + sort.Slice(lv, func(i, j int) bool { + if inverse { + return lv[i].UpdatedTime > lv[j].UpdatedTime + } + + return lv[i].UpdatedTime < lv[j].UpdatedTime + }) + + return lv +} + +// SortByDeletedTime sorts ListVINS by the DeletedTime field in ascending order. +// +// If inverse param is set to true, the order is reversed. +func (lv ListVINS) SortByDeletedTime(inverse bool) ListVINS { + if len(lv) < 2 { + return lv + } + + sort.Slice(lv, func(i, j int) bool { + if inverse { + return lv[i].DeletedTime > lv[j].DeletedTime + } + + return lv[i].DeletedTime < lv[j].DeletedTime + }) + + return lv +} diff --git a/pkg/cloudbroker/vins/vins.go b/pkg/cloudbroker/vins/vins.go index feb0426..7c063c7 100644 --- a/pkg/cloudbroker/vins/vins.go +++ b/pkg/cloudbroker/vins/vins.go @@ -2,7 +2,7 @@ package vins import ( - "github.com/rudecs/decort-sdk/interfaces" + "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces" ) // Structure for creating request to VINS