From 0828906a1b2a1c6773c6d3b0bf3777d88b1609f4 Mon Sep 17 00:00:00 2001 From: "yite.gu" Date: Tue, 2 Jul 2024 14:17:52 +0800 Subject: [PATCH] mgr/dashboard: add cephfs rename REST API Introduce rename for the cephfs REST API controller, we can rename the existing file or directory by it. Fixes: https://tracker.ceph.com/issues/66797 Signed-off-by: Yite Gu (cherry picked from commit bac2689fdd0221a9b77833f20b0c512115f9d9e5) --- qa/tasks/mgr/dashboard/test_cephfs.py | 12 +++++ .../mgr/dashboard/controllers/cephfs.py | 11 +++++ src/pybind/mgr/dashboard/openapi.yaml | 49 +++++++++++++++++++ src/pybind/mgr/dashboard/services/cephfs.py | 9 ++++ 4 files changed, 81 insertions(+) diff --git a/qa/tasks/mgr/dashboard/test_cephfs.py b/qa/tasks/mgr/dashboard/test_cephfs.py index 7a6b912a3fb8c..009c6b2e8cb3e 100644 --- a/qa/tasks/mgr/dashboard/test_cephfs.py +++ b/qa/tasks/mgr/dashboard/test_cephfs.py @@ -94,6 +94,12 @@ class CephfsTest(DashboardTestCase): self.assertIsInstance(data, dict) return data + def rename_path(self, src_path, dst_path): + params = {'src_path': src_path, 'dst_path': dst_path} + self._put(f"/api/cephfs/{self.get_fs_id()}/rename-path", + data=params) + self.assertStatus(200) + @DashboardTestCase.RunAs('test', 'test', ['block-manager']) def test_access_permissions(self): fs_id = self.get_fs_id() @@ -361,3 +367,9 @@ class CephfsTest(DashboardTestCase): self.fs.set_joinable() self._get(f"/api/cephfs/{fs_id}/clients") self.assertStatus(200) + + def test_rename_path(self): + self.mk_dirs('/apple') + self.rename_path('/apple', '/orange') + self.ls_dir('/orange', 0) + self.rm_dir('/orange') diff --git a/src/pybind/mgr/dashboard/controllers/cephfs.py b/src/pybind/mgr/dashboard/controllers/cephfs.py index ba7c46f4a69cf..9f9b7501f44cf 100644 --- a/src/pybind/mgr/dashboard/controllers/cephfs.py +++ b/src/pybind/mgr/dashboard/controllers/cephfs.py @@ -639,6 +639,17 @@ class CephFS(RESTController): cfs = self._cephfs_instance(fs_id) cfs.rm_snapshot(path, name) + @RESTController.Resource('PUT', path='/rename-path') + def rename_path(self, fs_id, src_path, dst_path) -> None: + """ + Rename a file or directory. + :param fs_id: The filesystem identifier. + :param src_path: The path to the existing file or directory. + :param dst_path: The new name of the file or directory. + """ + cfs = self._cephfs_instance(fs_id) + cfs.rename_path(src_path, dst_path) + class CephFSClients(object): def __init__(self, module_inst, fscid): diff --git a/src/pybind/mgr/dashboard/openapi.yaml b/src/pybind/mgr/dashboard/openapi.yaml index e9950f823a756..2f9ed4e6d88ee 100644 --- a/src/pybind/mgr/dashboard/openapi.yaml +++ b/src/pybind/mgr/dashboard/openapi.yaml @@ -3075,6 +3075,55 @@ paths: - jwt: [] tags: - Cephfs + /api/cephfs/{fs_id}/rename-path: + put: + description: "\n Rename a file or directory.\n :param fs_id: The\ + \ filesystem identifier.\n :param src_path: The path to the existing\ + \ file or directory.\n :param dst_path: The new name of the file or\ + \ directory.\n " + parameters: + - in: path + name: fs_id + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + properties: + dst_path: + type: string + src_path: + type: string + required: + - src_path + - dst_path + type: object + responses: + '200': + content: + application/vnd.ceph.api.v1.0+json: + type: object + description: Resource updated. + '202': + content: + application/vnd.ceph.api.v1.0+json: + type: object + description: Operation is still executing. Please check the task queue. + '400': + description: Operation exception. Please check the response body for details. + '401': + description: Unauthenticated access. Please login first. + '403': + description: Unauthorized access. Please check your permissions. + '500': + description: Unexpected error. Please check the response body for the stack + trace. + security: + - jwt: [] + tags: + - Cephfs /api/cephfs/{fs_id}/snapshot: delete: description: "\n Remove a snapshot.\n :param fs_id: The filesystem\ diff --git a/src/pybind/mgr/dashboard/services/cephfs.py b/src/pybind/mgr/dashboard/services/cephfs.py index ffbf9d0c81651..6a3cd6b72ba19 100644 --- a/src/pybind/mgr/dashboard/services/cephfs.py +++ b/src/pybind/mgr/dashboard/services/cephfs.py @@ -298,3 +298,12 @@ class CephFS(object): rfiles = int(self.cfs.getxattr(path, 'ceph.dir.rfiles')) rsubdirs = int(self.cfs.getxattr(path, 'ceph.dir.rsubdirs')) return {'bytes': rbytes, 'files': rfiles, 'subdirs': rsubdirs} + + def rename_path(self, src_path, dst_path) -> None: + """ + Rename a file or directory. + :param src: the path to the existing file or directory. + :param dst: the new name of the file or directory. + """ + logger.info("Renaming: from %s to %s", src_path, dst_path) + self.cfs.rename(src_path, dst_path) -- 2.39.5