cephfs admin: add a SubVolumePath function

The SubVolumePath function maps the subvolume to a path from the root
of the cephfs, and works like `ceph fs subvolume getpath ...`.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-08-31 17:11:40 -04:00 committed by John Mulligan
parent c4e1292da1
commit b66dcaf565
3 changed files with 67 additions and 0 deletions

View File

@ -137,6 +137,24 @@ func unmarshalResponseJSON(res []byte, status string, err error, v interface{})
return json.Unmarshal(res, v)
}
// extractPathResponse returns a cleaned up path from requests that get a path
// unless an error is encountered, then an error is returned.
func extractPathResponse(res []byte, status string, err error) (string, error) {
if err != nil {
return "", err
}
if status != "" {
return "", fmt.Errorf("error status: %s", status)
}
// if there's a trailing newline in the buffer strip it.
// ceph assumes a CLI wants the output of the buffer and there's
// no format=json mode available currently.
for len(res) >= 1 && res[len(res)-1] == '\n' {
res = res[:len(res)-1]
}
return string(res), nil
}
// modeString converts a unix-style mode value to a string-ified version in an
// octal representation (e.g. "777", "700", etc). This format is expected by
// some of the ceph JSON command inputs.

View File

@ -143,3 +143,20 @@ func (fsa *FSAdmin) ResizeSubVolume(
}
return result[0], nil
}
// SubVolumePath returns the path to the subvolume from the root of the file system.
//
// Similar To:
// ceph fs subvolume getpath <volume> --group-name=<group> <name>
func (fsa *FSAdmin) SubVolumePath(volume, group, name string) (string, error) {
m := map[string]string{
"prefix": "fs subvolume getpath",
"vol_name": volume,
"sub_name": name,
// ceph doesn't respond in json for this cmd (even if you ask)
}
if group != NoGroup {
m["group_name"] = group
}
return extractPathResponse(fsa.marshalMgrCommand(m))
}

View File

@ -173,3 +173,35 @@ func TestResizeSubVolume(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, rr)
}
func TestSubVolumePath(t *testing.T) {
fsa := getFSAdmin(t)
volume := "cephfs"
group := "svpGroup"
subname := "svp1"
err := fsa.CreateSubVolumeGroup(volume, group, nil)
assert.NoError(t, err)
defer func() {
err := fsa.RemoveSubVolumeGroup(volume, group)
assert.NoError(t, err)
}()
err = fsa.CreateSubVolume(volume, group, subname, nil)
assert.NoError(t, err)
defer func() {
err := fsa.RemoveSubVolume(volume, group, subname)
assert.NoError(t, err)
}()
path, err := fsa.SubVolumePath(volume, group, subname)
assert.NoError(t, err)
assert.Contains(t, path, group)
assert.Contains(t, path, subname)
assert.NotContains(t, path, "\n")
// invalid subname
path, err = fsa.SubVolumePath(volume, group, "oops")
assert.Error(t, err)
assert.Equal(t, "", path)
}