@@ -10,6 +10,7 @@ import (
1010 "path"
1111 "strconv"
1212 "strings"
13+ "sync"
1314 "time"
1415 "unicode"
1516
@@ -142,8 +143,10 @@ func (e *EmptyPoolError) Error() string {
142143
143144// Manager describes a filesystem manager for ZFS.
144145type Manager struct {
145- runner runners.Runner
146- config Config
146+ runner runners.Runner
147+ config Config
148+ mu * sync.Mutex
149+ snapshots []resources.Snapshot
147150}
148151
149152// Config defines configuration for ZFS filesystem manager.
@@ -158,6 +161,7 @@ func NewFSManager(runner runners.Runner, config Config) *Manager {
158161 m := Manager {
159162 runner : runner ,
160163 config : config ,
164+ mu : & sync.Mutex {},
161165 }
162166
163167 return & m
@@ -307,6 +311,20 @@ func (m *Manager) CreateSnapshot(poolSuffix, dataStateAt string) (string, error)
307311 }
308312 }
309313
314+ dataStateTime , err := util .ParseCustomTime (strings .TrimSuffix (dataStateAt , m .config .PreSnapshotSuffix ))
315+ if err != nil {
316+ return "" , fmt .Errorf ("failed to parse dataStateAt: %w" , err )
317+ }
318+
319+ m .addSnapshotToList (resources.Snapshot {
320+ ID : snapshotName ,
321+ CreatedAt : time .Now (),
322+ DataStateAt : dataStateTime ,
323+ Pool : m .config .Pool .Name ,
324+ })
325+
326+ go m .RefreshSnapshotList ()
327+
310328 return snapshotName , nil
311329}
312330
@@ -334,6 +352,8 @@ func (m *Manager) DestroySnapshot(snapshotName string) error {
334352 return errors .Wrap (err , "failed to run command" )
335353 }
336354
355+ m .removeSnapshotFromList (snapshotName )
356+
337357 return nil
338358}
339359
@@ -360,6 +380,8 @@ func (m *Manager) CleanupSnapshots(retentionLimit int) ([]string, error) {
360380
361381 lines := strings .Split (out , "\n " )
362382
383+ m .RefreshSnapshotList ()
384+
363385 return lines , nil
364386}
365387
@@ -487,8 +509,26 @@ func (m *Manager) GetFilesystemState() (models.FileSystem, error) {
487509 return fileSystem , nil
488510}
489511
490- // GetSnapshots returns a snapshot list.
491- func (m * Manager ) GetSnapshots () ([]resources.Snapshot , error ) {
512+ // SnapshotList returns a list of snapshots.
513+ func (m * Manager ) SnapshotList () []resources.Snapshot {
514+ snapshotList := m .snapshots
515+ return snapshotList
516+ }
517+
518+ // RefreshSnapshotList updates the list of snapshots.
519+ func (m * Manager ) RefreshSnapshotList () {
520+ snapshots , err := m .getSnapshots ()
521+ if err != nil {
522+ log .Err ("Failed to refresh snapshot list: " , err )
523+ return
524+ }
525+
526+ m .mu .Lock ()
527+ m .snapshots = snapshots
528+ m .mu .Unlock ()
529+ }
530+
531+ func (m * Manager ) getSnapshots () ([]resources.Snapshot , error ) {
492532 entries , err := m .listSnapshots (m .config .Pool .Name )
493533 if err != nil {
494534 return nil , fmt .Errorf ("failed to list snapshots: %w" , err )
@@ -517,6 +557,24 @@ func (m *Manager) GetSnapshots() ([]resources.Snapshot, error) {
517557 return snapshots , nil
518558}
519559
560+ func (m * Manager ) addSnapshotToList (snapshot resources.Snapshot ) {
561+ m .mu .Lock ()
562+ m .snapshots = append (m .snapshots , snapshot )
563+ m .mu .Unlock ()
564+ }
565+
566+ func (m * Manager ) removeSnapshotFromList (snapshotName string ) {
567+ for i , snapshot := range m .snapshots {
568+ if snapshot .ID == snapshotName {
569+ m .mu .Lock ()
570+ m .snapshots = append (m .snapshots [:i ], m .snapshots [i + 1 :]... )
571+ m .mu .Unlock ()
572+
573+ break
574+ }
575+ }
576+ }
577+
520578// ListFilesystems lists ZFS file systems (clones, pools).
521579func (m * Manager ) listFilesystems (pool string ) ([]* ListEntry , error ) {
522580 filter := snapshotFilter {
0 commit comments