mirror of
https://github.com/portainer/portainer.git
synced 2025-07-23 15:29:42 +02:00
* feat(api): remove SnapshotRaw from EndpointList response * feat(api): add pagination for EndpointList operation * feat(api): rename last_id query parameter to start * feat(api): implement filter for EndpointList operation * feat(home): front - endpoint backend pagination (#2990) * feat(home): endpoint pagination with backend * feat(api): remove default limit value * fix(endpoints): fix a minor issue with column span * fix(endpointgroup-create): fix an issue with endpoint group creation * feat(app): minor loading optimizations * refactor(api): small refactor of EndpointList operation * fix(home): fix minor loading text display issue * refactor(api): document bolt services functions * feat(home): minor optimization * fix(api): replace seek with index scanning for EndpointPaginated * fix(api): fix invalid starting index issue * fix(api): first implementation of working filter * fix(home): endpoints list keeps backend pagination when it needs to * fix(api): endpoint pagination doesn't drop the first item on pages >=2 anymore * fix(home): UI flickering on page/filter load/change * feat(api): support searching in associated endpoint group data * feat(api): declare EndpointList params as optional * feat(endpoints): backend pagination for endpoints view (#3004) * feat(endpoint-group): enable backend pagination (#3017) * feat(api): support groupID filter on endpoints route * feat(api): add new API operations endpointGroupAddEndpoint and endpointGroupDeleteEndpoint * feat(endpoint-groups): backend pagination support for create and edit * feat(endpoint-groups): debounce on filter for create/edit views * feat(endpoint-groups): filter assigned on create view * (endpoint-groups): unassigned endpoints edit view * refactor(endpoint-groups): code clean * feat(endpoint-groups): remove message for Unassigned group * refactor(api): endpoint group endpoint association refactor * refactor(api): rename files and remove comments * refactor(api): remove usage of utils * refactor(api): optional parameters * feat(api): update endpointListOperation behavior and parameters * refactor(api): remove unused methods associated to EndpointService * refactor(api): remove unused methods associated to EndpointService * refactor(api): minor refactor
145 lines
3.6 KiB
Go
145 lines
3.6 KiB
Go
package endpoint
|
|
|
|
import (
|
|
"github.com/boltdb/bolt"
|
|
"github.com/portainer/portainer/api"
|
|
"github.com/portainer/portainer/api/bolt/internal"
|
|
)
|
|
|
|
const (
|
|
// BucketName represents the name of the bucket where this service stores data.
|
|
BucketName = "endpoints"
|
|
)
|
|
|
|
// Service represents a service for managing endpoint data.
|
|
type Service struct {
|
|
db *bolt.DB
|
|
}
|
|
|
|
// NewService creates a new instance of a service.
|
|
func NewService(db *bolt.DB) (*Service, error) {
|
|
err := internal.CreateBucket(db, BucketName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Service{
|
|
db: db,
|
|
}, nil
|
|
}
|
|
|
|
// Endpoint returns an endpoint by ID.
|
|
func (service *Service) Endpoint(ID portainer.EndpointID) (*portainer.Endpoint, error) {
|
|
var endpoint portainer.Endpoint
|
|
identifier := internal.Itob(int(ID))
|
|
|
|
err := internal.GetObject(service.db, BucketName, identifier, &endpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &endpoint, nil
|
|
}
|
|
|
|
// UpdateEndpoint updates an endpoint.
|
|
func (service *Service) UpdateEndpoint(ID portainer.EndpointID, endpoint *portainer.Endpoint) error {
|
|
identifier := internal.Itob(int(ID))
|
|
return internal.UpdateObject(service.db, BucketName, identifier, endpoint)
|
|
}
|
|
|
|
// DeleteEndpoint deletes an endpoint.
|
|
func (service *Service) DeleteEndpoint(ID portainer.EndpointID) error {
|
|
identifier := internal.Itob(int(ID))
|
|
return internal.DeleteObject(service.db, BucketName, identifier)
|
|
}
|
|
|
|
// Endpoints return an array containing all the endpoints.
|
|
func (service *Service) Endpoints() ([]portainer.Endpoint, error) {
|
|
var endpoints = make([]portainer.Endpoint, 0)
|
|
|
|
err := service.db.View(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
cursor := bucket.Cursor()
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
var endpoint portainer.Endpoint
|
|
err := internal.UnmarshalObject(v, &endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
endpoints = append(endpoints, endpoint)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return endpoints, err
|
|
}
|
|
|
|
// CreateEndpoint assign an ID to a new endpoint and saves it.
|
|
func (service *Service) CreateEndpoint(endpoint *portainer.Endpoint) error {
|
|
return service.db.Update(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
// We manually manage sequences for endpoints
|
|
err := bucket.SetSequence(uint64(endpoint.ID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
data, err := internal.MarshalObject(endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return bucket.Put(internal.Itob(int(endpoint.ID)), data)
|
|
})
|
|
}
|
|
|
|
// GetNextIdentifier returns the next identifier for an endpoint.
|
|
func (service *Service) GetNextIdentifier() int {
|
|
return internal.GetNextIdentifier(service.db, BucketName)
|
|
}
|
|
|
|
// Synchronize creates, updates and deletes endpoints inside a single transaction.
|
|
func (service *Service) Synchronize(toCreate, toUpdate, toDelete []*portainer.Endpoint) error {
|
|
return service.db.Update(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
for _, endpoint := range toCreate {
|
|
id, _ := bucket.NextSequence()
|
|
endpoint.ID = portainer.EndpointID(id)
|
|
|
|
data, err := internal.MarshalObject(endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = bucket.Put(internal.Itob(int(endpoint.ID)), data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for _, endpoint := range toUpdate {
|
|
data, err := internal.MarshalObject(endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = bucket.Put(internal.Itob(int(endpoint.ID)), data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for _, endpoint := range toDelete {
|
|
err := bucket.Delete(internal.Itob(int(endpoint.ID)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|