Skip to content

Commit

Permalink
Fixed device count on controller.create_vd
Browse files Browse the repository at this point in the history
Asked in issue Additional params in methods - create_vd #2
  • Loading branch information
ralequi committed Apr 5, 2023
1 parent 8f3d7d2 commit 3040ad9
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 11 deletions.
51 changes: 51 additions & 0 deletions pystorcli2/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,54 @@ def get_nearest_separator():
drives.extend(drives_from_expression(expr[pos:]))
break
return drives


def expand_drive_ids(drives: str) -> str:
"""Expand drive ids to range if needed
Args:
drives (str): storcli drives expression (e:s|e:s-x|e:s-x,y;e:s-x,y,z)
Returns:
(str): expanded drives expression (without dashes)
"""
drive_list = drives.split(',')
output = ""

for i, drive in enumerate(drive_list):
drive = drive.strip()
encl, slot = drive.split(':')
new_output = drive

encl = encl.strip()
slot = slot.strip()

if '-' in slot:
begin, end = slot.split('-')

begin = begin.strip()
end = end.strip()

new_output = ','.join(['{0}:{1}'.format(encl, i)
for i in range(int(begin), int(end)+1)])

if i > 0:
output += ',' + new_output
else:
output += new_output

return output


def count_drives(drives: str) -> int:
"""Count number of drives in drives expression
Args:
drives (str): storcli drives expression (e:s|e:s-x|e:s-x,y;e:s-x,y,z)
Returns:
(int): number of drives
"""

expanded_drives = expand_drive_ids(drives)
return len(expanded_drives.split(','))
13 changes: 2 additions & 11 deletions pystorcli2/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,10 @@ def create_vd(self, name: str, raid: str, drives: str, strip: str = '64', PDperA
if int(raid) >= 10 and PDperArray is None:
# Try to count the number of drives in the array
# The format of the drives argument is e:s|e:s-x|e:s-x,y;e:s-x,y,z
# The number of drives is the number of commas plus the dashes intervals

numDrives = 0
drives2 = drives.split(':')
drives2 = drives2[1]
numDrives += drives2.count(',')+1
for interval in drives2.split(','):
if '-' in interval:
left = int(interval.split('-')[0])
right = int(interval.split('-')[1])
numDrives += right - left

numDrives = common.count_drives(drives)
PDperArray = numDrives//2

except ValueError:
pass

Expand Down
52 changes: 52 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 Rafael Leira, Naudit HPCN S.L.
#
# See LICENSE for details.
#
################################################################

import json
import os
import pytest

from pystorcli2 import common


class TestCommonFuncs():

def test_expand_drive_ids(self):
assert common.expand_drive_ids(
'0:0') == '0:0'
assert common.expand_drive_ids(
'0:0-1') == '0:0,0:1'
assert common.expand_drive_ids(
'0:0-2') == '0:0,0:1,0:2'
assert common.expand_drive_ids(
'0:0-1,0:3-4') == '0:0,0:1,0:3,0:4'
assert common.expand_drive_ids(
'0:0-1,0:3-4,1:0-1') == '0:0,0:1,0:3,0:4,1:0,1:1'
assert common.expand_drive_ids(
'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'
assert common.expand_drive_ids(
'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'
assert common.expand_drive_ids(
'177:18,177:19,177:20,177:21,177:22,177:23') == '177:18,177:19,177:20,177:21,177:22,177:23'

def test_count_drives(self):
assert common.count_drives(
'0:0') == 1
assert common.count_drives(
'0:0-1') == 2
assert common.count_drives(
'0:0-2') == 3
assert common.count_drives(
'0:0-1,0:3-4') == 4
assert common.count_drives(
'0:0-1,0:3-4,1:0-1') == 6
assert common.count_drives(
'0:0-1,0:3-4,1:0-1,5:3-4') == 8
assert common.count_drives(
'0:0-1 ,0:3-4, 1:0-1 , 5 : 3 - 4 ') == 8
assert common.count_drives(
'177:18,177:19,177:20,177:21,177:22,177:23') == 6

0 comments on commit 3040ad9

Please sign in to comment.