Skip to content

Commit

Permalink
feat: add API bdev_lvol_rename
Browse files Browse the repository at this point in the history
Longhorn 9488

Signed-off-by: Shuo Wu <[email protected]>
  • Loading branch information
shuo-wu committed Oct 23, 2024
1 parent 76b8114 commit 37a2949
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
37 changes: 37 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 @@ func BdevLvolCmd() cli.Command {
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,39 @@ func bdevLvolGetFragmap(c *cli.Context) error {

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 of the existing logical volume.",
},
cli.StringFlag{
Name: "new-name",
Usage: "New logical volume name.",
},
},
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>\"",
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 627 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L610 - L627 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 636 in app/cmd/basic/bdev_lvol.go

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L632-L636

Added lines #L632 - L636 were not covered by tests

renamed, err := spdkCli.BdevLvolRename(c.String("old-name"), c.String("new-name"))
if err != nil {
return err
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L638 - L641 were not covered by tests

return util.PrintObject(renamed)

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

View check run for this annotation

Codecov / codecov/patch

app/cmd/basic/bdev_lvol.go#L643

Added line #L643 was not covered by tests
}
19 changes: 19 additions & 0 deletions pkg/spdk/client/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,25 @@ func (c *Client) BdevLvolGetFragmap(name string, offset, size uint64) (*spdktype
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

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

// 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, cloneLvol1Renamed.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

0 comments on commit 37a2949

Please sign in to comment.