Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to use tags for GetSnapshot in vSphere #1158

Merged
merged 3 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions pkg/blockstorage/vmware/vmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,13 @@ func (p *FcdProvider) SnapshotGet(ctx context.Context, id string) (*blockstorage
if err != nil {
return nil, errors.Wrap(err, "Failed to convert object to snapshot")
}
snapID := vimID(snapshotID)
log.Debug().Print("RetrieveMetadata: " + volID + "," + snapshotID)
kvs, err := p.Gom.RetrieveMetadata(ctx, vimID(volID), &snapID, "")
tags, err := p.getSnapshotTags(ctx, id, volID)
if err != nil {
return nil, errors.Wrap(err, "Failed to get snapshot metadata")
}
log.Debug().Print("RetrieveMetadata done: " + volID + "," + snapshotID)
snapshot.Tags = convertKeyValueToTags(kvs)
snapshot.Tags = tags
return snapshot, nil
}
}
Expand Down Expand Up @@ -508,6 +507,24 @@ func (p *FcdProvider) VolumesList(ctx context.Context, tags map[string]string, z
return nil, errors.New("Not implemented")
}

func (p *FcdProvider) getSnapshotTags(ctx context.Context, fullSnapshotID string, volid string) ([]*blockstorage.KeyValue, error) {
if p.categoryID == "" {
if p.Gom == nil {
return nil, errors.New("GlobalObjectManager not initialized")
}
kvs, err := p.Gom.RetrieveMetadata(ctx, vimID(volid), nil, "")
if err != nil {
return nil, errors.Wrap(err, "Failed to get volume metadata")
}
return convertKeyValueToTags(kvs), nil
}
categoryTags, err := p.tagManager.GetTagsForCategory(ctx, p.categoryID)
if err != nil {
return nil, errors.Wrap(err, "Failed to list tags")
}
return p.getTagsFromSnapshotID(categoryTags, fullSnapshotID)
}

// SnapshotsList is part of blockstorage.Provider
func (p *FcdProvider) SnapshotsList(ctx context.Context, tags map[string]string) ([]*blockstorage.Snapshot, error) {
if p.categoryID == "" {
Expand Down Expand Up @@ -538,12 +555,26 @@ func (p *FcdProvider) SnapshotsList(ctx context.Context, tags map[string]string)
return snapshots, nil
}

func (p *FcdProvider) getTagsFromSnapshotID(categoryTags []vapitags.Tag, fullSnapshotID string) ([]*blockstorage.KeyValue, error) {
tags := map[string]string{}
for _, catTag := range categoryTags {
parsedTag := &snapshotTag{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parsedTag would read better as tag

if err := parsedTag.Parse(catTag.Name); err != nil {
return nil, errors.Wrapf(err, "Failed to parse tag")
}
snapId := SnapshotFullID(parsedTag.volid, parsedTag.snapid)
if snapId == fullSnapshotID {
tags[parsedTag.key] = parsedTag.value
}
}
return blockstorage.MapToKeyValue(tags), nil
}

func (p *FcdProvider) getSnapshotIDsFromTags(categoryTags []vapitags.Tag, tags map[string]string) ([]string, error) {
snapshotTagMap := map[string]map[string]string{}
for _, catTag := range categoryTags {
parsedTag := &snapshotTag{}
err := parsedTag.Parse(catTag.Name)
if err != nil {
if err := parsedTag.Parse(catTag.Name); err != nil {
return nil, errors.Wrapf(err, "Failed to parse tag")
}
snapId := SnapshotFullID(parsedTag.volid, parsedTag.snapid)
Expand Down
67 changes: 67 additions & 0 deletions pkg/blockstorage/vmware/vmware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vmware

import (
"context"
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -304,6 +305,72 @@ func (s *VMWareSuite) TestDeleteTagsSnapshot(c *C) {
}
}

func (s *VMWareSuite) TestGetSnapshotTags(c *C) {
ctx := context.Background()
for _, tc := range []struct {
snapshotID string
catID string
volID string
categoryTags []vapitags.Tag
expNumTags int
errGetTags error
errChecker Checker
}{
{ // success
snapshotID: "v1:s1",
volID: "v1",
categoryTags: []vapitags.Tag{
{Name: "v1:s1:t1:v1"},
{Name: "v1:s1:t2:v2"},
{Name: "v1:s2:t3:v3"},
{Name: "v3:s2:t4:v4"},
},
errChecker: IsNil,
catID: "something",
expNumTags: 2,
},
{ // bad tag
snapshotID: "v1:s1",
volID: "v1",
categoryTags: []vapitags.Tag{
{Name: "v1:s1:t1:v1"},
{Name: "v1:s1:t2:v2"},
{Name: "v1:s2:t3:v3"},
{Name: "v3:s2t4:v4"},
},
catID: "something",
errChecker: NotNil,
},
{ // bad tag
snapshotID: "v1:s1",
volID: "v1",
categoryTags: []vapitags.Tag{},
errGetTags: errors.New("get tags error"),
errChecker: NotNil,
catID: "something",
},
{ // empty cat id
errChecker: NotNil,
catID: "",
expNumTags: 0,
},
} {
ftm := &fakeTagManager{
retGetTagsForCategory: tc.categoryTags,
errGetTagsForCategory: tc.errGetTags,
}
provider := &FcdProvider{
categoryID: tc.catID,
tagManager: ftm,
}
tags, err := provider.getSnapshotTags(ctx, tc.snapshotID, tc.volID)
c.Assert(err, tc.errChecker)
if tc.errChecker == IsNil {
c.Assert(len(tags), Equals, tc.expNumTags)
}
}
}

type fakeTagManager struct {
retGetTagsForCategory []vapitags.Tag
errGetTagsForCategory error
Expand Down