diff --git a/cmd/internal/backup/providers/common/common.go b/cmd/internal/backup/providers/common/common.go index c8a98aa..9f04f64 100644 --- a/cmd/internal/backup/providers/common/common.go +++ b/cmd/internal/backup/providers/common/common.go @@ -1,11 +1,17 @@ package common import ( + "fmt" "slices" "github.com/metal-stack/backup-restore-sidecar/cmd/internal/backup/providers" ) +type backupVersionLister interface { + List() []*providers.BackupVersion +} + +// Sort the given list of backup versions func Sort(versions []*providers.BackupVersion, oldestFirst bool) { slices.SortFunc(versions, func(a, b *providers.BackupVersion) int { if oldestFirst { @@ -14,3 +20,22 @@ func Sort(versions []*providers.BackupVersion, oldestFirst bool) { return a.Date.Compare(b.Date) }) } + +// Latest returns latest backup version +func Latest(versions []*providers.BackupVersion) *providers.BackupVersion { + Sort(versions, true) + if len(versions) == 0 { + return nil + } + return versions[0] +} + +// Latest returns the backup version at given version +func Get(b backupVersionLister, version string) (*providers.BackupVersion, error) { + for _, backup := range b.List() { + if version == backup.Version { + return backup, nil + } + } + return nil, fmt.Errorf("version %q not found", version) +} diff --git a/cmd/internal/backup/providers/common/common_test.go b/cmd/internal/backup/providers/common/common_test.go index 38c2aba..a26c853 100644 --- a/cmd/internal/backup/providers/common/common_test.go +++ b/cmd/internal/backup/providers/common/common_test.go @@ -1,6 +1,7 @@ package common import ( + "reflect" "testing" "time" @@ -60,3 +61,31 @@ func TestSort(t *testing.T) { }) } } + +func TestLatest(t *testing.T) { + now := time.Now() + newestBackup := &providers.BackupVersion{Name: "5.tgz", Date: now.Add(5 * time.Hour)} + tests := []struct { + name string + versions []*providers.BackupVersion + want *providers.BackupVersion + }{ + { + versions: []*providers.BackupVersion{ + {Name: "0.tgz", Date: now}, + {Name: "2.tgz", Date: now.Add(2 * time.Hour)}, + {Name: "1.tgz", Date: now.Add(1 * time.Hour)}, + newestBackup, + {Name: "3.tgz", Date: now.Add(3 * time.Hour)}, + }, + want: newestBackup, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Latest(tt.versions); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Latest() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/cmd/internal/backup/providers/gcp/versions.go b/cmd/internal/backup/providers/gcp/versions.go index 7074777..7d106dc 100644 --- a/cmd/internal/backup/providers/gcp/versions.go +++ b/cmd/internal/backup/providers/gcp/versions.go @@ -1,7 +1,6 @@ package gcp import ( - "fmt" "strconv" "cloud.google.com/go/storage" @@ -14,17 +13,13 @@ type backupVersionsGCP struct { } func (b backupVersionsGCP) Latest() *providers.BackupVersion { - result := b.List() - if len(result) == 0 { - return nil - } - return result[0] + return common.Latest(b.List()) } func (b backupVersionsGCP) List() []*providers.BackupVersion { var result []*providers.BackupVersion - tmp := make(map[int64]bool) + tmp := make(map[int64]bool, len(result)) for _, attr := range b.objectAttrs { ok := tmp[attr.Generation] if !ok { @@ -43,10 +38,5 @@ func (b backupVersionsGCP) List() []*providers.BackupVersion { } func (b backupVersionsGCP) Get(version string) (*providers.BackupVersion, error) { - for _, backup := range b.List() { - if version == backup.Version { - return backup, nil - } - } - return nil, fmt.Errorf("version %q not found", version) + return common.Get(b, version) } diff --git a/cmd/internal/backup/providers/local/versions.go b/cmd/internal/backup/providers/local/versions.go index 02378ad..0c267ea 100644 --- a/cmd/internal/backup/providers/local/versions.go +++ b/cmd/internal/backup/providers/local/versions.go @@ -1,7 +1,6 @@ package local import ( - "fmt" "os" "strconv" @@ -14,12 +13,7 @@ type backupVersionsLocal struct { } func (b backupVersionsLocal) Latest() *providers.BackupVersion { - result := b.List() - if len(result) == 0 { - return nil - } - common.Sort(result, false) - return result[0] + return common.Latest(b.List()) } func (b backupVersionsLocal) List() []*providers.BackupVersion { @@ -42,10 +36,5 @@ func (b backupVersionsLocal) List() []*providers.BackupVersion { } func (b backupVersionsLocal) Get(version string) (*providers.BackupVersion, error) { - for _, backup := range b.List() { - if version == backup.Version { - return backup, nil - } - } - return nil, fmt.Errorf("version %q not found", version) + return common.Get(b, version) } diff --git a/cmd/internal/backup/providers/s3/versions.go b/cmd/internal/backup/providers/s3/versions.go index 7e7e4ba..084aeb8 100644 --- a/cmd/internal/backup/providers/s3/versions.go +++ b/cmd/internal/backup/providers/s3/versions.go @@ -1,8 +1,6 @@ package s3 import ( - "fmt" - "github.com/aws/aws-sdk-go/service/s3" "github.com/metal-stack/backup-restore-sidecar/cmd/internal/backup/providers" "github.com/metal-stack/backup-restore-sidecar/cmd/internal/backup/providers/common" @@ -15,11 +13,7 @@ type backupVersionsS3 struct { // Latest returns latest backup version func (b backupVersionsS3) Latest() *providers.BackupVersion { - result := b.List() - if len(result) == 0 { - return nil - } - return result[0] + return common.Latest(b.List()) } // List return a list of all backup versions @@ -41,10 +35,5 @@ func (b backupVersionsS3) List() []*providers.BackupVersion { // Get returns the backup entry of the given version func (b backupVersionsS3) Get(version string) (*providers.BackupVersion, error) { - for _, backup := range b.List() { - if version == backup.Version { - return backup, nil - } - } - return nil, fmt.Errorf("version %q not found", version) + return common.Get(b, version) }