Skip to content

Commit

Permalink
Added config module for tuning various client options
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 d5a2307 commit eb5e442
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 30 deletions.
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 95,20 @@ Usage
# delete volume
volume = pyscaleio.Volume.one_by_name("test_volume")
volume.delete()
* Tune client and models options:

.. code-block:: python
pyscaleio.configure(
# retries count for each request
request_retries=0,
# network timeout for requests
network_timeout=30,
# name of exported volume (according to udev/rules.d)
volume_name="emc-2{system_id}{volume_id}",
# prefix of exported volume
volume_prefix="/dev/disk/by-id")
volume = pyscaleio.Volume.one_by_name("test_volume")
assert volume.path == "/dev/disk/by-id/emc-27947a0127a79ce60ca29f20950000008"
2 changes: 2 additions & 0 deletions pyscaleio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
from .client import ScaleIOSession, ScaleIOClient, inject # noqa
from .config import ScaleIOConfig
from .manager import ScaleIOClientsManager
from .models import (
System, ProtectionDomain, StoragePool,
Expand All @@ -16,3 17,4 @@

get_client = ScaleIOClientsManager().get_client
add_client = ScaleIOClientsManager().register
configure = ScaleIOConfig().apply
6 changes: 3 additions & 3 deletions pyscaleio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 8,7 @@
from six.moves.urllib.parse import urljoin

import pyscaleio
from pyscaleio import constants
from pyscaleio import config
from pyscaleio import exceptions
from pyscaleio import utils

Expand All @@ -34,8 34,8 @@ class ScaleIOSession(object):
"""Endpoint template."""

def __init__(self, host, user, passwd, is_secure=True,
retries=constants.REQUEST_RETRIES,
timeout=constants.NETWORK_TIMEOUT):
retries=config.REQUEST_RETRIES,
timeout=config.NETWORK_TIMEOUT):
self.host = host
self.scheme = "https" if is_secure else "http"

Expand Down
60 changes: 60 additions & 0 deletions pyscaleio/config.py
Original file line number Diff line number Diff line change
@@ -0,0 1,60 @@
from __future__ import unicode_literals

from six import add_metaclass

from object_validator import validate, ValidationError
from object_validator import Integer, String, DictScheme

import pyscaleio.config
from pyscaleio import exceptions
from pyscaleio import utils


NETWORK_TIMEOUT = 30
"""Timeout for all network operations."""

REQUEST_RETRIES = 3
"""Default retries count for HTTP request."""

VOLUME_PREFIX = "/dev/disk/by-id"
"""Default prefix for volume path."""

VOLUME_NAME = "emc-vol-{system_id}-{volume_id}"
"""
Default name for volume path on SDC.
Must be parametrized with system_id and volume_id.
"""


@add_metaclass(utils.singleton)
class ScaleIOConfig(object):
"""ScaleIO config manager."""

__scheme__ = {
"network_timeout": Integer(min=0, optional=True),
"request_retries": Integer(min=0, optional=True),
"volume_prefix": String(optional=True),
"volume_name": String(optional=True),
}

@classmethod
def _get_scheme(cls):
"""Returns config scheme."""

return DictScheme(cls.__scheme__)

def _validate(self, options):
"""Validates config."""

try:
return validate("config", options, self._get_scheme())
except ValidationError as e:
raise exceptions.ScaleIOConfigError(e)

def apply(self, **options):
"""Applies config options to a config module."""

self._validate(options)

for option, value in options.items():
setattr(pyscaleio.config, option.upper(), value)
11 changes: 0 additions & 11 deletions pyscaleio/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 31,6 @@
"""Seconds in average month."""


NETWORK_TIMEOUT = 30
"""Timeout for all network operations."""

REQUEST_RETRIES = 3
"""Default retries count for HTTP request."""


VOLUME_TYPE_THIN = "ThinProvisioned"
"""Volume type with thin provisioning."""

Expand Down Expand Up @@ -74,7 67,3 @@
VOLUME_REMOVE_VTREE,
]
"""Valid volume remove modes"""


VOLUME_PATH = "emc-vol-{system_id}-{volume_id}"
"""Default template for volume path on SDC."""
6 changes: 6 additions & 0 deletions pyscaleio/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 49,12 @@ def __init__(self, exc):
"Resource validation error: {0}", exc)


class ScaleIOConfigError(Error):
def __init__(self, exc):
super(ScaleIOConfigError, self).__init__(
"Config validation error: {0}", exc)


class ScaleIOInvalidParameters(Error):
def __init__(self, *args, **kwargs):
super(ScaleIOInvalidParameters, self).__init__(*args, **kwargs)
Expand Down
14 changes: 2 additions & 12 deletions pyscaleio/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 3,11 @@
from six import add_metaclass

from pyscaleio import exceptions
from pyscaleio import utils
from pyscaleio.client import ScaleIOClient


class singleton(type):
"""Singleton meta-class."""

_instances = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]


@add_metaclass(singleton)
@add_metaclass(utils.singleton)
class ScaleIOClientsManager(object):
"""ScaleIO Clients manager."""

Expand Down
8 changes: 5 additions & 3 deletions pyscaleio/models.py
Original file line number Diff line number Diff line change
@@ -1,14 1,15 @@
from __future__ import unicode_literals

import os
from six import text_type as str
from collections import Mapping, Sequence

from inflection import camelize, underscore
from object_validator import validate, ValidationError
from object_validator import DictScheme, List, String, Integer, Bool

from six import text_type as str

import pyscaleio
from pyscaleio import config
from pyscaleio import constants
from pyscaleio import exceptions
from pyscaleio import utils
Expand Down Expand Up @@ -486,10 487,11 @@ def exports(self):

@property
def path(self):
return constants.VOLUME_PATH.format(
device_name = config.VOLUME_NAME.format(
system_id=self._client.system["id"],
volume_id=self["id"]
)
return os.path.join(config.VOLUME_PREFIX, device_name)

def rename(self, name):
"""Changes volume name.
Expand Down
11 changes: 11 additions & 0 deletions pyscaleio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,17 @@
from functools import wraps


class singleton(type):
"""Singleton meta-class."""

_instances = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]


def _drop_none(mapping):
"""
Removes all keys that points to None values.
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 1,27 @@
from __future__ import unicode_literals

import pytest
import mock

import pyscaleio
from pyscaleio import exceptions


def test_config_apply():

assert pyscaleio.config.NETWORK_TIMEOUT == 30
assert pyscaleio.config.REQUEST_RETRIES == 3
assert pyscaleio.config.VOLUME_PREFIX == "/dev/disk/by-id"

with mock.patch("pyscaleio.config", autospec=True):
pyscaleio.configure(network_timeout=10)
assert pyscaleio.config.NETWORK_TIMEOUT == 10

pyscaleio.configure(volume_prefix="/dev")
assert pyscaleio.config.VOLUME_PREFIX == "/dev"

with pytest.raises(exceptions.ScaleIOConfigError):
pyscaleio.configure(network_timeout="timeout")

with pytest.raises(exceptions.ScaleIOConfigError):
pyscaleio.configure(unexist_field="value")
2 changes: 1 addition & 1 deletion tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 265,7 @@ def test_volume_model(client):
assert not volume.exports

with mock.patch("pyscaleio.models.System.__scheme__", {}):
assert volume.path == "emc-vol-system-test"
assert volume.path == "/dev/disk/by-id/emc-vol-system-test"


def test_volume_model_exports(client):
Expand Down

0 comments on commit eb5e442

Please sign in to comment.