diff options
author | Andy Grover <agrover@redhat.com> | 2014-03-04 16:13:45 -0800 |
---|---|---|
committer | Andy Grover <agrover@redhat.com> | 2014-03-04 16:13:45 -0800 |
commit | b620aa22acb92b85cd7a0bc23381a2d3cbf882d8 (patch) | |
tree | ebbf88f57dd04a5e606dd1a8729ac6da9371aaae | |
parent | 7d0250ac863d253aa52b33c1c963bfe3be510bfb (diff) | |
download | rtslib-fb-b620aa22acb92b85cd7a0bc23381a2d3cbf882d8.tar.gz |
Add StorageObjectFactory
Give it a path and it figures out what kind of storage object to
instantiate.
Signed-off-by: Andy Grover <agrover@redhat.com>
-rw-r--r-- | rtslib/__init__.py | 1 | ||||
-rw-r--r-- | rtslib/tcm.py | 20 |
2 files changed, 21 insertions, 0 deletions
diff --git a/rtslib/__init__.py b/rtslib/__init__.py index d82a712..eba647b 100644 --- a/rtslib/__init__.py +++ b/rtslib/__init__.py @@ -24,6 +24,7 @@ from fabric import FabricModule from tcm import FileIOStorageObject, BlockStorageObject from tcm import PSCSIStorageObject, RDMCPStorageObject +from tcm import StorageObjectFactory __version__ = 'GIT_VERSION' __author__ = "Jerome Martin <jxm@risingtidesystems.com>" diff --git a/rtslib/tcm.py b/rtslib/tcm.py index a408b73..377f576 100644 --- a/rtslib/tcm.py +++ b/rtslib/tcm.py @@ -18,6 +18,7 @@ under the License. ''' import os +import stat import re import glob import resource @@ -740,6 +741,25 @@ class BlockStorageObject(StorageObject): return d +class StorageObjectFactory(object): + """ + Create a storage object based on a given path. + Only works for file & block. + """ + + def __new__(cls, path): + path = os.path.realpath(path) + name = path.strip("/").replace("/", "-") + if os.path.exists(path): + s = os.stat(path) + if stat.S_ISBLK(s.st_mode): + return BlockStorageObject(name=name, dev=path) + elif stat.S_ISREG(s.st_mode): + return FileIOStorageObject(name=name, dev=path, size=s.st_size) + + raise RTSLibError("Can't create storageobject from path: %s" % path) + + bs_params = { PSCSIStorageObject: dict(name='pscsi'), RDMCPStorageObject: dict(name='ramdisk', alt_dirprefix='rd_mcp'), |