pystorcli.common
Common
1# -*- coding: utf-8 -*- 2 3# Copyright (c) 2018, Martin Dojcak <martin@dojcak.sk> 4# Copyright (c) 2022, Rafael Leira & Naudit HPCN S.L. <rafael.leira@naudit.es> 5# See LICENSE for details. 6 7'''Common 8''' 9 10 11def response_data(data): 12 """StorCLI json output parser for respone data. 13 14 Args: 15 data (dict): formatted output data from command line 16 17 Returns: 18 dict: response data 19 """ 20 return data['Controllers'][0]['Response Data'] 21 22 23def response_property(data): 24 """StorCLI json output parser for property. 25 26 Args: 27 28 data (dict): formatted output data from command line 29 30 Returns: 31 dict: property data 32 """ 33 return response_data(data)['Controller Properties'] 34 35 36def response_cmd(data): 37 """StorCLI json output parser for general cmd status. 38 39 Args: 40 data (dict): formatted output data from command line 41 42 Returns: 43 dict: cmd status 44 """ 45 return data['Controllers'][0]['Command Status'] 46 47 48def response_setter(data): 49 """StorCLI json output parser for set cmd status. 50 51 Args: 52 data (dict): formatted output data from command line 53 54 Returns: 55 str: cmd detailed status value 56 """ 57 return response_cmd(data)['Detailed Status'][0]['Value'] 58 59 60def lower(func): 61 """Decorator to lower returned function string. 62 63 Args: 64 func (func): function 65 66 Returns: 67 str: lower output of func 68 """ 69 def wrapper(*args, **kwargs): 70 """func effective wrapper 71 """ 72 return func(*args, **kwargs).lower() 73 return wrapper 74 75 76def stringify(func): 77 """Decorator to convert obj to string. 78 79 Args: 80 func (func): function 81 82 Returns: 83 str: lower output of func 84 """ 85 def wrapper(*args, **kwargs): 86 """func effective wrapper 87 """ 88 return '{0}'.format(func(*args, **kwargs)) 89 return wrapper 90 91 92def drives_from_expression(expr): 93 """Generate list of drives from StorCLI drivers expression. 94 95 Args: 96 expr (str): drives string in StorCLI format (e:s|e:s-x|e:s-x,y;e:s-x,y,z) 97 98 Returns: 99 list: drives in format "enclosure:slot" 100 """ 101 # Test cases: 102 # e:s 103 # e1:s,e2:s 104 # e:s,e:s 105 # e:x-y 106 # e:a-b,c-d 107 # e:a-b,z,c-d 108 # e1:a-b,z,c-d,e2:a-b,z,c-d 109 110 def get_encl_id(pos=0): 111 """Get enclosure id from expression 112 """ 113 range_pos = expr[pos:].find(':') 114 encl_id = expr[pos:range_pos] 115 pos += range_pos + 1 116 return (encl_id, pos) 117 118 def get_nearest_separator(): 119 """Get nearest expression separator 120 """ 121 types = { 122 'slot': expr[pos:].find(',') % 1000, 123 'range': expr[pos:].find('-') % 1000, 124 'encl': expr[pos:].find(':') % 1000, 125 'end': len(expr[pos:]), 126 } 127 return sorted(list(types.items()), key=lambda x: x[1])[0] 128 129 drives = [] 130 encl_id, pos = get_encl_id(0) 131 132 while True: 133 sep_t, sep_pos = get_nearest_separator() 134 if sep_t == 'end': 135 # end of expression 136 drives.append('{0}:{1}'.format(encl_id, expr[pos:])) 137 break 138 if sep_t == 'slot': 139 # slot 140 drives.append("{0}:{1}".format(encl_id, expr[pos:pos+sep_pos])) 141 pos += sep_pos + 1 142 elif sep_t == 'range': 143 # range 144 range_start = expr[pos:pos+sep_pos] 145 pos += sep_pos + 1 146 147 range_sep_t, reange_sep_pos = get_nearest_separator() 148 if range_sep_t == 'end': 149 range_stop = len(expr[pos:]) 150 else: 151 range_stop = reange_sep_pos 152 153 range_end = expr[pos:pos+range_stop] 154 pos += range_stop + 1 155 156 for i in range(int(range_start), int(range_end)+1): 157 drives.append("{0}:{1}".format(encl_id, i)) 158 159 if range_sep_t == 'end': 160 break 161 else: 162 # enclosure 163 drives.extend(drives_from_expression(expr[pos:])) 164 break 165 return drives
12def response_data(data): 13 """StorCLI json output parser for respone data. 14 15 Args: 16 data (dict): formatted output data from command line 17 18 Returns: 19 dict: response data 20 """ 21 return data['Controllers'][0]['Response Data']
StorCLI json output parser for respone data.
Args: data (dict): formatted output data from command line
Returns: dict: response data
24def response_property(data): 25 """StorCLI json output parser for property. 26 27 Args: 28 29 data (dict): formatted output data from command line 30 31 Returns: 32 dict: property data 33 """ 34 return response_data(data)['Controller Properties']
StorCLI json output parser for property.
Args:
data (dict): formatted output data from command line
Returns: dict: property data
37def response_cmd(data): 38 """StorCLI json output parser for general cmd status. 39 40 Args: 41 data (dict): formatted output data from command line 42 43 Returns: 44 dict: cmd status 45 """ 46 return data['Controllers'][0]['Command Status']
StorCLI json output parser for general cmd status.
Args: data (dict): formatted output data from command line
Returns: dict: cmd status
49def response_setter(data): 50 """StorCLI json output parser for set cmd status. 51 52 Args: 53 data (dict): formatted output data from command line 54 55 Returns: 56 str: cmd detailed status value 57 """ 58 return response_cmd(data)['Detailed Status'][0]['Value']
StorCLI json output parser for set cmd status.
Args: data (dict): formatted output data from command line
Returns: str: cmd detailed status value
61def lower(func): 62 """Decorator to lower returned function string. 63 64 Args: 65 func (func): function 66 67 Returns: 68 str: lower output of func 69 """ 70 def wrapper(*args, **kwargs): 71 """func effective wrapper 72 """ 73 return func(*args, **kwargs).lower() 74 return wrapper
Decorator to lower returned function string.
Args: func (func): function
Returns: str: lower output of func
77def stringify(func): 78 """Decorator to convert obj to string. 79 80 Args: 81 func (func): function 82 83 Returns: 84 str: lower output of func 85 """ 86 def wrapper(*args, **kwargs): 87 """func effective wrapper 88 """ 89 return '{0}'.format(func(*args, **kwargs)) 90 return wrapper
Decorator to convert obj to string.
Args: func (func): function
Returns: str: lower output of func
93def drives_from_expression(expr): 94 """Generate list of drives from StorCLI drivers expression. 95 96 Args: 97 expr (str): drives string in StorCLI format (e:s|e:s-x|e:s-x,y;e:s-x,y,z) 98 99 Returns: 100 list: drives in format "enclosure:slot" 101 """ 102 # Test cases: 103 # e:s 104 # e1:s,e2:s 105 # e:s,e:s 106 # e:x-y 107 # e:a-b,c-d 108 # e:a-b,z,c-d 109 # e1:a-b,z,c-d,e2:a-b,z,c-d 110 111 def get_encl_id(pos=0): 112 """Get enclosure id from expression 113 """ 114 range_pos = expr[pos:].find(':') 115 encl_id = expr[pos:range_pos] 116 pos += range_pos + 1 117 return (encl_id, pos) 118 119 def get_nearest_separator(): 120 """Get nearest expression separator 121 """ 122 types = { 123 'slot': expr[pos:].find(',') % 1000, 124 'range': expr[pos:].find('-') % 1000, 125 'encl': expr[pos:].find(':') % 1000, 126 'end': len(expr[pos:]), 127 } 128 return sorted(list(types.items()), key=lambda x: x[1])[0] 129 130 drives = [] 131 encl_id, pos = get_encl_id(0) 132 133 while True: 134 sep_t, sep_pos = get_nearest_separator() 135 if sep_t == 'end': 136 # end of expression 137 drives.append('{0}:{1}'.format(encl_id, expr[pos:])) 138 break 139 if sep_t == 'slot': 140 # slot 141 drives.append("{0}:{1}".format(encl_id, expr[pos:pos+sep_pos])) 142 pos += sep_pos + 1 143 elif sep_t == 'range': 144 # range 145 range_start = expr[pos:pos+sep_pos] 146 pos += sep_pos + 1 147 148 range_sep_t, reange_sep_pos = get_nearest_separator() 149 if range_sep_t == 'end': 150 range_stop = len(expr[pos:]) 151 else: 152 range_stop = reange_sep_pos 153 154 range_end = expr[pos:pos+range_stop] 155 pos += range_stop + 1 156 157 for i in range(int(range_start), int(range_end)+1): 158 drives.append("{0}:{1}".format(encl_id, i)) 159 160 if range_sep_t == 'end': 161 break 162 else: 163 # enclosure 164 drives.extend(drives_from_expression(expr[pos:])) 165 break 166 return drives
Generate list of drives from StorCLI drivers expression.
Args: expr (str): drives string in StorCLI format (e:s|e:s-x|e:s-x,y;e:s-x,y,z)
Returns: list: drives in format "enclosure:slot"