diff --git a/manila/api/v2/share_network_subnets.py b/manila/api/v2/share_network_subnets.py index d41bbeccf9..20d0819cd7 100644 --- a/manila/api/v2/share_network_subnets.py +++ b/manila/api/v2/share_network_subnets.py @@ -92,6 +92,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 d418368138..9e166dfc55 100644 --- a/manila/db/sqlalchemy/models.py +++ b/manila/db/sqlalchemy/models.py @@ -1142,9 +1142,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 `_.