Coverage for manila/share/drivers/dell_emc/plugins/unity/utils.py: 94%

61 statements  

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

1# Copyright (c) 2016 EMC Corporation. 

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""" Utility module for EMC Unity Manila Driver """ 

16 

17import fnmatch 

18 

19from oslo_log import log 

20from oslo_utils import units 

21 

22from manila import exception 

23from manila.i18n import _ 

24 

25LOG = log.getLogger(__name__) 

26 

27 

28def do_match(full, matcher_list): 

29 matched = set() 

30 

31 full = set([item.strip() for item in full]) 

32 if matcher_list is None: 

33 # default to all 

34 matcher_list = set('*') 

35 else: 

36 matcher_list = set([item.strip() for item in matcher_list]) 

37 

38 for item in full: 

39 for matcher in matcher_list: 

40 if fnmatch.fnmatchcase(item, matcher): 

41 matched.add(item) 

42 return matched, full - matched 

43 

44 

45def match_ports(ports_list, port_ids_conf): 

46 """Filters the port in `ports_list` with the port id in `port_ids_conf`. 

47 

48 A tuple of (`sp_ports_map`, `unmanaged_port_ids`) is returned, in which 

49 `sp_ports_map` is a dict whose key is SPA or SPB, value is the matched port 

50 id set, `unmanaged_port_ids` is the un-matched port id set. 

51 """ 

52 patterns = (set('*') if port_ids_conf is None 

53 else set(item.strip() for item in port_ids_conf 

54 if item.strip())) 

55 if not patterns: 

56 patterns = set('*') 

57 

58 sp_ports_map = {} 

59 unmanaged_port_ids = set() 

60 for port in ports_list: 

61 port_id = port.get_id() 

62 for pattern in patterns: 

63 if fnmatch.fnmatchcase(port_id, pattern): 

64 # parentStorageProcessor property is deprecated in Unity 5.x 

65 if port.parent_storage_processor: 65 ↛ 68line 65 didn't jump to line 68 because the condition on line 65 was always true

66 sp = port.parent_storage_processor 

67 else: 

68 sp = port.storage_processor 

69 sp_id = sp.get_id() 

70 ports_set = sp_ports_map.setdefault(sp_id, set()) 

71 ports_set.add(port_id) 

72 break 

73 else: 

74 unmanaged_port_ids.add(port_id) 

75 return sp_ports_map, unmanaged_port_ids 

76 

77 

78def find_ports_by_mtu(all_ports, port_ids_conf, mtu): 

79 valid_ports = list(filter(lambda p: p.mtu == mtu, all_ports)) 

80 managed_port_map, unmatched = match_ports(valid_ports, port_ids_conf) 

81 if not managed_port_map: 81 ↛ 82line 81 didn't jump to line 82 because the condition on line 81 was never true

82 msg = (_('None of the configured port %(conf)s matches the mtu ' 

83 '%(mtu)s.') % {'conf': port_ids_conf, 'mtu': mtu}) 

84 raise exception.ShareBackendException(msg=msg) 

85 return managed_port_map 

86 

87 

88def gib_to_byte(size_gib): 

89 return size_gib * units.Gi 

90 

91 

92def get_share_backend_id(share): 

93 """Get backend share id. 

94 

95 Try to get backend share id from path in case this is managed share, 

96 use share['id'] when path is empty. 

97 """ 

98 

99 backend_share_id = None 

100 try: 

101 export_locations = share['export_locations'][0] 

102 path = export_locations['path'] 

103 if share['share_proto'].lower() == 'nfs': 

104 # 10.0.0.1:/example_share_name 

105 backend_share_id = path.split(':/')[-1] 

106 if share['share_proto'].lower() == 'cifs': 

107 # \\10.0.0.1\example_share_name 

108 backend_share_id = path.split('\\')[-1] 

109 except Exception as e: 

110 LOG.warning('Cannot get share name from path, make sure the path ' 

111 'is right. Error details: %s', e) 

112 if backend_share_id and (backend_share_id != share['id']): 

113 return backend_share_id 

114 else: 

115 return share['id'] 

116 

117 

118def get_snapshot_id(snapshot): 

119 """Get backend snapshot id. 

120 

121 Take the id from provider_location in case this is managed snapshot. 

122 """ 

123 return snapshot['provider_location'] or snapshot['id']