mirror of https://github.com/portainer/portainer
102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
|
package edgestack
|
||
|
|
||
|
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 = "edge_stack"
|
||
|
)
|
||
|
|
||
|
// Service represents a service for managing Edge stack 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
|
||
|
}
|
||
|
|
||
|
// EdgeStacks returns an array containing all edge stacks
|
||
|
func (service *Service) EdgeStacks() ([]portainer.EdgeStack, error) {
|
||
|
var stacks = make([]portainer.EdgeStack, 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 stack portainer.EdgeStack
|
||
|
err := internal.UnmarshalObject(v, &stack)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
stacks = append(stacks, stack)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
|
||
|
return stacks, err
|
||
|
}
|
||
|
|
||
|
// EdgeStack returns an Edge stack by ID.
|
||
|
func (service *Service) EdgeStack(ID portainer.EdgeStackID) (*portainer.EdgeStack, error) {
|
||
|
var stack portainer.EdgeStack
|
||
|
identifier := internal.Itob(int(ID))
|
||
|
|
||
|
err := internal.GetObject(service.db, BucketName, identifier, &stack)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &stack, nil
|
||
|
}
|
||
|
|
||
|
// CreateEdgeStack assign an ID to a new Edge stack and saves it.
|
||
|
func (service *Service) CreateEdgeStack(edgeStack *portainer.EdgeStack) error {
|
||
|
return service.db.Update(func(tx *bolt.Tx) error {
|
||
|
bucket := tx.Bucket([]byte(BucketName))
|
||
|
|
||
|
if edgeStack.ID == 0 {
|
||
|
id, _ := bucket.NextSequence()
|
||
|
edgeStack.ID = portainer.EdgeStackID(id)
|
||
|
}
|
||
|
|
||
|
data, err := internal.MarshalObject(edgeStack)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return bucket.Put(internal.Itob(int(edgeStack.ID)), data)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// UpdateEdgeStack updates an Edge stack.
|
||
|
func (service *Service) UpdateEdgeStack(ID portainer.EdgeStackID, edgeStack *portainer.EdgeStack) error {
|
||
|
identifier := internal.Itob(int(ID))
|
||
|
return internal.UpdateObject(service.db, BucketName, identifier, edgeStack)
|
||
|
}
|
||
|
|
||
|
// DeleteEdgeStack deletes an Edge stack.
|
||
|
func (service *Service) DeleteEdgeStack(ID portainer.EdgeStackID) error {
|
||
|
identifier := internal.Itob(int(ID))
|
||
|
return internal.DeleteObject(service.db, BucketName, identifier)
|
||
|
}
|
||
|
|
||
|
// GetNextIdentifier returns the next identifier for an endpoint.
|
||
|
func (service *Service) GetNextIdentifier() int {
|
||
|
return internal.GetNextIdentifier(service.db, BucketName)
|
||
|
}
|