-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.go
57 lines (47 loc) · 1.63 KB
/
storage.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
package machina
import (
"path"
)
// StorageName is the name of a storage pool on the host system.
type StorageName string
// StoragePath is the path of a storage pool on the host system.
type StoragePath string
// StorageType identifies the type of storage provided by a storage pool.
type StorageType string
// StoragePattern is a file storage naming pattern.
type StoragePattern StringPattern
// Expand returns the storage path for the given machine and volume.
//
// TODO: Consider allowing other attributes or arbitrary values to be used
// as variables.
func (p StoragePattern) Expand(mapper PatternMapper) StoragePath {
return StoragePath(StringPattern(p).Expand(mapper))
}
// Storage types.
const (
RawStorage = StorageType("raw")
ISOStorage = StorageType("iso")
FirmwareStorage = StorageType("firmware")
)
// Storage defines the common parameters for a storage pool.
type Storage struct {
Path StoragePath `json:"path"`
Pattern StoragePattern `json:"pattern,omitempty"`
Type StorageType `json:"type,omitempty"`
ReadOnly bool `json:"readonly,omitempty"`
}
// StorageMap maps storage names to storage pools on the local system.
type StorageMap map[StorageName]Storage
// Volume returns the path of a volume.
func (s Storage) Volume(machine MachineInfo, vars Vars, volume VolumeName) VolumePath {
var p StoragePath
switch {
case s.Pattern != "":
p = s.Pattern.Expand(MergeVars(machine.Vars(), volume.Vars(), vars).Map)
case s.Type != "":
p = StoragePath(volume) + "." + StoragePath(s.Type)
default:
p = StoragePath(volume) + ".raw"
}
return VolumePath(path.Join(string(s.Path), string(p)))
}