Skip to content

Commit 3040ad9

Browse files
committed
Fixed device count on controller.create_vd
Asked in issue Additional params in methods - create_vd #2
1 parent 8f3d7d2 commit 3040ad9

File tree

3 files changed

+105
-11
lines changed

3 files changed

+105
-11
lines changed

pystorcli2/common.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,54 @@ def get_nearest_separator():
221221
drives.extend(drives_from_expression(expr[pos:]))
222222
break
223223
return drives
224+
225+
226+
def expand_drive_ids(drives: str) -> str:
227+
"""Expand drive ids to range if needed
228+
229+
Args:
230+
drives (str): storcli drives expression (e:s|e:s-x|e:s-x,y;e:s-x,y,z)
231+
232+
Returns:
233+
(str): expanded drives expression (without dashes)
234+
"""
235+
drive_list = drives.split(',')
236+
output = ""
237+
238+
for i, drive in enumerate(drive_list):
239+
drive = drive.strip()
240+
encl, slot = drive.split(':')
241+
new_output = drive
242+
243+
encl = encl.strip()
244+
slot = slot.strip()
245+
246+
if '-' in slot:
247+
begin, end = slot.split('-')
248+
249+
begin = begin.strip()
250+
end = end.strip()
251+
252+
new_output = ','.join(['{0}:{1}'.format(encl, i)
253+
for i in range(int(begin), int(end)+1)])
254+
255+
if i > 0:
256+
output += ',' + new_output
257+
else:
258+
output += new_output
259+
260+
return output
261+
262+
263+
def count_drives(drives: str) -> int:
264+
"""Count number of drives in drives expression
265+
266+
Args:
267+
drives (str): storcli drives expression (e:s|e:s-x|e:s-x,y;e:s-x,y,z)
268+
269+
Returns:
270+
(int): number of drives
271+
"""
272+
273+
expanded_drives = expand_drive_ids(drives)
274+
return len(expanded_drives.split(','))

pystorcli2/controller.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,10 @@ def create_vd(self, name: str, raid: str, drives: str, strip: str = '64', PDperA
312312
if int(raid) >= 10 and PDperArray is None:
313313
# Try to count the number of drives in the array
314314
# The format of the drives argument is e:s|e:s-x|e:s-x,y;e:s-x,y,z
315-
# The number of drives is the number of commas plus the dashes intervals
316-
317-
numDrives = 0
318-
drives2 = drives.split(':')
319-
drives2 = drives2[1]
320-
numDrives += drives2.count(',')+1
321-
for interval in drives2.split(','):
322-
if '-' in interval:
323-
left = int(interval.split('-')[0])
324-
right = int(interval.split('-')[1])
325-
numDrives += right - left
326315

316+
numDrives = common.count_drives(drives)
327317
PDperArray = numDrives//2
318+
328319
except ValueError:
329320
pass
330321

tests/test_common.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2021 Rafael Leira, Naudit HPCN S.L.
4+
#
5+
# See LICENSE for details.
6+
#
7+
################################################################
8+
9+
import json
10+
import os
11+
import pytest
12+
13+
from pystorcli2 import common
14+
15+
16+
class TestCommonFuncs():
17+
18+
def test_expand_drive_ids(self):
19+
assert common.expand_drive_ids(
20+
'0:0') == '0:0'
21+
assert common.expand_drive_ids(
22+
'0:0-1') == '0:0,0:1'
23+
assert common.expand_drive_ids(
24+
'0:0-2') == '0:0,0:1,0:2'
25+
assert common.expand_drive_ids(
26+
'0:0-1,0:3-4') == '0:0,0:1,0:3,0:4'
27+
assert common.expand_drive_ids(
28+
'0:0-1,0:3-4,1:0-1') == '0:0,0:1,0:3,0:4,1:0,1:1'
29+
assert common.expand_drive_ids(
30+
'0:0-1,0:3-4,1:0-1,5:3-4') == '0:0,0:1,0:3,0:4,1:0,1:1,5:3,5:4'
31+
assert common.expand_drive_ids(
32+
'0:0-1 ,0:3-4, 1:0-1 , 5 : 3 - 4 ') == '0:0,0:1,0:3,0:4,1:0,1:1,5:3,5:4'
33+
assert common.expand_drive_ids(
34+
'177:18,177:19,177:20,177:21,177:22,177:23') == '177:18,177:19,177:20,177:21,177:22,177:23'
35+
36+
def test_count_drives(self):
37+
assert common.count_drives(
38+
'0:0') == 1
39+
assert common.count_drives(
40+
'0:0-1') == 2
41+
assert common.count_drives(
42+
'0:0-2') == 3
43+
assert common.count_drives(
44+
'0:0-1,0:3-4') == 4
45+
assert common.count_drives(
46+
'0:0-1,0:3-4,1:0-1') == 6
47+
assert common.count_drives(
48+
'0:0-1,0:3-4,1:0-1,5:3-4') == 8
49+
assert common.count_drives(
50+
'0:0-1 ,0:3-4, 1:0-1 , 5 : 3 - 4 ') == 8
51+
assert common.count_drives(
52+
'177:18,177:19,177:20,177:21,177:22,177:23') == 6

0 commit comments

Comments
 (0)