2020-06-25 03:25:51 +00:00
|
|
|
package edgejob
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/boltdb/bolt"
|
2021-04-06 10:08:43 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-06-25 03:25:51 +00:00
|
|
|
"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 {
|
2021-04-06 10:08:43 +00:00
|
|
|
connection *internal.DbConnection
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new instance of a service.
|
2021-04-06 10:08:43 +00:00
|
|
|
func NewService(connection *internal.DbConnection) (*Service, error) {
|
|
|
|
err := internal.CreateBucket(connection, BucketName)
|
2020-06-25 03:25:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
2021-04-06 10:08:43 +00:00
|
|
|
connection: connection,
|
2020-06-25 03:25:51 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EdgeJobs returns a list of Edge jobs
|
|
|
|
func (service *Service) EdgeJobs() ([]portainer.EdgeJob, error) {
|
|
|
|
var edgeJobs = make([]portainer.EdgeJob, 0)
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2020-06-25 03:25:51 +00:00
|
|
|
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))
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := internal.GetObject(service.connection, BucketName, identifier, &edgeJob)
|
2020-06-25 03:25:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &edgeJob, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEdgeJob creates a new Edge job
|
|
|
|
func (service *Service) CreateEdgeJob(edgeJob *portainer.EdgeJob) error {
|
2021-04-06 10:08:43 +00:00
|
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
2020-06-25 03:25:51 +00:00
|
|
|
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))
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.UpdateObject(service.connection, BucketName, identifier, edgeJob)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteEdgeJob deletes an Edge job
|
|
|
|
func (service *Service) DeleteEdgeJob(ID portainer.EdgeJobID) error {
|
|
|
|
identifier := internal.Itob(int(ID))
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.DeleteObject(service.connection, BucketName, identifier)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNextIdentifier returns the next identifier for an endpoint.
|
|
|
|
func (service *Service) GetNextIdentifier() int {
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.GetNextIdentifier(service.connection, BucketName)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|