Merge "Add delete CLI helper to ShareCommands"

This commit is contained in:
Zuul 2024-04-09 17:09:35 +00:00 committed by Gerrit Code Review
commit 221d4d940d
3 changed files with 57 additions and 0 deletions

View File

@ -82,6 +82,7 @@ SHARE_SERVERS_UPDATE_HELP = ("List of share servers to be updated, separated "
"by commas.")
SHARE_SERVERS_UPDATE_CAPABILITIES_HELP = (
"List of share server capabilities to be updated, separated by commas.")
SHARE_DELETE_HELP = ("Share ID to be deleted.")
# Decorators for actions
@ -402,6 +403,30 @@ class ShareCommands(object):
}
print(msg % msg_args)
@args('--share_id', required=True, help=SHARE_DELETE_HELP)
def delete(self, share_id):
"""Delete manila share from the database.
This command is useful after a share's manager service
has been decommissioned.
"""
ctxt = context.get_admin_context()
share = db.share_get(ctxt, share_id)
active_replicas = []
# We delete "active" replicas at the end
for share_instance in share['instances']:
if share_instance['replica_state'] == "active":
active_replicas.append(share_instance)
else:
db.share_instance_delete(ctxt, share_instance['id'])
for share_instance in active_replicas:
db.share_instance_delete(ctxt, share_instance['id'])
print("Deleted share instance %s" % share_instance['id'])
# finally, clean up the share
print("Deleted share %s" % share_id)
class ShareServerCommands(object):
@args('--share_servers', required=True,

View File

@ -439,6 +439,33 @@ class ManilaCmdManageTestCase(test.TestCase):
db.share_resources_host_update.assert_called_once_with(
'admin_ctxt', current_host, new_host)
def test_share_delete(self):
share_id = "fake_share_id"
share = {
'id': share_id,
'instances': [
{'id': 'instance_id1', 'replica_state': 'active'},
{'id': 'instance_id2', 'replica_state': 'error'},
{'id': 'instance_id3', 'replica_state': 'active'},
]
}
self.mock_object(context, 'get_admin_context',
mock.Mock(return_value='admin_ctxt'))
self.mock_object(db, 'share_get',
mock.Mock(return_value=share))
self.mock_object(db, 'share_instance_delete',
mock.Mock(return_value=None))
self.share_cmds.delete(share_id)
db.share_instance_delete.assert_has_calls([
mock.call('admin_ctxt', 'instance_id2'),
mock.call('admin_ctxt', 'instance_id1'),
mock.call('admin_ctxt', 'instance_id3'),
])
self.assertEqual(3, db.share_instance_delete.call_count)
def test_share_server_update_capability(self):
self.mock_object(context, 'get_admin_context',
mock.Mock(return_value='admin_ctxt'))

View File

@ -0,0 +1,5 @@
---
fixes:
-|
Launchpad `bug 1867030 <https://bugs.launchpad.net/manila/+bug/186730>`_
has been fixed for delete share.