Fix some types in the FS and VMware drivers

The _get_datadir_path_and_priority and _parse_datastore_info_and_weight methods
may return the priority (or weight) as an integer or a string. This causes a few
issues:

- the caller must cast the result to an integer
- the code is harder to read
- static checkers such as mypy have a hard time processing these methods

We make sure that both methods now return the priority as an integer.

Closes-Bug: #1813092
Change-Id: I9435c88560b01737f9310fe2cba1ca4c84e0f3fa
This commit is contained in:
Cyril Roelandt 2019-01-20 00:35:17 +01:00
parent 8814fe8273
commit ac5abddb6f
4 changed files with 10 additions and 8 deletions

View File

@ -436,7 +436,7 @@ class Store(glance_store.driver.Store):
(datadir_path, (datadir_path,
priority) = self._get_datadir_path_and_priority(datadir) priority) = self._get_datadir_path_and_priority(datadir)
priority_paths = self.priority_data_map.setdefault( priority_paths = self.priority_data_map.setdefault(
int(priority), []) priority, [])
self._check_directory_paths(datadir_path, directory_paths, self._check_directory_paths(datadir_path, directory_paths,
priority_paths) priority_paths)
directory_paths.add(datadir_path) directory_paths.add(datadir_path)
@ -490,8 +490,9 @@ class Store(glance_store.driver.Store):
parts = [part.strip() for part in datadir.rsplit(":", 1)] parts = [part.strip() for part in datadir.rsplit(":", 1)]
datadir_path = parts[0] datadir_path = parts[0]
if len(parts) == 2 and parts[1]: if len(parts) == 2 and parts[1]:
priority = parts[1] try:
if not priority.isdigit(): priority = int(parts[1])
except ValueError:
msg = (_("Invalid priority value %(priority)s in " msg = (_("Invalid priority value %(priority)s in "
"filesystem configuration") % {'priority': priority}) "filesystem configuration") % {'priority': priority})
LOG.exception(msg) LOG.exception(msg)

View File

@ -463,8 +463,9 @@ class Store(glance_store.Store):
raise exceptions.BadStoreConfiguration( raise exceptions.BadStoreConfiguration(
store_name='vmware_datastore', reason=msg) store_name='vmware_datastore', reason=msg)
if len(parts) == 3 and parts[2]: if len(parts) == 3 and parts[2]:
weight = parts[2] try:
if not weight.isdigit(): weight = int(parts[2])
except ValueError:
msg = (_('Invalid weight value %(weight)s in ' msg = (_('Invalid weight value %(weight)s in '
'vmware_datastores configuration') % 'vmware_datastores configuration') %
{'weight': weight}) {'weight': weight})
@ -501,7 +502,7 @@ class Store(glance_store.Store):
LOG.error(msg) LOG.error(msg)
raise exceptions.BadStoreConfiguration( raise exceptions.BadStoreConfiguration(
store_name='vmware_datastore', reason=msg) store_name='vmware_datastore', reason=msg)
ds_map.setdefault(int(weight), []).append(ds_obj) ds_map.setdefault(weight, []).append(ds_obj)
return ds_map return ds_map
def configure_add(self): def configure_add(self):

View File

@ -391,7 +391,7 @@ class TestMultiStore(base.MultiStoreBaseTest,
parts = self.store._parse_datastore_info_and_weight(datastore) parts = self.store._parse_datastore_info_and_weight(datastore)
self.assertEqual('a', parts[0]) self.assertEqual('a', parts[0])
self.assertEqual('b', parts[1]) self.assertEqual('b', parts[1])
self.assertEqual('100', parts[2]) self.assertEqual(100, parts[2])
def test_parse_datastore_info_and_weight_default_weight(self): def test_parse_datastore_info_and_weight_default_weight(self):
datastore = 'a:b' datastore = 'a:b'

View File

@ -396,7 +396,7 @@ class TestStore(base.StoreBaseTest,
parts = self.store._parse_datastore_info_and_weight(datastore) parts = self.store._parse_datastore_info_and_weight(datastore)
self.assertEqual('a', parts[0]) self.assertEqual('a', parts[0])
self.assertEqual('b', parts[1]) self.assertEqual('b', parts[1])
self.assertEqual('100', parts[2]) self.assertEqual(100, parts[2])
def test_parse_datastore_info_and_weight_default_weight(self): def test_parse_datastore_info_and_weight_default_weight(self):
datastore = 'a:b' datastore = 'a:b'