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

feat: add API bdev_lvol_rename #157

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 49 additions & 0 deletions app/cmd/basic/bdev_lvol.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
BdevLvolCheckShallowCopyCmd(),
BdevLvolGetXattrCmd(),
BdevLvolGetFragmapCmd(),
BdevLvolRenameCmd(),

Check warning on line 35 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L35

Added line #L35 was not covered by tests
},
}
}
Expand Down Expand Up @@ -605,3 +606,51 @@

return util.PrintObject(output)
}

func BdevLvolRenameCmd() cli.Command {
return cli.Command{
Name: "rename",
Flags: []cli.Flag{
cli.StringFlag{
Name: "old-name",
Usage: "The UUID or alias (<LVSTORE NAME>/<LVOL NAME>) of the existing logical volume",
Required: true,
},
cli.StringFlag{
Name: "new-name",
Usage: "New logical volume name.",
Required: true,
},
},
Usage: "Rename a logical volume. New name will rename only the alias of the logical volume: \"rename --old-name <LVSTORE NAME>/<LVOL NAME> --new-name <LVOL NAME>\" or \"rename --old-name <UUID> --new-name <LVOL NAME>\"",
Action: func(c *cli.Context) {
if err := bdevLvolRename(c); err != nil {
logrus.WithError(err).Fatalf("Failed to run rename bdev lvol command")
}

Check warning on line 629 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L610-L629

Added lines #L610 - L629 were not covered by tests
},
}
}

func bdevLvolRename(c *cli.Context) error {
spdkCli, err := client.NewClient(context.Background())
if err != nil {
return err
}

Check warning on line 638 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L634-L638

Added lines #L634 - L638 were not covered by tests

oldName := c.String("old-name")
newName := c.String("new-name")

if oldName == "" || newName == "" {
return fmt.Errorf("both old-name and new-name must be provided")
}
if oldName == newName {
return fmt.Errorf("old-name and new-name must be different")
}

Check warning on line 648 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L640-L648

Added lines #L640 - L648 were not covered by tests

renamed, err := spdkCli.BdevLvolRename(oldName, newName)
if err != nil {
return fmt.Errorf("failed to rename logical volume from %q to %q: %v", oldName, newName, err)
}

Check warning on line 653 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L650-L653

Added lines #L650 - L653 were not covered by tests

return util.PrintObject(renamed)

Check warning on line 655 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L655

Added line #L655 was not covered by tests
}
20 changes: 20 additions & 0 deletions pkg/spdk/client/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,26 @@
return &result, nil
}

// BdevLvolRename renames a logical volume.
//
// "oldName": Required. UUID or alias of the existing logical volume.
//
// "newName": Required. New logical volume name.
func (c *Client) BdevLvolRename(oldName, newName string) (renamed bool, err error) {
req := spdktypes.BdevLvolRenameRequest{
OldName: oldName,
NewName: newName,
}

cmdOutput, err := c.jsonCli.SendCommandWithLongTimeout("bdev_lvol_rename", req)
if err != nil {
return false, err
}

Check warning on line 519 in pkg/spdk/client/basic.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/client/basic.go#L518-L519

Added lines #L518 - L519 were not covered by tests

err = json.Unmarshal(cmdOutput, &renamed)
return renamed, err
}

// BdevRaidCreate constructs a new RAID bdev.
//
// "name": Required. a RAID bdev name rather than an alias or a UUID.
Expand Down
17 changes: 16 additions & 1 deletion pkg/spdk/spdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ func (s *TestSuite) TestSPDKBasic(c *C) {
c.Assert(snapLvol1.DriverSpecific.Lvol.Xattrs[client.UserCreated], Equals, "true")
c.Assert(snapLvol1.DriverSpecific.Lvol.Xattrs[client.SnapshotTimestamp], Equals, snapshotTimestamp)

cloneLvolUUID1, err := spdkCli.BdevLvolClone(snapLvolUUID1, "clone111")
cloneName1 := "clone111"
cloneLvolUUID1, err := spdkCli.BdevLvolClone(snapLvolUUID1, cloneName1)
c.Assert(err, IsNil)
defer func() {
deleted, err := spdkCli.BdevLvolDelete(cloneLvolUUID1)
Expand All @@ -229,13 +230,27 @@ func (s *TestSuite) TestSPDKBasic(c *C) {
c.Assert(err, IsNil)
c.Assert(len(lvolList), Equals, 1)
cloneLvol1 := lvolList[0]
c.Assert(len(cloneLvol1.Aliases), Equals, 1)
c.Assert(cloneLvol1.Aliases[0], Equals, spdktypes.GetLvolAlias(lvsName, cloneName1))
c.Assert(cloneLvol1.CreationTime, Not(Equals), "")
c.Assert(cloneLvol1.DriverSpecific.Lvol, NotNil)
c.Assert(cloneLvol1.DriverSpecific.Lvol.Snapshot, Equals, false)
c.Assert(cloneLvol1.DriverSpecific.Lvol.Clone, Equals, true)
c.Assert(cloneLvol1.DriverSpecific.Lvol.Xattrs[client.UserCreated], Equals, "true")
c.Assert(cloneLvol1.DriverSpecific.Lvol.Xattrs[client.SnapshotTimestamp], Equals, "")

cloneRenamed1 := "clone111-tmp"
renamed, err := spdkCli.BdevLvolRename(cloneLvolUUID1, cloneRenamed1)
c.Assert(err, IsNil)
c.Assert(renamed, Equals, true)
lvolList, err = spdkCli.BdevLvolGet(cloneLvolUUID1, 0)
c.Assert(err, IsNil)
c.Assert(len(lvolList), Equals, 1)
cloneLvol1Renamed := lvolList[0]
c.Assert(len(cloneLvol1Renamed.Aliases), Equals, 1)
c.Assert(cloneLvol1Renamed.Aliases[0], Equals, spdktypes.GetLvolAlias(lvsName, cloneRenamed1))
c.Assert(cloneLvol1Renamed.CreationTime, Equals, cloneLvol1.CreationTime)

decoupled, err := spdkCli.BdevLvolDecoupleParent(cloneLvolUUID1)
c.Assert(err, IsNil)
c.Assert(decoupled, Equals, true)
Expand Down
5 changes: 5 additions & 0 deletions pkg/spdk/types/lvol.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ type BdevLvolGetFragmapRequest struct {
Size uint64 `json:"size"`
}

type BdevLvolRenameRequest struct {
OldName string `json:"old_name"`
NewName string `json:"new_name"`
}

func GetLvolAlias(lvsName, lvolName string) string {
return fmt.Sprintf("%s/%s", lvsName, lvolName)
}
Expand Down
Loading