forked from NetApp/trident
/
snapshot.go
141 lines (113 loc) · 3.63 KB
/
snapshot.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2018 NetApp, Inc. All Rights Reserved.
package storage
import (
"fmt"
"regexp"
)
const SnapshotTimestampFormat = "2006-01-02T15:04:05Z"
const SnapshotNameFormat = "20060102T150405Z"
var snapshotIDRegex = regexp.MustCompile(`^(?P<volume>[^\s/]+)/(?P<snapshot>[^\s/]+)$`)
type SnapshotConfig struct {
Version string `json:"version,omitempty"`
Name string `json:"name,omitempty"`
InternalName string `json:"internalName,omitempty"`
VolumeName string `json:"volumeName,omitempty"`
VolumeInternalName string `json:"volumeInternalName,omitempty"`
}
func (c *SnapshotConfig) ID() string {
return MakeSnapshotID(c.VolumeName, c.Name)
}
func (c *SnapshotConfig) Validate() error {
if c.Name == "" || c.VolumeName == "" {
return fmt.Errorf("the following fields for \"Snapshot\" are mandatory: name and volumeName")
}
return nil
}
type Snapshot struct {
Config *SnapshotConfig
Created string `json:"dateCreated"` // The UTC time that the snapshot was created, in RFC3339 format
SizeBytes int64 `json:"size"` // The size of the volume at the time the snapshot was created
State SnapshotState
}
type SnapshotState string
const (
SnapshotStateOnline = SnapshotState("online")
SnapshotStateMissingBackend = SnapshotState("missing_backend")
SnapshotStateMissingVolume = SnapshotState("missing_volume")
)
func (s SnapshotState) IsOnline() bool {
return s == SnapshotStateOnline
}
func (s SnapshotState) IsMissingBackend() bool {
return s == SnapshotStateMissingBackend
}
func (s SnapshotState) IsMissingVolume() bool {
return s == SnapshotStateMissingVolume
}
type SnapshotExternal struct {
Snapshot
}
func (s *SnapshotExternal) ID() string {
return MakeSnapshotID(s.Config.VolumeName, s.Config.Name)
}
type SnapshotPersistent struct {
Snapshot
}
func NewSnapshot(config *SnapshotConfig, created string, sizeBytes int64) *Snapshot {
return &Snapshot{
Config: config,
Created: created,
SizeBytes: sizeBytes,
State: SnapshotStateOnline,
}
}
func (s *Snapshot) ConstructExternal() *SnapshotExternal {
clone := s.ConstructClone()
return &SnapshotExternal{Snapshot: *clone}
}
func (s *Snapshot) ConstructPersistent() *SnapshotPersistent {
clone := s.ConstructClone()
return &SnapshotPersistent{Snapshot: *clone}
}
func (s *Snapshot) ConstructClone() *Snapshot {
return &Snapshot{
Config: &SnapshotConfig{
Version: s.Config.Version,
Name: s.Config.Name,
InternalName: s.Config.InternalName,
VolumeName: s.Config.VolumeName,
VolumeInternalName: s.Config.VolumeInternalName,
},
Created: s.Created,
SizeBytes: s.SizeBytes,
State: s.State,
}
}
func (s *Snapshot) ID() string {
return MakeSnapshotID(s.Config.VolumeName, s.Config.Name)
}
func (s *SnapshotPersistent) ConstructExternal() *SnapshotExternal {
clone := s.ConstructClone()
return &SnapshotExternal{Snapshot: *clone}
}
func MakeSnapshotID(volumeName, snapshotName string) string {
return fmt.Sprintf("%s/%s", volumeName, snapshotName)
}
func ParseSnapshotID(snapshotID string) (string, string, error) {
match := snapshotIDRegex.FindStringSubmatch(snapshotID)
paramsMap := make(map[string]string)
for i, name := range snapshotIDRegex.SubexpNames() {
if i > 0 && i <= len(match) {
paramsMap[name] = match[i]
}
}
volumeName, ok := paramsMap["volume"]
if !ok {
return "", "", fmt.Errorf("snapshot ID %s does not contain a volume name", volumeName)
}
snapshotName, ok := paramsMap["snapshot"]
if !ok {
return "", "", fmt.Errorf("snapshot ID %s does not contain a snapshot name", volumeName)
}
return volumeName, snapshotName, nil
}