-
Notifications
You must be signed in to change notification settings - Fork 30
/
list.go
122 lines (102 loc) · 3.16 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package backupstore
import (
"errors"
"fmt"
"runtime"
"strings"
"github.com/gammazero/workerpool"
"github.com/longhorn/backupstore/types"
"github.com/longhorn/backupstore/util"
)
type VolumeInfo struct {
Name string
Size int64 `json:",string"`
Labels map[string]string
Created string
LastBackupName string
LastBackupAt string
DataStored int64 `json:",string"`
Messages map[types.MessageType]string
Backups map[string]*BackupInfo `json:",omitempty"`
BackingImageName string
BackingImageChecksum string
StorageClassname string
DataEngine string
}
type BackupInfo struct {
Name string
URL string
SnapshotName string
SnapshotCreated string
Created string
Size int64 `json:",string"`
Labels map[string]string
Parameters map[string]string
IsIncremental bool
CompressionMethod string `json:",omitempty"`
NewlyUploadedDataSize int64 `json:",string"`
ReUploadedDataSize int64 `json:",string"`
VolumeName string `json:",omitempty"`
VolumeSize int64 `json:",string,omitempty"`
VolumeCreated string `json:",omitempty"`
VolumeBackingImageName string `json:",omitempty"`
Messages map[types.MessageType]string
}
func addListVolume(driver BackupStoreDriver, volumeName string, volumeOnly bool) (*VolumeInfo, error) {
if volumeName == "" {
return nil, fmt.Errorf("invalid empty volume Name")
}
if !util.ValidateName(volumeName) {
return nil, fmt.Errorf("invalid volume name %v", volumeName)
}
volumeInfo := &VolumeInfo{Messages: make(map[types.MessageType]string)}
if !volumeExists(driver, volumeName) {
// If the backup volume folder exist but volume.cfg not exist
// save the error in Messages field
volumeInfo.Messages[types.MessageTypeError] = fmt.Sprintf("cannot find %v in backupstore", getVolumeFilePath(volumeName))
return volumeInfo, nil
}
if volumeOnly {
return volumeInfo, nil
}
// try to find all backups for this volume
backupNames, err := getBackupNamesForVolume(driver, volumeName)
if err != nil {
volumeInfo.Messages[types.MessageTypeError] = err.Error()
return volumeInfo, nil
}
volumeInfo.Backups = make(map[string]*BackupInfo)
for _, backupName := range backupNames {
volumeInfo.Backups[backupName] = &BackupInfo{}
}
return volumeInfo, nil
}
func List(volumeName, destURL string, volumeOnly bool) (map[string]*VolumeInfo, error) {
driver, err := GetBackupStoreDriver(destURL)
if err != nil {
return nil, err
}
jobQueues := workerpool.New(runtime.NumCPU() * 16)
defer jobQueues.StopWait()
var resp = make(map[string]*VolumeInfo)
volumeNames := []string{volumeName}
if volumeName == "" {
volumeNames, err = getVolumeNames(jobQueues, driver)
if err != nil {
return nil, err
}
}
var errs []string
for _, volumeName := range volumeNames {
volumeInfo, err := addListVolume(driver, volumeName, volumeOnly)
if err != nil {
errs = append(errs, err.Error())
continue
}
resp[volumeName] = volumeInfo
}
if len(errs) > 0 {
return nil, errors.New(strings.Join(errs, "\n"))
}
return resp, nil
}