Skip to content

Commit

Permalink
Added throttle method to Volume model
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Ushanov <[email protected]>
  • Loading branch information
gmmephisto committed Apr 28, 2016
1 parent eb5e442 commit 19ec7ea
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pyscaleio/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 64,15 @@ class ScaleIONotBothParameters(ScaleIOInvalidParameters):
def __init__(self, first, second):
super(ScaleIONotBothParameters, self).__init__(
"Use either '{0}' or '{1}', not both", first, second)


class ScaleIORequiredParameters(ScaleIOInvalidParameters):
def __init__(self, *params):
super(ScaleIORequiredParameters, self).__init__(
"One of the parameter(s) must be specified: {0}", ", ".join(params))


class ScaleIOInvalidLimit(ScaleIOInvalidParameters):
def __init__(self, limit, message):
super(ScaleIOInvalidLimit, self).__init__(
"Invalid '{0}' limit: {1}", limit, message)
33 changes: 33 additions & 0 deletions pyscaleio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 524,39 @@ def snapshot(self, name=None):

return Volume(result["volumeIdList"][0])

def throttle(self, sdc_id=None, sdc_guid=None, iops=None, mbps=None):
"""Throttles I/O on current volume.
:param iops: I/O operations in seconds (> 10)
:param mbps: network bandwidth as megabytes in seconds
"""

if sdc_id and sdc_guid:
raise exceptions.ScaleIONotBothParameters("sdc_id", "sdc_guid")

if iops is None and mbps is None:
raise exceptions.ScaleIORequiredParameters("iops", "mbps")

if iops is not None and iops != 0 and iops <= 10:
raise exceptions.ScaleIOInvalidLimit("iops", "must be greater than 10.")

if mbps is not None:
mbps = mbps * constants.KILOBYTE
if bool(mbps % constants.KILOBYTE):
raise exceptions.ScaleIOInvalidLimit("mbps", "must be granular to 1024 KB")

data = {}
if sdc_id:
data["sdcId"] = sdc_id
if sdc_guid:
data["guid"] = sdc_guid
if iops is not None:
data["iopsLimit"] = str(iops)
if mbps is not None:
data["bandwidthLimitInKbps"] = str(mbps)

return super(Volume, self).perform("setMappedSdcLimits", data)

def export(self, sdc_id=None, sdc_guid=None, multiple=False):
"""Exports volume to specified SDC.
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 349,39 @@ def test_volume_unexport_negative(client, method):
m.assert_not_called()


@pytest.mark.parametrize(("kw", "result"), [
({"sdc_id": "test", "iops": 1000}, {"sdcId": "test", "iopsLimit": "1000"}),
({"sdc_id": "test", "iops": 0}, {"sdcId": "test", "iopsLimit": "0"}),
({"sdc_guid": "test", "mbps": 2048}, {"guid": "test", "bandwidthLimitInKbps": "2097152"}),
({"sdc_guid": "test", "mbps": 0}, {"guid": "test", "bandwidthLimitInKbps": "0"})
])
def test_volume_throttle_positive(client, kw, result):

with mock.patch("pyscaleio.models.Volume.__scheme__", {}):
volume = Volume(instance={"id": "test", "links": []})

with mock.patch("pyscaleio.ScaleIOClient.perform_action_on") as m:
volume.throttle(**kw)
m.assert_called_once_with("Volume", "test", "setMappedSdcLimits", result)


@pytest.mark.parametrize(("kw", "exception"), [
({"sdc_id": "test"}, exceptions.ScaleIORequiredParameters),
({"sdc_id": "test", "iops": 5}, exceptions.ScaleIOInvalidLimit),
({"sdc_id": "test", "mbps": 1024.5}, exceptions.ScaleIOInvalidLimit),
({"sdc_id": "test", "sdc_guid": "test_guid"}, exceptions.ScaleIONotBothParameters),
])
def test_volume_throttle_negative(client, kw, exception):

with mock.patch("pyscaleio.models.Volume.__scheme__", {}):
volume = Volume(instance={"id": "test", "links": []})

with mock.patch("pyscaleio.ScaleIOClient.perform_action_on") as m:
with pytest.raises(exception):
volume.throttle(**kw)
m.assert_not_called()


@pytest.mark.parametrize(("kw", "result"), [
({}, {}), ({"name": "test_snapshot"}, {"snapshotName": "test_snapshot"})
])
Expand Down

0 comments on commit 19ec7ea

Please sign in to comment.