2022-09-22 17:05:10 -03:00
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2023-06-20 17:51:34 -03:00
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
2022-09-22 17:05:10 -03:00
|
|
|
)
|
|
|
|
|
2023-10-23 15:52:37 -03:00
|
|
|
const BucketName = "snapshots"
|
2022-09-22 17:05:10 -03:00
|
|
|
|
|
|
|
type Service struct {
|
2023-06-22 18:28:07 -03:00
|
|
|
dataservices.BaseDataService[portainer.Snapshot, portainer.EndpointID]
|
2022-09-22 17:05:10 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewService(connection portainer.Connection) (*Service, error) {
|
|
|
|
err := connection.SetServiceName(BucketName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
2023-06-22 18:28:07 -03:00
|
|
|
BaseDataService: dataservices.BaseDataService[portainer.Snapshot, portainer.EndpointID]{
|
|
|
|
Bucket: BucketName,
|
|
|
|
Connection: connection,
|
|
|
|
},
|
2022-09-22 17:05:10 -03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:11:18 -03:00
|
|
|
func (service *Service) Tx(tx portainer.Transaction) ServiceTx {
|
|
|
|
return ServiceTx{
|
2023-06-22 18:28:07 -03:00
|
|
|
BaseDataServiceTx: dataservices.BaseDataServiceTx[portainer.Snapshot, portainer.EndpointID]{
|
|
|
|
Bucket: BucketName,
|
|
|
|
Connection: service.Connection,
|
|
|
|
Tx: tx,
|
|
|
|
},
|
2022-09-22 17:05:10 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) Create(snapshot *portainer.Snapshot) error {
|
2023-06-22 18:28:07 -03:00
|
|
|
return service.Connection.CreateObjectWithId(BucketName, int(snapshot.EndpointID), snapshot)
|
2022-09-22 17:05:10 -03:00
|
|
|
}
|
2025-03-24 19:33:05 -03:00
|
|
|
|
|
|
|
func (service *Service) ReadWithoutSnapshotRaw(ID portainer.EndpointID) (*portainer.Snapshot, error) {
|
|
|
|
var snapshot *portainer.Snapshot
|
|
|
|
|
|
|
|
err := service.Connection.ViewTx(func(tx portainer.Transaction) error {
|
|
|
|
var err error
|
|
|
|
snapshot, err = service.Tx(tx).ReadWithoutSnapshotRaw(ID)
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return snapshot, err
|
|
|
|
}
|
2025-05-23 12:42:45 +12:00
|
|
|
|
|
|
|
func (service *Service) ReadRawMessage(ID portainer.EndpointID) (*portainer.SnapshotRawMessage, error) {
|
|
|
|
var snapshot *portainer.SnapshotRawMessage
|
|
|
|
|
|
|
|
err := service.Connection.ViewTx(func(tx portainer.Transaction) error {
|
|
|
|
var err error
|
|
|
|
snapshot, err = service.Tx(tx).ReadRawMessage(ID)
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return snapshot, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) CreateRawMessage(snapshot *portainer.SnapshotRawMessage) error {
|
|
|
|
return service.Connection.CreateObjectWithId(BucketName, int(snapshot.EndpointID), snapshot)
|
|
|
|
}
|