mirror of https://github.com/portainer/portainer
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
|
package edgejob
|
||
|
|
||
|
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 = "edgejobs"
|
||
|
)
|
||
|
|
||
|
// Service represents a service for managing edge jobs 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
|
||
|
}
|
||
|
|
||
|
// EdgeJobs returns a list of Edge jobs
|
||
|
func (service *Service) EdgeJobs() ([]portainer.EdgeJob, error) {
|
||
|
var edgeJobs = make([]portainer.EdgeJob, 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 edgeJob portainer.EdgeJob
|
||
|
err := internal.UnmarshalObject(v, &edgeJob)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
edgeJobs = append(edgeJobs, edgeJob)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
|
||
|
return edgeJobs, err
|
||
|
}
|
||
|
|
||
|
// EdgeJob returns an Edge job by ID
|
||
|
func (service *Service) EdgeJob(ID portainer.EdgeJobID) (*portainer.EdgeJob, error) {
|
||
|
var edgeJob portainer.EdgeJob
|
||
|
identifier := internal.Itob(int(ID))
|
||
|
|
||
|
err := internal.GetObject(service.db, BucketName, identifier, &edgeJob)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &edgeJob, nil
|
||
|
}
|
||
|
|
||
|
// CreateEdgeJob creates a new Edge job
|
||
|
func (service *Service) CreateEdgeJob(edgeJob *portainer.EdgeJob) error {
|
||
|
return service.db.Update(func(tx *bolt.Tx) error {
|
||
|
bucket := tx.Bucket([]byte(BucketName))
|
||
|
|
||
|
if edgeJob.ID == 0 {
|
||
|
id, _ := bucket.NextSequence()
|
||
|
edgeJob.ID = portainer.EdgeJobID(id)
|
||
|
}
|
||
|
|
||
|
data, err := internal.MarshalObject(edgeJob)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return bucket.Put(internal.Itob(int(edgeJob.ID)), data)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// UpdateEdgeJob updates an Edge job by ID
|
||
|
func (service *Service) UpdateEdgeJob(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error {
|
||
|
identifier := internal.Itob(int(ID))
|
||
|
return internal.UpdateObject(service.db, BucketName, identifier, edgeJob)
|
||
|
}
|
||
|
|
||
|
// DeleteEdgeJob deletes an Edge job
|
||
|
func (service *Service) DeleteEdgeJob(ID portainer.EdgeJobID) 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)
|
||
|
}
|