2018-11-05 20:58:15 +00:00
|
|
|
package schedule
|
|
|
|
|
|
|
|
import (
|
2021-04-06 10:08:43 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/internal"
|
2018-11-05 20:58:15 +00:00
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// BucketName represents the name of the bucket where this service stores data.
|
|
|
|
BucketName = "schedules"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service represents a service for managing schedule data.
|
|
|
|
type Service struct {
|
2021-04-06 10:08:43 +00:00
|
|
|
connection *internal.DbConnection
|
2018-11-05 20:58:15 +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)
|
2018-11-05 20:58:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
2021-04-06 10:08:43 +00:00
|
|
|
connection: connection,
|
2018-11-05 20:58:15 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule returns a schedule by ID.
|
|
|
|
func (service *Service) Schedule(ID portainer.ScheduleID) (*portainer.Schedule, error) {
|
|
|
|
var schedule portainer.Schedule
|
|
|
|
identifier := internal.Itob(int(ID))
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := internal.GetObject(service.connection, BucketName, identifier, &schedule)
|
2018-11-05 20:58:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &schedule, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSchedule updates a schedule.
|
|
|
|
func (service *Service) UpdateSchedule(ID portainer.ScheduleID, schedule *portainer.Schedule) error {
|
|
|
|
identifier := internal.Itob(int(ID))
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.UpdateObject(service.connection, BucketName, identifier, schedule)
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSchedule deletes a schedule.
|
|
|
|
func (service *Service) DeleteSchedule(ID portainer.ScheduleID) error {
|
|
|
|
identifier := internal.Itob(int(ID))
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.DeleteObject(service.connection, BucketName, identifier)
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Schedules return a array containing all the schedules.
|
|
|
|
func (service *Service) Schedules() ([]portainer.Schedule, error) {
|
|
|
|
var schedules = make([]portainer.Schedule, 0)
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2018-11-05 20:58:15 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var schedule portainer.Schedule
|
|
|
|
err := internal.UnmarshalObject(v, &schedule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
schedules = append(schedules, schedule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return schedules, err
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:49:48 +00:00
|
|
|
// SchedulesByJobType return a array containing all the schedules
|
|
|
|
// with the specified JobType.
|
|
|
|
func (service *Service) SchedulesByJobType(jobType portainer.JobType) ([]portainer.Schedule, error) {
|
|
|
|
var schedules = make([]portainer.Schedule, 0)
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2018-11-06 09:49:48 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var schedule portainer.Schedule
|
|
|
|
err := internal.UnmarshalObject(v, &schedule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if schedule.JobType == jobType {
|
|
|
|
schedules = append(schedules, schedule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return schedules, err
|
|
|
|
}
|
|
|
|
|
2018-11-05 20:58:15 +00:00
|
|
|
// CreateSchedule assign an ID to a new schedule and saves it.
|
|
|
|
func (service *Service) CreateSchedule(schedule *portainer.Schedule) error {
|
2021-04-06 10:08:43 +00:00
|
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
2018-11-05 20:58:15 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
// We manually manage sequences for schedules
|
|
|
|
err := bucket.SetSequence(uint64(schedule.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := internal.MarshalObject(schedule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Put(internal.Itob(int(schedule.ID)), data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNextIdentifier returns the next identifier for a schedule.
|
|
|
|
func (service *Service) GetNextIdentifier() int {
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.GetNextIdentifier(service.connection, BucketName)
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|