v9.0.0
This commit is contained in:
53
pkg/cloudapi/locations/filter.go
Normal file
53
pkg/cloudapi/locations/filter.go
Normal file
@@ -0,0 +1,53 @@
|
||||
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)
|
||||
}
|
||||
|
||||
// FilterByGID returns ListLocations with specified GID.
|
||||
func (ll ListLocations) FilterByGID(gid uint64) ListLocations {
|
||||
predicate := func(il ItemLocation) bool {
|
||||
return il.GID == gid
|
||||
}
|
||||
|
||||
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.Data {
|
||||
if predicate(item) {
|
||||
result.Data = append(result.Data, item)
|
||||
}
|
||||
}
|
||||
|
||||
result.EntryCount = uint64(len(result.Data))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FindOne returns first found ItemLocation
|
||||
// If none was found, returns an empty struct.
|
||||
func (ll ListLocations) FindOne() ItemLocation {
|
||||
if len(ll.Data) == 0 {
|
||||
return ItemLocation{}
|
||||
}
|
||||
|
||||
return ll.Data[0]
|
||||
}
|
||||
78
pkg/cloudapi/locations/filter_test.go
Normal file
78
pkg/cloudapi/locations/filter_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package locations
|
||||
|
||||
import "testing"
|
||||
|
||||
var locationItems = ListLocations{
|
||||
Data: []ItemLocation{
|
||||
{
|
||||
GID: 212,
|
||||
ID: 1,
|
||||
GUID: 1,
|
||||
LocationCode: "alfa",
|
||||
Name: "alfa",
|
||||
Flag: "",
|
||||
Meta: []interface{}{
|
||||
"cloudbroker",
|
||||
"location",
|
||||
1,
|
||||
},
|
||||
CKey: "",
|
||||
},
|
||||
{
|
||||
GID: 222,
|
||||
ID: 2,
|
||||
GUID: 2,
|
||||
LocationCode: "beta",
|
||||
Name: "beta",
|
||||
Flag: "",
|
||||
Meta: []interface{}{
|
||||
"cloudbroker",
|
||||
"location",
|
||||
1,
|
||||
},
|
||||
CKey: "",
|
||||
},
|
||||
{
|
||||
GID: 232,
|
||||
ID: 3,
|
||||
GUID: 3,
|
||||
LocationCode: "gamma",
|
||||
Name: "gamma",
|
||||
Flag: "",
|
||||
Meta: []interface{}{
|
||||
"cloudbroker",
|
||||
"location",
|
||||
1,
|
||||
},
|
||||
CKey: "",
|
||||
},
|
||||
},
|
||||
EntryCount: 3,
|
||||
}
|
||||
|
||||
func TestFilterByID(t *testing.T) {
|
||||
actual := locationItems.FilterByID(1).FindOne()
|
||||
|
||||
if actual.ID != 1 {
|
||||
t.Fatal("expected ID 1, found: ", actual.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByName(t *testing.T) {
|
||||
actual := locationItems.FilterByName("gamma").FindOne()
|
||||
|
||||
if actual.Name != "gamma" {
|
||||
t.Fatal("expected Name 'gamma', found: ", actual.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFunc(t *testing.T) {
|
||||
actual := locationItems.FilterFunc(func(il ItemLocation) bool {
|
||||
return il.GID == 212
|
||||
}).
|
||||
FindOne()
|
||||
|
||||
if actual.GID != 212 {
|
||||
t.Fatal("expected GID 212, found: ", actual.GID)
|
||||
}
|
||||
}
|
||||
18
pkg/cloudapi/locations/get_url.go
Normal file
18
pkg/cloudapi/locations/get_url.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// GetURL gets the portal URL
|
||||
func (l Locations) GetURL(ctx context.Context) (string, error) {
|
||||
url := "/cloudapi/locations/getUrl"
|
||||
|
||||
res, err := l.client.DecortApiCall(ctx, http.MethodPost, url, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(res), nil
|
||||
}
|
||||
10
pkg/cloudapi/locations/ids.go
Normal file
10
pkg/cloudapi/locations/ids.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package locations
|
||||
|
||||
// IDs gets array of LocationIDs from ListLocations struct
|
||||
func (ll ListLocations) IDs() []uint64 {
|
||||
res := make([]uint64, 0, len(ll.Data))
|
||||
for _, l := range ll.Data {
|
||||
res = append(res, l.GID)
|
||||
}
|
||||
return res
|
||||
}
|
||||
71
pkg/cloudapi/locations/list.go
Normal file
71
pkg/cloudapi/locations/list.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/internal/validators"
|
||||
)
|
||||
|
||||
// ListRequest struct to get list of locations
|
||||
type ListRequest struct {
|
||||
// Page number
|
||||
// Required: false
|
||||
Page uint64 `url:"page,omitempty" json:"page,omitempty"`
|
||||
|
||||
// Page size
|
||||
// Required: false
|
||||
Size uint64 `url:"size,omitempty" json:"size,omitempty"`
|
||||
|
||||
// Find by flag
|
||||
// Required: false
|
||||
Flag string `url:"flag,omitempty" json:"flag,omitempty"`
|
||||
|
||||
// Find by name
|
||||
// Required: false
|
||||
Name string `url:"name,omitempty" json:"name,omitempty"`
|
||||
|
||||
// Find by ID
|
||||
// Required: false
|
||||
ByID uint64 `url:"by_id,omitempty" json:"by_id,omitempty"`
|
||||
|
||||
// Find by code location
|
||||
// Required: false
|
||||
LocationCode string `url:"locationCode,omitempty" json:"locationCode,omitempty"`
|
||||
|
||||
// Sort by one of supported fields, format +|-(field)
|
||||
// Required: false
|
||||
SortBy string `url:"sortBy,omitempty" json:"sortBy,omitempty" validate:"omitempty,sortBy"`
|
||||
}
|
||||
|
||||
// List gets list of all locations as a ListLocations struct
|
||||
func (l Locations) List(ctx context.Context, req ListRequest) (*ListLocations, error) {
|
||||
|
||||
res, err := l.ListRaw(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := ListLocations{}
|
||||
|
||||
err = json.Unmarshal(res, &list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &list, nil
|
||||
}
|
||||
|
||||
// ListRaw gets list of all locations as an array of bytes
|
||||
func (l Locations) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
|
||||
|
||||
if err := validators.ValidateRequest(req); err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudapi/locations/list"
|
||||
|
||||
res, err := l.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
return res, err
|
||||
}
|
||||
18
pkg/cloudapi/locations/locations.go
Normal file
18
pkg/cloudapi/locations/locations.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// API Actor api for managing locations
|
||||
package locations
|
||||
|
||||
import (
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/interfaces"
|
||||
)
|
||||
|
||||
// Structure for creating request to locations
|
||||
type Locations struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
// Builder for locations endpoints
|
||||
func New(client interfaces.Caller) *Locations {
|
||||
return &Locations{
|
||||
client,
|
||||
}
|
||||
}
|
||||
40
pkg/cloudapi/locations/models.go
Normal file
40
pkg/cloudapi/locations/models.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package locations
|
||||
|
||||
// Main information about locations
|
||||
type ItemLocation struct {
|
||||
// AuthBroker
|
||||
AuthBroker []string `json:"authBroker"`
|
||||
|
||||
// Grid ID
|
||||
GID uint64 `json:"gid"`
|
||||
|
||||
// ID
|
||||
ID uint64 `json:"id"`
|
||||
|
||||
// GUID
|
||||
GUID uint64 `json:"guid"`
|
||||
|
||||
// Location code
|
||||
LocationCode string `json:"locationCode"`
|
||||
|
||||
// Name
|
||||
Name string `json:"name"`
|
||||
|
||||
// Flag
|
||||
Flag string `json:"flag"`
|
||||
|
||||
// Meta
|
||||
Meta []interface{} `json:"_meta"`
|
||||
|
||||
// CKey
|
||||
CKey string `json:"_ckey"`
|
||||
}
|
||||
|
||||
// List of locations
|
||||
type ListLocations struct {
|
||||
// Data
|
||||
Data []ItemLocation `json:"data"`
|
||||
|
||||
// Entry count
|
||||
EntryCount uint64 `json:"entryCount"`
|
||||
}
|
||||
43
pkg/cloudapi/locations/serialize.go
Normal file
43
pkg/cloudapi/locations/serialize.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/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.Data) == 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)
|
||||
}
|
||||
Reference in New Issue
Block a user