From 2b52056721297b494a1d09b773b3d48666cc159f Mon Sep 17 00:00:00 2001 From: silvacarloss Date: Tue, 31 Jan 2023 13:22:08 -0300 Subject: [PATCH] Check share network for share groups before deletion Share network subnets were not checking for the existence of share groups while processing a delete request. Then, if a share network had a share server that was tied to a share group, it would end up deleting the share server, leaving the share group orphan. When we triggered the deletion of the share group, it would fail, as the share server was not known to Manila anymore. Fix that issue by adding an extra validation step during the share network subnet delete API. Closes-Bug: #2004212 Depends-On: https://review.opendev.org/c/openstack/manila-tempest-plugin/+/875981 Change-Id: I563bf925523fa44689c83f432ce5a460276afef7 --- manila/api/v2/share_network_subnets.py | 9 +++++++++ manila/db/sqlalchemy/models.py | 8 +++++--- ...bnet-deletion-when-group-exists-a35355feb1bf6848.yaml | 7 +++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/bug-2004212-prevent-subnet-deletion-when-group-exists-a35355feb1bf6848.yaml diff --git a/manila/api/v2/share_network_subnets.py b/manila/api/v2/share_network_subnets.py index ae403f8ded..96e8f80a53 100644 --- a/manila/api/v2/share_network_subnets.py +++ b/manila/api/v2/share_network_subnets.py @@ -89,6 +89,15 @@ class ShareNetworkSubnetController(wsgi.Controller): LOG.error(msg) raise exc.HTTPConflict(explanation=msg) + share_groups = db_api.share_group_get_all_by_share_server( + context, share_server['id']) + if share_groups: + msg = _("Cannot delete share network subnet %(id)s, it has " + "one or more share groups.") % { + 'id': share_network_subnet_id} + LOG.error(msg) + raise exc.HTTPConflict(explanation=msg) + # NOTE(silvacarlose): Do not allow the deletion of any share server # if any of them has the flag is_auto_deletable = False if not self._all_share_servers_are_auto_deletable( diff --git a/manila/db/sqlalchemy/models.py b/manila/db/sqlalchemy/models.py index d4acc21f75..cc571e03e9 100644 --- a/manila/db/sqlalchemy/models.py +++ b/manila/db/sqlalchemy/models.py @@ -1094,9 +1094,11 @@ class ShareServer(BASE, ManilaBase): 'ShareInstance.deleted == "False")') share_groups = orm.relationship( - "ShareGroup", backref='share_server', primaryjoin='and_(' - 'ShareServer.id == ShareGroup.share_server_id,' - 'ShareGroup.deleted == "False")') + "ShareGroup", + backref='share_server', + primaryjoin='and_(' + 'ShareServer.id == ShareGroup.share_server_id,' + 'ShareGroup.deleted == "False")') _backend_details = orm.relationship( "ShareServerBackendDetails", diff --git a/releasenotes/notes/bug-2004212-prevent-subnet-deletion-when-group-exists-a35355feb1bf6848.yaml b/releasenotes/notes/bug-2004212-prevent-subnet-deletion-when-group-exists-a35355feb1bf6848.yaml new file mode 100644 index 0000000000..a5e48a6bb5 --- /dev/null +++ b/releasenotes/notes/bug-2004212-prevent-subnet-deletion-when-group-exists-a35355feb1bf6848.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixed an issue that allowed share network subnets to be deleted when they + were still related to a share group. An exception will now be raised when + Manila identify such existing relationship. For more details, please refer + to `Launchpad Bug 2004212 `_.