Skip to content

Commit 1a6269a

Browse files
committed
state: add VolumeGroupSnapshot APIs
1 parent 3973588 commit 1a6269a

File tree

2 files changed

+113
-5
lines changed

2 files changed

+113
-5
lines changed

pkg/state/state.go

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,21 @@ type Volume struct {
5858
}
5959

6060
type Snapshot struct {
61+
Name string
62+
Id string
63+
VolID string
64+
Path string
65+
CreationTime *timestamp.Timestamp
66+
SizeBytes int64
67+
ReadyToUse bool
68+
GroupSnapshotID string
69+
}
70+
71+
type GroupSnapshot struct {
6172
Name string
6273
Id string
63-
VolID string
64-
Path string
74+
SnapshotIDs []string
6575
CreationTime *timestamp.Timestamp
66-
SizeBytes int64
6776
ReadyToUse bool
6877
}
6978

@@ -112,11 +121,32 @@ type State interface {
112121
// snapshot ID. It is not an error when such a snapshot
113122
// does not exist.
114123
DeleteSnapshot(snapshotID string) error
124+
125+
// GetGroupSnapshotByID retrieves a groupsnapshot by its unique ID or
126+
// returns an error including that ID when not found.
127+
GetGroupSnapshotByID(vgsID string) (GroupSnapshot, error)
128+
129+
// GetGroupSnapshotByName retrieves a groupsnapshot by its name or
130+
// returns an error including that name when not found.
131+
GetGroupSnapshotByName(volName string) (GroupSnapshot, error)
132+
133+
// GetGroupSnapshots returns all currently existing groupsnapshots.
134+
GetGroupSnapshots() []GroupSnapshot
135+
136+
// UpdateGroupSnapshot updates the existing hostpath groupsnapshot,
137+
// identified by its snapshot ID, or adds it if it does not exist yet.
138+
UpdateGroupSnapshot(snapshot GroupSnapshot) error
139+
140+
// DeleteGroupSnapshot deletes the groupsnapshot with the given
141+
// groupsnapshot ID. It is not an error when such a groupsnapshot does
142+
// not exist.
143+
DeleteGroupSnapshot(groupSnapshotID string) error
115144
}
116145

117146
type resources struct {
118-
Volumes []Volume
119-
Snapshots []Snapshot
147+
Volumes []Volume
148+
Snapshots []Snapshot
149+
GroupSnapshots []GroupSnapshot
120150
}
121151

122152
type state struct {
@@ -257,3 +287,50 @@ func (s *state) DeleteSnapshot(snapshotID string) error {
257287
}
258288
return nil
259289
}
290+
291+
func (s *state) GetGroupSnapshotByID(groupSnapshotID string) (GroupSnapshot, error) {
292+
for _, groupSnapshot := range s.GroupSnapshots {
293+
if groupSnapshot.Id == groupSnapshotID {
294+
return groupSnapshot, nil
295+
}
296+
}
297+
return GroupSnapshot{}, status.Errorf(codes.NotFound, "groupsnapshot id %s does not exist in the groupsnapshots list", groupSnapshotID)
298+
}
299+
300+
func (s *state) GetGroupSnapshotByName(name string) (GroupSnapshot, error) {
301+
for _, groupSnapshot := range s.GroupSnapshots {
302+
if groupSnapshot.Name == name {
303+
return groupSnapshot, nil
304+
}
305+
}
306+
return GroupSnapshot{}, status.Errorf(codes.NotFound, "groupsnapshot name %s does not exist in the groupsnapshots list", name)
307+
}
308+
309+
func (s *state) GetGroupSnapshots() []GroupSnapshot {
310+
groupSnapshots := make([]GroupSnapshot, len(s.GroupSnapshots))
311+
for i, groupSnapshot := range s.GroupSnapshots {
312+
groupSnapshots[i] = groupSnapshot
313+
}
314+
return groupSnapshots
315+
}
316+
317+
func (s *state) UpdateGroupSnapshot(update GroupSnapshot) error {
318+
for i, groupSnapshot := range s.GroupSnapshots {
319+
if groupSnapshot.Id == update.Id {
320+
s.GroupSnapshots[i] = update
321+
return s.dump()
322+
}
323+
}
324+
s.GroupSnapshots = append(s.GroupSnapshots, update)
325+
return s.dump()
326+
}
327+
328+
func (s *state) DeleteGroupSnapshot(groupSnapshotID string) error {
329+
for i, groupSnapshot := range s.GroupSnapshots {
330+
if groupSnapshot.Id == groupSnapshotID {
331+
s.GroupSnapshots = append(s.GroupSnapshots[:i], s.GroupSnapshots[i+1:]...)
332+
return s.dump()
333+
}
334+
}
335+
return nil
336+
}

pkg/state/state_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,34 @@ func TestSnapshotsFromSameSource(t *testing.T) {
124124
_, err = s.GetSnapshotByName("bar-name")
125125
require.NoError(t, err, "get existing snapshot by name 'bar-name'")
126126
}
127+
128+
func TestVolumeGroupSnapshots(t *testing.T) {
129+
tmp := t.TempDir()
130+
statefileName := path.Join(tmp, "state.json")
131+
132+
s, err := New(statefileName)
133+
require.NoError(t, err, "construct state")
134+
require.Empty(t, s.GetGroupSnapshots(), "initial groupsnapshots")
135+
136+
_, err = s.GetGroupSnapshotByID("foo")
137+
require.Equal(t, codes.NotFound, status.Convert(err).Code(), "GetSnapshotByID of non-existent groupsnapshot")
138+
require.Contains(t, status.Convert(err).Message(), "foo")
139+
140+
err = s.UpdateGroupSnapshot(GroupSnapshot{Id: "foo", Name: "bar"})
141+
require.NoError(t, err, "add groupsnapshot")
142+
143+
s, err = New(statefileName)
144+
require.NoError(t, err, "reconstruct state")
145+
_, err = s.GetGroupSnapshotByID("foo")
146+
require.NoError(t, err, "get existing groupsnapshot by ID")
147+
_, err = s.GetGroupSnapshotByName("bar")
148+
require.NoError(t, err, "get existing groupsnapshot by name")
149+
150+
err = s.DeleteGroupSnapshot("foo")
151+
require.NoError(t, err, "delete existing groupsnapshot")
152+
153+
err = s.DeleteGroupSnapshot("foo")
154+
require.NoError(t, err, "delete non-existent groupsnapshot")
155+
156+
require.Empty(t, s.GetGroupSnapshots(), "final groupsnapshots")
157+
}

0 commit comments

Comments
 (0)