Coverage for manila/api/v2/share_unmanage.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright 2015 Mirantis inc. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); you may 

4# not use this file except in compliance with the License. You may obtain 

5# a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

12# License for the specific language governing permissions and limitations 

13# under the License. 

14 

15from http import client as http_client 

16 

17from oslo_log import log 

18import webob 

19from webob import exc 

20 

21from manila.api.openstack import wsgi 

22from manila.common import constants 

23from manila import exception 

24from manila.i18n import _ 

25from manila import share 

26 

27LOG = log.getLogger(__name__) 

28 

29 

30class ShareUnmanageMixin(object): 

31 

32 @wsgi.Controller.authorize("unmanage") 

33 def _unmanage(self, req, id, body=None, allow_dhss_true=False): 

34 """Unmanage a share.""" 

35 context = req.environ['manila.context'] 

36 

37 LOG.info("Unmanage share with id: %s", id, context=context) 

38 

39 try: 

40 share = self.share_api.get(context, id) 

41 if share.get('is_soft_deleted'): 

42 msg = _("Share '%s cannot be unmanaged, " 

43 "since it has been soft deleted.") % share['id'] 

44 raise exc.HTTPForbidden(explanation=msg) 

45 if share.get('has_replicas'): 

46 msg = _("Share %s has replicas. It cannot be unmanaged " 

47 "until all replicas are removed.") % share['id'] 

48 raise exc.HTTPConflict(explanation=msg) 

49 if (not allow_dhss_true and 

50 share['instance'].get('share_server_id')): 

51 msg = _("Operation 'unmanage' is not supported for shares " 

52 "that are created on top of share servers " 

53 "(created with share-networks).") 

54 raise exc.HTTPForbidden(explanation=msg) 

55 elif share['status'] in constants.TRANSITIONAL_STATUSES: 

56 msg = _("Share with transitional state can not be unmanaged. " 

57 "Share '%(s_id)s' is in '%(state)s' state.") % dict( 

58 state=share['status'], s_id=share['id']) 

59 raise exc.HTTPForbidden(explanation=msg) 

60 snapshots = self.share_api.db.share_snapshot_get_all_for_share( 

61 context, id) 

62 if snapshots: 

63 msg = _("Share '%(s_id)s' can not be unmanaged because it has " 

64 "'%(amount)s' dependent snapshot(s).") % { 

65 's_id': id, 'amount': len(snapshots)} 

66 raise exc.HTTPForbidden(explanation=msg) 

67 filters = {'share_id': id} 

68 backups = self.share_api.db.share_backups_get_all(context, filters) 

69 if backups: 

70 msg = _("Share '%(s_id)s' can not be unmanaged because it has " 

71 "'%(amount)s' dependent backup(s).") % { 

72 's_id': id, 'amount': len(backups)} 

73 raise exc.HTTPForbidden(explanation=msg) 

74 self.share_api.unmanage(context, share) 

75 except exception.NotFound as e: 

76 raise exc.HTTPNotFound(explanation=e.msg) 

77 except (exception.InvalidShare, exception.PolicyNotAuthorized) as e: 

78 raise exc.HTTPForbidden(explanation=e.msg) 

79 

80 return webob.Response(status_int=http_client.ACCEPTED) 

81 

82 

83class ShareUnmanageController(ShareUnmanageMixin, wsgi.Controller): 

84 """The Unmanage API controller for the OpenStack API.""" 

85 

86 resource_name = "share" 

87 

88 def __init__(self, *args, **kwargs): 

89 super(ShareUnmanageController, self).__init__(*args, **kwargs) 

90 self.share_api = share.API() 

91 

92 @wsgi.Controller.api_version('1.0', '2.6') 

93 def unmanage(self, req, id): 

94 return self._unmanage(req, id) 

95 

96 

97def create_resource(): 

98 return wsgi.Resource(ShareUnmanageController())