From a8e6251a80c4923ce3943e89ea3c3cb0008ff55e Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Thu, 16 Jan 2014 20:57:45 +0400 Subject: [PATCH] Add SnapshotCollection.ByUUID. --- debian/snapshot.go | 10 ++++++++++ debian/snapshot_test.go | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/debian/snapshot.go b/debian/snapshot.go index 62e6e048..17f06ccd 100644 --- a/debian/snapshot.go +++ b/debian/snapshot.go @@ -179,6 +179,16 @@ func (collection *SnapshotCollection) ByName(name string) (*Snapshot, error) { return nil, fmt.Errorf("snapshot with name %s not found", name) } +// ByUUID looks up snapshot by UUID +func (collection *SnapshotCollection) ByUUID(uuid string) (*Snapshot, error) { + for _, s := range collection.list { + if s.UUID == uuid { + return s, nil + } + } + return nil, fmt.Errorf("snapshot with uuid %s not found", uuid) +} + // ForEach runs method for each snapshot func (collection *SnapshotCollection) ForEach(handler func(*Snapshot) error) error { var err error diff --git a/debian/snapshot_test.go b/debian/snapshot_test.go index 41003150..adbb7d0e 100644 --- a/debian/snapshot_test.go +++ b/debian/snapshot_test.go @@ -93,7 +93,7 @@ func (s *SnapshotCollectionSuite) TearDownTest(c *C) { s.db.Close() } -func (s *SnapshotCollectionSuite) TestAddByName(c *C) { +func (s *SnapshotCollectionSuite) TestAddByNameByUUID(c *C) { snapshot, err := s.collection.ByName("snap1") c.Assert(err, ErrorMatches, "*.not found") @@ -110,6 +110,10 @@ func (s *SnapshotCollectionSuite) TestAddByName(c *C) { snapshot, err = collection.ByName("snap1") c.Assert(err, IsNil) c.Assert(snapshot.String(), Equals, s.snapshot1.String()) + + snapshot, err = collection.ByUUID(s.snapshot1.UUID) + c.Assert(err, IsNil) + c.Assert(snapshot.String(), Equals, s.snapshot1.String()) } func (s *SnapshotCollectionSuite) TestUpdateLoadComplete(c *C) {