Coverage for manila/api/v1/share_servers.py: 98%

92 statements  

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

1# Copyright 2014 OpenStack Foundation 

2# All Rights Reserved. 

3# 

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

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

6# a copy of the License at 

7# 

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

9# 

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

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

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

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16from http import client as http_client 

17 

18from oslo_log import log 

19import webob 

20from webob import exc 

21 

22from manila.api.openstack import wsgi 

23from manila.api.views import share_servers as share_servers_views 

24from manila.common import constants 

25from manila.db import api as db_api 

26from manila import exception 

27from manila.i18n import _ 

28from manila import share 

29 

30LOG = log.getLogger(__name__) 

31 

32 

33class ShareServerController(wsgi.Controller): 

34 """The Share Server API controller for the OpenStack API.""" 

35 

36 _view_builder_class = share_servers_views.ViewBuilder 

37 resource_name = 'share_server' 

38 

39 def __init__(self): 

40 self.share_api = share.API() 

41 super(ShareServerController, self).__init__() 

42 

43 @wsgi.Controller.authorize 

44 def index(self, req): 

45 """Returns a list of share servers.""" 

46 

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

48 

49 search_opts = {} 

50 search_opts.update(req.GET) 

51 

52 ss_filters = {} 

53 if 'host' in search_opts: 

54 ss_filters['host'] = search_opts.pop('host') 

55 if 'status' in search_opts: 

56 ss_filters['status'] = search_opts.pop('status') 

57 if 'source_share_server_id' in search_opts: 57 ↛ 58line 57 didn't jump to line 58 because the condition on line 57 was never true

58 ss_filters['source_share_server_id'] = search_opts.pop( 

59 'source_share_server_id') 

60 if 'identifier' in search_opts: 

61 ss_filters['identifier'] = search_opts.pop('identifier') 

62 share_servers = db_api.share_server_get_all_with_filters( 

63 context, ss_filters) 

64 

65 for s in share_servers: 

66 try: 

67 share_network = db_api.share_network_get( 

68 context, s.share_network_id) 

69 s.project_id = share_network['project_id'] 

70 if share_network['name']: 

71 s.share_network_name = share_network['name'] 

72 else: 

73 s.share_network_name = share_network['id'] 

74 except exception.ShareNetworkNotFound: 

75 # NOTE(dviroel): The share-network may already be deleted while 

76 # the share-server is in 'deleting' state. In this scenario, 

77 # we will return some empty values. 

78 LOG.debug("Unable to retrieve share network details for share " 

79 "server %(server)s, the network %(network)s was " 

80 "not found.", 

81 {'server': s.id, 'network': s.share_network_id}) 

82 s.project_id = '' 

83 s.share_network_name = '' 

84 if search_opts: 

85 for k, v in search_opts.items(): 

86 share_servers = [s for s in share_servers if 

87 (hasattr(s, k) and 

88 s[k] == v or k == 'share_network' and 

89 v in [s.share_network_name, 

90 s.share_network_id] or 

91 k == 'share_network_subnet_id' and 

92 v in s.share_network_subnet_ids)] 

93 return self._view_builder.build_share_servers(req, share_servers) 

94 

95 @wsgi.Controller.authorize 

96 def show(self, req, id): 

97 """Return data about the requested share server.""" 

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

99 try: 

100 server = db_api.share_server_get(context, id) 

101 share_network = db_api.share_network_get( 

102 context, server['share_network_id']) 

103 server.project_id = share_network['project_id'] 

104 if share_network['name']: 

105 server.share_network_name = share_network['name'] 

106 else: 

107 server.share_network_name = share_network['id'] 

108 except exception.ShareServerNotFound as e: 

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

110 except exception.ShareNetworkNotFound: 

111 msg = _("Share server could not be found. Its associated share " 

112 "network %s does not exist.") % server['share_network_id'] 

113 raise exc.HTTPNotFound(explanation=msg) 

114 return self._view_builder.build_share_server(req, server) 

115 

116 @wsgi.Controller.authorize 

117 def details(self, req, id): 

118 """Return details for requested share server.""" 

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

120 try: 

121 share_server = db_api.share_server_get(context, id) 

122 except exception.ShareServerNotFound as e: 

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

124 

125 return self._view_builder.build_share_server_details( 

126 share_server['backend_details']) 

127 

128 @wsgi.Controller.authorize 

129 def delete(self, req, id): 

130 """Delete specified share server.""" 

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

132 try: 

133 share_server = db_api.share_server_get(context, id) 

134 except exception.ShareServerNotFound as e: 

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

136 allowed_statuses = [constants.STATUS_ERROR, constants.STATUS_ACTIVE] 

137 if share_server['status'] not in allowed_statuses: 

138 data = { 

139 'status': share_server['status'], 

140 'allowed_statuses': allowed_statuses, 

141 } 

142 msg = _("Share server's actual status is %(status)s, allowed " 

143 "statuses for deletion are %(allowed_statuses)s.") % (data) 

144 raise exc.HTTPForbidden(explanation=msg) 

145 LOG.debug("Deleting share server with id: %s.", id) 

146 try: 

147 self.share_api.delete_share_server(context, share_server) 

148 except exception.ShareServerInUse as e: 

149 raise exc.HTTPConflict(explanation=e.msg) 

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

151 

152 

153def create_resource(): 

154 return wsgi.Resource(ShareServerController())