summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvladimir.p <vladimir@zadarastorage.com>2011-08-24 10:16:20 -0700
committervladimir.p <vladimir@zadarastorage.com>2011-08-24 10:16:20 -0700
commit207ce4f19655e70d14f3a67a45ba6acf8f12380d (patch)
treedaa5e1f5ea1d7b9fd9be64af66cc507495dfe489
parent163923f57d075fa5d46a5b54073fc6cd30e5c624 (diff)
downloadnova-207ce4f19655e70d14f3a67a45ba6acf8f12380d.tar.gz
nova-207ce4f19655e70d14f3a67a45ba6acf8f12380d.tar.xz
nova-207ce4f19655e70d14f3a67a45ba6acf8f12380d.zip
added volume type search by extra_spec
-rw-r--r--nova/tests/test_volume_types.py55
-rw-r--r--nova/volume/volume_types.py34
2 files changed, 87 insertions, 2 deletions
diff --git a/nova/tests/test_volume_types.py b/nova/tests/test_volume_types.py
index a20c18b18..1b4c9396c 100644
--- a/nova/tests/test_volume_types.py
+++ b/nova/tests/test_volume_types.py
@@ -152,3 +152,58 @@ class VolumeTypeTestCase(test.TestCase):
new2 = volume_types.get_volume_type(self.ctxt, new['id'])
self.assertEqual(new, new2)
+
+ def test_volume_type_search_by_extra_spec(self):
+ """Ensure volume types get by extra spec returns correct type"""
+ volume_types.create(self.ctxt, "type1", {"key1": "val1",
+ "key2": "val2"})
+ volume_types.create(self.ctxt, "type2", {"key2": "val2",
+ "key3": "val3"})
+ volume_types.create(self.ctxt, "type3", {"key3": "another_value",
+ "key4": "val4"})
+
+ vol_types = volume_types.get_all_types(self.ctxt,
+ search_opts={'extra_specs': {"key1": "val1"}})
+ LOG.info("vol_types: %s" % vol_types)
+ self.assertEqual(len(vol_types), 1)
+ self.assertTrue("type1" in vol_types.keys())
+ self.assertEqual(vol_types['type1']['extra_specs'],
+ {"key1": "val1", "key2": "val2"})
+
+ vol_types = volume_types.get_all_types(self.ctxt,
+ search_opts={'extra_specs': {"key2": "val2"}})
+ LOG.info("vol_types: %s" % vol_types)
+ self.assertEqual(len(vol_types), 2)
+ self.assertTrue("type1" in vol_types.keys())
+ self.assertTrue("type2" in vol_types.keys())
+
+
+ vol_types = volume_types.get_all_types(self.ctxt,
+ search_opts={'extra_specs': {"key3": "val3"}})
+ LOG.info("vol_types: %s" % vol_types)
+ self.assertEqual(len(vol_types), 1)
+ self.assertTrue("type2" in vol_types.keys())
+
+
+ def test_volume_type_search_by_extra_spec_multiple(self):
+ """Ensure volume types get by extra spec returns correct type"""
+ volume_types.create(self.ctxt, "type1", {"key1": "val1",
+ "key2": "val2",
+ "key3": "val3"})
+ volume_types.create(self.ctxt, "type2", {"key2": "val2",
+ "key3": "val3"})
+ volume_types.create(self.ctxt, "type3", {"key1": "val1",
+ "key3": "val3",
+ "key4": "val4"})
+
+ vol_types = volume_types.get_all_types(self.ctxt,
+ search_opts={'extra_specs': {"key1": "val1",
+ "key3": "val3"}})
+ LOG.info("vol_types: %s" % vol_types)
+ self.assertEqual(len(vol_types), 2)
+ self.assertTrue("type1" in vol_types.keys())
+ self.assertTrue("type3" in vol_types.keys())
+ self.assertEqual(vol_types['type1']['extra_specs'],
+ {"key1": "val1", "key2": "val2", "key3": "val3"})
+ self.assertEqual(vol_types['type3']['extra_specs'],
+ {"key1": "val1", "key3": "val3", "key4": "val4"})
diff --git a/nova/volume/volume_types.py b/nova/volume/volume_types.py
index 9df1e39f8..9b02d4ccc 100644
--- a/nova/volume/volume_types.py
+++ b/nova/volume/volume_types.py
@@ -68,13 +68,43 @@ def purge(context, name):
raise exception.ApiError(_("Unknown volume type: %s") % name)
-def get_all_types(context, inactive=0):
+def get_all_types(context, inactive=0, search_opts={}):
"""Get all non-deleted volume_types.
Pass true as argument if you want deleted volume types returned also.
"""
- return db.volume_type_get_all(context, inactive)
+ vol_types = db.volume_type_get_all(context, inactive)
+
+ if search_opts:
+ LOG.debug(_("Searching by: %s") % str(search_opts))
+
+ def _check_extra_specs_match(vol_type, searchdict):
+ for k, v in searchdict.iteritems():
+ if k not in vol_type['extra_specs'].keys()\
+ or vol_type['extra_specs'][k] != v:
+ return False
+ return True
+
+ # search_option to filter_name mapping.
+ filter_mapping = {'extra_specs': _check_extra_specs_match}
+
+ result = {}
+ for type_name, type_args in vol_types.iteritems():
+ # go over all filters in the list
+ for opt, values in search_opts.iteritems():
+ try:
+ filter_func = filter_mapping[opt]
+ except KeyError:
+ # no such filter - ignore it, go to next filter
+ continue
+ else:
+ if filter_func(type_args, values):
+ # if one of conditions didn't match - remove
+ result[type_name] = type_args
+ break
+ vol_types = result
+ return vol_types
def get_volume_type(context, id):